Skip to content

Commit

Permalink
Merge pull request #12007 from sergeyklay/2.1.x
Browse files Browse the repository at this point in the history
Cleanup Phalcon\Db\AdapterInterface::connect
  • Loading branch information
sergeyklay authored Jul 23, 2016
2 parents 71ab2b2 + 499ef6d commit 71c9652
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 89 deletions.
11 changes: 3 additions & 8 deletions phalcon/db/adapter.zep
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down Expand Up @@ -31,7 +31,6 @@ use Phalcon\Events\ManagerInterface;
*/
abstract class Adapter implements EventsAwareInterface
{

/**
* Event Manager
*
Expand All @@ -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
Expand Down Expand Up @@ -1117,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;
}
Expand Down
55 changes: 32 additions & 23 deletions phalcon/db/adapter/pdo.zep
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down Expand Up @@ -31,21 +31,27 @@ use Phalcon\Db\Result\Pdo as ResultPdo;
*
* Phalcon\Db\Adapter\Pdo is the Phalcon\Db that internally uses PDO to connect to a database
*
*<code>
* $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
* 'host' => '192.168.0.11',
* 'username' => 'sigma',
* 'password' => 'secret',
* 'dbname' => 'blog',
* 'port' => '3306'
* ));
* <code>
* use Phalcon\Db\Adapter\Pdo\Mysql;
*
* $config = [
* 'host' => 'localhost',
* 'dbname' => 'blog',
* 'port' => 3306,
* 'username' => 'sigma',
* 'password' => 'secret'
* ];
*
* $connection = new Mysql($config);
*</code>
*/
abstract class Pdo extends Adapter
{

/**
* PDO Handler
*
* @var \Pdo
*/
protected _pdo;

Expand All @@ -64,32 +70,33 @@ 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.
*
*<code>
* //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();
* </code>
*
* @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 {
let descriptor = this->_descriptor;
if empty descriptor {
let descriptor = (array) this->_descriptor;
}

/**
Expand Down Expand Up @@ -153,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;
}

/**
Expand Down
13 changes: 6 additions & 7 deletions phalcon/db/adapter/pdo/mysql.zep
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down Expand Up @@ -33,15 +33,14 @@ use Phalcon\Db\Adapter\Pdo as PdoAdapter;
* Specific functions for the Mysql database system
*
*<code>
*
* 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);
Expand Down
34 changes: 18 additions & 16 deletions phalcon/db/adapter/pdo/oracle.zep
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand All @@ -29,16 +29,17 @@ use Phalcon\Db\Adapter\Pdo as PdoAdapter;
* Phalcon\Db\Adapter\Pdo\Oracle
*
* Specific functions for the Oracle database system
* <code>
*
* $config = array(
* "dbname" => "//localhost/dbname",
* "username" => "oracle",
* "password" => "oracle"
* );
* <code>
* 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);
* </code>
*/
class Oracle extends PdoAdapter implements AdapterInterface
Expand All @@ -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 {
let descriptor = this->_descriptor;
if empty descriptor {
let descriptor = (array) 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" {
Expand All @@ -82,7 +82,9 @@ class Oracle extends PdoAdapter implements AdapterInterface
/**
* Returns an array of Phalcon\Db\Column objects describing a table
*
* <code>print_r($connection->describeColumns("posts")); ?></code>
* <code>
* print_r($connection->describeColumns("posts"));
* </code>
*/
public function describeColumns(string! table, string schema = null) -> <Column[]>
{
Expand Down
34 changes: 18 additions & 16 deletions phalcon/db/adapter/pdo/postgresql.zep
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand All @@ -30,17 +30,20 @@ use Phalcon\Db\Exception;
* Phalcon\Db\Adapter\Pdo\Postgresql
*
* Specific functions for the Postgresql database system
*
* <code>
* 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);
* </code>
*/
class Postgresql extends PdoAdapter implements AdapterInterface
Expand All @@ -53,16 +56,13 @@ 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;
var schema, sql, status;

if descriptor === null {
let descriptor = this->_descriptor;
if empty descriptor {
let descriptor = (array) this->_descriptor;
}

if fetch schema, descriptor["schema"] {
Expand All @@ -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;
}

/**
Expand Down
21 changes: 8 additions & 13 deletions phalcon/db/adapter/pdo/sqlite.zep
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down Expand Up @@ -35,13 +35,11 @@ use Phalcon\Db\Adapter\Pdo as PdoAdapter;
* Phalcon\Db\Adapter\Pdo\Sqlite
*
* Specific functions for the Sqlite database system
* <code>
*
* $config = array(
* "dbname" => "/tmp/test.sqlite"
* );
* <code>
* use Phalcon\Db\Adapter\Pdo\Sqlite;
*
* $connection = new \Phalcon\Db\Adapter\Pdo\Sqlite($config);
* $connection = new Sqlite(['dbname' => '/tmp/test.sqlite']);
* </code>
*/
class Sqlite extends PdoAdapter implements AdapterInterface
Expand All @@ -54,16 +52,13 @@ 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 {
let descriptor = this->_descriptor;
if empty descriptor {
let descriptor = (array) this->_descriptor;
}

if !fetch dbname, descriptor["dbname"] {
Expand All @@ -72,7 +67,7 @@ class Sqlite extends PdoAdapter implements AdapterInterface

let descriptor["dsn"] = dbname;

parent::connect(descriptor);
return parent::connect(descriptor);
}

/**
Expand Down
Loading

0 comments on commit 71c9652

Please sign in to comment.