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 diff --git a/src/PDOChainer/DBAL.php b/src/PDOChainer/DBAL.php index d9550cf..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 2bc6826..ef91c54 100755 --- a/src/PDOChainer/PDOChainer.php +++ b/src/PDOChainer/PDOChainer.php @@ -1,162 +1,172 @@ - - */ - -namespace PDOChainer; - -/** - * Main PDO wrapper class. - */ -class PDOChainer -{ - private $host = '127.0.0.1'; - private $dbname = 'test'; - private $user = 'root'; - private $pass = ''; - private $errorMode = \PDO::ERRMODE_WARNING; - - 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; - $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->setAttribute(\PDO::ATTR_ERRMODE, $errorMode); - } 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], $valuesArray[2]); - } - 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; - } -} \ No newline at end of file + + */ + +namespace PDOChainer; + +/** + * Main PDO wrapper class. + */ +class PDOChainer +{ + private $host = '127.0.0.1'; + private $port = 3306; + private $dbname = null; + 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 $options + */ + 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; + $connectionOptions = []; + if (isset($options['persistent'])) { + $connectionOptions[\PDO::ATTR_PERSISTENT] = $options['persistent']; + } + $dsn = 'mysql:host='.$host.';port='.$port.';dbname='.$dbname; + try { + $db = new \PDO($dsn, $user, $pass, $connectionOptions); + $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; + } +}