From 903ae8a581fac1e6917fc3e31d2ad8fb91df80c3 Mon Sep 17 00:00:00 2001 From: ctrlaltca <> Date: Thu, 12 Jul 2012 11:21:01 +0000 Subject: standardize the use of unix eol; use svn properties to enforce native eol --- .../Data/Common/Sqlite/TSqliteCommandBuilder.php | 92 ++--- framework/Data/Common/Sqlite/TSqliteMetaData.php | 420 ++++++++++----------- .../Data/Common/Sqlite/TSqliteTableColumn.php | 126 +++---- framework/Data/Common/Sqlite/TSqliteTableInfo.php | 92 ++--- 4 files changed, 365 insertions(+), 365 deletions(-) (limited to 'framework/Data/Common/Sqlite') diff --git a/framework/Data/Common/Sqlite/TSqliteCommandBuilder.php b/framework/Data/Common/Sqlite/TSqliteCommandBuilder.php index d162ed60..d7d82812 100644 --- a/framework/Data/Common/Sqlite/TSqliteCommandBuilder.php +++ b/framework/Data/Common/Sqlite/TSqliteCommandBuilder.php @@ -1,47 +1,47 @@ - - * @link http://www.pradosoft.com/ + + * @link http://www.pradosoft.com/ * @copyright Copyright © 2005-2012 PradoSoft - * @license http://www.pradosoft.com/license/ - * @version $Id: TDbCommandBuilder.php 1863 2007-04-12 12:43:49Z wei $ - * @package System.Data.Common - */ - -Prado::using('System.Data.Common.TDbCommandBuilder'); - -/** - * TSqliteCommandBuilder provides specifics methods to create limit/offset query commands - * for Sqlite database. - * - * @author Wei Zhuo - * @version $Id: TDbCommandBuilder.php 1863 2007-04-12 12:43:49Z wei $ - * @package System.Data.Common - * @since 3.1 - */ -class TSqliteCommandBuilder extends TDbCommandBuilder -{ - /** - * Alters the sql to apply $limit and $offset. - * @param string SQL query string. - * @param integer maximum number of rows, -1 to ignore limit. - * @param integer row offset, -1 to ignore offset. - * @return string SQL with limit and offset. - */ - public function applyLimitOffset($sql, $limit=-1, $offset=-1) - { - $limit = $limit!==null ? intval($limit) : -1; - $offset = $offset!==null ? intval($offset) : -1; - if($limit > 0 || $offset > 0) - { - $limitStr = ' LIMIT '.$limit; - $offsetStr = $offset >= 0 ? ' OFFSET '.$offset : ''; - return $sql.$limitStr.$offsetStr; - } - else - return $sql; - } -} - + * @license http://www.pradosoft.com/license/ + * @version $Id: TDbCommandBuilder.php 1863 2007-04-12 12:43:49Z wei $ + * @package System.Data.Common + */ + +Prado::using('System.Data.Common.TDbCommandBuilder'); + +/** + * TSqliteCommandBuilder provides specifics methods to create limit/offset query commands + * for Sqlite database. + * + * @author Wei Zhuo + * @version $Id: TDbCommandBuilder.php 1863 2007-04-12 12:43:49Z wei $ + * @package System.Data.Common + * @since 3.1 + */ +class TSqliteCommandBuilder extends TDbCommandBuilder +{ + /** + * Alters the sql to apply $limit and $offset. + * @param string SQL query string. + * @param integer maximum number of rows, -1 to ignore limit. + * @param integer row offset, -1 to ignore offset. + * @return string SQL with limit and offset. + */ + public function applyLimitOffset($sql, $limit=-1, $offset=-1) + { + $limit = $limit!==null ? intval($limit) : -1; + $offset = $offset!==null ? intval($offset) : -1; + if($limit > 0 || $offset > 0) + { + $limitStr = ' LIMIT '.$limit; + $offsetStr = $offset >= 0 ? ' OFFSET '.$offset : ''; + return $sql.$limitStr.$offsetStr; + } + else + return $sql; + } +} + diff --git a/framework/Data/Common/Sqlite/TSqliteMetaData.php b/framework/Data/Common/Sqlite/TSqliteMetaData.php index 5acfad9d..d101d179 100644 --- a/framework/Data/Common/Sqlite/TSqliteMetaData.php +++ b/framework/Data/Common/Sqlite/TSqliteMetaData.php @@ -1,210 +1,210 @@ - - * @link http://www.pradosoft.com/ - * @copyright Copyright © 2005-2012 PradoSoft - * @license http://www.pradosoft.com/license/ - * @version $Id: TSqliteMetaData.php 1861 2007-04-12 08:05:03Z wei $ - * @package System.Data.Common.Sqlite - */ - -/** - * Load the base TDbMetaData class. - */ -Prado::using('System.Data.Common.TDbMetaData'); -Prado::using('System.Data.Common.Sqlite.TSqliteTableInfo'); - -/** - * TSqliteMetaData loads SQLite database table and column information. - * - * @author Wei Zhuo - * @version $Id: TSqliteMetaData.php 1861 2007-04-12 08:05:03Z wei $ - * @package System.Data.Commom.Sqlite - * @since 3.1 - */ -class TSqliteMetaData extends TDbMetaData -{ - /** - * @return string TDbTableInfo class name. - */ - protected function getTableInfoClass() - { - return 'TSqliteTableInfo'; - } - - /** - * Quotes a table name for use in a query. - * @param string $name table name - * @return string the properly quoted table name - */ - public function quoteTableName($name) - { - return parent::quoteTableName($name, "'", "'"); - } - - /** - * Quotes a column name for use in a query. - * @param string $name column name - * @return string the properly quoted column name - */ - public function quoteColumnName($name) - { - return parent::quoteColumnName($name, '"', '"'); - } - - /** - * Quotes a column alias for use in a query. - * @param string $name column alias - * @return string the properly quoted column alias - */ - public function quoteColumnAlias($name) - { - return parent::quoteColumnAlias($name, '"', '"'); - } - - /** - * Get the column definitions for given table. - * @param string table name. - * @return TPgsqlTableInfo table information. - */ - protected function createTableInfo($tableName) - { - $tableName = str_replace("'",'',$tableName); - $this->getDbConnection()->setActive(true); - $table = $this->getDbConnection()->quoteString($tableName); - $sql = "PRAGMA table_info({$table})"; - $command = $this->getDbConnection()->createCommand($sql); - $foreign = $this->getForeignKeys($table); - $index=0; - $columns=array(); - $primary=array(); - foreach($command->query() as $col) - { - $col['index'] = $index++; - $column = $this->processColumn($col, $foreign); - $columns[$col['name']] = $column; - if($column->getIsPrimaryKey()) - $primary[] = $col['name']; - } - $info['TableName'] = $tableName; - if($this->getIsView($tableName)) - $info['IsView'] = true; - if(count($columns)===0) - throw new TDbException('dbmetadata_invalid_table_view', $tableName); - $class = $this->getTableInfoClass(); - $tableInfo = new $class($info,$primary,$foreign); - $tableInfo->getColumns()->copyFrom($columns); - return $tableInfo; - } - - /** - * @param string table name. - * @return boolean true if the table is a view. - */ - protected function getIsView($tableName) - { - $sql = 'SELECT count(*) FROM sqlite_master WHERE type="view" AND name= :table'; - $this->getDbConnection()->setActive(true); - $command = $this->getDbConnection()->createCommand($sql); - $command->bindValue(':table', $tableName); - return intval($command->queryScalar()) === 1; - } - - /** - * @param array column information. - * @param array foreign key details. - * @return TSqliteTableColumn column details. - */ - protected function processColumn($col, $foreign) - { - $columnId = $col['name']; //use column name as column Id - - $info['ColumnName'] = '"'.$columnId.'"'; //quote the column names! - $info['ColumnId'] = $columnId; - $info['ColumnIndex'] = $col['index']; - - if($col['notnull']!=='99') - $info['AllowNull'] = true; - - if($col['pk']==='1') - $info['IsPrimaryKey'] = true; - if($this->isForeignKeyColumn($columnId, $foreign)) - $info['IsForeignKey'] = true; - - if($col['dflt_value']!==null) - $info['DefaultValue'] = $col['dflt_value']; - - $type = strtolower($col['type']); - $info['AutoIncrement'] = $type==='integer' && $col['pk']==='1'; - - $info['DbType'] = $type; - $match=array(); - if(is_int($pos=strpos($type, '(')) && preg_match('/\((.*)\)/', $type, $match)) - { - $ps = explode(',', $match[1]); - if(count($ps)===2) - { - $info['NumericPrecision'] = intval($ps[0]); - $info['NumericScale'] = intval($ps[1]); - } - else - $info['ColumnSize']=intval($match[1]); - $info['DbType'] = substr($type,0,$pos); - } - - return new TSqliteTableColumn($info); - } - - /** - * - * - * @param string quoted table name. - * @return array foreign key details. - */ - protected function getForeignKeys($table) - { - $sql = "PRAGMA foreign_key_list({$table})"; - $command = $this->getDbConnection()->createCommand($sql); - $fkeys = array(); - foreach($command->query() as $col) - { - $fkeys[$col['table']]['keys'][$col['from']] = $col['to']; - $fkeys[$col['table']]['table'] = $col['table']; - } - return count($fkeys) > 0 ? array_values($fkeys) : $fkeys; - } - - /** - * @param string column name. - * @param array foreign key column names. - * @return boolean true if column is a foreign key. - */ - protected function isForeignKeyColumn($columnId, $foreign) - { - foreach($foreign as $fk) - { - if(in_array($columnId, array_keys($fk['keys']))) - return true; - } - return false; - } -} - -/** - -CREATE TABLE foo -( - id INTEGER NOT NULL PRIMARY KEY, - id2 CHAR(2) -); - -CREATE TABLE bar -( - id INTEGER NOT NULL PRIMARY KEY, - foo_id INTEGER - CONSTRAINT fk_foo_id REFERENCES foo(id) ON DELETE CASCADE -); -*/ - + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2012 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Id: TSqliteMetaData.php 1861 2007-04-12 08:05:03Z wei $ + * @package System.Data.Common.Sqlite + */ + +/** + * Load the base TDbMetaData class. + */ +Prado::using('System.Data.Common.TDbMetaData'); +Prado::using('System.Data.Common.Sqlite.TSqliteTableInfo'); + +/** + * TSqliteMetaData loads SQLite database table and column information. + * + * @author Wei Zhuo + * @version $Id: TSqliteMetaData.php 1861 2007-04-12 08:05:03Z wei $ + * @package System.Data.Commom.Sqlite + * @since 3.1 + */ +class TSqliteMetaData extends TDbMetaData +{ + /** + * @return string TDbTableInfo class name. + */ + protected function getTableInfoClass() + { + return 'TSqliteTableInfo'; + } + + /** + * Quotes a table name for use in a query. + * @param string $name table name + * @return string the properly quoted table name + */ + public function quoteTableName($name) + { + return parent::quoteTableName($name, "'", "'"); + } + + /** + * Quotes a column name for use in a query. + * @param string $name column name + * @return string the properly quoted column name + */ + public function quoteColumnName($name) + { + return parent::quoteColumnName($name, '"', '"'); + } + + /** + * Quotes a column alias for use in a query. + * @param string $name column alias + * @return string the properly quoted column alias + */ + public function quoteColumnAlias($name) + { + return parent::quoteColumnAlias($name, '"', '"'); + } + + /** + * Get the column definitions for given table. + * @param string table name. + * @return TPgsqlTableInfo table information. + */ + protected function createTableInfo($tableName) + { + $tableName = str_replace("'",'',$tableName); + $this->getDbConnection()->setActive(true); + $table = $this->getDbConnection()->quoteString($tableName); + $sql = "PRAGMA table_info({$table})"; + $command = $this->getDbConnection()->createCommand($sql); + $foreign = $this->getForeignKeys($table); + $index=0; + $columns=array(); + $primary=array(); + foreach($command->query() as $col) + { + $col['index'] = $index++; + $column = $this->processColumn($col, $foreign); + $columns[$col['name']] = $column; + if($column->getIsPrimaryKey()) + $primary[] = $col['name']; + } + $info['TableName'] = $tableName; + if($this->getIsView($tableName)) + $info['IsView'] = true; + if(count($columns)===0) + throw new TDbException('dbmetadata_invalid_table_view', $tableName); + $class = $this->getTableInfoClass(); + $tableInfo = new $class($info,$primary,$foreign); + $tableInfo->getColumns()->copyFrom($columns); + return $tableInfo; + } + + /** + * @param string table name. + * @return boolean true if the table is a view. + */ + protected function getIsView($tableName) + { + $sql = 'SELECT count(*) FROM sqlite_master WHERE type="view" AND name= :table'; + $this->getDbConnection()->setActive(true); + $command = $this->getDbConnection()->createCommand($sql); + $command->bindValue(':table', $tableName); + return intval($command->queryScalar()) === 1; + } + + /** + * @param array column information. + * @param array foreign key details. + * @return TSqliteTableColumn column details. + */ + protected function processColumn($col, $foreign) + { + $columnId = $col['name']; //use column name as column Id + + $info['ColumnName'] = '"'.$columnId.'"'; //quote the column names! + $info['ColumnId'] = $columnId; + $info['ColumnIndex'] = $col['index']; + + if($col['notnull']!=='99') + $info['AllowNull'] = true; + + if($col['pk']==='1') + $info['IsPrimaryKey'] = true; + if($this->isForeignKeyColumn($columnId, $foreign)) + $info['IsForeignKey'] = true; + + if($col['dflt_value']!==null) + $info['DefaultValue'] = $col['dflt_value']; + + $type = strtolower($col['type']); + $info['AutoIncrement'] = $type==='integer' && $col['pk']==='1'; + + $info['DbType'] = $type; + $match=array(); + if(is_int($pos=strpos($type, '(')) && preg_match('/\((.*)\)/', $type, $match)) + { + $ps = explode(',', $match[1]); + if(count($ps)===2) + { + $info['NumericPrecision'] = intval($ps[0]); + $info['NumericScale'] = intval($ps[1]); + } + else + $info['ColumnSize']=intval($match[1]); + $info['DbType'] = substr($type,0,$pos); + } + + return new TSqliteTableColumn($info); + } + + /** + * + * + * @param string quoted table name. + * @return array foreign key details. + */ + protected function getForeignKeys($table) + { + $sql = "PRAGMA foreign_key_list({$table})"; + $command = $this->getDbConnection()->createCommand($sql); + $fkeys = array(); + foreach($command->query() as $col) + { + $fkeys[$col['table']]['keys'][$col['from']] = $col['to']; + $fkeys[$col['table']]['table'] = $col['table']; + } + return count($fkeys) > 0 ? array_values($fkeys) : $fkeys; + } + + /** + * @param string column name. + * @param array foreign key column names. + * @return boolean true if column is a foreign key. + */ + protected function isForeignKeyColumn($columnId, $foreign) + { + foreach($foreign as $fk) + { + if(in_array($columnId, array_keys($fk['keys']))) + return true; + } + return false; + } +} + +/** + +CREATE TABLE foo +( + id INTEGER NOT NULL PRIMARY KEY, + id2 CHAR(2) +); + +CREATE TABLE bar +( + id INTEGER NOT NULL PRIMARY KEY, + foo_id INTEGER + CONSTRAINT fk_foo_id REFERENCES foo(id) ON DELETE CASCADE +); +*/ + diff --git a/framework/Data/Common/Sqlite/TSqliteTableColumn.php b/framework/Data/Common/Sqlite/TSqliteTableColumn.php index 915debbe..6783e1ad 100644 --- a/framework/Data/Common/Sqlite/TSqliteTableColumn.php +++ b/framework/Data/Common/Sqlite/TSqliteTableColumn.php @@ -1,64 +1,64 @@ - - * @link http://www.pradosoft.com/ + + * @link http://www.pradosoft.com/ * @copyright Copyright © 2005-2012 PradoSoft - * @license http://www.pradosoft.com/license/ - * @version $Id: TSqliteTableColumn.php 1861 2007-04-12 08:05:03Z wei $ - * @package System.Data.Common.Sqlite - */ - -/** - * Load common TDbTableCommon class. - */ -Prado::using('System.Data.Common.TDbTableColumn'); - -/** - * Describes the column metadata of the schema for a PostgreSQL database table. - * - * @author Wei Zhuo - * @version $Id: TSqliteTableColumn.php 1861 2007-04-12 08:05:03Z wei $ - * @package System.Data.Common.Sqlite - * @since 3.1 - */ -class TSqliteTableColumn extends TDbTableColumn -{ - /** - * @TODO add sqlite types. - */ - private static $types = array(); - - /** - * Overrides parent implementation, returns PHP type from the db type. - * @return boolean derived PHP primitive type from the column db type. - */ - public function getPHPType() - { - $dbtype = strtolower($this->getDbType()); - foreach(self::$types as $type => $dbtypes) - { - if(in_array($dbtype, $dbtypes)) - return $type; - } - return 'string'; - } - - /** - * @return boolean true if column will auto-increment when the column value is inserted as null. - */ - public function getAutoIncrement() - { - return $this->getInfo('AutoIncrement', false); - } - - /** - * @return boolean true if auto increment is true. - */ - public function hasSequence() - { - return $this->getAutoIncrement(); - } -} - + * @license http://www.pradosoft.com/license/ + * @version $Id: TSqliteTableColumn.php 1861 2007-04-12 08:05:03Z wei $ + * @package System.Data.Common.Sqlite + */ + +/** + * Load common TDbTableCommon class. + */ +Prado::using('System.Data.Common.TDbTableColumn'); + +/** + * Describes the column metadata of the schema for a PostgreSQL database table. + * + * @author Wei Zhuo + * @version $Id: TSqliteTableColumn.php 1861 2007-04-12 08:05:03Z wei $ + * @package System.Data.Common.Sqlite + * @since 3.1 + */ +class TSqliteTableColumn extends TDbTableColumn +{ + /** + * @TODO add sqlite types. + */ + private static $types = array(); + + /** + * Overrides parent implementation, returns PHP type from the db type. + * @return boolean derived PHP primitive type from the column db type. + */ + public function getPHPType() + { + $dbtype = strtolower($this->getDbType()); + foreach(self::$types as $type => $dbtypes) + { + if(in_array($dbtype, $dbtypes)) + return $type; + } + return 'string'; + } + + /** + * @return boolean true if column will auto-increment when the column value is inserted as null. + */ + public function getAutoIncrement() + { + return $this->getInfo('AutoIncrement', false); + } + + /** + * @return boolean true if auto increment is true. + */ + public function hasSequence() + { + return $this->getAutoIncrement(); + } +} + diff --git a/framework/Data/Common/Sqlite/TSqliteTableInfo.php b/framework/Data/Common/Sqlite/TSqliteTableInfo.php index fe9ab846..420dce85 100644 --- a/framework/Data/Common/Sqlite/TSqliteTableInfo.php +++ b/framework/Data/Common/Sqlite/TSqliteTableInfo.php @@ -1,47 +1,47 @@ - - * @link http://www.pradosoft.com/ + + * @link http://www.pradosoft.com/ * @copyright Copyright © 2005-2012 PradoSoft - * @license http://www.pradosoft.com/license/ - * @version $Id: TSqliteTableInfo.php 1861 2007-04-12 08:05:03Z wei $ - * @package System.Data.Common.Sqlite - */ - -/** - * Loads the base TDbTableInfo class and TSqliteTableColumn class. - */ -Prado::using('System.Data.Common.TDbTableInfo'); -Prado::using('System.Data.Common.Sqlite.TSqliteTableColumn'); - -/** - * TSqliteTableInfo class provides additional table information for PostgreSQL database. - * - * @author Wei Zhuo - * @version $Id: TSqliteTableInfo.php 1861 2007-04-12 08:05:03Z wei $ - * @package System.Data.Common.Sqlite - * @since 3.1 - */ -class TSqliteTableInfo extends TDbTableInfo -{ - /** - * @param TDbConnection database connection. - * @return TDbCommandBuilder new command builder - */ - public function createCommandBuilder($connection) - { - Prado::using('System.Data.Common.Sqlite.TSqliteCommandBuilder'); - return new TSqliteCommandBuilder($connection,$this); - } - - /** - * @return string full name of the table, database dependent. - */ - public function getTableFullName() - { - return "'".$this->getTableName()."'"; - } -} - + * @license http://www.pradosoft.com/license/ + * @version $Id: TSqliteTableInfo.php 1861 2007-04-12 08:05:03Z wei $ + * @package System.Data.Common.Sqlite + */ + +/** + * Loads the base TDbTableInfo class and TSqliteTableColumn class. + */ +Prado::using('System.Data.Common.TDbTableInfo'); +Prado::using('System.Data.Common.Sqlite.TSqliteTableColumn'); + +/** + * TSqliteTableInfo class provides additional table information for PostgreSQL database. + * + * @author Wei Zhuo + * @version $Id: TSqliteTableInfo.php 1861 2007-04-12 08:05:03Z wei $ + * @package System.Data.Common.Sqlite + * @since 3.1 + */ +class TSqliteTableInfo extends TDbTableInfo +{ + /** + * @param TDbConnection database connection. + * @return TDbCommandBuilder new command builder + */ + public function createCommandBuilder($connection) + { + Prado::using('System.Data.Common.Sqlite.TSqliteCommandBuilder'); + return new TSqliteCommandBuilder($connection,$this); + } + + /** + * @return string full name of the table, database dependent. + */ + public function getTableFullName() + { + return "'".$this->getTableName()."'"; + } +} + -- cgit v1.2.3