Skip to content

Commit

Permalink
Make inflected column name to be in natural case instead of lowercase
Browse files Browse the repository at this point in the history
The 'inflected_name' property of column metadata must be in the same letter case as column names seen in a response from a mysql server to work properly.

The column names also must be in the natural case to make it compatible with mysql_* functions responses and to make model attributes in the phpactiverecord usage also in the natural case.

To make it happen:

* StandardInflector->variablize($s) no more lowercases column names.
* Connection::$PDO_OPTIONS[PDO::ATTR_CASE] is set to PDO::CASE_NATURAL.

After this change all usage of model attributes that correspond to columns having uppercase letters must be used as-is now.

Example:
Table `commutators` have a column `networkNode_id`.

Before this change it should be written as:
$commutator->networknode_id

After this change it should be written as:
$commutator->networkNode_id
  • Loading branch information
xak2000 committed Oct 30, 2019
1 parent 81b902b commit 3f89750
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 120 deletions.
2 changes: 1 addition & 1 deletion lib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ abstract class Connection
* @var array
*/
static $PDO_OPTIONS = array(
PDO::ATTR_CASE => PDO::CASE_LOWER,
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
PDO::ATTR_STRINGIFY_FETCHES => false);
Expand Down
238 changes: 119 additions & 119 deletions lib/Inflector.php
Original file line number Diff line number Diff line change
@@ -1,119 +1,119 @@
<?php
/**
* @package ActiveRecord
*/
namespace ActiveRecord;

/**
* @package ActiveRecord
*/
abstract class Inflector
{
/**
* Get an instance of the {@link Inflector} class.
*
* @return object
*/
public static function instance()
{
return new StandardInflector();
}

/**
* Turn a string into its camelized version.
*
* @param string $s string to convert
* @return string
*/
public function camelize($s)
{
$s = preg_replace('/[_-]+/','_',trim($s));
$s = str_replace(' ', '_', $s);

$camelized = '';

for ($i=0,$n=strlen($s); $i<$n; ++$i)
{
if ($s[$i] == '_' && $i+1 < $n)
$camelized .= strtoupper($s[++$i]);
else
$camelized .= $s[$i];
}

$camelized = trim($camelized,' _');

if (strlen($camelized) > 0)
$camelized[0] = strtolower($camelized[0]);

return $camelized;
}

/**
* Determines if a string contains all uppercase characters.
*
* @param string $s string to check
* @return bool
*/
public static function is_upper($s)
{
return (strtoupper($s) === $s);
}

/**
* Determines if a string contains all lowercase characters.
*
* @param string $s string to check
* @return bool
*/
public static function is_lower($s)
{
return (strtolower($s) === $s);
}

/**
* Convert a camelized string to a lowercase, underscored string.
*
* @param string $s string to convert
* @return string
*/
public function uncamelize($s)
{
$normalized = '';

for ($i=0,$n=strlen($s); $i<$n; ++$i)
{
if (ctype_alpha($s[$i]) && self::is_upper($s[$i]))
$normalized .= '_' . strtolower($s[$i]);
else
$normalized .= $s[$i];
}
return trim($normalized,' _');
}

/**
* Convert a string with space into a underscored equivalent.
*
* @param string $s string to convert
* @return string
*/
public function underscorify($s)
{
return preg_replace(array('/[_\- ]+/','/([a-z])([A-Z])/'),array('_','\\1_\\2'),trim($s));
}

public function keyify($class_name)
{
return strtolower($this->underscorify(denamespace($class_name))) . '_id';
}

abstract function variablize($s);
}

/**
* @package ActiveRecord
*/
class StandardInflector extends Inflector
{
public function tableize($s) { return Utils::pluralize(strtolower($this->underscorify($s))); }
public function variablize($s) { return str_replace(array('-',' '),array('_','_'),strtolower(trim($s))); }
}
<?php
/**
* @package ActiveRecord
*/
namespace ActiveRecord;

/**
* @package ActiveRecord
*/
abstract class Inflector
{
/**
* Get an instance of the {@link Inflector} class.
*
* @return object
*/
public static function instance()
{
return new StandardInflector();
}

/**
* Turn a string into its camelized version.
*
* @param string $s string to convert
* @return string
*/
public function camelize($s)
{
$s = preg_replace('/[_-]+/','_',trim($s));
$s = str_replace(' ', '_', $s);

$camelized = '';

for ($i=0,$n=strlen($s); $i<$n; ++$i)
{
if ($s[$i] == '_' && $i+1 < $n)
$camelized .= strtoupper($s[++$i]);
else
$camelized .= $s[$i];
}

$camelized = trim($camelized,' _');

if (strlen($camelized) > 0)
$camelized[0] = strtolower($camelized[0]);

return $camelized;
}

/**
* Determines if a string contains all uppercase characters.
*
* @param string $s string to check
* @return bool
*/
public static function is_upper($s)
{
return (strtoupper($s) === $s);
}

/**
* Determines if a string contains all lowercase characters.
*
* @param string $s string to check
* @return bool
*/
public static function is_lower($s)
{
return (strtolower($s) === $s);
}

/**
* Convert a camelized string to a lowercase, underscored string.
*
* @param string $s string to convert
* @return string
*/
public function uncamelize($s)
{
$normalized = '';

for ($i=0,$n=strlen($s); $i<$n; ++$i)
{
if (ctype_alpha($s[$i]) && self::is_upper($s[$i]))
$normalized .= '_' . strtolower($s[$i]);
else
$normalized .= $s[$i];
}
return trim($normalized,' _');
}

/**
* Convert a string with space into a underscored equivalent.
*
* @param string $s string to convert
* @return string
*/
public function underscorify($s)
{
return preg_replace(array('/[_\- ]+/','/([a-z])([A-Z])/'),array('_','\\1_\\2'),trim($s));
}

public function keyify($class_name)
{
return strtolower($this->underscorify(denamespace($class_name))) . '_id';
}

abstract function variablize($s);
}

/**
* @package ActiveRecord
*/
class StandardInflector extends Inflector
{
public function tableize($s) { return Utils::pluralize(strtolower($this->underscorify($s))); }
public function variablize($s) { return str_replace(array('-',' '),array('_','_'),trim($s)); }
}

0 comments on commit 3f89750

Please sign in to comment.