Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
70e97312
Commit
70e97312
authored
Jun 06, 2013
by
Gevik Babakhani
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added foreign keys and cleanup internal pg types
parent
3d79d992
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
38 deletions
+71
-38
QueryBuilder.php
framework/yii/db/pgsql/QueryBuilder.php
+1
-9
Schema.php
framework/yii/db/pgsql/Schema.php
+70
-29
No files found.
framework/yii/db/pgsql/QueryBuilder.php
View file @
70e97312
...
@@ -8,9 +8,6 @@
...
@@ -8,9 +8,6 @@
namespace
yii\db\pgsql
;
namespace
yii\db\pgsql
;
use
yii\db\Exception
;
use
yii\base\InvalidParamException
;
/**
/**
* QueryBuilder is the query builder for PostgreSQL databases.
* QueryBuilder is the query builder for PostgreSQL databases.
*
*
...
@@ -24,7 +21,7 @@ class QueryBuilder extends \yii\db\QueryBuilder {
...
@@ -24,7 +21,7 @@ class QueryBuilder extends \yii\db\QueryBuilder {
*/
*/
public
$typeMap
=
array
(
public
$typeMap
=
array
(
Schema
::
TYPE_PK
=>
'bigserial not null primary key'
,
Schema
::
TYPE_PK
=>
'bigserial not null primary key'
,
Schema
::
TYPE_STRING
=>
'varchar
(255)
'
,
Schema
::
TYPE_STRING
=>
'varchar'
,
Schema
::
TYPE_TEXT
=>
'text'
,
Schema
::
TYPE_TEXT
=>
'text'
,
Schema
::
TYPE_SMALLINT
=>
'smallint'
,
Schema
::
TYPE_SMALLINT
=>
'smallint'
,
Schema
::
TYPE_INTEGER
=>
'integer'
,
Schema
::
TYPE_INTEGER
=>
'integer'
,
...
@@ -40,9 +37,4 @@ class QueryBuilder extends \yii\db\QueryBuilder {
...
@@ -40,9 +37,4 @@ class QueryBuilder extends \yii\db\QueryBuilder {
Schema
::
TYPE_MONEY
=>
'numeric(19,4)'
,
Schema
::
TYPE_MONEY
=>
'numeric(19,4)'
,
);
);
public
function
insert
(
$table
,
$columns
,
&
$params
)
{
$sql
=
parent
::
insert
(
$table
,
$columns
,
$params
);
return
$sql
.
' RETURNING *'
;
}
}
}
framework/yii/db/pgsql/Schema.php
View file @
70e97312
...
@@ -33,31 +33,24 @@ class Schema extends \yii\db\Schema {
...
@@ -33,31 +33,24 @@ class Schema extends \yii\db\Schema {
*/
*/
public
$typeMap
=
array
(
public
$typeMap
=
array
(
'abstime'
=>
self
::
TYPE_TIMESTAMP
,
'abstime'
=>
self
::
TYPE_TIMESTAMP
,
//'aclitem' => self::TYPE_STRING,
'bit'
=>
self
::
TYPE_STRING
,
'bit'
=>
self
::
TYPE_STRING
,
'boolean'
=>
self
::
TYPE_BOOLEAN
,
'boolean'
=>
self
::
TYPE_BOOLEAN
,
'box'
=>
self
::
TYPE_STRING
,
'box'
=>
self
::
TYPE_STRING
,
'character'
=>
self
::
TYPE_STRING
,
'character'
=>
self
::
TYPE_STRING
,
'bytea'
=>
self
::
TYPE_BINARY
,
'bytea'
=>
self
::
TYPE_BINARY
,
'char'
=>
self
::
TYPE_STRING
,
'char'
=>
self
::
TYPE_STRING
,
//'cid' => self::TYPE_STRING,
'cidr'
=>
self
::
TYPE_STRING
,
'cidr'
=>
self
::
TYPE_STRING
,
'circle'
=>
self
::
TYPE_STRING
,
'circle'
=>
self
::
TYPE_STRING
,
'date'
=>
self
::
TYPE_DATE
,
'date'
=>
self
::
TYPE_DATE
,
//'daterange' => self::TYPE_STRING,
'real'
=>
self
::
TYPE_FLOAT
,
'real'
=>
self
::
TYPE_FLOAT
,
'double precision'
=>
self
::
TYPE_DECIMAL
,
'double precision'
=>
self
::
TYPE_DECIMAL
,
//'gtsvector' => self::TYPE_STRING,
'inet'
=>
self
::
TYPE_STRING
,
'inet'
=>
self
::
TYPE_STRING
,
'smallint'
=>
self
::
TYPE_SMALLINT
,
'smallint'
=>
self
::
TYPE_SMALLINT
,
'integer'
=>
self
::
TYPE_INTEGER
,
'integer'
=>
self
::
TYPE_INTEGER
,
//'int4range' => self::TYPE_STRING, //unknown
'bigint'
=>
self
::
TYPE_BIGINT
,
'bigint'
=>
self
::
TYPE_BIGINT
,
//'int8range' => self::TYPE_STRING, // unknown
'interval'
=>
self
::
TYPE_STRING
,
'interval'
=>
self
::
TYPE_STRING
,
'json'
=>
self
::
TYPE_STRING
,
'json'
=>
self
::
TYPE_STRING
,
'line'
=>
self
::
TYPE_STRING
,
'line'
=>
self
::
TYPE_STRING
,
//'lseg' => self::TYPE_STRING,
'macaddr'
=>
self
::
TYPE_STRING
,
'macaddr'
=>
self
::
TYPE_STRING
,
'money'
=>
self
::
TYPE_MONEY
,
'money'
=>
self
::
TYPE_MONEY
,
'name'
=>
self
::
TYPE_STRING
,
'name'
=>
self
::
TYPE_STRING
,
...
@@ -65,38 +58,49 @@ class Schema extends \yii\db\Schema {
...
@@ -65,38 +58,49 @@ class Schema extends \yii\db\Schema {
'numrange'
=>
self
::
TYPE_DECIMAL
,
'numrange'
=>
self
::
TYPE_DECIMAL
,
'oid'
=>
self
::
TYPE_BIGINT
,
// should not be used. it's pg internal!
'oid'
=>
self
::
TYPE_BIGINT
,
// should not be used. it's pg internal!
'path'
=>
self
::
TYPE_STRING
,
'path'
=>
self
::
TYPE_STRING
,
//'pg_node_tree' => self::TYPE_STRING,
'point'
=>
self
::
TYPE_STRING
,
'point'
=>
self
::
TYPE_STRING
,
'polygon'
=>
self
::
TYPE_STRING
,
'polygon'
=>
self
::
TYPE_STRING
,
//'refcursor' => self::TYPE_STRING,
//'regclass' => self::TYPE_STRING,
//'regconfig' => self::TYPE_STRING,
//'regdictionary' => self::TYPE_STRING,
//'regoper' => self::TYPE_STRING,
//'regoperator' => self::TYPE_STRING,
//'regproc' => self::TYPE_STRING,
//'regprocedure' => self::TYPE_STRING,
//'regtype' => self::TYPE_STRING,
//'reltime' => self::TYPE_STRING,
//'smgr' => self::TYPE_STRING,
'text'
=>
self
::
TYPE_TEXT
,
'text'
=>
self
::
TYPE_TEXT
,
//'tid' => self::TYPE_STRING,
'time without time zone'
=>
self
::
TYPE_TIME
,
'time without time zone'
=>
self
::
TYPE_TIME
,
'timestamp without time zone'
=>
self
::
TYPE_TIMESTAMP
,
'timestamp without time zone'
=>
self
::
TYPE_TIMESTAMP
,
'timestamp with time zone'
=>
self
::
TYPE_TIMESTAMP
,
'timestamp with time zone'
=>
self
::
TYPE_TIMESTAMP
,
'time with time zone'
=>
self
::
TYPE_TIMESTAMP
,
'time with time zone'
=>
self
::
TYPE_TIMESTAMP
,
//'tinterval' => self::TYPE_STRING,
//'tsquery' => self::TYPE_STRING,
//'tsrange' => self::TYPE_STRING,
//'tstzrange' => self::TYPE_STRING,
//'tsvector' => self::TYPE_STRING,
//'txid_snapshot' => self::TYPE_STRING,
'unknown'
=>
self
::
TYPE_STRING
,
'unknown'
=>
self
::
TYPE_STRING
,
'uuid'
=>
self
::
TYPE_STRING
,
'uuid'
=>
self
::
TYPE_STRING
,
'bit varying'
=>
self
::
TYPE_STRING
,
'bit varying'
=>
self
::
TYPE_STRING
,
'character varying'
=>
self
::
TYPE_STRING
,
'character varying'
=>
self
::
TYPE_STRING
,
//'xid' => self::TYPE_STRING,
'xml'
=>
self
::
TYPE_STRING
'xml'
=>
self
::
TYPE_STRING
/*
* internal PG types
* 'aclitem' => self::TYPE_STRING,
* 'cid' => self::TYPE_STRING,
* 'daterange' => self::TYPE_STRING,
* 'gtsvector' => self::TYPE_STRING,
* 'int4range' => self::TYPE_STRING, //unknown
* 'lseg' => self::TYPE_STRING,
* 'int8range' => self::TYPE_STRING, // unknown
* 'pg_node_tree' => self::TYPE_STRING,
* 'refcursor' => self::TYPE_STRING,
* 'regclass' => self::TYPE_STRING,
* 'regconfig' => self::TYPE_STRING,
* 'regdictionary' => self::TYPE_STRING,
* 'regoper' => self::TYPE_STRING,
* 'regoperator' => self::TYPE_STRING,
* 'regproc' => self::TYPE_STRING,
* 'regprocedure' => self::TYPE_STRING,
* 'regtype' => self::TYPE_STRING,
* 'reltime' => self::TYPE_STRING,
* 'smgr' => self::TYPE_STRING,
* 'tid' => self::TYPE_STRING,
* 'xid' => self::TYPE_STRING,
* 'tinterval' => self::TYPE_STRING,
* 'tsquery' => self::TYPE_STRING,
* 'tsrange' => self::TYPE_STRING,
* 'tstzrange' => self::TYPE_STRING,
* 'tsvector' => self::TYPE_STRING,
* 'txid_snapshot' => self::TYPE_STRING
*/
);
);
/**
/**
...
@@ -155,14 +159,51 @@ class Schema extends \yii\db\Schema {
...
@@ -155,14 +159,51 @@ class Schema extends \yii\db\Schema {
*/
*/
protected
function
findConstraints
(
$table
)
{
protected
function
findConstraints
(
$table
)
{
$tableName
=
$this
->
quoteValue
(
$table
->
name
);
$tableSchema
=
$this
->
quoteValue
(
$table
->
schemaName
);
$database
=
$this
->
quoteValue
(
$this
->
db
->
pdo
->
getCurrentDatabase
());
//We need to extract the constraints de hard way since:
//http://www.postgresql.org/message-id/26677.1086673982@sss.pgh.pa.us
$sql
=
<<<SQL
select
ct.conname as containst,
c.relname as table_name,
ns.nspname as table_schema,
current_database() as table_catalog,
(select string_agg(attname,',') attname from pg_attribute where attrelid=ct.conrelid and attnum = any(ct.conkey)) as columns,
fc.relname as foreign_table_name,
fns.nspname as foreign_table_schema,
current_database() as foreign_table_catalog,
(select string_agg(attname,',') attname from pg_attribute where attrelid=ct.confrelid and attnum = any(ct.confkey)) as foreign_columns
from
pg_constraint ct
inner join pg_class c on c.oid=ct.conrelid
inner join pg_namespace ns on c.relnamespace=ns.oid
left join pg_class fc on fc.oid=ct.confrelid
left join pg_namespace fns on fc.relnamespace=fns.oid
where
ct.contype='f'
and c.relname={$tableName}
and ns.nspname={$tableSchema}
and current_database() = {$database}
SQL;
try
{
try
{
$constraints
=
$this
->
db
->
createCommand
(
$sql
)
->
queryAll
();
$constraints
=
$this
->
db
->
createCommand
(
$sql
)
->
queryAll
();
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
return
false
;
return
false
;
}
}
foreach
(
$constraints
as
$constraint
)
{
foreach
(
$constraints
as
$constraint
)
{
$column
=
$this
->
loadColumnSchema
(
$column
);
$columns
=
explode
(
','
,
$constraint
[
'columns'
]);
$table
->
columns
[
$column
->
name
]
=
$column
;
$fcolumns
=
explode
(
','
,
$constraint
[
'foreign_columns'
]);
$citem
=
array
(
$constraint
[
'foreign_table_name'
]);
foreach
(
$columns
as
$idx
=>
$column
)
{
$citem
[]
=
array
(
$fcolumns
[
$idx
]
=>
$column
);
}
$table
->
foreignKeys
[]
=
$citem
;
}
}
return
true
;
return
true
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment