From: avl Date: Wed, 3 Sep 2008 21:57:55 +0000 (+0300) Subject: ORM Expression for Mysql X-Git-Tag: 0.5~509 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=8c249cc2ab1be230a03330274cdff2cdbfc99f0a;p=ipf.git ORM Expression for Mysql --- diff --git a/ipf/admin/models/_generated/BaseAdminLog.php b/ipf/admin/models/_generated/BaseAdminLog.php index 31b037e..930e5eb 100644 --- a/ipf/admin/models/_generated/BaseAdminLog.php +++ b/ipf/admin/models/_generated/BaseAdminLog.php @@ -1,34 +1,34 @@ -setTableName('admin_log'); - $this->hasColumn('username', 'string', 32, array('type' => 'string', 'length' => '32')); - $this->hasColumn('user_id', 'integer', null, array('type' => 'integer')); - $this->hasColumn('object_id', 'integer', null, array('type' => 'integer')); - $this->hasColumn('object_class', 'string', 200, array('type' => 'string', 'length' => '200')); - $this->hasColumn('object_repr', 'string', 200, array('type' => 'string', 'length' => '200')); - $this->hasColumn('action_flag', 'integer', null, array('type' => 'integer')); - $this->hasColumn('change_message', 'string', 200, array('type' => 'string', 'length' => '200')); - - - $this->index('idx_object_id', array('fields' => 'object_id')); - $this->index('idx_object_class', array('fields' => 'object_class')); - $this->index('idx_created_at', array('fields' => 'created_at')); - $this->index('idx_action_flag', array('fields' => 'action_flag')); - $this->option('type', 'INNODB'); - $this->option('collate', 'utf8_unicode_ci'); - $this->option('charset', 'utf8'); - } - - public function setUp() - { - $timestampable0 = new IPF_ORM_Template_Timestampable(array('updated' => array('disabled' => true))); - $this->actAs($timestampable0); - } +setTableName('admin_log'); + $this->hasColumn('username', 'string', 32, array('type' => 'string', 'length' => '32')); + $this->hasColumn('user_id', 'integer', null, array('type' => 'integer')); + $this->hasColumn('object_id', 'integer', null, array('type' => 'integer')); + $this->hasColumn('object_class', 'string', 200, array('type' => 'string', 'length' => '200')); + $this->hasColumn('object_repr', 'string', 200, array('type' => 'string', 'length' => '200')); + $this->hasColumn('action_flag', 'integer', null, array('type' => 'integer')); + $this->hasColumn('change_message', 'string', 200, array('type' => 'string', 'length' => '200')); + + + $this->index('idx_object_id', array('fields' => 'object_id')); + $this->index('idx_object_class', array('fields' => 'object_class')); + $this->index('idx_created_at', array('fields' => 'created_at')); + $this->index('idx_action_flag', array('fields' => 'action_flag')); + $this->option('type', 'INNODB'); + $this->option('collate', 'utf8_unicode_ci'); + $this->option('charset', 'utf8'); + } + + public function setUp() + { + $timestampable0 = new IPF_ORM_Template_Timestampable(array('updated' => array('disabled' => true))); + $this->actAs($timestampable0); + } } \ No newline at end of file diff --git a/ipf/auth/models/_generated/BasePermission.php b/ipf/auth/models/_generated/BasePermission.php index 1fabde2..4be9891 100644 --- a/ipf/auth/models/_generated/BasePermission.php +++ b/ipf/auth/models/_generated/BasePermission.php @@ -1,30 +1,30 @@ -setTableName('auth_permission'); - $this->hasColumn('name', 'string', 255, array('unique' => true, 'type' => 'string', 'length' => '255')); - - $this->option('type', 'INNODB'); - $this->option('collate', 'utf8_unicode_ci'); - $this->option('charset', 'utf8'); - } - - public function setUp() - { - $this->hasMany('User as Users', array('refClass' => 'UserPermission', - 'local' => 'permission_id', - 'foreign' => 'user_id')); - - $this->hasMany('RolePermission', array('local' => 'id', - 'foreign' => 'permission_id')); - - $this->hasMany('UserPermission', array('local' => 'id', - 'foreign' => 'permission_id')); - } +setTableName('auth_permission'); + $this->hasColumn('name', 'string', 255, array('unique' => true, 'type' => 'string', 'length' => '255')); + + $this->option('type', 'INNODB'); + $this->option('collate', 'utf8_unicode_ci'); + $this->option('charset', 'utf8'); + } + + public function setUp() + { + $this->hasMany('User as Users', array('refClass' => 'UserPermission', + 'local' => 'permission_id', + 'foreign' => 'user_id')); + + $this->hasMany('RolePermission', array('local' => 'id', + 'foreign' => 'permission_id')); + + $this->hasMany('UserPermission', array('local' => 'id', + 'foreign' => 'permission_id')); + } } \ No newline at end of file diff --git a/ipf/auth/models/_generated/BaseRole.php b/ipf/auth/models/_generated/BaseRole.php index d48cde0..6486330 100644 --- a/ipf/auth/models/_generated/BaseRole.php +++ b/ipf/auth/models/_generated/BaseRole.php @@ -1,30 +1,30 @@ -setTableName('auth_role'); - $this->hasColumn('name', 'string', 255, array('unique' => true, 'type' => 'string', 'length' => '255')); - - $this->option('type', 'INNODB'); - $this->option('collate', 'utf8_unicode_ci'); - $this->option('charset', 'utf8'); - } - - public function setUp() - { - $this->hasMany('User as Users', array('refClass' => 'UserRole', - 'local' => 'role_id', - 'foreign' => 'user_id')); - - $this->hasMany('RolePermission', array('local' => 'id', - 'foreign' => 'role_id')); - - $this->hasMany('UserRole', array('local' => 'id', - 'foreign' => 'role_id')); - } +setTableName('auth_role'); + $this->hasColumn('name', 'string', 255, array('unique' => true, 'type' => 'string', 'length' => '255')); + + $this->option('type', 'INNODB'); + $this->option('collate', 'utf8_unicode_ci'); + $this->option('charset', 'utf8'); + } + + public function setUp() + { + $this->hasMany('User as Users', array('refClass' => 'UserRole', + 'local' => 'role_id', + 'foreign' => 'user_id')); + + $this->hasMany('RolePermission', array('local' => 'id', + 'foreign' => 'role_id')); + + $this->hasMany('UserRole', array('local' => 'id', + 'foreign' => 'role_id')); + } } \ No newline at end of file diff --git a/ipf/auth/models/_generated/BaseRolePermission.php b/ipf/auth/models/_generated/BaseRolePermission.php index 5f49ef4..807bb5b 100644 --- a/ipf/auth/models/_generated/BaseRolePermission.php +++ b/ipf/auth/models/_generated/BaseRolePermission.php @@ -1,27 +1,27 @@ -setTableName('auth_role_permission'); - $this->hasColumn('role_id', 'integer', null, array('type' => 'integer', 'primary' => true)); - $this->hasColumn('permission_id', 'integer', null, array('type' => 'integer', 'primary' => true)); - - $this->option('type', 'INNODB'); - $this->option('collate', 'utf8_unicode_ci'); - $this->option('charset', 'utf8'); - } - - public function setUp() - { - $this->hasOne('Role', array('local' => 'role_id', - 'foreign' => 'id')); - - $this->hasOne('Permission', array('local' => 'permission_id', - 'foreign' => 'id')); - } +setTableName('auth_role_permission'); + $this->hasColumn('role_id', 'integer', null, array('type' => 'integer', 'primary' => true)); + $this->hasColumn('permission_id', 'integer', null, array('type' => 'integer', 'primary' => true)); + + $this->option('type', 'INNODB'); + $this->option('collate', 'utf8_unicode_ci'); + $this->option('charset', 'utf8'); + } + + public function setUp() + { + $this->hasOne('Role', array('local' => 'role_id', + 'foreign' => 'id')); + + $this->hasOne('Permission', array('local' => 'permission_id', + 'foreign' => 'id')); + } } \ No newline at end of file diff --git a/ipf/auth/models/_generated/BaseUser.php b/ipf/auth/models/_generated/BaseUser.php index 7f4f772..85e5e88 100644 --- a/ipf/auth/models/_generated/BaseUser.php +++ b/ipf/auth/models/_generated/BaseUser.php @@ -1,45 +1,45 @@ -setTableName('auth_user'); - $this->hasColumn('username', 'string', 32, array('type' => 'string', 'notblank' => true, 'notnull' => true, 'unique' => true, 'length' => '32')); - $this->hasColumn('password', 'string', 128, array('type' => 'string', 'notblank' => true, 'notnull' => true, 'length' => '128')); - $this->hasColumn('first_name', 'string', 32, array('type' => 'string', 'length' => '32')); - $this->hasColumn('last_name', 'string', 32, array('type' => 'string', 'length' => '32')); - $this->hasColumn('email', 'string', 200, array('type' => 'string', 'email' => true, 'notnull' => true, 'notblank' => true, 'unique' => true, 'length' => '200')); - $this->hasColumn('is_staff', 'boolean', null, array('type' => 'boolean', 'notnull' => true, 'default' => false)); - $this->hasColumn('is_active', 'boolean', null, array('type' => 'boolean', 'notnull' => true, 'default' => false)); - $this->hasColumn('is_superuser', 'boolean', null, array('type' => 'boolean', 'notnull' => true, 'default' => false)); - $this->hasColumn('last_login', 'timestamp', null, array('type' => 'timestamp')); - - $this->option('type', 'INNODB'); - $this->option('collate', 'utf8_unicode_ci'); - $this->option('charset', 'utf8'); - } - - public function setUp() - { - $this->hasMany('Role as Roles', array('refClass' => 'UserRole', - 'local' => 'user_id', - 'foreign' => 'role_id')); - - $this->hasMany('Permission as Permissions', array('refClass' => 'UserPermission', - 'local' => 'user_id', - 'foreign' => 'permission_id')); - - $this->hasMany('UserRole', array('local' => 'id', - 'foreign' => 'user_id')); - - $this->hasMany('UserPermission', array('local' => 'id', - 'foreign' => 'user_id')); - - $timestampable0 = new IPF_ORM_Template_Timestampable(); - $this->actAs($timestampable0); - } +setTableName('auth_user'); + $this->hasColumn('username', 'string', 32, array('type' => 'string', 'notblank' => true, 'notnull' => true, 'unique' => true, 'length' => '32')); + $this->hasColumn('password', 'string', 128, array('type' => 'string', 'notblank' => true, 'notnull' => true, 'length' => '128')); + $this->hasColumn('first_name', 'string', 32, array('type' => 'string', 'length' => '32')); + $this->hasColumn('last_name', 'string', 32, array('type' => 'string', 'length' => '32')); + $this->hasColumn('email', 'string', 200, array('type' => 'string', 'email' => true, 'notnull' => true, 'notblank' => true, 'unique' => true, 'length' => '200')); + $this->hasColumn('is_staff', 'boolean', null, array('type' => 'boolean', 'notnull' => true, 'default' => false)); + $this->hasColumn('is_active', 'boolean', null, array('type' => 'boolean', 'notnull' => true, 'default' => false)); + $this->hasColumn('is_superuser', 'boolean', null, array('type' => 'boolean', 'notnull' => true, 'default' => false)); + $this->hasColumn('last_login', 'timestamp', null, array('type' => 'timestamp')); + + $this->option('type', 'INNODB'); + $this->option('collate', 'utf8_unicode_ci'); + $this->option('charset', 'utf8'); + } + + public function setUp() + { + $this->hasMany('Role as Roles', array('refClass' => 'UserRole', + 'local' => 'user_id', + 'foreign' => 'role_id')); + + $this->hasMany('Permission as Permissions', array('refClass' => 'UserPermission', + 'local' => 'user_id', + 'foreign' => 'permission_id')); + + $this->hasMany('UserRole', array('local' => 'id', + 'foreign' => 'user_id')); + + $this->hasMany('UserPermission', array('local' => 'id', + 'foreign' => 'user_id')); + + $timestampable0 = new IPF_ORM_Template_Timestampable(); + $this->actAs($timestampable0); + } } \ No newline at end of file diff --git a/ipf/auth/models/_generated/BaseUserPermission.php b/ipf/auth/models/_generated/BaseUserPermission.php index f8cfbd7..988247a 100644 --- a/ipf/auth/models/_generated/BaseUserPermission.php +++ b/ipf/auth/models/_generated/BaseUserPermission.php @@ -1,27 +1,27 @@ -setTableName('auth_user_permission'); - $this->hasColumn('user_id', 'integer', null, array('type' => 'integer', 'primary' => true)); - $this->hasColumn('permission_id', 'integer', null, array('type' => 'integer', 'primary' => true)); - - $this->option('type', 'INNODB'); - $this->option('collate', 'utf8_unicode_ci'); - $this->option('charset', 'utf8'); - } - - public function setUp() - { - $this->hasOne('User', array('local' => 'user_id', - 'foreign' => 'id')); - - $this->hasOne('Permission', array('local' => 'permission_id', - 'foreign' => 'id')); - } +setTableName('auth_user_permission'); + $this->hasColumn('user_id', 'integer', null, array('type' => 'integer', 'primary' => true)); + $this->hasColumn('permission_id', 'integer', null, array('type' => 'integer', 'primary' => true)); + + $this->option('type', 'INNODB'); + $this->option('collate', 'utf8_unicode_ci'); + $this->option('charset', 'utf8'); + } + + public function setUp() + { + $this->hasOne('User', array('local' => 'user_id', + 'foreign' => 'id')); + + $this->hasOne('Permission', array('local' => 'permission_id', + 'foreign' => 'id')); + } } \ No newline at end of file diff --git a/ipf/auth/models/_generated/BaseUserRole.php b/ipf/auth/models/_generated/BaseUserRole.php index ec5b476..363ff01 100644 --- a/ipf/auth/models/_generated/BaseUserRole.php +++ b/ipf/auth/models/_generated/BaseUserRole.php @@ -1,27 +1,27 @@ -setTableName('auth_user_role'); - $this->hasColumn('user_id', 'integer', null, array('type' => 'integer', 'primary' => true)); - $this->hasColumn('role_id', 'integer', null, array('type' => 'integer', 'primary' => true)); - - $this->option('type', 'INNODB'); - $this->option('collate', 'utf8_unicode_ci'); - $this->option('charset', 'utf8'); - } - - public function setUp() - { - $this->hasOne('User', array('local' => 'user_id', - 'foreign' => 'id')); - - $this->hasOne('Role', array('local' => 'role_id', - 'foreign' => 'id')); - } +setTableName('auth_user_role'); + $this->hasColumn('user_id', 'integer', null, array('type' => 'integer', 'primary' => true)); + $this->hasColumn('role_id', 'integer', null, array('type' => 'integer', 'primary' => true)); + + $this->option('type', 'INNODB'); + $this->option('collate', 'utf8_unicode_ci'); + $this->option('charset', 'utf8'); + } + + public function setUp() + { + $this->hasOne('User', array('local' => 'user_id', + 'foreign' => 'id')); + + $this->hasOne('Role', array('local' => 'role_id', + 'foreign' => 'id')); + } } \ No newline at end of file diff --git a/ipf/orm/expression.php b/ipf/orm/expression.php new file mode 100644 index 0000000..a866412 --- /dev/null +++ b/ipf/orm/expression.php @@ -0,0 +1,72 @@ +_tokenizer = new IPF_ORM_Query_Tokenizer(); + $this->setExpression($expr); + if ($conn !== null) { + $this->_conn = $conn; + } + } + + public function getConnection() + { + if ( ! isset($this->_conn)) { + return IPF_ORM_Manager::connection(); + } + + return $this->_conn; + } + + public function setExpression($clause) + { + $this->_expression = $this->parseClause($clause); + } + + public function parseExpression($expr) + { + $pos = strpos($expr, '('); + $quoted = (substr($expr, 0, 1) === "'" && substr($expr, -1) === "'"); + if ($pos === false || $quoted) { + return $expr; + } + + // get the name of the function + $name = substr($expr, 0, $pos); + $argStr = substr($expr, ($pos + 1), -1); + + // parse args + foreach ($this->_tokenizer->bracketExplode($argStr, ',') as $arg) { + $args[] = $this->parseClause($arg); + } + + return call_user_func_array(array($this->getConnection()->expression, $name), $args); + } + + public function parseClause($clause) + { + $e = $this->_tokenizer->bracketExplode($clause, ' '); + + foreach ($e as $k => $expr) { + $e[$k] = $this->parseExpression($expr); + } + + return implode(' ', $e); + } + + public function getSql() + { + return $this->_expression; + } + + public function __toString() + { + return $this->getSql(); + } +} diff --git a/ipf/orm/expression/driver.php b/ipf/orm/expression/driver.php new file mode 100644 index 0000000..a256d8e --- /dev/null +++ b/ipf/orm/expression/driver.php @@ -0,0 +1,280 @@ +getIdentifier($column); + return 'AVG(' . $column . ')'; + } + + public function count($column) + { + $column = $this->getIdentifier($column); + return 'COUNT(' . $column . ')'; + } + + public function max($column) + { + $column = $this->getIdentifier($column); + return 'MAX(' . $column . ')'; + } + + public function min($column) + { + $column = $this->getIdentifier($column); + return 'MIN(' . $column . ')'; + } + + public function sum($column) + { + $column = $this->getIdentifier($column); + return 'SUM(' . $column . ')'; + } + + public function md5($column) + { + $column = $this->getIdentifier($column); + return 'MD5(' . $column . ')'; + } + + public function length($column) + { + $column = $this->getIdentifier($column); + return 'LENGTH(' . $column . ')'; + } + + public function round($column, $decimals = 0) + { + $column = $this->getIdentifier($column); + + return 'ROUND(' . $column . ', ' . $decimals . ')'; + } + + public function mod($expression1, $expression2) + { + $expression1 = $this->getIdentifier($expression1); + $expression2 = $this->getIdentifier($expression2); + return 'MOD(' . $expression1 . ', ' . $expression2 . ')'; + } + + public function trim($str) + { + return 'TRIM(' . $str . ')'; + } + + public function rtrim($str) + { + return 'RTRIM(' . $str . ')'; + } + + public function ltrim($str) + { + return 'LTRIM(' . $str . ')'; + } + + public function upper($str) + { + return 'UPPER(' . $str . ')'; + } + + public function lower($str) + { + return 'LOWER(' . $str . ')'; + } + + public function locate($str, $substr) + { + return 'LOCATE(' . $str . ', ' . $substr . ')'; + } + + public function now() + { + return 'NOW()'; + } + + public function soundex($value) + { + throw new IPF_ORM_Exception('SQL soundex function not supported by this driver.'); + } + + public function substring($value, $from, $len = null) + { + $value = $this->getIdentifier($value); + if ($len === null) + return 'SUBSTRING(' . $value . ' FROM ' . $from . ')'; + else { + $len = $this->getIdentifier($len); + return 'SUBSTRING(' . $value . ' FROM ' . $from . ' FOR ' . $len . ')'; + } + } + + public function concat() + { + $args = func_get_args(); + + return 'CONCAT(' . join(', ', (array) $args) . ')'; + } + + public function not($expression) + { + $expression = $this->getIdentifier($expression); + return 'NOT(' . $expression . ')'; + } + + private function basicMath($type, array $args) + { + $elements = $this->getIdentifiers($args); + if (count($elements) < 1) { + return ''; + } + if (count($elements) == 1) { + return $elements[0]; + } else { + return '(' . implode(' ' . $type . ' ', $elements) . ')'; + } + } + + public function add(array $args) + { + return $this->basicMath('+', $args); + } + + public function sub(array $args) + { + return $this->basicMath('-', $args ); + } + + public function mul(array $args) + { + return $this->basicMath('*', $args); + } + + public function div(array $args) + { + return $this->basicMath('/', $args); + } + + public function eq($value1, $value2) + { + $value1 = $this->getIdentifier($value1); + $value2 = $this->getIdentifier($value2); + return $value1 . ' = ' . $value2; + } + + public function neq($value1, $value2) + { + $value1 = $this->getIdentifier($value1); + $value2 = $this->getIdentifier($value2); + return $value1 . ' <> ' . $value2; + } + + public function gt($value1, $value2) + { + $value1 = $this->getIdentifier($value1); + $value2 = $this->getIdentifier($value2); + return $value1 . ' > ' . $value2; + } + + public function gte($value1, $value2) + { + $value1 = $this->getIdentifier($value1); + $value2 = $this->getIdentifier($value2); + return $value1 . ' >= ' . $value2; + } + + public function lt($value1, $value2) + { + $value1 = $this->getIdentifier($value1); + $value2 = $this->getIdentifier($value2); + return $value1 . ' < ' . $value2; + } + + public function lte($value1, $value2) + { + $value1 = $this->getIdentifier($value1); + $value2 = $this->getIdentifier($value2); + return $value1 . ' <= ' . $value2; + } + + public function in($column, $values) + { + if ( ! is_array($values)) { + $values = array($values); + } + $values = $this->getIdentifiers($values); + $column = $this->getIdentifier($column); + + if (count($values) == 0) { + throw new IPF_ORM_Exception('Values array for IN operator should not be empty.'); + } + return $column . ' IN (' . implode(', ', $values) . ')'; + } + + public function isNull($expression) + { + $expression = $this->getIdentifier($expression); + return $expression . ' IS NULL'; + } + + public function isNotNull($expression) + { + $expression = $this->getIdentifier($expression); + return $expression . ' IS NOT NULL'; + } + + public function between($expression, $value1, $value2) + { + $expression = $this->getIdentifier($expression); + $value1 = $this->getIdentifier($value1); + $value2 = $this->getIdentifier($value2); + return $expression . ' BETWEEN ' .$value1 . ' AND ' . $value2; + } + + public function guid() + { + throw new IPF_ORM_Exception('method not implemented'); + } + + public function acos($value) + { + return 'ACOS(' . $value . ')'; + } + + public function sin($value) + { + return 'SIN(' . $value . ')'; + } + + public function pi() + { + return 'PI()'; + } + + public function cos($value) + { + return 'COS(' . $value . ')'; + } + + public function __call($m, $a) + { + if ($this->conn->getAttribute(IPF_ORM::ATTR_PORTABILITY) & IPF_ORM::PORTABILITY_EXPR) { + throw new IPF_ORM_Exception('Unknown expression ' . $m); + } + return $m . '(' . implode(', ', $a) . ')'; + } +} \ No newline at end of file diff --git a/ipf/orm/expression/mysql.php b/ipf/orm/expression/mysql.php new file mode 100644 index 0000000..8730023 --- /dev/null +++ b/ipf/orm/expression/mysql.php @@ -0,0 +1,69 @@ + $value) { + if ($key % 2) { + $match .= $value; + } else { + $match .= $this->conn->escapePattern($this->conn->escape($value)); + } + } + $match.= "'"; + $match.= $this->patternEscapeString(); + return $match; + } + + public function guid() + { + return 'UUID()'; + } + + public function year($column) + { + $column = $this->getIdentifier($column); + return 'YEAR(' . $column . ')'; + } + + public function month($column) + { + $column = $this->getIdentifier($column); + return 'MONTH(' . $column . ')'; + } + + public function day($column) + { + $column = $this->getIdentifier($column); + return 'DAY(' . $column . ')'; + } +} \ No newline at end of file diff --git a/ipf/session/models/_generated/BaseSession.php b/ipf/session/models/_generated/BaseSession.php index 4408de9..b126310 100644 --- a/ipf/session/models/_generated/BaseSession.php +++ b/ipf/session/models/_generated/BaseSession.php @@ -1,16 +1,16 @@ -setTableName('session'); - $this->hasColumn('session_key', 'string', 40, array('primary' => true, 'type' => 'string', 'length' => '40')); - $this->hasColumn('session_data', 'string', null, array('type' => 'string')); - $this->hasColumn('expire_data', 'timestamp', null, array('type' => 'timestamp')); - } - +setTableName('session'); + $this->hasColumn('session_key', 'string', 40, array('primary' => true, 'type' => 'string', 'length' => '40')); + $this->hasColumn('session_data', 'string', null, array('type' => 'string')); + $this->hasColumn('expire_data', 'timestamp', null, array('type' => 'timestamp')); + } + } \ No newline at end of file