Permalink
Browse files

CUBRID Database 9.0 Support.

  • Loading branch information...
1 parent c496ead commit c0b90c73bba4c1f2ce630085921d2400ef53628f @kadishmal committed Dec 26, 2012
@@ -0,0 +1,70 @@
+CREATE TABLE tbl_lookup
+(
+ id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
+ name VARCHAR(128) NOT NULL,
+ code INTEGER NOT NULL,
+ `type` VARCHAR(128) NOT NULL,
+ `position` INTEGER NOT NULL
+);
+
+CREATE TABLE tbl_user
+(
+ id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
+ username VARCHAR(128) NOT NULL,
+ password VARCHAR(64) NOT NULL,
+ email VARCHAR(128) NOT NULL,
+ profile STRING
+);
+
+CREATE TABLE tbl_post
+(
+ id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
+ title VARCHAR(128) NOT NULL,
+ content STRING NOT NULL,
+ tags STRING,
+ status INTEGER NOT NULL,
+ create_time INTEGER,
+ update_time INTEGER,
+ author_id INTEGER NOT NULL,
+ CONSTRAINT FK_post_author FOREIGN KEY (author_id)
+ REFERENCES tbl_user (id) ON DELETE CASCADE ON UPDATE RESTRICT
+);
+
+CREATE TABLE tbl_comment
+(
+ id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
+ content STRING NOT NULL,
+ status INTEGER NOT NULL,
+ create_time INTEGER,
+ author VARCHAR(128) NOT NULL,
+ email VARCHAR(128) NOT NULL,
+ url VARCHAR(128),
+ post_id INTEGER NOT NULL,
+ CONSTRAINT FK_comment_post FOREIGN KEY (post_id)
+ REFERENCES tbl_post (id) ON DELETE CASCADE ON UPDATE RESTRICT
+);
+
+CREATE TABLE tbl_tag
+(
+ id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
+ name VARCHAR(128) NOT NULL,
+ frequency INTEGER DEFAULT 1
+);
+
+INSERT INTO tbl_lookup (name, `type`, code, `position`) VALUES ('Draft', 'PostStatus', 1, 1);
+INSERT INTO tbl_lookup (name, `type`, code, `position`) VALUES ('Published', 'PostStatus', 2, 2);
+INSERT INTO tbl_lookup (name, `type`, code, `position`) VALUES ('Archived', 'PostStatus', 3, 3);
+INSERT INTO tbl_lookup (name, `type`, code, `position`) VALUES ('Pending Approval', 'CommentStatus', 1, 1);
+INSERT INTO tbl_lookup (name, `type`, code, `position`) VALUES ('Approved', 'CommentStatus', 2, 2);
+
+INSERT INTO tbl_user (username, password, email) VALUES ('demo','$2a$10$JTJf6/XqC94rrOtzuF397OHa4mbmZrVTBOQCmYD9U.obZRUut4BoC','webmaster@example.com');
+INSERT INTO tbl_post (title, content, status, create_time, update_time, author_id, tags) VALUES ('Welcome!','This blog system is developed using Yii. It is meant to demonstrate how to use Yii to build a complete real-world application. Complete source code may be found in the Yii releases.
+
+Feel free to try this system by writing new posts and posting comments.',2,1230952187,1230952187,1,'yii, blog');
+INSERT INTO tbl_post (title, content, status, create_time, update_time, author_id, tags) VALUES ('A Test Post', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', 2,1230952187,1230952187,1,'test');
+
+INSERT INTO tbl_comment (content, status, create_time, author, email, post_id) VALUES ('This is a test comment.', 2, 1230952187, 'Tester', 'tester@example.com', 2);
+
+INSERT INTO tbl_tag (name) VALUES ('yii');
+INSERT INTO tbl_tag (name) VALUES ('blog');
+INSERT INTO tbl_tag (name) VALUES ('test');
@@ -699,7 +699,11 @@ public static function registerAutoloader($callback, $append=false)
'CDbExpression' => '/db/schema/CDbExpression.php',
'CDbSchema' => '/db/schema/CDbSchema.php',
'CDbTableSchema' => '/db/schema/CDbTableSchema.php',
- 'CMssqlColumnSchema' => '/db/schema/mssql/CMssqlColumnSchema.php',
+ 'CCubridColumnSchema' => '/db/schema/cubrid/CCubridColumnSchema.php',
+ 'CCubridCommandBuilder' => '/db/schema/cubrid/CCubridCommandBuilder.php',
+ 'CCubridSchema' => '/db/schema/cubrid/CCubridSchema.php',
+ 'CCubridTableSchema' => '/db/schema/cubrid/CCubridTableSchema.php',
+ 'CMssqlColumnSchema' => '/db/schema/mssql/CMssqlColumnSchema.php',
'CMssqlCommandBuilder' => '/db/schema/mssql/CMssqlCommandBuilder.php',
'CMssqlPdoAdapter' => '/db/schema/mssql/CMssqlPdoAdapter.php',
'CMssqlSchema' => '/db/schema/mssql/CMssqlSchema.php',
@@ -233,6 +233,7 @@ class CDbConnection extends CApplicationComponent
* @since 1.1.6
*/
public $driverMap=array(
+ 'cubrid'=>'CCubridSchema', // CUBRID
'pgsql'=>'CPgsqlSchema', // PostgreSQL
'mysqli'=>'CMysqlSchema', // MySQL
'mysql'=>'CMysqlSchema', // MySQL
@@ -0,0 +1,66 @@
+<?php
+/**
+ * CCubridColumnSchema class file.
+ *
+ * @author Esen Sagynov <kadismal@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2011 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
+
+/**
+ * CCubridColumnSchema class describes the column meta data of a CUBRID table.
+ *
+ * @author Esen Sagynov <kadismal@gmail.com>
+ * @version $Id: CCubridColumnSchema.php
+ * @package system.db.schema.cubrid
+ * @since 1.1.8
+ */
+class CCubridColumnSchema extends CDbColumnSchema
+{
+ /**
+ * Extracts the PHP type from DB type.
+ * @param string $dbType DB type
+ */
+ protected function extractType($dbType)
+ {
+ if(preg_match('/(FLO|REA|DOU|NUM|DEC)/',$dbType))
+ $this->type='double';
+ // The following "bool" and 'boolean" are for future compatibility.
+ // As of CUBRID 9.0, they are not supported.
+ else if(strpos($dbType,'BOOL')!==false)
+ $this->type='boolean';
+ else if(preg_match('/(INT|BIT|SMA|SHO|NUM)/',$dbType))
+ $this->type='integer';
+ else
+ $this->type='string';
+ }
+
+ /**
+ * Extracts the default value for the column.
+ * The value is typecasted to correct PHP type.
+ * @param mixed $defaultValue the default value obtained from metadata
+ */
+ protected function extractDefault($defaultValue)
+ {
+ if($this->dbType==='TIMESTAMP' && $defaultValue==='CURRENT_TIMESTAMP')
+ $this->defaultValue=null;
+ else
+ parent::extractDefault($defaultValue);
+ }
+
+ /**
+ * Extracts size, precision and scale information from column's DB type.
+ * @param string $dbType the column's DB type
+ */
+ protected function extractLimit($dbType)
+ {
+ parent::extractLimit($dbType);
+ // CUBRID does not set limits to numeric data types.
+ // So, we need to set the defalt limit size for INTEGER equal to 11.
+ if ($this->size==null && strpos($dbType,'INT')!==false)
+ {
+ $this->size=$this->precision=11;
+ }
+ }
+}
@@ -0,0 +1,227 @@
+<?php
+/**
+ * CCubridCommandBuilder class file.
+ *
+ * @author Esen Sagynov <kadishmal@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2011 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
+
+/**
+ * CCubridCommandBuilder provides basic methods to create query commands for tables for Mssql Servers.
+ *
+ * @author Esen Sagynov <kadishmal@gmail.com>
+ * @version $Id: CCubridCommandBuilder.php
+ * @package system.db.schema.cubrid
+ * @since 1.1.12
+ */
+class CCubridCommandBuilder extends CDbCommandBuilder
+{
+ // CUBRID operator special characters
+ private $operatorSpecialChars = array('+', '-', '*', '/', '%', '|', '!', '<', '>', '=', '^', '&' , '~');
+ /**
+ * Creates a SELECT command for a single table.
+ * @param CDbTableSchema $table the table metadata
+ * @param CDbCriteria $criteria the query criteria
+ * @param string $alias the alias name of the primary table. Defaults to 't'.
+ * @return CDbCommand query command.
+ */
+ public function createFindCommand($table,$criteria,$alias='t')
+ {
+ $columns = $table->getColumnNames();
+
+ $select=is_array($criteria->select) ? implode(', ',$criteria->select) : $criteria->select;
+
+ $criteria->select = $this->quoteColumnNames($columns, $select);
+ $criteria->condition = $this->quoteColumnNames($columns, $criteria->condition);
+ $criteria->order = $this->quoteColumnNames($columns, $criteria->order);
+ $criteria->group = $this->quoteColumnNames($columns, $criteria->group);
+ $criteria->having = $this->quoteColumnNames($columns, $criteria->having);
+
+ return parent::createFindCommand($table,$criteria,$alias);
+ }
+
+ /**
+ * Creates a COUNT(*) command for a single table.
+ * @param mixed $table the table schema ({@link CDbTableSchema}) or the table name (string).
+ * @param CDbCriteria $criteria the query criteria
+ * @param string $alias the alias name of the primary table. Defaults to 't'.
+ * @return CDbCommand query command.
+ */
+ public function createCountCommand($table,$criteria,$alias='t')
+ {
+ $columns = $table->getColumnNames();
+
+ $select=is_array($criteria->select) ? implode(', ',$criteria->select) : $criteria->select;
+
+ $criteria->select = $this->quoteColumnNames($columns, $select);
+ $criteria->condition = $this->quoteColumnNames($columns, $criteria->condition);
+ $criteria->order = $this->quoteColumnNames($columns, $criteria->order);
+ $criteria->group = $this->quoteColumnNames($columns, $criteria->group);
+ $criteria->having = $this->quoteColumnNames($columns, $criteria->having);
+
+ return parent::createCountCommand($table,$criteria,$alias);
+ }
+
+ /**
+ * Creates a DELETE command.
+ * @param mixed $table the table schema ({@link CDbTableSchema}) or the table name (string).
+ * @param CDbCriteria $criteria the query criteria
+ * @return CDbCommand delete command.
+ */
+ public function createDeleteCommand($table,$criteria)
+ {
+ $columns = $table->getColumnNames();
+ // DELETE FROM query has only WHERE condition, no ORDER BY,
+ // so quote only WHERE condition.
+ $criteria->condition = $this->quoteColumnNames($columns, $criteria->condition);
+
+ return parent::createDeleteCommand($table,$criteria);
+ }
+
+ /**
+ * Creates an UPDATE command.
+ * @param mixed $table the table schema ({@link CDbTableSchema}) or the table name (string).
+ * @param array $data list of columns to be updated (name=>value)
+ * @param CDbCriteria $criteria the query criteria
+ * @return CDbCommand update command.
+ */
+ public function createUpdateCommand($table,$data,$criteria)
+ {
+ $columns = $table->getColumnNames();
+
+ $criteria->condition = $this->quoteColumnNames($columns, $criteria->condition);
+ $criteria->order = $this->quoteColumnNames($columns, $criteria->order);
+ $criteria->group = $this->quoteColumnNames($columns, $criteria->group);
+ $criteria->having = $this->quoteColumnNames($columns, $criteria->having);
+
+ return parent::createUpdateCommand($table,$data,$criteria);
+ }
+
+ /**
+ * Creates an UPDATE command that increments/decrements certain columns.
+ * @param mixed $table the table schema ({@link CDbTableSchema}) or the table name (string).
+ * @param array $counters counters to be updated (counter increments/decrements indexed by column names.)
+ * @param CDbCriteria $criteria the query criteria
+ * @return CDbCommand the created command
+ * @throws CException if no counter is specified
+ */
+ public function createUpdateCounterCommand($table,$counters,$criteria)
+ {
+ $columns = $table->getColumnNames();
+
+ $criteria->condition = $this->quoteColumnNames($columns, $criteria->condition);
+ $criteria->order = $this->quoteColumnNames($columns, $criteria->order);
+ $criteria->group = $this->quoteColumnNames($columns, $criteria->group);
+ $criteria->having = $this->quoteColumnNames($columns, $criteria->having);
+
+ return parent::createUpdateCounterCommand($table,$counters,$criteria);
+ }
+
+ private function quoteColumnNames($columns, $expression)
+ {
+ if (strlen($expression) > 0)
+ {
+ $pos = 0;
+ $newExpression = '';
+
+ // search for the beginning of a character string which is
+ // enclosed in single quotes in CUBRID. We will not parse them,
+ // so will skip.
+ while (($posEnd = stripos($expression, "'")) !== false)
+ {
+ $newExpression .= $this->quoteColumnNamesInRange($columns, substr($expression, $pos, $posEnd - $pos));
+ // find the closing quote. It MUST be present, otherwise this is
+ // user's mistake.
+ $pos = stripos($expression, "'", $posEnd + 1);
+ // If two consequtive single quotes found, the first one
+ // escapes the second. So, we have to look further.
+ while ($pos !== false && substr($expression, $posEnd + 1, 1) == "'")
+ {
+ $pos = strpos($expression, "'", $pos + 1);
+ }
+ // add back the quoted character string itself
+ $newExpression .= substr($expression, $posEnd, $pos - $posEnd + 1);
+ // now parse the rest part
+ $expression = substr($expression, $pos + 1);
+ }
+ // parse the rest part
+ $newExpression .= $this->quoteColumnNamesInRange($columns, $expression);
+ $expression = $newExpression;
+ }
+
+ return $expression;
+ }
+
+ private function quoteColumnNamesInRange($columns, $expression)
+ {
+ if (strlen($expression) > 0)
+ {
+ // search for the beginning of a identifier wrapper (`) which
+ // wraps the column names.
+ $posEnd = stripos($expression, '`');
+
+ if ($posEnd === false)
+ {
+ foreach ($columns as $column)
+ {
+ $pos = $posEnd = 0;
+ $len = strlen($column);
+
+ while (($posEnd = stripos($expression, $column, $pos)) !== false)
+ {
+ if ($posEnd > 0)
+ {
+ $prevChar = substr($expression, $posEnd - 1, 1);
+ }
+ else{
+ $prevChar = false;
+ }
+ $nextChar = substr($expression, $posEnd + $len, 1);
+ // 1. Check if it is not a placeholder, i.e. if the
+ // previous character is not a collon.
+ // 2. Also we have to make sure it's not a part of another
+ // column name, eg. "type" in "category_type". In this
+ // case which have to look further.
+ // 3. We also need to ignore space and a comma delimeter
+ // between columns.
+ // 4. If there is no any character before/after (false)
+ // also skip.
+ if ($prevChar !== ':' &&
+ (in_array($prevChar, $this->operatorSpecialChars) OR
+ $prevChar == ' ' OR $prevChar == ',' OR $prevChar == '(' OR $prevChar === false
+ ) &&
+ (in_array($nextChar, $this->operatorSpecialChars) OR
+ $nextChar == ' ' OR $nextChar == ',' OR $nextChar === false
+ ))
+ {
+ $expression = substr($expression, 0, $posEnd) .
+ $this->schema->quoteSimpleColumnName($column) .
+ substr($expression, $posEnd + $len);
+
+ // 2 for one openning quote and one closing quote
+ $posEnd += 2;
+ }
+ // move further beyond this column name
+ $pos = $posEnd + $len;
+ }
+ }
+ }
+ // If the column name has already been quoted, then we just skip it.
+ else{
+ $newExpression = $this->quoteColumnNamesInRange($columns, substr($expression, 0, $posEnd));
+ // find the closing quote. It MUST be present, otherwise this is
+ // user's mistake.
+ $pos = stripos($expression, '`', $posEnd + 1);
+ // add back the quoted column name itself
+ $newExpression .= substr($expression, $posEnd, $pos - $posEnd + 1);
+ // now parse the rest part
+ $newExpression .= $this->quoteColumnNamesInRange($columns, substr($expression, $pos + 1));
+ $expression = $newExpression;
+ }
+ }
+
+ return $expression;
+ }
+}
Oops, something went wrong.

0 comments on commit c0b90c7

Please sign in to comment.