From 9cef0385c81cf325a98d904ac1369685fc918dbe Mon Sep 17 00:00:00 2001 From: Yohn Date: Mon, 3 Dec 2012 21:18:25 -0500 Subject: [PATCH 1/8] changed ->update() backtick I was having a problem with the update function, and saw that it used `$fields[] = "`{$data[0]}' = :{$data[0]}";` and it needed to have the backtick ` instead of the regular quote ' after the data[0] It kept throwing errors before then.. --- src/PDOChainer/DBAL.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PDOChainer/DBAL.php b/src/PDOChainer/DBAL.php index d9550cf..f131ada 100755 --- a/src/PDOChainer/DBAL.php +++ b/src/PDOChainer/DBAL.php @@ -82,7 +82,7 @@ function insert($table, array $dataArr){ function update($table, array $dataArr, array $whereArr = array(), $limit = 1){ $fields = $params = $values = $where = array(); foreach($dataArr as $data){ - $fields[] = "`{$data[0]}' = :{$data[0]}"; + $fields[] = "`{$data[0]}` = :{$data[0]}"; $values[] = array(":{$data[0]}", $data[1], (isset($data[2]) ? $data[2] : \PDO::PARAM_STR)); } $i = 0; From 7fa8a97248fd4bc8214182d28259a1b64f997f54 Mon Sep 17 00:00:00 2001 From: flr Date: Tue, 4 Dec 2012 13:25:42 +0400 Subject: [PATCH 2/8] bindValues() fix --- src/PDOChainer/PDOChainer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PDOChainer/PDOChainer.php b/src/PDOChainer/PDOChainer.php index 2bc6826..edaf5cf 100755 --- a/src/PDOChainer/PDOChainer.php +++ b/src/PDOChainer/PDOChainer.php @@ -85,7 +85,7 @@ public function bindValue($name, $value, $type = \PDO::PARAM_STR) { */ public function bindValues(array $binds) { foreach($binds as $valuesArray) { - $this->bindValue($valuesArray[0], $valuesArray[1], $valuesArray[2]); + $this->bindValue($valuesArray[0], $valuesArray[1], (isset($valuesArray[2]) ? $valuesArray[2] : \PDO::PARAM_STR)); } return $this; } From 0dac11751bfa7b0dbc6efe6a7f2781327c7d0923 Mon Sep 17 00:00:00 2001 From: flr Date: Thu, 23 May 2013 15:13:56 +0400 Subject: [PATCH 3/8] Add port param --- src/PDOChainer/PDOChainer.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/PDOChainer/PDOChainer.php b/src/PDOChainer/PDOChainer.php index edaf5cf..b2ed7a8 100755 --- a/src/PDOChainer/PDOChainer.php +++ b/src/PDOChainer/PDOChainer.php @@ -17,6 +17,7 @@ class PDOChainer { private $host = '127.0.0.1'; + private $port = 3306; private $dbname = 'test'; private $user = 'root'; private $pass = ''; @@ -32,12 +33,13 @@ class PDOChainer */ public function __construct(array $options = array()) { $host = isset($options['host']) ? $options['host'] : $this->host; + $port = isset($options['port']) ? $options['port'] : $this->port; $dbname = isset($options['dbname']) ? $options['dbname'] : $this->dbname; $user = isset($options['user']) ? $options['user'] : $this->user; $pass = isset($options['pass']) ? $options['pass'] : $this->pass; $errorMode = isset($options['errorMode']) ? $options['errorMode'] : $this->errorMode; try { - $db = new \PDO('mysql:host='.$host.';dbname='.$dbname, $user, $pass); + $db = new \PDO('mysql:host='.$host.';port='.$port.';;dbname='.$dbname, $user, $pass); $db->setAttribute(\PDO::ATTR_ERRMODE, $errorMode); } catch (\PDOException $e) { trigger_error('DataBase error: ' . $e->getMessage(), E_USER_ERROR); @@ -159,4 +161,4 @@ public function lastInsertId() { public function rowCount() { return ($this->pdoStatement) ? $this->pdoStatement->rowCount() : false; } -} \ No newline at end of file +} From 32ecf978da4b8ece5ca0d22de4ac5118257dfeeb Mon Sep 17 00:00:00 2001 From: flr Date: Thu, 20 Jun 2013 14:15:21 +0400 Subject: [PATCH 4/8] Fix --- src/PDOChainer/PDOChainer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PDOChainer/PDOChainer.php b/src/PDOChainer/PDOChainer.php index b2ed7a8..fcdc8bc 100755 --- a/src/PDOChainer/PDOChainer.php +++ b/src/PDOChainer/PDOChainer.php @@ -39,7 +39,7 @@ public function __construct(array $options = array()) { $pass = isset($options['pass']) ? $options['pass'] : $this->pass; $errorMode = isset($options['errorMode']) ? $options['errorMode'] : $this->errorMode; try { - $db = new \PDO('mysql:host='.$host.';port='.$port.';;dbname='.$dbname, $user, $pass); + $db = new \PDO('mysql:host='.$host.';port='.$port.';dbname='.$dbname, $user, $pass); $db->setAttribute(\PDO::ATTR_ERRMODE, $errorMode); } catch (\PDOException $e) { trigger_error('DataBase error: ' . $e->getMessage(), E_USER_ERROR); From a406cee10d7dd0e1085613b8b67da3cb8d499cc6 Mon Sep 17 00:00:00 2001 From: flr Date: Sun, 27 Oct 2013 17:19:33 +0400 Subject: [PATCH 5/8] Composer --- .gitignore | 1 + LICENSE | 32 ++++++++++++++++---------------- composer.json | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+), 16 deletions(-) create mode 100755 .gitignore create mode 100755 composer.json diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/LICENSE b/LICENSE index ba964e7..acd12be 100755 --- a/LICENSE +++ b/LICENSE @@ -1,20 +1,20 @@ -MIT License ------------ +The MIT License (MIT) -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: +Copyright (c) 2013 Evgeniy Udodov -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. \ No newline at end of file +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/composer.json b/composer.json new file mode 100755 index 0000000..981ea85 --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name": "flrnull/php-pdo-chainer", + "type": "library", + "description": "PHP PDO wrapper", + "keywords": ["php", "pdo", "wrapper", "chainer"], + "license": "MIT", + "authors": [ + { + "name": "flrnull", + "email": "flr.null@gmail.com" + } + ], + "require": { + "php": ">=5.3.0" + }, + "autoload": { + "psr-0": { + "PDOChainer\\": "src/" + } + } +} \ No newline at end of file From 47f62eb64fa71e7cd7e44a77a2f5b859becd04d2 Mon Sep 17 00:00:00 2001 From: Evgeniy Udodov Date: Tue, 17 Jun 2014 12:26:11 +0400 Subject: [PATCH 6/8] utf-8 fix --- src/PDOChainer/PDOChainer.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/PDOChainer/PDOChainer.php b/src/PDOChainer/PDOChainer.php index fcdc8bc..1f76d16 100755 --- a/src/PDOChainer/PDOChainer.php +++ b/src/PDOChainer/PDOChainer.php @@ -22,6 +22,7 @@ class PDOChainer private $user = 'root'; private $pass = ''; private $errorMode = \PDO::ERRMODE_WARNING; + private $charset = 'utf8'; private $pdo; // Db handler private $pdoStatement; // Statement object @@ -38,9 +39,11 @@ public function __construct(array $options = array()) { $user = isset($options['user']) ? $options['user'] : $this->user; $pass = isset($options['pass']) ? $options['pass'] : $this->pass; $errorMode = isset($options['errorMode']) ? $options['errorMode'] : $this->errorMode; + $charset = isset($options['charset']) ? $options['charset'] : $this->charset; try { $db = new \PDO('mysql:host='.$host.';port='.$port.';dbname='.$dbname, $user, $pass); $db->setAttribute(\PDO::ATTR_ERRMODE, $errorMode); + $db->exec("set names {$charset}"); } catch (\PDOException $e) { trigger_error('DataBase error: ' . $e->getMessage(), E_USER_ERROR); } From 186af5b30775d108f6b3cfbc67a13fcca02a7a5d Mon Sep 17 00:00:00 2001 From: Evgeniy Udodov Date: Sun, 15 Nov 2015 19:53:06 +0300 Subject: [PATCH 7/8] CRLF to LF --- src/PDOChainer/DBAL.php | 334 +++++++++++++++++----------------- src/PDOChainer/PDOChainer.php | 334 +++++++++++++++++----------------- 2 files changed, 334 insertions(+), 334 deletions(-) diff --git a/src/PDOChainer/DBAL.php b/src/PDOChainer/DBAL.php index f131ada..af201d8 100755 --- a/src/PDOChainer/DBAL.php +++ b/src/PDOChainer/DBAL.php @@ -1,168 +1,168 @@ - - */ - -namespace PDOChainer; - -/** - * DBAL over PDOChainer realization. - */ -class DBAL -{ - - /** - * PDOChainer link. - * - * @var \PDOChainer\PDOChainer - */ - private $pdo; - - /** - * Default constructor. - * - * @param \PDOChainer\PDOChainer $pdo - */ - public function __construct(\PDOChainer\PDOChainer $pdo) { - $this->pdo = $pdo; - } - - /** - * Inserts data into DataBase. - * - * @param String $table - * @param Array $data - * Array ( - * array('id', 2, \PDO::PARAM_INT), - * array('name', 'James', \PDO::PARAM_STR), - * ) - * - * @return int|false Inserted ID or false - */ - function insert($table, array $dataArr){ - $fields = $params = $values = array(); - foreach ($dataArr as $data) { - $fields[] = "`{$data[0]}`"; - $params[] = ":{$data[0]}"; - $values[] = array(":{$data[0]}", $data[1], (isset($data[2]) ? $data[2] : \PDO::PARAM_STR)); - } - - $fields = implode(',', $fields); - $params = implode(',', $params); - - $sql = "INSERT INTO `{$table}` ({$fields}) VALUES ({$params})"; - $this->pdo->prepare($sql)->bindValues($values)->execute(); - return $this->pdo->lastInsertId(); - } - - /** - * Updates data in DataBase. - * - * @param String $table - * @param Array $dataArr - * Array ( - * array('id', 2, \PDO::PARAM_INT), - * array('name', 'James', \PDO::PARAM_STR), - * ... - * ) - * @param Array $whereArr - * Array ( - * array('id', 2, \PDO::PARAM_INT), - * ) - * @param int $limit - * - * @return int Affected rows count - */ - function update($table, array $dataArr, array $whereArr = array(), $limit = 1){ - $fields = $params = $values = $where = array(); - foreach($dataArr as $data){ - $fields[] = "`{$data[0]}` = :{$data[0]}"; - $values[] = array(":{$data[0]}", $data[1], (isset($data[2]) ? $data[2] : \PDO::PARAM_STR)); - } - $i = 0; - foreach($whereArr as $wData){ - $i++; // The $i is in there because row wouldnt update with :value already being set above - $where[] = "`{$wData[0]}` = :{$wData[0]}{$i}"; - $values[] = array(":{$wData[0]}{$i}", $wData[1], (isset($wData[2]) ? $wData[2] : \PDO::PARAM_STR)); - } - - $fields = implode(',', $fields); - $whereStr = count($where) ? 'WHERE '.implode(' AND ', $where) : ''; - - $sql = "UPDATE `{$table}` SET {$fields} {$whereStr} LIMIT {$limit}"; - $this->pdo->prepare($sql)->bindValues($values)->execute(); - return $this->pdo->rowCount(); - } - - /** - * Removes data from DataBase. - * - * @param String $table - * @param Array $dataArr - * Array ( - * array('id', 2, \PDO::PARAM_INT), - * array('name', 'James', \PDO::PARAM_STR), - * ... - * ) - * @param int $limit - * - * @return int Affected rows count - */ - function delete($table, array $dataArr, $limit = 1){ - foreach($dataArr as $data){ - $fields[] = "`{$data[0]}` = :{$data[0]}"; - $values[] = array(":{$data[0]}", $data[1], (isset($data[2]) ? $data[2] : \PDO::PARAM_STR)); - } - - $fields = implode(' AND ', $fields); - - $sql = "DELETE FROM `{$table}` WHERE {$fields} LIMIT {$limit}"; - $this->pdo->prepare($sql)->bindValues($values)->execute(); - return $this->pdo->rowCount(); - } - - /** - * Inserts multiple data into DataBase. - * - * @param String $table - * @param Array $dataArr - * Array ( - * array ( - * array('id', 2, \PDO::PARAM_INT), - * array('name', 'James', \PDO::PARAM_STR), - * ), - * ... - * ) - * - * @return int|false Last inserted ID or false - */ - function insertMulti($table, array $dataArr){ - $i = 0; - $fields = array(); - foreach($dataArr as $data){ - $placeholders = array(); - foreach($data as $rowData){ - $i++; - if(!in_array("`{$rowData[0]}`", $fields)) { - $fields[] = "`{$rowData[0]}`"; - } - $placeholders[] = ":{$rowData[0]}{$i}"; - $values[] = array(":{$rowData[0]}{$i}", $rowData[1], (isset($rowData[2]) ? $rowData[2] : \PDO::PARAM_STR)); - } - $params[] = '(' . implode(',', $placeholders) . ')'; - } - - $fields = implode(',', $fields); - $params = implode(',', $params); - - $sql = "INSERT INTO `{$table}` ({$fields}) VALUES {$params}"; - $this->pdo->prepare($sql)->bindValues($values)->execute(); - return $this->pdo->lastInsertId(); - } + + */ + +namespace PDOChainer; + +/** + * DBAL over PDOChainer realization. + */ +class DBAL +{ + + /** + * PDOChainer link. + * + * @var \PDOChainer\PDOChainer + */ + private $pdo; + + /** + * Default constructor. + * + * @param \PDOChainer\PDOChainer $pdo + */ + public function __construct(\PDOChainer\PDOChainer $pdo) { + $this->pdo = $pdo; + } + + /** + * Inserts data into DataBase. + * + * @param String $table + * @param Array $data + * Array ( + * array('id', 2, \PDO::PARAM_INT), + * array('name', 'James', \PDO::PARAM_STR), + * ) + * + * @return int|false Inserted ID or false + */ + function insert($table, array $dataArr){ + $fields = $params = $values = array(); + foreach ($dataArr as $data) { + $fields[] = "`{$data[0]}`"; + $params[] = ":{$data[0]}"; + $values[] = array(":{$data[0]}", $data[1], (isset($data[2]) ? $data[2] : \PDO::PARAM_STR)); + } + + $fields = implode(',', $fields); + $params = implode(',', $params); + + $sql = "INSERT INTO `{$table}` ({$fields}) VALUES ({$params})"; + $this->pdo->prepare($sql)->bindValues($values)->execute(); + return $this->pdo->lastInsertId(); + } + + /** + * Updates data in DataBase. + * + * @param String $table + * @param Array $dataArr + * Array ( + * array('id', 2, \PDO::PARAM_INT), + * array('name', 'James', \PDO::PARAM_STR), + * ... + * ) + * @param Array $whereArr + * Array ( + * array('id', 2, \PDO::PARAM_INT), + * ) + * @param int $limit + * + * @return int Affected rows count + */ + function update($table, array $dataArr, array $whereArr = array(), $limit = 1){ + $fields = $params = $values = $where = array(); + foreach($dataArr as $data){ + $fields[] = "`{$data[0]}` = :{$data[0]}"; + $values[] = array(":{$data[0]}", $data[1], (isset($data[2]) ? $data[2] : \PDO::PARAM_STR)); + } + $i = 0; + foreach($whereArr as $wData){ + $i++; // The $i is in there because row wouldnt update with :value already being set above + $where[] = "`{$wData[0]}` = :{$wData[0]}{$i}"; + $values[] = array(":{$wData[0]}{$i}", $wData[1], (isset($wData[2]) ? $wData[2] : \PDO::PARAM_STR)); + } + + $fields = implode(',', $fields); + $whereStr = count($where) ? 'WHERE '.implode(' AND ', $where) : ''; + + $sql = "UPDATE `{$table}` SET {$fields} {$whereStr} LIMIT {$limit}"; + $this->pdo->prepare($sql)->bindValues($values)->execute(); + return $this->pdo->rowCount(); + } + + /** + * Removes data from DataBase. + * + * @param String $table + * @param Array $dataArr + * Array ( + * array('id', 2, \PDO::PARAM_INT), + * array('name', 'James', \PDO::PARAM_STR), + * ... + * ) + * @param int $limit + * + * @return int Affected rows count + */ + function delete($table, array $dataArr, $limit = 1){ + foreach($dataArr as $data){ + $fields[] = "`{$data[0]}` = :{$data[0]}"; + $values[] = array(":{$data[0]}", $data[1], (isset($data[2]) ? $data[2] : \PDO::PARAM_STR)); + } + + $fields = implode(' AND ', $fields); + + $sql = "DELETE FROM `{$table}` WHERE {$fields} LIMIT {$limit}"; + $this->pdo->prepare($sql)->bindValues($values)->execute(); + return $this->pdo->rowCount(); + } + + /** + * Inserts multiple data into DataBase. + * + * @param String $table + * @param Array $dataArr + * Array ( + * array ( + * array('id', 2, \PDO::PARAM_INT), + * array('name', 'James', \PDO::PARAM_STR), + * ), + * ... + * ) + * + * @return int|false Last inserted ID or false + */ + function insertMulti($table, array $dataArr){ + $i = 0; + $fields = array(); + foreach($dataArr as $data){ + $placeholders = array(); + foreach($data as $rowData){ + $i++; + if(!in_array("`{$rowData[0]}`", $fields)) { + $fields[] = "`{$rowData[0]}`"; + } + $placeholders[] = ":{$rowData[0]}{$i}"; + $values[] = array(":{$rowData[0]}{$i}", $rowData[1], (isset($rowData[2]) ? $rowData[2] : \PDO::PARAM_STR)); + } + $params[] = '(' . implode(',', $placeholders) . ')'; + } + + $fields = implode(',', $fields); + $params = implode(',', $params); + + $sql = "INSERT INTO `{$table}` ({$fields}) VALUES {$params}"; + $this->pdo->prepare($sql)->bindValues($values)->execute(); + return $this->pdo->lastInsertId(); + } } \ No newline at end of file diff --git a/src/PDOChainer/PDOChainer.php b/src/PDOChainer/PDOChainer.php index 1f76d16..fb2f819 100755 --- a/src/PDOChainer/PDOChainer.php +++ b/src/PDOChainer/PDOChainer.php @@ -1,167 +1,167 @@ - - */ - -namespace PDOChainer; - -/** - * Main PDO wrapper class. - */ -class PDOChainer -{ - private $host = '127.0.0.1'; - private $port = 3306; - private $dbname = 'test'; - private $user = 'root'; - private $pass = ''; - private $errorMode = \PDO::ERRMODE_WARNING; - private $charset = 'utf8'; - - private $pdo; // Db handler - private $pdoStatement; // Statement object - - /** - * Main constructor. - * - * @param Array $params Db connection params - */ - public function __construct(array $options = array()) { - $host = isset($options['host']) ? $options['host'] : $this->host; - $port = isset($options['port']) ? $options['port'] : $this->port; - $dbname = isset($options['dbname']) ? $options['dbname'] : $this->dbname; - $user = isset($options['user']) ? $options['user'] : $this->user; - $pass = isset($options['pass']) ? $options['pass'] : $this->pass; - $errorMode = isset($options['errorMode']) ? $options['errorMode'] : $this->errorMode; - $charset = isset($options['charset']) ? $options['charset'] : $this->charset; - try { - $db = new \PDO('mysql:host='.$host.';port='.$port.';dbname='.$dbname, $user, $pass); - $db->setAttribute(\PDO::ATTR_ERRMODE, $errorMode); - $db->exec("set names {$charset}"); - } catch (\PDOException $e) { - trigger_error('DataBase error: ' . $e->getMessage(), E_USER_ERROR); - } - $this->pdo = $db; - } - - /** - * PDO prepare. - * - * @param String $query - * - * @return \PDOChainer\PDOChainer - */ - public function prepare($query) { - $this->pdoStatement = $this->pdo->prepare($query); - return $this; - } - - /** - * PDO bindValue. - * - * @param String $name - * @param String $value - * @param int $type - * - * @return \PDOChainer\PDOChainer - */ - public function bindValue($name, $value, $type = \PDO::PARAM_STR) { - $this->pdoStatement->bindValue($name, $value, $type); - return $this; - } - - /** - * PDO bindValues for array of values. - * - * @param Array $binds - * Array ( - * array(':id', 2, \PDO::PARAM_INT), - * array(':name', 'James', \PDO::PARAM_STR), - * ... - * ) - * - * @return \PDOChainer\PDOChainer - */ - public function bindValues(array $binds) { - foreach($binds as $valuesArray) { - $this->bindValue($valuesArray[0], $valuesArray[1], (isset($valuesArray[2]) ? $valuesArray[2] : \PDO::PARAM_STR)); - } - return $this; - } - - /** - * PDO execute. - * - * @return \PDOChainer\PDOChainer - */ - public function execute() { - try { - $this->pdoStatement->execute(); - } catch (\PDOException $e) { - trigger_error('DataBase error: ' . $e->getMessage(), E_USER_ERROR); - } - return $this; - } - - /** - * PDO fetch. - * - * @param int $type - * - * @return Array|false - */ - public function fetch($type = \PDO::FETCH_BOTH) { - return ($this->pdoStatement) ? $this->pdoStatement->fetch($type) : false; - } - - /** - * PDO fetchAll. - * - * @param int $type - * - * @return Array|false - */ - public function fetchAll($type = \PDO::FETCH_BOTH) { - return ($this->pdoStatement) ? $this->pdoStatement->fetchAll($type) : false; - } - - /** - * PDO query. - * - * @param String $query - * - * @return \PDOChainer\PDOChainer - */ - public function query($query) { - try { - $this->pdoStatement = $this->pdo->query($query); - } catch (\PDOException $e) { - trigger_error('DataBase error: ' . $e->getMessage(), E_USER_ERROR); - } - return $this; - } - - /** - * PDO lastInsertId. - * - * @return String Last inserted ID - */ - public function lastInsertId() { - return $this->pdo->lastInsertId(); - } - - /** - * PDO rowCount. - * - * @return int|false - */ - public function rowCount() { - return ($this->pdoStatement) ? $this->pdoStatement->rowCount() : false; - } -} + + */ + +namespace PDOChainer; + +/** + * Main PDO wrapper class. + */ +class PDOChainer +{ + private $host = '127.0.0.1'; + private $port = 3306; + private $dbname = 'test'; + private $user = 'root'; + private $pass = ''; + private $errorMode = \PDO::ERRMODE_WARNING; + private $charset = 'utf8'; + + private $pdo; // Db handler + private $pdoStatement; // Statement object + + /** + * Main constructor. + * + * @param Array $params Db connection params + */ + public function __construct(array $options = array()) { + $host = isset($options['host']) ? $options['host'] : $this->host; + $port = isset($options['port']) ? $options['port'] : $this->port; + $dbname = isset($options['dbname']) ? $options['dbname'] : $this->dbname; + $user = isset($options['user']) ? $options['user'] : $this->user; + $pass = isset($options['pass']) ? $options['pass'] : $this->pass; + $errorMode = isset($options['errorMode']) ? $options['errorMode'] : $this->errorMode; + $charset = isset($options['charset']) ? $options['charset'] : $this->charset; + try { + $db = new \PDO('mysql:host='.$host.';port='.$port.';dbname='.$dbname, $user, $pass); + $db->setAttribute(\PDO::ATTR_ERRMODE, $errorMode); + $db->exec("set names {$charset}"); + } catch (\PDOException $e) { + trigger_error('DataBase error: ' . $e->getMessage(), E_USER_ERROR); + } + $this->pdo = $db; + } + + /** + * PDO prepare. + * + * @param String $query + * + * @return \PDOChainer\PDOChainer + */ + public function prepare($query) { + $this->pdoStatement = $this->pdo->prepare($query); + return $this; + } + + /** + * PDO bindValue. + * + * @param String $name + * @param String $value + * @param int $type + * + * @return \PDOChainer\PDOChainer + */ + public function bindValue($name, $value, $type = \PDO::PARAM_STR) { + $this->pdoStatement->bindValue($name, $value, $type); + return $this; + } + + /** + * PDO bindValues for array of values. + * + * @param Array $binds + * Array ( + * array(':id', 2, \PDO::PARAM_INT), + * array(':name', 'James', \PDO::PARAM_STR), + * ... + * ) + * + * @return \PDOChainer\PDOChainer + */ + public function bindValues(array $binds) { + foreach($binds as $valuesArray) { + $this->bindValue($valuesArray[0], $valuesArray[1], (isset($valuesArray[2]) ? $valuesArray[2] : \PDO::PARAM_STR)); + } + return $this; + } + + /** + * PDO execute. + * + * @return \PDOChainer\PDOChainer + */ + public function execute() { + try { + $this->pdoStatement->execute(); + } catch (\PDOException $e) { + trigger_error('DataBase error: ' . $e->getMessage(), E_USER_ERROR); + } + return $this; + } + + /** + * PDO fetch. + * + * @param int $type + * + * @return Array|false + */ + public function fetch($type = \PDO::FETCH_BOTH) { + return ($this->pdoStatement) ? $this->pdoStatement->fetch($type) : false; + } + + /** + * PDO fetchAll. + * + * @param int $type + * + * @return Array|false + */ + public function fetchAll($type = \PDO::FETCH_BOTH) { + return ($this->pdoStatement) ? $this->pdoStatement->fetchAll($type) : false; + } + + /** + * PDO query. + * + * @param String $query + * + * @return \PDOChainer\PDOChainer + */ + public function query($query) { + try { + $this->pdoStatement = $this->pdo->query($query); + } catch (\PDOException $e) { + trigger_error('DataBase error: ' . $e->getMessage(), E_USER_ERROR); + } + return $this; + } + + /** + * PDO lastInsertId. + * + * @return String Last inserted ID + */ + public function lastInsertId() { + return $this->pdo->lastInsertId(); + } + + /** + * PDO rowCount. + * + * @return int|false + */ + public function rowCount() { + return ($this->pdoStatement) ? $this->pdoStatement->rowCount() : false; + } +} From ab63ebafa5c7cc3b2b9c9939ca64e9ea5f9db99c Mon Sep 17 00:00:00 2001 From: Evgeniy Udodov Date: Sun, 15 Nov 2015 20:06:11 +0300 Subject: [PATCH 8/8] Add persistent option --- src/PDOChainer/PDOChainer.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/PDOChainer/PDOChainer.php b/src/PDOChainer/PDOChainer.php index fb2f819..ef91c54 100755 --- a/src/PDOChainer/PDOChainer.php +++ b/src/PDOChainer/PDOChainer.php @@ -18,7 +18,7 @@ class PDOChainer { private $host = '127.0.0.1'; private $port = 3306; - private $dbname = 'test'; + private $dbname = null; private $user = 'root'; private $pass = ''; private $errorMode = \PDO::ERRMODE_WARNING; @@ -26,11 +26,11 @@ class PDOChainer private $pdo; // Db handler private $pdoStatement; // Statement object - + /** * Main constructor. - * - * @param Array $params Db connection params + * + * @param array $options */ public function __construct(array $options = array()) { $host = isset($options['host']) ? $options['host'] : $this->host; @@ -40,8 +40,13 @@ public function __construct(array $options = array()) { $pass = isset($options['pass']) ? $options['pass'] : $this->pass; $errorMode = isset($options['errorMode']) ? $options['errorMode'] : $this->errorMode; $charset = isset($options['charset']) ? $options['charset'] : $this->charset; + $connectionOptions = []; + if (isset($options['persistent'])) { + $connectionOptions[\PDO::ATTR_PERSISTENT] = $options['persistent']; + } + $dsn = 'mysql:host='.$host.';port='.$port.';dbname='.$dbname; try { - $db = new \PDO('mysql:host='.$host.';port='.$port.';dbname='.$dbname, $user, $pass); + $db = new \PDO($dsn, $user, $pass, $connectionOptions); $db->setAttribute(\PDO::ATTR_ERRMODE, $errorMode); $db->exec("set names {$charset}"); } catch (\PDOException $e) {