Commit e4353beb authored by Alexander Butenko's avatar Alexander Butenko

Added Map() to transform result

parent 4b1dd0df
...@@ -152,6 +152,12 @@ class MysqliDb ...@@ -152,6 +152,12 @@ class MysqliDb
*/ */
protected $_lockInShareMode = false; protected $_lockInShareMode = false;
/**
* Key field for Map()'ed result array
* @var string
*/
protected $_mapKey = null;
/** /**
* Variables for query execution tracing * Variables for query execution tracing
* *
...@@ -266,6 +272,7 @@ class MysqliDb ...@@ -266,6 +272,7 @@ class MysqliDb
$this->_tableName = ''; $this->_tableName = '';
$this->_lastInsertId = null; $this->_lastInsertId = null;
$this->_updateColumns = null; $this->_updateColumns = null;
$this->_mapKey = null;
} }
/** /**
...@@ -998,7 +1005,10 @@ class MysqliDb ...@@ -998,7 +1005,10 @@ class MysqliDb
} }
} }
$this->count++; $this->count++;
array_push ($results, $x); if ($this->_mapKey)
$results[$row[$this->_mapKey]] = count ($row) > 2 ? $x : end ($x);
else
array_push ($results, $x);
} }
if ($shouldStoreResult) if ($shouldStoreResult)
$stmt->free_result(); $stmt->free_result();
...@@ -1528,4 +1538,16 @@ class MysqliDb ...@@ -1528,4 +1538,16 @@ class MysqliDb
$this->get ('information_schema.tables', $count); $this->get ('information_schema.tables', $count);
return $this->count == $count; return $this->count == $count;
} }
/**
* Return result as an associative array with $idField field value used as a record key
*
* @param String $idField field name to use for a mapped element key
*
* @return Array Returns an array($k => $v) if get(.."param1, param2"), array ($k => array ($v, $v)) otherwise
*/
public function map ($idField) {
$this->_mapKey = $idField;
return $this;
}
} // END class } // END class
...@@ -186,7 +186,7 @@ class dbObject { ...@@ -186,7 +186,7 @@ class dbObject {
*/ */
private function JsonBuilder () { private function JsonBuilder () {
$this->returnType = 'Json'; $this->returnType = 'Json';
return $return; return $this;
} }
/** /**
......
...@@ -198,6 +198,29 @@ foreach ($logins as $login) ...@@ -198,6 +198,29 @@ foreach ($logins as $login)
echo $login; echo $login;
``` ```
### Result transformation / map
Instead of getting an pure array of results its possible to get result in an associative array with a needed key. If only 2 fields to fetch will be set in get(),
method will return result in array($k => $v) and array ($k => array ($v, $v)) in rest of the cases.
```php
$user = $db->map ('login')->ObjectBuilder()->getOne ('users', 'login, id');
Array
(
[user1] => 1
)
$user = $db->map ('login')->ObjectBuilder()->getOne ('users', 'id,login,createdAt');
Array
(
[user1] => stdClass Object
(
[id] => 1
[login] => user1
[createdAt] => 2015-10-22 22:27:53
)
)
```
### Defining a return type ### Defining a return type
MysqliDb can return result in 3 different formats: Array of Array, Array of Objects and a Json string. To select a return type use ArrayBuilder(), ObjectBuilder() and JsonBuilder() methods. Note that ArrayBuilder() is a default return type MysqliDb can return result in 3 different formats: Array of Array, Array of Objects and a Json string. To select a return type use ArrayBuilder(), ObjectBuilder() and JsonBuilder() methods. Note that ArrayBuilder() is a default return type
......
...@@ -386,6 +386,28 @@ if ($db->totalCount != 3) { ...@@ -386,6 +386,28 @@ if ($db->totalCount != 3) {
echo "error in totalCount"; echo "error in totalCount";
exit; exit;
} }
$result = $db->map ('id')->ArrayBuilder()->getOne ('users', 'id,login');
if (key ($result) != 1 && $result[1] != 'user1') {
echo 'map string=string failed';
exit;
}
$result = $db->map ('id')->ArrayBuilder()->getOne ('users', 'id,login,createdAt');
if (key ($result) != 1 && !is_array ($result[1])) {
echo 'map string=array failed';
exit;
}
$result = $db->map ('id')->ObjectBuilder()->getOne ('users', 'id,login');
if (key ($result) != 1 && $result[1] != 'user1') {
echo 'map object string=string failed';
exit;
}
$result = $db->map ('id')->ObjectBuilder()->getOne ('users', 'id,login,createdAt');
if (key ($result) != 1 && !is_object ($result[1])) {
echo 'map string=object failed';
exit;
}
/// ///
//TODO: insert test //TODO: insert test
$db->delete("users"); $db->delete("users");
...@@ -396,7 +418,6 @@ if ($db->count != 0) { ...@@ -396,7 +418,6 @@ if ($db->count != 0) {
} }
$db->delete("products"); $db->delete("products");
//print_r($db->rawQuery("CALL simpleproc(?)",Array("test"))); //print_r($db->rawQuery("CALL simpleproc(?)",Array("test")));
print_r ($db->trace); print_r ($db->trace);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment