From 1fb2d706c4f263d8c210ad3b16812ae68958bacb Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Wed, 2 Aug 2017 11:16:59 -0400 Subject: [PATCH 01/17] fixed limit variable ordering in the comments --- MysqliDb.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/MysqliDb.php b/MysqliDb.php index 48a705b0..1fa07a43 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -582,7 +582,7 @@ public function rawQueryValue($query, $bindParams = null) * A method to perform select query * * @param string $query Contains a user-provided select query. - * @param int|array $numRows Array to define SQL limit in format Array ($count, $offset) + * @param int|array $numRows Array to define SQL limit in format Array ($offset, $count) * * @return array Contains the returned rows from the query. */ @@ -654,7 +654,7 @@ public function withTotalCount() * A convenient SELECT * function. * * @param string $tableName The name of the database table to work with. - * @param int|array $numRows Array to define SQL limit in format Array ($count, $offset) + * @param int|array $numRows Array to define SQL limit in format Array ($offset, $count) * or only $count * @param string $columns Desired columns * @@ -858,7 +858,7 @@ public function update($tableName, $tableData, $numRows = null) * Delete query. Call the "where" method first. * * @param string $tableName The name of the database table to work with. - * @param int|array $numRows Array to define SQL limit in format Array ($count, $offset) + * @param int|array $numRows Array to define SQL limit in format Array ($offset, $count) * or only $count * * @return bool Indicates success. 0 or 1. @@ -1474,7 +1474,7 @@ private function _buildInsert($tableName, $insertData, $operation) * any passed update data, and the desired rows. * It then builds the SQL query. * - * @param int|array $numRows Array to define SQL limit in format Array ($count, $offset) + * @param int|array $numRows Array to define SQL limit in format Array ($offset, $count) * or only $count * @param array $tableData Should contain an array of data for updating the database. * @@ -1868,7 +1868,7 @@ protected function _buildOrderBy() /** * Abstraction method that will build the LIMIT part of the WHERE statement * - * @param int|array $numRows Array to define SQL limit in format Array ($count, $offset) + * @param int|array $numRows Array to define SQL limit in format Array ($offset, $count) * or only $count * * @return void From 04b2a29aad71044f695abd101c6368df49383848 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Wed, 2 Aug 2017 12:06:10 -0400 Subject: [PATCH 02/17] Expand readme on autoreconnect and multipe connections usage --- readme.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/readme.md b/readme.md index c780c6c4..8fcdb9ff 100644 --- a/readme.md +++ b/readme.md @@ -78,6 +78,12 @@ If no table prefix were set during object creation its possible to set it later $db->setPrefix ('my_'); ``` +If connection to mysql will be dropped Mysqlidb will try to automatically reconnect to the database once. +To disable this behavoir use +```php +$db->autoReconnect = false; +``` + If you need to get already created mysqliDb object from another class or function use ```php function init () { @@ -92,6 +98,24 @@ If you need to get already created mysqliDb object from another class or functio } ``` +### Multiple database connection +If you need to connect to multiple databases use following method: +```php +$db->addConnection('slave', Array ( + 'host' => 'host', + 'username' => 'username', + 'password' => 'password', + 'db'=> 'databaseName', + 'port' => 3306, + 'prefix' => 'my_', + 'charset' => 'utf8') +); +``` +To select database use connection() method +```php +$users = $db->connection('slave')->get('users'); +``` + ### Objects mapping dbObject.php is an object mapping library built on top of mysqliDb to provide model representation functionality. See dbObject manual for more information From 0685cb052ddfd1ac65e5b9ef3f219ea35acfb030 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Wed, 2 Aug 2017 22:56:28 -0400 Subject: [PATCH 03/17] update release numbers --- MysqliDb.php | 2 +- dbObject.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/MysqliDb.php b/MysqliDb.php index 1fa07a43..39e3049b 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -10,7 +10,7 @@ * @copyright Copyright (c) 2010-2016 * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License * @link http://github.com/joshcam/PHP-MySQLi-Database-Class - * @version 2.8-master + * @version 2.9-master */ class MysqliDb diff --git a/dbObject.php b/dbObject.php index ea00efee..d891387e 100644 --- a/dbObject.php +++ b/dbObject.php @@ -8,7 +8,7 @@ * @copyright Copyright (c) 2015 * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License * @link http://github.com/joshcam/PHP-MySQLi-Database-Class - * @version 2.6-master + * @version 2.9-master * * @method int count () * @method dbObject ArrayBuilder() From 2959ecb6000da5ef399f5b09e7a9055733d56231 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Wed, 2 Aug 2017 22:57:19 -0400 Subject: [PATCH 04/17] update copyright years --- MysqliDb.php | 2 +- dbObject.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/MysqliDb.php b/MysqliDb.php index 39e3049b..1e2df454 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -7,7 +7,7 @@ * @author Jeffery Way * @author Josh Campbell * @author Alexander V. Butenko - * @copyright Copyright (c) 2010-2016 + * @copyright Copyright (c) 2010-2017 * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License * @link http://github.com/joshcam/PHP-MySQLi-Database-Class * @version 2.9-master diff --git a/dbObject.php b/dbObject.php index d891387e..0da3881b 100644 --- a/dbObject.php +++ b/dbObject.php @@ -5,7 +5,7 @@ * @category Database Access * @package MysqliDb * @author Alexander V. Butenko - * @copyright Copyright (c) 2015 + * @copyright Copyright (c) 2015-2017 * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License * @link http://github.com/joshcam/PHP-MySQLi-Database-Class * @version 2.9-master From 2481e9446ef28aad7cee98cd93cbadb70fdd04b3 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Tue, 8 Aug 2017 11:18:12 -0400 Subject: [PATCH 05/17] Bug #623: Mistake in Regexp --- dbObject.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbObject.php b/dbObject.php index 9b5bbee2..3f663ae2 100644 --- a/dbObject.php +++ b/dbObject.php @@ -651,7 +651,7 @@ private function validate ($data) { $regexp = "/^[0-9\.]*$/"; break; case "bool": - $regexp = '/^[yes|no|0|1|true|false]$/i'; + $regexp = '/^(yes|no|0|1|true|false)$/i'; break; case "datetime": $regexp = "/^[0-9a-zA-Z -:]*$/"; From 8d4dcb1473e1b50496b010e9b5f8b3bf205c9d83 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Tue, 8 Aug 2017 11:24:44 -0400 Subject: [PATCH 06/17] Fix for #554 Fixing Join usage with 'USING' instead of 'ON' --- MysqliDb.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/MysqliDb.php b/MysqliDb.php index 389c8089..f5e9d4b4 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -1004,7 +1004,7 @@ public function orHaving($havingProp, $havingValue = null, $operator = null) */ public function join($joinTable, $joinCondition, $joinType = '') { - $allowedTypes = array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'); + $allowedTypes = array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER', 'NATURAL'); $joinType = strtoupper(trim($joinType)); if ($joinType && !in_array($joinType, $allowedTypes)) { @@ -1649,7 +1649,7 @@ protected function _buildJoinOld() $joinStr = $joinTable; } - $this->_query .= " " . $joinType . " JOIN " . $joinStr . + $this->_query .= " " . $joinType . " JOIN " . $joinStr . (false !== stripos($joinCondition, 'using') ? " " : " on ") . $joinCondition; } @@ -2356,7 +2356,9 @@ protected function _buildJoin () { else $joinStr = $joinTable; - $this->_query .= " " . $joinType. " JOIN " . $joinStr ." on " . $joinCondition; + $this->_query .= " " . $joinType. " JOIN " . $joinStr . + (false !== stripos($joinCondition, 'using') ? " " : " on ") + . $joinCondition; // Add join and query if (!empty($this->_joinAnd) && isset($this->_joinAnd[$joinStr])) { From 4d4971e708a35ed62bba1c9ace2ad115a1cf1095 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Wed, 9 Aug 2017 09:12:58 -0400 Subject: [PATCH 07/17] Fix empty exception message --- MysqliDb.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/MysqliDb.php b/MysqliDb.php index f5e9d4b4..9f49cddf 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -1900,8 +1900,11 @@ protected function _prepareQuery() { $stmt = $this->mysqli()->prepare($this->_query); - if ($stmt !== false) - goto release; + if ($stmt !== false) { + if ($this->traceEnabled) + $this->traceStartQ = microtime(true); + return $stmt; + } if ($this->mysqli()->errno === 2006 && $this->autoReconnect === true && $this->autoReconnectCount === 0) { $this->connect($this->defConnectionName); @@ -1909,15 +1912,11 @@ protected function _prepareQuery() return $this->_prepareQuery(); } + $error = $this->mysqli()->error; + $query = $this->_query; + $errno = $this->mysqli()->errno; $this->reset(); - throw new Exception(sprintf('%s query: %s', $this->mysqli()->error, $this->_query), $this->mysqli()->errno); - - release: - if ($this->traceEnabled) { - $this->traceStartQ = microtime(true); - } - - return $stmt; + throw new Exception(sprintf('%s query: %s', error, query), errno); } /** From 64d335c4cc63e9bd46d12b7e3db86323beb8eebf Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Wed, 9 Aug 2017 09:52:05 -0400 Subject: [PATCH 08/17] fix a typo --- MysqliDb.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MysqliDb.php b/MysqliDb.php index 9f49cddf..6ccbb9b1 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -1916,7 +1916,7 @@ protected function _prepareQuery() $query = $this->_query; $errno = $this->mysqli()->errno; $this->reset(); - throw new Exception(sprintf('%s query: %s', error, query), errno); + throw new Exception(sprintf('%s query: %s', $error, $query), $errno); } /** From ca246f165f9fb4c9a6cc1cbf123b718dfdda31e6 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Thu, 14 Sep 2017 21:17:15 -0400 Subject: [PATCH 09/17] Added LIKE case description into readme --- readme.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 45fa2aed..eaf5e62f 100644 --- a/readme.md +++ b/readme.md @@ -490,7 +490,7 @@ $results = $db->get('users'); // Gives: SELECT * FROM users WHERE id IN (1, 5, 27, -1, 'd'); ``` -OR CASE +OR CASE: ```php $db->where ('firstName', 'John'); $db->orWhere ('firstName', 'Peter'); @@ -505,6 +505,13 @@ $results = $db->get("users"); // Gives: SELECT * FROM users where lastName IS NOT NULL ``` +LIKE comparison: +```php +$db->where ("fullName", 'John%', 'like'); +$results = $db->get("users"); +// Gives: SELECT * FROM users where fullName like 'John%' +``` + Also you can use raw where conditions: ```php $db->where ("id != companyId"); From 51cc4d4889c8c87298069700e2d3e89eac1d9b76 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Wed, 29 Nov 2017 10:56:29 -0400 Subject: [PATCH 10/17] do not reset connection name when transtation is on --- MysqliDb.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/MysqliDb.php b/MysqliDb.php index f0353ed2..74435711 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -416,7 +416,9 @@ protected function reset() $this->_lastInsertId = null; $this->_updateColumns = null; $this->_mapKey = null; - $this->defConnectionName = 'default'; + if(!$this->_transaction_in_progress ) { + $this->defConnectionName = 'default'; + } $this->autoReconnectCount = 0; return $this; } From 7c0c177d9890d9907ccc8365cf5f5a7daa32c2f4 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Wed, 29 Nov 2017 10:57:14 -0400 Subject: [PATCH 11/17] 2.9 release --- MysqliDb.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MysqliDb.php b/MysqliDb.php index 74435711..441d3f0c 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -10,7 +10,7 @@ * @copyright Copyright (c) 2010-2017 * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License * @link http://github.com/joshcam/PHP-MySQLi-Database-Class - * @version 2.9-master + * @version 2.9 */ class MysqliDb From eab49fe338c7c7c70f7f53e1bf23ad907ea49694 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Thu, 30 Nov 2017 06:28:17 -0400 Subject: [PATCH 12/17] v2.9.1 bugfix release declate missing variable --- MysqliDb.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/MysqliDb.php b/MysqliDb.php index ce5ff740..7e5826c3 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -10,7 +10,7 @@ * @copyright Copyright (c) 2010-2017 * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License * @link http://github.com/joshcam/PHP-MySQLi-Database-Class - * @version 2.9 + * @version 2.9.1 */ class MysqliDb @@ -220,6 +220,11 @@ class MysqliDb public $autoReconnect = true; protected $autoReconnectCount = 0; + /** + * @var bool Operations in transaction indicator + */ + protected $_transaction_in_progress = false; + /** * @param string $host * @param string $username From 53cc9f71ec35700af10deaeaf76611e86b005fe5 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Thu, 8 Feb 2018 01:02:17 -0400 Subject: [PATCH 13/17] update composer.json --- composer.json | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 3d2d13b8..1fa9b58d 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { - "name": "joshcam/mysqli-database-class", - "description": "PHP MySQL Wrapper and object mapper which utilizes MySQLi and prepared statements", - "license": "GPL-3.0", + "name": "joshcam/mysqli-database-class", + "description": "PHP MySQL Wrapper and object mapper which utilizes MySQLi and prepared statements", + "license": "GPL-3.0-or-later", "authors": [ { "name": "Josh Campbell", @@ -16,10 +16,10 @@ "role": "Developer" } ], - "require": { - "php": ">=5.3.0" - }, - "autoload": { - "files": ["MysqliDb.php", "dbObject.php"] - } + "require": { + "php": ">=5.3.0" + }, + "autoload": { + "files": ["MysqliDb.php", "dbObject.php"] + } } From b40fd92a5a618b01d40f45be3bac4696881ef160 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Thu, 8 Feb 2018 01:06:46 -0400 Subject: [PATCH 14/17] tag 2.9.2 --- MysqliDb.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MysqliDb.php b/MysqliDb.php index 7e5826c3..6a55bddf 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -10,7 +10,7 @@ * @copyright Copyright (c) 2010-2017 * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License * @link http://github.com/joshcam/PHP-MySQLi-Database-Class - * @version 2.9.1 + * @version 2.9.2 */ class MysqliDb From 995d0fe093c6a0655ced1fe1060f1db10c9aac25 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Thu, 8 Feb 2018 01:10:28 -0400 Subject: [PATCH 15/17] back to development --- MysqliDb.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MysqliDb.php b/MysqliDb.php index 6a55bddf..a594765d 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -10,7 +10,7 @@ * @copyright Copyright (c) 2010-2017 * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License * @link http://github.com/joshcam/PHP-MySQLi-Database-Class - * @version 2.9.2 + * @version 2.9.3-master */ class MysqliDb From 043f3f2edd53dff3834b9a40bfda45c7d27cdaf0 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Sat, 24 Aug 2019 05:11:07 -0400 Subject: [PATCH 16/17] remove legacy >where("firstname", Array ('LIKE' => '%John%')) support --- MysqliDb.php | 6 ------ tests/mysqliDbTests.php | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/MysqliDb.php b/MysqliDb.php index a594765d..f6cb724a 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -907,12 +907,6 @@ public function delete($tableName, $numRows = null) */ public function where($whereProp, $whereValue = 'DBNULL', $operator = '=', $cond = 'AND') { - // forkaround for an old operation api - if (is_array($whereValue) && ($key = key($whereValue)) != "0") { - $operator = $key; - $whereValue = $whereValue[$key]; - } - if (count($this->_where) == 0) { $cond = ''; } diff --git a/tests/mysqliDbTests.php b/tests/mysqliDbTests.php index bd35bc30..dd74b578 100644 --- a/tests/mysqliDbTests.php +++ b/tests/mysqliDbTests.php @@ -251,7 +251,7 @@ function createTable ($name, $data) { //$users = $db->get("users"); //print_r ($users); -$db->where("firstname", Array ('LIKE' => '%John%')); +$db->where("firstname", '%John%', 'like'); $users = $db->get("users"); if ($db->count != 1) { echo "Invalid insert count in LIKE: ".$db->count; From 5fd69fc8e065168fd7d798088945a71727ffd331 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Sat, 24 Aug 2019 05:20:12 -0400 Subject: [PATCH 17/17] 2.9.3 release --- MysqliDb.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MysqliDb.php b/MysqliDb.php index 23f67aa8..738d9230 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -10,7 +10,7 @@ * @copyright Copyright (c) 2010-2017 * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License * @link http://github.com/joshcam/PHP-MySQLi-Database-Class - * @version 2.9.2 + * @version 2.9.3 */ class MysqliDb