From 638b1c995175f0de4ae3cdf5149b1a261f76e82d Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Sat, 23 Jul 2016 19:47:27 +0300 Subject: [PATCH 1/3] Cleanup Phalcon\Db\AdapterInterface::connect --- phalcon/db/adapter/pdo.zep | 26 +++++++++++++--------- phalcon/db/adapter/pdo/mysql.zep | 13 +++++------ phalcon/db/adapter/pdo/oracle.zep | 32 ++++++++++++++------------- phalcon/db/adapter/pdo/postgresql.zep | 26 +++++++++++----------- phalcon/db/adapter/pdo/sqlite.zep | 19 ++++++++-------- phalcon/db/adapterinterface.zep | 9 +++----- 6 files changed, 63 insertions(+), 62 deletions(-) diff --git a/phalcon/db/adapter/pdo.zep b/phalcon/db/adapter/pdo.zep index 513777bfe13..82ab15af2a5 100644 --- a/phalcon/db/adapter/pdo.zep +++ b/phalcon/db/adapter/pdo.zep @@ -3,7 +3,7 @@ +------------------------------------------------------------------------+ | Phalcon Framework | +------------------------------------------------------------------------+ - | Copyright (c) 2011-2016 Phalcon Team (https://phalconphp.com) | + | Copyright (c) 2011-2016 Phalcon Team (https://phalconphp.com) | +------------------------------------------------------------------------+ | This source file is subject to the New BSD License that is bundled | | with this package in the file docs/LICENSE.txt. | @@ -31,14 +31,18 @@ use Phalcon\Db\Result\Pdo as ResultPdo; * * Phalcon\Db\Adapter\Pdo is the Phalcon\Db that internally uses PDO to connect to a database * - * - * $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array( - * 'host' => '192.168.0.11', - * 'username' => 'sigma', - * 'password' => 'secret', - * 'dbname' => 'blog', - * 'port' => '3306' - * )); + * + * use Phalcon\Db\Adapter\Pdo\Mysql; + * + * $config = [ + * 'host' => 'localhost', + * 'dbname' => 'blog', + * 'port' => 3306, + * 'username' => 'sigma', + * 'password' => 'secret' + * ]; + * + * $connection = new Mysql($config); * */ abstract class Pdo extends Adapter @@ -83,12 +87,12 @@ abstract class Pdo extends Adapter * @param array descriptor * @return boolean */ - public function connect(descriptor = null) + public function connect(array descriptor = null) -> boolean { var username, password, dsnParts, dsnAttributes, persistent, options, key, value; - if descriptor === null { + if empty descriptor { let descriptor = this->_descriptor; } diff --git a/phalcon/db/adapter/pdo/mysql.zep b/phalcon/db/adapter/pdo/mysql.zep index 87210ad5676..e6adfdd2e14 100644 --- a/phalcon/db/adapter/pdo/mysql.zep +++ b/phalcon/db/adapter/pdo/mysql.zep @@ -3,7 +3,7 @@ +------------------------------------------------------------------------+ | Phalcon Framework | +------------------------------------------------------------------------+ - | Copyright (c) 2011-2016 Phalcon Team (https://phalconphp.com) | + | Copyright (c) 2011-2016 Phalcon Team (https://phalconphp.com) | +------------------------------------------------------------------------+ | This source file is subject to the New BSD License that is bundled | | with this package in the file docs/LICENSE.txt. | @@ -33,15 +33,14 @@ use Phalcon\Db\Adapter\Pdo as PdoAdapter; * Specific functions for the Mysql database system * * - * * use Phalcon\Db\Adapter\Pdo\Mysql; * * $config = [ - * "host" => "192.168.0.11", - * "dbname" => "blog", - * "port" => 3306, - * "username" => "sigma", - * "password" => "secret" + * 'host' => 'localhost', + * 'dbname' => 'blog', + * 'port' => 3306, + * 'username' => 'sigma', + * 'password' => 'secret' * ]; * * $connection = new Mysql($config); diff --git a/phalcon/db/adapter/pdo/oracle.zep b/phalcon/db/adapter/pdo/oracle.zep index 30e1945320d..b97ccac366f 100644 --- a/phalcon/db/adapter/pdo/oracle.zep +++ b/phalcon/db/adapter/pdo/oracle.zep @@ -3,7 +3,7 @@ +------------------------------------------------------------------------+ | Phalcon Framework | +------------------------------------------------------------------------+ - | Copyright (c) 2011-2016 Phalcon Team (https://phalconphp.com) | + | Copyright (c) 2011-2016 Phalcon Team (https://phalconphp.com) | +------------------------------------------------------------------------+ | This source file is subject to the New BSD License that is bundled | | with this package in the file docs/LICENSE.txt. | @@ -29,16 +29,17 @@ use Phalcon\Db\Adapter\Pdo as PdoAdapter; * Phalcon\Db\Adapter\Pdo\Oracle * * Specific functions for the Oracle database system - * * - * $config = array( - * "dbname" => "//localhost/dbname", - * "username" => "oracle", - * "password" => "oracle" - * ); + * + * use Phalcon\Db\Adapter\Pdo\Oracle; * - * $connection = new \Phalcon\Db\Adapter\Pdo\Oracle($config); + * $config = [ + * 'dbname' => '//localhost/dbname', + * 'username' => 'oracle', + * 'password' => 'oracle' + * ]; * + * $connection = new Oracle($config); * */ class Oracle extends PdoAdapter implements AdapterInterface @@ -51,22 +52,21 @@ class Oracle extends PdoAdapter implements AdapterInterface /** * This method is automatically called in Phalcon\Db\Adapter\Pdo constructor. * Call it when you need to restore a database connection. - * - * @param array descriptor - * @return boolean */ - public function connect(descriptor = null) -> boolean + public function connect(array descriptor = null) -> boolean { var startup, value, status; - if !descriptor { + if empty descriptor { let descriptor = this->_descriptor; } let status = parent::connect(descriptor); /** - * Database session settings initiated with each HTTP request. Oracle behaviour depends on particular NLS* parameter. Check if the developer has defined custom startup or create one from scratch + * Database session settings initiated with each HTTP request. + * Oracle behaviour depends on particular NLS* parameter. + * Check if the developer has defined custom startup or create one from scratch */ if fetch startup, descriptor["startup"] { if typeof startup == "array" { @@ -82,7 +82,9 @@ class Oracle extends PdoAdapter implements AdapterInterface /** * Returns an array of Phalcon\Db\Column objects describing a table * - * print_r($connection->describeColumns("posts")); ?> + * + * print_r($connection->describeColumns("posts")); + * */ public function describeColumns(string! table, string schema = null) -> { diff --git a/phalcon/db/adapter/pdo/postgresql.zep b/phalcon/db/adapter/pdo/postgresql.zep index d74fa74ae6c..52e86f9cd95 100644 --- a/phalcon/db/adapter/pdo/postgresql.zep +++ b/phalcon/db/adapter/pdo/postgresql.zep @@ -3,7 +3,7 @@ +------------------------------------------------------------------------+ | Phalcon Framework | +------------------------------------------------------------------------+ - | Copyright (c) 2011-2016 Phalcon Team (https://phalconphp.com) | + | Copyright (c) 2011-2016 Phalcon Team (https://phalconphp.com) | +------------------------------------------------------------------------+ | This source file is subject to the New BSD License that is bundled | | with this package in the file docs/LICENSE.txt. | @@ -30,17 +30,20 @@ use Phalcon\Db\Exception; * Phalcon\Db\Adapter\Pdo\Postgresql * * Specific functions for the Postgresql database system + * * + * use Phalcon\Db\Adapter\Pdo\Postgresql; * - * $config = array( - * "host" => "192.168.0.11", - * "dbname" => "blog", - * "username" => "postgres", - * "password" => "" - * ); + * $config = [ + * 'host' => 'localhost', + * 'dbname' => 'blog', + * 'port' => 5432, + * 'username' => 'postgres', + * 'password' => 'secret' + * ]; * - * $connection = new \Phalcon\Db\Adapter\Pdo\Postgresql($config); * + * $connection = new Postgresql($config); * */ class Postgresql extends PdoAdapter implements AdapterInterface @@ -53,15 +56,12 @@ class Postgresql extends PdoAdapter implements AdapterInterface /** * This method is automatically called in Phalcon\Db\Adapter\Pdo constructor. * Call it when you need to restore a database connection. - * - * @param array $descriptor - * @return boolean */ - public function connect(descriptor = null) + public function connect(array descriptor = null) -> boolean { var schema, sql; - if descriptor === null { + if empty descriptor { let descriptor = this->_descriptor; } diff --git a/phalcon/db/adapter/pdo/sqlite.zep b/phalcon/db/adapter/pdo/sqlite.zep index 5a5092ecb2c..c57ffac5aa7 100644 --- a/phalcon/db/adapter/pdo/sqlite.zep +++ b/phalcon/db/adapter/pdo/sqlite.zep @@ -3,7 +3,7 @@ +------------------------------------------------------------------------+ | Phalcon Framework | +------------------------------------------------------------------------+ - | Copyright (c) 2011-2016 Phalcon Team (https://phalconphp.com) | + | Copyright (c) 2011-2016 Phalcon Team (https://phalconphp.com) | +------------------------------------------------------------------------+ | This source file is subject to the New BSD License that is bundled | | with this package in the file docs/LICENSE.txt. | @@ -35,13 +35,15 @@ use Phalcon\Db\Adapter\Pdo as PdoAdapter; * Phalcon\Db\Adapter\Pdo\Sqlite * * Specific functions for the Sqlite database system + * * + * use Phalcon\Db\Adapter\Pdo\Sqlite; * - * $config = array( - * "dbname" => "/tmp/test.sqlite" - * ); + * $config = [ + * 'dbname' => '/tmp/test.sqlite' + * ]; * - * $connection = new \Phalcon\Db\Adapter\Pdo\Sqlite($config); + * $connection = new Sqlite($config); * */ class Sqlite extends PdoAdapter implements AdapterInterface @@ -54,15 +56,12 @@ class Sqlite extends PdoAdapter implements AdapterInterface /** * This method is automatically called in Phalcon\Db\Adapter\Pdo constructor. * Call it when you need to restore a database connection. - * - * @param array $descriptor - * @return boolean */ - public function connect(descriptor = null) + public function connect(array descriptor = null) -> boolean { var dbname; - if descriptor === null { + if empty descriptor { let descriptor = this->_descriptor; } diff --git a/phalcon/db/adapterinterface.zep b/phalcon/db/adapterinterface.zep index e74f56b6dcb..878e2fcf2c3 100644 --- a/phalcon/db/adapterinterface.zep +++ b/phalcon/db/adapterinterface.zep @@ -20,7 +20,7 @@ namespace Phalcon\Db; /** - * Phalcon\Db\Adapter\Pdo\Mysql + * Phalcon\Db\AdapterInterface * * Interface for Phalcon\Db adapters */ @@ -255,13 +255,10 @@ interface AdapterInterface public function getDialect() -> ; /** - * This method is automatically called in Phalcon\Db\Adapter\Pdo constructor. + * This method is automatically called in \Phalcon\Db\Adapter\Pdo constructor. * Call it when you need to restore a database connection - * - * @param array descriptor - * @return boolean */ - public function connect(descriptor = null); + public function connect(array descriptor = null) -> boolean; /** * Sends SQL statements to the database server returning the success state. From dd5e09986d112f4410262d53cbf26c6c903b5463 Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Sat, 23 Jul 2016 20:44:21 +0300 Subject: [PATCH 2/3] Cleaned Phalcon\Db\Adapter --- phalcon/db/adapter.zep | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/phalcon/db/adapter.zep b/phalcon/db/adapter.zep index 2a38f412ead..8b8d2db32e7 100644 --- a/phalcon/db/adapter.zep +++ b/phalcon/db/adapter.zep @@ -3,7 +3,7 @@ +------------------------------------------------------------------------+ | Phalcon Framework | +------------------------------------------------------------------------+ - | Copyright (c) 2011-2016 Phalcon Team (https://phalconphp.com) | + | Copyright (c) 2011-2016 Phalcon Team (https://phalconphp.com) | +------------------------------------------------------------------------+ | This source file is subject to the New BSD License that is bundled | | with this package in the file docs/LICENSE.txt. | @@ -31,7 +31,6 @@ use Phalcon\Events\ManagerInterface; */ abstract class Adapter implements EventsAwareInterface { - /** * Event Manager * @@ -41,10 +40,8 @@ abstract class Adapter implements EventsAwareInterface /** * Descriptor used to connect to a database - * - * @var \stdClass */ - protected _descriptor; + protected _descriptor = []; /** * Name of the dialect used From 499ef6d0a0fe8f55a6fde0044d20701ee14cc371 Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Sat, 23 Jul 2016 20:57:39 +0300 Subject: [PATCH 3/3] Fixed Phalcon\Db\Adapter\Pdo --- phalcon/db/adapter.zep | 4 +--- phalcon/db/adapter/pdo.zep | 29 ++++++++++++++++----------- phalcon/db/adapter/pdo/oracle.zep | 2 +- phalcon/db/adapter/pdo/postgresql.zep | 8 +++++--- phalcon/db/adapter/pdo/sqlite.zep | 10 +++------ 5 files changed, 27 insertions(+), 26 deletions(-) diff --git a/phalcon/db/adapter.zep b/phalcon/db/adapter.zep index 8b8d2db32e7..979fe850a73 100644 --- a/phalcon/db/adapter.zep +++ b/phalcon/db/adapter.zep @@ -1114,10 +1114,8 @@ abstract class Adapter implements EventsAwareInterface /** * Return descriptor used to connect to the active database - * - * @return array */ - public function getDescriptor() + public function getDescriptor() -> array { return this->_descriptor; } diff --git a/phalcon/db/adapter/pdo.zep b/phalcon/db/adapter/pdo.zep index 82ab15af2a5..d01b756a8c3 100644 --- a/phalcon/db/adapter/pdo.zep +++ b/phalcon/db/adapter/pdo.zep @@ -50,6 +50,8 @@ abstract class Pdo extends Adapter /** * PDO Handler + * + * @var \Pdo */ protected _pdo; @@ -68,24 +70,25 @@ abstract class Pdo extends Adapter } /** - * This method is automatically called in Phalcon\Db\Adapter\Pdo constructor. - * Call it when you need to restore a database connection + * This method is automatically called in \Phalcon\Db\Adapter\Pdo constructor. + * + * Call it when you need to restore a database connection. * * - * //Make a connection - * $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array( - * 'host' => '192.168.0.11', + * use Phalcon\Db\Adapter\Pdo\Mysql; + * + * // Make a connection + * $connection = new Mysql([ + * 'host' => 'localhost', * 'username' => 'sigma', * 'password' => 'secret', - * 'dbname' => 'blog', - * )); + * 'dbname' => 'blog', + * 'port' => 3306, + * ]); * - * //Reconnect + * // Reconnect * $connection->connect(); * - * - * @param array descriptor - * @return boolean */ public function connect(array descriptor = null) -> boolean { @@ -93,7 +96,7 @@ abstract class Pdo extends Adapter persistent, options, key, value; if empty descriptor { - let descriptor = this->_descriptor; + let descriptor = (array) this->_descriptor; } /** @@ -157,6 +160,8 @@ abstract class Pdo extends Adapter * Create the connection using PDO */ let this->_pdo = new \Pdo(this->_type . ":" . dsnAttributes, username, password, options); + + return true; } /** diff --git a/phalcon/db/adapter/pdo/oracle.zep b/phalcon/db/adapter/pdo/oracle.zep index b97ccac366f..897f9ffb655 100644 --- a/phalcon/db/adapter/pdo/oracle.zep +++ b/phalcon/db/adapter/pdo/oracle.zep @@ -58,7 +58,7 @@ class Oracle extends PdoAdapter implements AdapterInterface var startup, value, status; if empty descriptor { - let descriptor = this->_descriptor; + let descriptor = (array) this->_descriptor; } let status = parent::connect(descriptor); diff --git a/phalcon/db/adapter/pdo/postgresql.zep b/phalcon/db/adapter/pdo/postgresql.zep index 52e86f9cd95..95ba4aeff18 100644 --- a/phalcon/db/adapter/pdo/postgresql.zep +++ b/phalcon/db/adapter/pdo/postgresql.zep @@ -59,10 +59,10 @@ class Postgresql extends PdoAdapter implements AdapterInterface */ public function connect(array descriptor = null) -> boolean { - var schema, sql; + var schema, sql, status; if empty descriptor { - let descriptor = this->_descriptor; + let descriptor = (array) this->_descriptor; } if fetch schema, descriptor["schema"] { @@ -77,12 +77,14 @@ class Postgresql extends PdoAdapter implements AdapterInterface } } - parent::connect(descriptor); + let status = parent::connect(descriptor); if ! empty schema { let sql = "SET search_path TO '" . schema . "'"; this->execute(sql); } + + return status; } /** diff --git a/phalcon/db/adapter/pdo/sqlite.zep b/phalcon/db/adapter/pdo/sqlite.zep index c57ffac5aa7..ac333836043 100644 --- a/phalcon/db/adapter/pdo/sqlite.zep +++ b/phalcon/db/adapter/pdo/sqlite.zep @@ -39,11 +39,7 @@ use Phalcon\Db\Adapter\Pdo as PdoAdapter; * * use Phalcon\Db\Adapter\Pdo\Sqlite; * - * $config = [ - * 'dbname' => '/tmp/test.sqlite' - * ]; - * - * $connection = new Sqlite($config); + * $connection = new Sqlite(['dbname' => '/tmp/test.sqlite']); * */ class Sqlite extends PdoAdapter implements AdapterInterface @@ -62,7 +58,7 @@ class Sqlite extends PdoAdapter implements AdapterInterface var dbname; if empty descriptor { - let descriptor = this->_descriptor; + let descriptor = (array) this->_descriptor; } if !fetch dbname, descriptor["dbname"] { @@ -71,7 +67,7 @@ class Sqlite extends PdoAdapter implements AdapterInterface let descriptor["dsn"] = dbname; - parent::connect(descriptor); + return parent::connect(descriptor); } /**