Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1050 from sergeysviridenko/3.2.create_reference
Added describeReferences method in postgresql DB
- Loading branch information
Showing
3 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
/* | ||
+------------------------------------------------------------------------+ | ||
| Phalcon Framework | | ||
+------------------------------------------------------------------------+ | ||
| Copyright (c) 2011-2017 Phalcon Team (http://www.phalconphp.com) | | ||
+------------------------------------------------------------------------+ | ||
| This source file is subject to the New BSD License that is bundled | | ||
| with this package in the file LICENSE.txt. | | ||
| | | ||
| If you did not receive a copy of the license and are unable to | | ||
| obtain it through the world-wide-web, please send an email | | ||
| to license@phalconphp.com so we can send you a copy immediately. | | ||
+------------------------------------------------------------------------+ | ||
| Authors: Sergii Svyrydenko <sergey.v.sviridenko@gmail.com> | | ||
| | | ||
+------------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Phalcon\Db\Adapter\Pdo; | ||
|
||
use Phalcon\Db\ReferenceInterface; | ||
use Phalcon\Db\Reference; | ||
use Phalcon\Db\Exception; | ||
use Phalcon\Db; | ||
|
||
/** | ||
* Phalcon\Db\Dialect\Postgresql | ||
* | ||
* @package Phalcon\Db\Adapter\Pdo | ||
*/ | ||
class PostgresqlExtended extends Postgresql | ||
{ | ||
/** | ||
* Lists table references | ||
* | ||
* @param string $table | ||
* @param string $schema | ||
* @return Reference | ||
* | ||
*/ | ||
public function describeReferences($table, $schema = NULL) | ||
{ | ||
$references = []; | ||
|
||
foreach ($this->fetchAll($this->_dialect->describeReferences($table, $schema),Db::FETCH_NUM) as $reference) { | ||
$constraintName = $reference[2]; | ||
if (!isset($references[$constraintName])) { | ||
$referencedSchema = $reference[3]; | ||
$referencedTable = $reference[4]; | ||
$referenceUpdate = $reference[6]; | ||
$referenceDelete = $reference[7]; | ||
$columns = []; | ||
$referencedColumns = []; | ||
} else { | ||
$referencedSchema = $references[$constraintName]["referencedSchema"]; | ||
$referencedTable = $references[$constraintName]["referencedTable"]; | ||
$columns = $references[$constraintName]["columns"]; | ||
$referencedColumns = $references[$constraintName]["referencedColumns"]; | ||
$referenceUpdate = $references[$constraintName]["onUpdate"]; | ||
$referenceDelete = $references[$constraintName]["onDelete"]; | ||
} | ||
|
||
$columns[] = $reference[1]; | ||
$referencedColumns[] = $reference[5]; | ||
|
||
$references[$constraintName] = [ | ||
"referencedSchema" => $referencedSchema, | ||
"referencedTable" => $referencedTable, | ||
"columns" => $columns, | ||
"referencedColumns" => $referencedColumns, | ||
"onUpdate" => $referenceUpdate, | ||
"onDelete" => $referenceDelete | ||
]; | ||
} | ||
|
||
$referenceObjects = []; | ||
|
||
foreach ($references as $name => $arrayReference) { | ||
$referenceObjects[$name] = new Reference($name, [ | ||
"referencedSchema" => $arrayReference["referencedSchema"], | ||
"referencedTable" => $arrayReference["referencedTable"], | ||
"columns" => $arrayReference["columns"], | ||
"referencedColumns" => $arrayReference["referencedColumns"], | ||
"onUpdate" => $arrayReference["onUpdate"], | ||
"onDelete" => $arrayReference["onDelete"] | ||
]); | ||
} | ||
|
||
return $referenceObjects; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/* | ||
+------------------------------------------------------------------------+ | ||
| Phalcon Framework | | ||
+------------------------------------------------------------------------+ | ||
| Copyright (c) 2011-2017 Phalcon Team (http://www.phalconphp.com) | | ||
+------------------------------------------------------------------------+ | ||
| This source file is subject to the New BSD License that is bundled | | ||
| with this package in the file LICENSE.txt. | | ||
| | | ||
| If you did not receive a copy of the license and are unable to | | ||
| obtain it through the world-wide-web, please send an email | | ||
| to license@phalconphp.com so we can send you a copy immediately. | | ||
+------------------------------------------------------------------------+ | ||
| Authors: Sergii Svyrydenko <sergey.v.sviridenko@gmail.com> | | ||
| | | ||
+------------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Phalcon\Db\Dialect; | ||
|
||
use Phalcon\Db\ReferenceInterface; | ||
|
||
/** | ||
* Phalcon\Db\Dialect\PostgresqlExtended | ||
* | ||
* @package Phalcon\Db\Dialect | ||
*/ | ||
class PostgresqlExtended extends Postgresql | ||
{ | ||
/** | ||
* Generates SQL to query foreign keys on a table | ||
* | ||
* @param string $table | ||
* @param string $schema | ||
* @return string | ||
*/ | ||
public function describeReferences($table, $schema = NULL) | ||
{ | ||
$sql = " | ||
SELECT DISTINCT | ||
tc.table_name as TABLE_NAME, | ||
kcu.column_name as COLUMN_NAME, | ||
tc.constraint_name as CONSTRAINT_NAME, | ||
tc.table_catalog as REFERENCED_TABLE_SCHEMA, | ||
ccu.table_name AS REFERENCED_TABLE_NAME, | ||
ccu.column_name AS REFERENCED_COLUMN_NAME, | ||
rc.update_rule AS UPDATE_RULE, | ||
rc.delete_rule AS DELETE_RULE | ||
FROM information_schema.table_constraints AS tc | ||
JOIN information_schema.key_column_usage AS kcu | ||
ON tc.constraint_name = kcu.constraint_name | ||
JOIN information_schema.constraint_column_usage AS ccu | ||
ON ccu.constraint_name = tc.constraint_name | ||
JOIN information_schema.referential_constraints rc | ||
ON tc.constraint_catalog = rc.constraint_catalog | ||
AND tc.constraint_schema = rc.constraint_schema | ||
AND tc.constraint_name = rc.constraint_name | ||
AND tc.constraint_type = 'FOREIGN KEY' | ||
WHERE constraint_type = 'FOREIGN KEY' | ||
AND "; | ||
|
||
if ($schema) { | ||
$sql .= "tc.table_schema = '" . $schema . "' AND tc.table_name='" . $table . "'"; | ||
} else { | ||
$sql .= "tc.table_schema = 'public' AND tc.table_name='" . $table . "'"; | ||
} | ||
|
||
return $sql; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters