Skip to content

Commit

Permalink
Fixes issue #514.
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangxue committed Jun 8, 2013
1 parent 51972f2 commit cf1e12a
Showing 1 changed file with 61 additions and 53 deletions.
114 changes: 61 additions & 53 deletions framework/yii/db/pgsql/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use yii\db\ColumnSchema;

/**
* Schema is the class for retrieving metadata from a PostgreSQL database
* Schema is the class for retrieving metadata from a PostgreSQL database
* (version 9.x and above).
*
* @author Gevik Babakhani <[email protected]>
Expand All @@ -23,60 +23,61 @@ class Schema extends \yii\db\Schema

/**
* The default schema used for the current session.
* @var string
* @var string
*/
public $defaultSchema = 'public';

/**
* @var array mapping from physical column types (keys) to abstract
* @var array mapping from physical column types (keys) to abstract
* column types (values)
*/
public $typeMap = array(
'abstime' => self::TYPE_TIMESTAMP,
'bit' => self::TYPE_STRING,
'boolean' => self::TYPE_BOOLEAN,
'box' => self::TYPE_STRING,
'character' => self::TYPE_STRING,
'bytea' => self::TYPE_BINARY,
'char' => self::TYPE_STRING,
'cidr' => self::TYPE_STRING,
'circle' => self::TYPE_STRING,
'date' => self::TYPE_DATE,
'real' => self::TYPE_FLOAT,
'double precision' => self::TYPE_DECIMAL,
'inet' => self::TYPE_STRING,
'smallint' => self::TYPE_SMALLINT,
'integer' => self::TYPE_INTEGER,
'bigint' => self::TYPE_BIGINT,
'interval' => self::TYPE_STRING,
'json' => self::TYPE_STRING,
'line' => self::TYPE_STRING,
'macaddr' => self::TYPE_STRING,
'money' => self::TYPE_MONEY,
'name' => self::TYPE_STRING,
'numeric' => self::TYPE_STRING,
'numrange' => self::TYPE_DECIMAL,
'oid' => self::TYPE_BIGINT, // should not be used. it's pg internal!
'path' => self::TYPE_STRING,
'point' => self::TYPE_STRING,
'polygon' => self::TYPE_STRING,
'text' => self::TYPE_TEXT,
'time without time zone' => self::TYPE_TIME,
'timestamp without time zone' => self::TYPE_TIMESTAMP,
'timestamp with time zone' => self::TYPE_TIMESTAMP,
'time with time zone' => self::TYPE_TIMESTAMP,
'unknown' => self::TYPE_STRING,
'uuid' => self::TYPE_STRING,
'bit varying' => self::TYPE_STRING,
'character varying' => self::TYPE_STRING,
'xml' => self::TYPE_STRING
'abstime' => self::TYPE_TIMESTAMP,
'bit' => self::TYPE_STRING,
'boolean' => self::TYPE_BOOLEAN,
'box' => self::TYPE_STRING,
'character' => self::TYPE_STRING,
'bytea' => self::TYPE_BINARY,
'char' => self::TYPE_STRING,
'cidr' => self::TYPE_STRING,
'circle' => self::TYPE_STRING,
'date' => self::TYPE_DATE,
'real' => self::TYPE_FLOAT,
'double precision' => self::TYPE_DECIMAL,
'inet' => self::TYPE_STRING,
'smallint' => self::TYPE_SMALLINT,
'integer' => self::TYPE_INTEGER,
'bigint' => self::TYPE_BIGINT,
'interval' => self::TYPE_STRING,
'json' => self::TYPE_STRING,
'line' => self::TYPE_STRING,
'macaddr' => self::TYPE_STRING,
'money' => self::TYPE_MONEY,
'name' => self::TYPE_STRING,
'numeric' => self::TYPE_STRING,
'numrange' => self::TYPE_DECIMAL,
'oid' => self::TYPE_BIGINT, // should not be used. it's pg internal!
'path' => self::TYPE_STRING,
'point' => self::TYPE_STRING,
'polygon' => self::TYPE_STRING,
'text' => self::TYPE_TEXT,
'time without time zone' => self::TYPE_TIME,
'timestamp without time zone' => self::TYPE_TIMESTAMP,
'timestamp with time zone' => self::TYPE_TIMESTAMP,
'time with time zone' => self::TYPE_TIMESTAMP,
'unknown' => self::TYPE_STRING,
'uuid' => self::TYPE_STRING,
'bit varying' => self::TYPE_STRING,
'character varying' => self::TYPE_STRING,
'xml' => self::TYPE_STRING
);

/**
* Creates a query builder for the MySQL database.
* @return QueryBuilder query builder instance
*/
public function createQueryBuilder() {
public function createQueryBuilder()
{
return new QueryBuilder($this->db);
}

Expand All @@ -85,7 +86,8 @@ public function createQueryBuilder() {
* @param TableSchema $table the table metadata object
* @param string $name the table name
*/
protected function resolveTableNames($table, $name) {
protected function resolveTableNames($table, $name)
{
$parts = explode('.', str_replace('"', '', $name));
if (isset($parts[1])) {
$table->schemaName = $parts[0];
Expand All @@ -104,7 +106,8 @@ protected function resolveTableNames($table, $name) {
* @param string $name table name
* @return string the properly quoted table name
*/
public function quoteSimpleTableName($name) {
public function quoteSimpleTableName($name)
{
return strpos($name, '"') !== false ? $name : '"' . $name . '"';
}

Expand All @@ -113,20 +116,24 @@ public function quoteSimpleTableName($name) {
* @param string $name table name
* @return TableSchema|null driver dependent table metadata. Null if the table does not exist.
*/
public function loadTableSchema($name) {
public function loadTableSchema($name)
{
$table = new TableSchema();
$this->resolveTableNames($table, $name);
if ($this->findColumns($table)) {
$this->findConstraints($table);
return $table;
} else {
return null;
}
}

/**
* Collects the foreign key column details for the given table.
* @param TableSchema $table the table metadata
*/
protected function findConstraints($table) {
protected function findConstraints($table)
{

$tableName = $this->quoteValue($table->name);
$tableSchema = $this->quoteValue($table->schemaName);
Expand Down Expand Up @@ -175,7 +182,8 @@ protected function findConstraints($table) {
* @param TableSchema $table the table metadata
* @return boolean whether the table exists in the database
*/
protected function findColumns($table) {
protected function findColumns($table)
{
$tableName = $this->db->quoteValue($table->name);
$schemaName = $this->db->quoteValue($table->schemaName);
$sql = <<<SQL
Expand Down Expand Up @@ -234,11 +242,11 @@ protected function findColumns($table) {
a.attnum;
SQL;

try {
$columns = $this->db->createCommand($sql)->queryAll();
} catch (\Exception $e) {
$columns = $this->db->createCommand($sql)->queryAll();
if (empty($columns)) {
return false;
}

foreach ($columns as $column) {
$column = $this->loadColumnSchema($column);
$table->columns[$column->name] = $column;
Expand All @@ -257,7 +265,8 @@ protected function findColumns($table) {
* @param array $info column information
* @return ColumnSchema the column schema object
*/
protected function loadColumnSchema($info) {
protected function loadColumnSchema($info)
{
$column = new ColumnSchema();
$column->allowNull = $info['is_nullable'];
$column->autoIncrement = $info['is_autoinc'];
Expand All @@ -280,5 +289,4 @@ protected function loadColumnSchema($info) {
$column->phpType = $this->getColumnPhpType($column);
return $column;
}

}
}

0 comments on commit cf1e12a

Please sign in to comment.