From 32b73a0b1dfcd33e07e7fd2b2cf2fd7d42cf48a0 Mon Sep 17 00:00:00 2001 From: wei <> Date: Thu, 12 Apr 2007 12:43:49 +0000 Subject: Add Sqlite driver. --- framework/Data/Common/Pgsql/TPgsqlMetaData.php | 47 +++++++++++------------ framework/Data/Common/Pgsql/TPgsqlTableColumn.php | 20 +++++----- 2 files changed, 33 insertions(+), 34 deletions(-) (limited to 'framework/Data/Common/Pgsql') diff --git a/framework/Data/Common/Pgsql/TPgsqlMetaData.php b/framework/Data/Common/Pgsql/TPgsqlMetaData.php index b789192f..e964cfe7 100644 --- a/framework/Data/Common/Pgsql/TPgsqlMetaData.php +++ b/framework/Data/Common/Pgsql/TPgsqlMetaData.php @@ -119,12 +119,27 @@ EOD; */ protected function createNewTableInfo($schemaName,$tableName) { - $info['SchemaName'] = $schemaName; - $info['TableName'] = $tableName; + $info['SchemaName'] = $this->assertIdentifier($schemaName); + $info['TableName'] = $this->assertIdentifier($tableName); if($this->getIsView($schemaName,$tableName)) $info['IsView'] = true; - list($primary, $foreign, $unique) = $this->getConstraintKeys($schemaName, $tableName); - return new TPgsqlTableInfo($info,$primary,$foreign, $unique); + list($primary, $foreign) = $this->getConstraintKeys($schemaName, $tableName); + return new TPgsqlTableInfo($info,$primary,$foreign); + } + + /** + * @param string table name, schema name or column name. + * @return string a valid identifier. + * @throws TDbException when table name contains a double quote ("). + */ + protected function assertIdentifier($name) + { + if(strpos($name, '"')!==false) + { + $ref = 'http://www.postgresql.org/docs/7.4/static/sql-syntax.html#SQL-SYNTAX-IDENTIFIERS'; + throw new TDbException('dbcommon_invalid_identifier_name', $name, $ref); + } + return $name; } /** @@ -163,8 +178,6 @@ EOD; $info['IsPrimaryKey'] = true; if($this->isForeignKeyColumn($columnId, $tableInfo)) $info['IsForeignKey'] = true; - if(in_array($columnId, $tableInfo->getUniqueKeys())) - $info['IsUnique'] = true; if($col['atttypmod'] > 0) $info['ColumnSize'] = $col['atttypmod'] - 4; @@ -222,10 +235,10 @@ EOD; } /** - * Gets the primary, foreign key, and unique column details for the given table. + * Gets the primary and foreign key column details for the given table. * @param string schema name * @param string table name. - * @return array tuple ($primary, $foreign, $unique) + * @return array tuple ($primary, $foreign) */ protected function getConstraintKeys($schemaName, $tableName) { @@ -245,7 +258,6 @@ EOD; $command->bindValue(':schema', $schemaName); $primary = array(); $foreign = array(); - $unique = array(); foreach($command->query() as $row) { switch($row['contype']) @@ -257,13 +269,9 @@ EOD; if(($fkey = $this->getForeignKeys($row['consrc']))!==null) $foreign[] = $fkey; break; - case 'u': - if(($ukey = $this->getUniqueKey($row['consrc']))!==null) - $unique[] = $ukey; - break; } } - return array($primary,$foreign,$unique); + return array($primary,$foreign); } /** @@ -279,17 +287,6 @@ EOD; return array(); } - /** - * @param string pgsql unique constraint definition - * @return string column id if found, null otherwise. - */ - protected function getUniqueKey($src) - { - $matches=array(); - if(preg_match('/UNIQUE\s+\(([^\)]+)\)/i', $src, $matches)) - return $matches[1]; - } - /** * Gets foreign relationship constraint keys and table name * @param string pgsql foreign key definition diff --git a/framework/Data/Common/Pgsql/TPgsqlTableColumn.php b/framework/Data/Common/Pgsql/TPgsqlTableColumn.php index 66053a63..a00f4a8f 100644 --- a/framework/Data/Common/Pgsql/TPgsqlTableColumn.php +++ b/framework/Data/Common/Pgsql/TPgsqlTableColumn.php @@ -25,23 +25,25 @@ Prado::using('System.Data.Common.TDbTableColumn'); */ class TPgsqlTableColumn extends TDbTableColumn { + private static $types=array( + 'integer' => array('bit', 'bit varying', 'real', 'serial', 'int', 'integer'), + 'boolean' => array('boolean'), + 'float' => array('bigint', 'bigserial', 'double precision', 'money', 'numeric') + ); + /** * Overrides parent implementation, returns PHP type from the db type. * @return boolean derived PHP primitive type from the column db type. */ public function getPHPType() { - switch(strtolower($this->getDbType())) + $dbtype = strtolower($this->getDbType()); + foreach(self::$types as $type => $dbtypes) { - case 'bit': case 'bit varying': case 'real': case 'serial': case 'int': case 'integer': - return 'integer'; - case 'boolean': - return 'boolean'; - case 'bigint': case 'bigserial': case 'double precision': case 'money': case 'numeric': - return 'float'; - default: - return 'string'; + if(in_array($dbtype, $dbtypes)) + return $type; } + return 'string'; } } -- cgit v1.2.3