Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added describeReferences method in postgresql DB #12969

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# [3.2.2](https://github.com/phalcon/cphalcon/releases/tag/v3.2.2) (2017-XX-XX)
- Fixed `Phalcon\Db\Adapter\Pdo\Postgresql::describeColumns` to work properly with `DOUBLE PRECISION` and `REAL` data types [#12842](https://github.com/phalcon/cphalcon/issues/12842)
- Fixed `Phalcon\Paginator\Adapter\QueryBuilder::getPaginate` to use the db connection service of the model [#12957](https://github.com/phalcon/cphalcon/issues/12957)
- Fixed `Phalcon\Paginator\Adapter\QueryBuilder::getPaginate` to escape reserverd words [#12950](https://github.com/phalcon/cphalcon/issues/12950)


# [3.2.1](https://github.com/phalcon/cphalcon/releases/tag/v3.2.1) (2017-07-10)
- Added `Phalcon\Db\Dialect\Mysql::getForeignKeyChecks` to generate a SQL to check the foreign key settings [#2604](https://github.com/phalcon/cphalcon/issues/2604), [phalcon/phalcon-devtools#556](https://github.com/phalcon/phalcon-devtools/issues/556)
Expand All @@ -12,6 +14,8 @@
- Fixed `Phalcon\Cache\Backend\Apcu::flush` to use APCu instead APC [#12934](https://github.com/phalcon/cphalcon/issues/12934)
- Fixed `Phalcon\Db\Adapter\Pdo\Mysql::addForeignKey` for proper creating the foreign key with a desired key name [#2604](https://github.com/phalcon/cphalcon/issues/2604), [phalcon/phalcon-devtools#556](https://github.com/phalcon/phalcon-devtools/issues/556)
- Fixed `Phalcon\Db\Dialect\Mysql::addForeignKey` to generate correct SQL [#2604](https://github.com/phalcon/cphalcon/issues/2604), [phalcon/phalcon-devtools#556](https://github.com/phalcon/phalcon-devtools/issues/556)
- Fixed `Phalcon\Db\Dialect\Postgresql::describeReferences` to generate correct SQL
- Added `Phalcon\Db\Adapter\Pdo\Postgresql::describeReferences` for proper creating `Reference` object [#438](https://github.com/phalcon/phalcon-devtools/issues/438)

# [3.2.0](https://github.com/phalcon/cphalcon/releases/tag/v3.2.0) (2017-06-19)
- Phalcon will now trigger `E_DEPREACATED` by using `Phalcon\Mvc\Model\Criteria::addWhere`, `Phalcon\Debug::getMajorVersion`, `Phalcon\Dispatcher::setModelBinding`, `Phalcon\Tag::resetInput`, `Phalcon\Mvc\Model\Validator::__construct`
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"php": ">=5.5"
},
"require-dev": {
"doctrine/instantiator": "1.0.5",
"phpdocumentor/reflection-docblock": "^2.0",
"phpunit/phpunit": "^4.8",
"mustache/mustache": "^2.11",
Expand Down
71 changes: 70 additions & 1 deletion phalcon/db/adapter/pdo/postgresql.zep
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ use Phalcon\Db\Column;
use Phalcon\Db\RawValue;
use Phalcon\Db\Adapter\Pdo as PdoAdapter;
use Phalcon\Db\Exception;
use Phalcon\Db;
use Phalcon\Db\Reference;
use Phalcon\Db\ReferenceInterface;

/**
* Phalcon\Db\Adapter\Pdo\Postgresql
Expand Down Expand Up @@ -105,7 +108,7 @@ class Postgresql extends PdoAdapter
* We're using FETCH_NUM to fetch the columns
* 0:name, 1:type, 2:size, 3:numericsize, 4: numericscale, 5: null, 6: key, 7: extra, 8: position, 9 default
*/
for field in this->fetchAll(this->_dialect->describeColumns(table, schema), \Phalcon\Db::FETCH_NUM) {
for field in this->fetchAll(this->_dialect->describeColumns(table, schema), Db::FETCH_NUM) {

/**
* By default the bind types is two
Expand Down Expand Up @@ -398,4 +401,70 @@ class Postgresql extends PdoAdapter
{
return true;
}

/**
* Lists table references
*
*<code>
* print_r(
* $connection->describeReferences("robots_parts")
* );
*</code>
*/
public function describeReferences(string! table, string! schema = null) -> <ReferenceInterface[]>
{
var references, reference,
arrayReference, constraintName, referenceObjects, name,
referencedSchema, referencedTable, columns, referencedColumns,
referenceUpdate, referenceDelete;

let references = [];

for reference in this->fetchAll(this->_dialect->describeReferences(table, schema), Db::FETCH_NUM) {

let constraintName = reference[2];
if !isset references[constraintName] {
let referencedSchema = reference[3];
let referencedTable = reference[4];
let referenceUpdate = reference[6];
let referenceDelete = reference[7];
let columns = [];
let referencedColumns = [];

} else {
let referencedSchema = references[constraintName]["referencedSchema"];
let referencedTable = references[constraintName]["referencedTable"];
let columns = references[constraintName]["columns"];
let referencedColumns = references[constraintName]["referencedColumns"];
let referenceUpdate = references[constraintName]["onUpdate"];
let referenceDelete = references[constraintName]["onDelete"];
}

let columns[] = reference[1],
referencedColumns[] = reference[5];

let references[constraintName] = [
"referencedSchema" : referencedSchema,
"referencedTable" : referencedTable,
"columns" : columns,
"referencedColumns" : referencedColumns,
"onUpdate" : referenceUpdate,
"onDelete" : referenceDelete
];
}

let referenceObjects = [];
for name, arrayReference in references {
let 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;
}
}
23 changes: 22 additions & 1 deletion phalcon/db/dialect/postgresql.zep
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,28 @@ class Postgresql extends Dialect
*/
public function describeReferences(string! table, string schema = null) -> string
{
var 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 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 WHERE constraint_type = 'FOREIGN KEY' AND ";
var 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 {
let sql .= "tc.table_schema = '" . schema . "' AND tc.table_name='" . table . "'";
Expand Down
10 changes: 6 additions & 4 deletions phalcon/paginator/adapter/querybuilder.zep
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class QueryBuilder extends Adapter
}

if !hasHaving {
totalBuilder->groupBy(null)->columns(["COUNT(DISTINCT ".groupColumn.") AS rowcount"]);
totalBuilder->groupBy(null)->columns(["COUNT(DISTINCT ".groupColumn.") AS [rowcount]"]);
} else {
totalBuilder->columns(["DISTINCT ".groupColumn]);
}
Expand All @@ -224,15 +224,17 @@ class QueryBuilder extends Adapter
* If we have having perform native count on temp table
*/
if hasHaving {
let sql = totalQuery->getSql();
let modelClass = builder->_models;
let sql = totalQuery->getSql(),
modelClass = builder->_models;

if typeof modelClass == "array" {
let modelClass = array_values(modelClass)[0];
}

let model = new {modelClass}();
let dbService = model->getReadConnectionService();
let db = totalBuilder->getDI()->get(dbService);
let row = db->fetchOne("SELECT COUNT(*) as rowcount FROM (" . sql["sql"] . ") as T1", Db::FETCH_ASSOC, sql["bind"]),
let row = db->fetchOne("SELECT COUNT(*) as \"rowcount\" FROM (" . sql["sql"] . ") as T1", Db::FETCH_ASSOC, sql["bind"]),
rowcount = row ? intval(row["rowcount"]) : 0,
totalPages = intval(ceil(rowcount / limit));
} else {
Expand Down
23 changes: 17 additions & 6 deletions phalcon/security.zep
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,8 @@ class Security implements InjectionAwareInterface

/**
* Testing for LibreSSL
*
* @deprecated Will be removed in 4.0.0
*/
public function hasLibreSsl() -> boolean
{
Expand All @@ -499,9 +501,12 @@ class Security implements InjectionAwareInterface
}

/**
* Getting OpenSSL or LibreSSL version
* Getting OpenSSL or LibreSSL version.
*
* Parse OPENSSL_VERSION_TEXT because OPENSSL_VERSION_NUMBER is no use for LibreSSL.
* This constant show not the current system openssl library version but version PHP was compiled with.
*
* @deprecated Will be removed in 4.0.0
* @link https://bugs.php.net/bug.php?id=71143
*
* <code>
Expand All @@ -512,19 +517,25 @@ class Security implements InjectionAwareInterface
*/
public function getSslVersionNumber() -> int
{
var matches;
var major, minor, patch, matches = null;

preg_match("#^(?:Libre|Open)SSL ([\d]+)\.([\d]+)(\.([\d]+))?$#", OPENSSL_VERSION_TEXT, matches);
if !defined("OPENSSL_VERSION_TEXT") {
return 0;
}

preg_match("#(?:Libre|Open)SSL ([\d]+)\.([\d]+)(?:\.([\d]+))?#", OPENSSL_VERSION_TEXT, matches);

if !isset matches[2] {
return 0;
}

var patch = 0;
let major = (int) matches[1],
minor = (int) matches[2];

if isset matches[3] {
let patch = intval(matches[3]);
let patch = (int) matches[3];
}

return (10000 * intval(matches[2])) + (100 * intval(matches[2])) + patch;
return 10000 * major + 100 * minor + patch;
}
}
2 changes: 1 addition & 1 deletion phalcon/validation/validator/file.zep
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class File extends Validator

let byteUnits = ["B": 0, "K": 10, "M": 20, "G": 30, "T": 40, "KB": 10, "MB": 20, "GB": 30, "TB": 40],
maxSize = this->getOption("maxSize"),
matches = NULL,
matches = null,
unit = "B";

if typeof maxSize == "array" {
Expand Down
3 changes: 3 additions & 0 deletions tests/_ci/install_zephir.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ cp ./bin/zephir-cmd ${HOME}/bin/zephir
rm ./bin/zephir-cmd

cd ${TRAVIS_BUILD_DIR}


zephir version
27 changes: 27 additions & 0 deletions tests/_data/models/Robos.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Phalcon\Test\Models;

use Phalcon\Mvc\Model;

/**
* Robos
*
* "Robôs" is robots in portuguese
*
* @author David Napierata
*
* @package Phalcon\Test\Models
*/
class Robos extends Model
{
public function getSource()
{
return 'robots';
}

public function initialize()
{
$this->setConnectionService('dbTwo');
}
}
1 change: 1 addition & 0 deletions tests/_data/schemas/postgresql/phalcon_test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ ALTER TABLE public.foreign_key_child OWNER TO postgres;

ALTER TABLE public.tipo_documento_id_seq OWNER TO postgres;

ALTER TABLE foreign_key_child ADD CONSTRAINT test_describeReferences FOREIGN KEY (child_int) REFERENCES foreign_key_parent (refer_int) ON UPDATE CASCADE ON DELETE RESTRICT;
--
-- Name: tipo_documento_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--
Expand Down
29 changes: 29 additions & 0 deletions tests/_fixtures/metadata/test_describereference.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Phalcon\Db\Reference;

/**
* Fixture for Reference method
*
* @copyright (c) 2011-2017 Phalcon Team
* @link http://www.phalconphp.com
* @author Sergii Svyrydenko <sergey.v.sviridenko@gmail.com>
* @package Helper\Dialect\PostgresqlTrait
*
* The contents of this file are 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 that we can send you a copy immediately.
*/

return
new Reference('test_describereferences', [
'referencedTable' => 'foreign_key_parent',
'referencedSchema' => 'phalcon_test',
'columns' => ['child_int'],
'referencedColumns' => ['refer_int'],
'onUpdate' => 'CASCADE',
'onDelete' => 'RESTRICT',
]);
18 changes: 18 additions & 0 deletions tests/_fixtures/postgresql/example6.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
SELECT COUNT(tc.constraint_name)
FROM information_schema.table_constraints tc
INNER JOIN information_schema.key_column_usage kcu
ON tc.constraint_catalog = kcu.constraint_catalog
AND tc.constraint_schema = kcu.constraint_schema
AND tc.constraint_name = kcu.constraint_name
INNER 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'
INNER JOIN information_schema.constraint_column_usage ccu
ON rc.unique_constraint_catalog = ccu.constraint_catalog
AND rc.unique_constraint_schema = ccu.constraint_schema
AND rc.unique_constraint_name = ccu.constraint_name
WHERE tc.constraint_name = '$foreignKeyName'
AND rc.update_rule = 'RESTRICT'
AND rc.delete_rule = 'CASCADE'
22 changes: 22 additions & 0 deletions tests/_fixtures/postgresql/example7.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

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 tc.table_schema = 'public' AND tc.table_name='table'
22 changes: 22 additions & 0 deletions tests/_fixtures/postgresql/example8.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

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 tc.table_schema = 'schema' AND tc.table_name='table'
Loading