From 9b62b349316f4b4528904f2786e65169089f7d09 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Mon, 9 Feb 2015 20:32:06 +0000 Subject: [PATCH 1/6] Add RAND () orderBy value --- MysqliDb.php | 8 ++++++-- readme.md | 3 ++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/MysqliDb.php b/MysqliDb.php index fd547df6..172eb796 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -794,8 +794,12 @@ protected function _buildOrderBy () { return; $this->_query .= " ORDER BY "; - foreach ($this->_orderBy as $prop => $value) - $this->_query .= $prop . " " . $value . ", "; + foreach ($this->_orderBy as $prop => $value) { + if (strtolower (str_replace (" ", "", $prop)) == 'rand()') + $this->_query .= "rand(), "; + else + $this->_query .= $prop . " " . $value . ", "; + } $this->_query = rtrim ($this->_query, ', ') . " "; } diff --git a/readme.md b/readme.md index f3d1ca97..aa1f842c 100644 --- a/readme.md +++ b/readme.md @@ -254,8 +254,9 @@ $results = $db ```php $db->orderBy("id","asc"); $db->orderBy("login","Desc"); +$db->orderBy("RAND ()"); $results = $db->get('users'); -// Gives: SELECT * FROM users ORDER BY id ASC,login DESC; +// Gives: SELECT * FROM users ORDER BY id ASC,login DESC, RAND (); ``` ### Grouping method From 179cde2de23e79b96f59a3040c0e9790141088c2 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Mon, 9 Feb 2015 20:44:45 +0000 Subject: [PATCH 2/6] Added getValue function --- MysqliDb.php | 17 +++++++++++++++++ readme.md | 7 +++++++ 2 files changed, 24 insertions(+) diff --git a/MysqliDb.php b/MysqliDb.php index 172eb796..e5367280 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -283,6 +283,23 @@ public function getOne($tableName, $columns = '*') return null; } + /** + * A convenient SELECT * function to get one value. + * + * @param string $tableName The name of the database table to work with. + * + * @return array Contains the returned column from the select query. + */ + public function getValue($tableName, $column) + { + $res = $this->get ($tableName, 1, "{$column} as retval"); + + if (isset($res[0]["retval"])) + return $res[0]["retval"]; + + return null; + } + /** * * @param getOne ("users", "sum(id), count(*) as cnt"); echo "total ".$stats['cnt']. "users found"; ``` +or select one column or function result + +```php +$count = getValue ("users", "count(*)"); +echo "{$count} users found"; +``` + ### Delete Query ```php $db->where('id', 1); From ed4594a9a09c551bd52f17eb5375f74372c08ee2 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Tue, 10 Feb 2015 07:29:18 +0000 Subject: [PATCH 3/6] Reset count on each result fetching --- MysqliDb.php | 1 + 1 file changed, 1 insertion(+) diff --git a/MysqliDb.php b/MysqliDb.php index e5367280..4980846c 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -644,6 +644,7 @@ protected function _dynamicBindResults(mysqli_stmt $stmt) call_user_func_array(array($stmt, 'bind_result'), $parameters); + $this->count = 0; while ($stmt->fetch()) { $x = array(); foreach ($row as $key => $val) { From a3f111b87c0a09a559371cc1954777e3c9807d75 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Tue, 10 Feb 2015 08:19:55 +0000 Subject: [PATCH 4/6] update tests --- tests.php | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/tests.php b/tests.php index e57a561a..f5f3966d 100644 --- a/tests.php +++ b/tests.php @@ -93,12 +93,13 @@ function createTable ($name, $data) { $db->rawQuery($q); } +// rawQuery test foreach ($tables as $name => $fields) { $db->rawQuery("DROP TABLE ".$prefix.$name); createTable ($prefix.$name, $fields); } - +// insert test with autoincrement foreach ($data as $name => $datas) { foreach ($datas as $d) { $id = $db->insert($name, $d); @@ -106,16 +107,48 @@ function createTable ($name, $data) { $d['id'] = $id; else { echo "failed to insert: ".$db->getLastQuery() ."\n". $db->getLastError(); + exit; } } } +// bad insert test +$badUser = Array ('login' => null, + 'customerId' => 10, + 'firstName' => 'John', + 'lastName' => 'Doe', + 'password' => 'test', + 'createdAt' => $db->now(), + 'expires' => $db->now('+1Y'), + 'loginCount' => $db->inc() + ); +$id = $db->insert ("users", $badUser); +if ($id) { + echo "bad insert test failed"; + exit; +} + +// insert without autoincrement +$q = "create table {$prefix}test (id int(10), name varchar(10));"; +$db->rawQuery($q); +$id = $db->insert ("test", Array ("id" => 1, "name" => "testname")); +if (!$id) { + echo "insert without autoincrement failed"; + exit; +} +$db->get("test"); +if ($db->count != 1) { + echo "insert without autoincrement failed -- wrong insert count"; + exit; +} + $db->orderBy("id","asc"); $users = $db->get("users"); if ($db->count != 3) { echo "Invalid total insert count"; exit; } + $db->where ("active", true); $users = $db->get("users"); if ($db->count != 1) { @@ -246,8 +279,8 @@ function createTable ($name, $data) { $db2 = $db->copy(); $db2->where ("userId", $usersQ); -$res = $db2->getOne ("products", "count(id) as cnt"); -if ($res['cnt'] != 2) { +$cnt = $db2->getValue ("products", "count(id)"); +if ($cnt != 2) { echo "Invalid select result with subquery"; exit; } @@ -259,6 +292,10 @@ function createTable ($name, $data) { exit; } $db->delete("products"); + +$q = "drop table {$prefix}test;"; +$db->rawQuery($q); + echo "All done"; //print_r($db->rawQuery("CALL simpleproc(?)",Array("test"))); From 06f6a3727db71217a54e9ce7c38be2fcfbcd92f5 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Tue, 10 Feb 2015 08:20:27 +0000 Subject: [PATCH 5/6] issue #150 Support for insert() without an insert id --- MysqliDb.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/MysqliDb.php b/MysqliDb.php index 4980846c..ae816c6f 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -317,8 +317,15 @@ public function insert($tableName, $insertData) $stmt->execute(); $this->_stmtError = $stmt->error; $this->reset(); + $this->count = $stmt->affected_rows; + + if ($stmt->affected_rows < 1) + return false; + + if ($stmt->insert_id > 0) + return $stmt->insert_id; - return ($stmt->affected_rows > 0 ? $stmt->insert_id : false); + return true; } /** From e5937d769c265fac4bf9b766206ed1f286098b47 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Tue, 10 Feb 2015 08:39:37 +0000 Subject: [PATCH 6/6] Order by field support --- MysqliDb.php | 9 ++++++++- readme.md | 7 +++++++ tests.php | 8 ++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/MysqliDb.php b/MysqliDb.php index ae816c6f..3a9dfcb9 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -446,7 +446,7 @@ public function join($joinTable, $joinCondition, $joinType = '') * * @return MysqliDb */ - public function orderBy($orderByField, $orderbyDirection = "DESC") + public function orderBy($orderByField, $orderbyDirection = "DESC", $customFields = null) { $allowedDirection = Array ("ASC", "DESC"); $orderbyDirection = strtoupper (trim ($orderbyDirection)); @@ -455,6 +455,13 @@ public function orderBy($orderByField, $orderbyDirection = "DESC") if (empty($orderbyDirection) || !in_array ($orderbyDirection, $allowedDirection)) die ('Wrong order direction: '.$orderbyDirection); + if (is_array ($customFields)) { + foreach ($customFields as $key => $value) + $customFields[$key] = preg_replace ("/[^-a-z0-9\.\(\),_]+/i",'', $value); + + $orderByField = 'FIELD (' . $orderByField . ', "' . implode('","', $customFields) . '")'; + } + $this->_orderBy[$orderByField] = $orderbyDirection; return $this; } diff --git a/readme.md b/readme.md index c591bf79..25b3c353 100644 --- a/readme.md +++ b/readme.md @@ -266,6 +266,13 @@ $results = $db->get('users'); // Gives: SELECT * FROM users ORDER BY id ASC,login DESC, RAND (); ``` +order by values example: +```php +$db->orderBy('userGroup', 'ASC', array('superuser', 'admin', 'users')); +$db->get('users'); +// Gives: SELECT * FROM users ORDER BY FIELD (userGroup, 'superuser', 'admin', 'users') ASC; +``` + ### Grouping method ```php $db->groupBy ("name"); diff --git a/tests.php b/tests.php index f5f3966d..f12b891e 100644 --- a/tests.php +++ b/tests.php @@ -149,6 +149,14 @@ function createTable ($name, $data) { exit; } +// order by field +$db->orderBy("login","asc", Array ("user3","user2","user1")); +$login = $db->getValue ("users", "login"); +if ($login != "user3") { + echo "order by field test failed"; + exit; +} + $db->where ("active", true); $users = $db->get("users"); if ($db->count != 1) {