Skip to content

Commit

Permalink
Merge pull request #1050 from sergeysviridenko/3.2.create_reference
Browse files Browse the repository at this point in the history
Added describeReferences method in postgresql DB
  • Loading branch information
sergeyklay committed Jul 17, 2017
2 parents b7857ce + fef90b6 commit 61e8163
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 0 deletions.
93 changes: 93 additions & 0 deletions scripts/Phalcon/Db/Adapter/Pdo/PostgresqlExtended.php
@@ -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;
}
}
72 changes: 72 additions & 0 deletions scripts/Phalcon/Db/Dialect/PostgresqlExtended.php
@@ -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;
}
}
10 changes: 10 additions & 0 deletions scripts/Phalcon/Mvc/Model/Migration.php
Expand Up @@ -35,6 +35,8 @@
use Phalcon\Version\ItemCollection as VersionCollection;
use Phalcon\Db\Dialect\MysqlExtended;
use Phalcon\Db\Adapter\Pdo\MysqlExtended as AdapterMysqlExtended;
use Phalcon\Db\Dialect\PostgresqlExtended;
use Phalcon\Db\Adapter\Pdo\PostgresqlExtended as AdapterPostgresqlExtended;

/**
* Phalcon\Mvc\Model\Migration
Expand Down Expand Up @@ -83,6 +85,7 @@ class Migration
* Prepares component
*
* @param \Phalcon\Config $database Database config
* @since 3.2.1 Using Postgresql::describeReferences and PostgresqlExtended dialect class
*
* @throws \Phalcon\Db\Exception
*/
Expand All @@ -99,6 +102,8 @@ public static function setup($database)
*/
if ($database->adapter == 'Mysql') {
$adapter = AdapterMysqlExtended::class;
} elseif ($database->adapter == 'Postgresql') {
$adapter = AdapterPostgresqlExtended::class;
} else {
$adapter = '\\Phalcon\\Db\\Adapter\\Pdo\\'.$database->adapter;
}
Expand All @@ -117,6 +122,11 @@ public static function setup($database)
self::$_connection->setDialect(new MysqlExtended);
}

//Connection custom dialect Dialect/PostgresqlExtended
if ($database->adapter == 'Postgresql') {
self::$_connection->setDialect(new PostgresqlExtended);
}

if (Migrations::isConsole()) {
$profiler = new Profiler();

Expand Down

0 comments on commit 61e8163

Please sign in to comment.