Browse Source

Savepoint, implementing fkeys.

tags/2.0.0-beta
Gevik Babakhani 12 years ago
parent
commit
97270a389a
  1. 76
      framework/yii/db/pgsql/Schema.php
  2. 5
      tests/unit/data/postgres.sql

76
framework/yii/db/pgsql/Schema.php

@ -136,12 +136,30 @@ class Schema extends \yii\db\Schema {
$table = new TableSchema(); $table = new TableSchema();
$this->resolveTableNames($table, $name); $this->resolveTableNames($table, $name);
if ($this->findColumns($table)) { if ($this->findColumns($table)) {
$this->findForeignKeys($table); $this->findConstraints($table);
return $table; return $table;
} }
} }
/** /**
* Collects the foreign key column details for the given table.
* @param TableSchema $table the table metadata
*/
protected function findConstraints($table) {
try {
$constraints = $this->db->createCommand($sql)->queryAll();
} catch (\Exception $e) {
return false;
}
foreach ($constraints as $constraint) {
$column = $this->loadColumnSchema($column);
$table->columns[$column->name] = $column;
}
return true;
}
/**
* Collects the metadata of table columns. * Collects the metadata of table columns.
* @param TableSchema $table the table metadata * @param TableSchema $table the table metadata
* @return boolean whether the table exists in the database * @return boolean whether the table exists in the database
@ -163,13 +181,41 @@ SELECT
a.attnotnull = false AS is_nullable, a.attnotnull = false AS is_nullable,
CAST(pg_get_expr(ad.adbin, ad.adrelid) AS varchar) AS column_default, CAST(pg_get_expr(ad.adbin, ad.adrelid) AS varchar) AS column_default,
coalesce(pg_get_expr(ad.adbin, ad.adrelid) ~ 'nextval',false) AS is_autoinc, coalesce(pg_get_expr(ad.adbin, ad.adrelid) ~ 'nextval',false) AS is_autoinc,
array_to_string((select array_agg(enumlabel) from pg_enum where enumtypid=a.atttypid)::varchar[],',') as enum_values array_to_string((select array_agg(enumlabel) from pg_enum where enumtypid=a.atttypid)::varchar[],',') as enum_values,
CASE atttypid
WHEN 21 /*int2*/ THEN 16
WHEN 23 /*int4*/ THEN 32
WHEN 20 /*int8*/ THEN 64
WHEN 1700 /*numeric*/ THEN
CASE WHEN atttypmod = -1
THEN null
ELSE ((atttypmod - 4) >> 16) & 65535
END
WHEN 700 /*float4*/ THEN 24 /*FLT_MANT_DIG*/
WHEN 701 /*float8*/ THEN 53 /*DBL_MANT_DIG*/
ELSE null
END AS numeric_precision,
CASE
WHEN atttypid IN (21, 23, 20) THEN 0
WHEN atttypid IN (1700) THEN
CASE
WHEN atttypmod = -1 THEN null
ELSE (atttypmod - 4) & 65535
END
ELSE null
END AS numeric_scale,
CAST(
information_schema._pg_char_max_length(information_schema._pg_truetypid(a, t), information_schema._pg_truetypmod(a, t))
AS numeric
) AS size,
a.attnum = any (ct.conkey) as is_pkey
FROM FROM
pg_class c pg_class c
LEFT JOIN pg_attribute a ON a.attrelid = c.oid LEFT JOIN pg_attribute a ON a.attrelid = c.oid
LEFT JOIN pg_attrdef ad ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum LEFT JOIN pg_attrdef ad ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum
LEFT JOIN pg_type t ON a.atttypid = t.oid LEFT JOIN pg_type t ON a.atttypid = t.oid
LEFT JOIN pg_namespace d ON d.oid = c.relnamespace LEFT JOIN pg_namespace d ON d.oid = c.relnamespace
LEFT join pg_constraint ct on ct.conrelid=c.oid and ct.contype='p'
WHERE WHERE
a.attnum > 0 a.attnum > 0
and c.relname = {$tableName} and c.relname = {$tableName}
@ -186,10 +232,9 @@ SQL;
} }
foreach ($columns as $column) { foreach ($columns as $column) {
$column = $this->loadColumnSchema($column); $column = $this->loadColumnSchema($column);
if ($column->name == 'numbers') $table->columns[$column->name] = $column;
print_r($column);
} }
die(); return true;
} }
/** /**
@ -205,14 +250,19 @@ SQL;
$column->dbType = $info['data_type']; $column->dbType = $info['data_type'];
$column->defaultValue = $info['column_default']; $column->defaultValue = $info['column_default'];
$column->enumValues = explode(',', str_replace(array("''"), array("'"), $info['enum_values'])); $column->enumValues = explode(',', str_replace(array("''"), array("'"), $info['enum_values']));
//$column->isPrimaryKey $column->unsigned = false; // has no meanining in PG
$column->isPrimaryKey = $info['is_pkey'];
$column->name = $info['column_name']; $column->name = $info['column_name'];
//$column->phpType $column->precision = $info['numeric_precision'];
//$column->precision $column->scale = $info['numeric_scale'];
//$column->scale $column->size = $info['size'];
//$column->size;
//$column->type if (isset($this->typeMap[$column->dbType])) {
//$column->unsigned $column->type = $this->typeMap[$column->dbType];
} else {
$column->type = self::TYPE_STRING;
}
$column->phpType = $this->getColumnPhpType($column);
return $column; return $column;
} }

5
tests/unit/data/postgres.sql

@ -22,11 +22,14 @@ CREATE TABLE tbl_customer (
id serial not null primary key, id serial not null primary key,
email varchar(128) NOT NULL, email varchar(128) NOT NULL,
name varchar(128) NOT NULL, name varchar(128) NOT NULL,
age numeric(3),
zipcode varchar(6),
address text, address text,
status integer DEFAULT 0, status integer DEFAULT 0,
fullname fullname, fullname fullname,
mood mood, mood mood,
numbers integer[] numbers integer[],
amount numeric(6,4)
); );
comment on column public.tbl_customer.email is 'someone@example.com'; comment on column public.tbl_customer.email is 'someone@example.com';

Loading…
Cancel
Save