Commit 59cb2d40 authored by Alexander Butenko's avatar Alexander Butenko

mysqlidb: Added ArrayBuilder,ObjectBuilder and JsonBuilder to be consistent with dbObject

parent 6a3e2477
...@@ -112,6 +112,14 @@ class MysqliDb ...@@ -112,6 +112,14 @@ class MysqliDb
*/ */
protected $isSubQuery = false; protected $isSubQuery = false;
/**
* Return type: 'Array' to return results as array, 'Object' as object
* 'Json' as json string
*
* @var string
*/
public $returnType = 'Object';
/** /**
* Variables for query execution tracing * Variables for query execution tracing
* *
...@@ -211,6 +219,38 @@ class MysqliDb ...@@ -211,6 +219,38 @@ class MysqliDb
$this->_bindParams = array(''); // Create the empty 0 index $this->_bindParams = array(''); // Create the empty 0 index
$this->_query = null; $this->_query = null;
$this->_queryOptions = array(); $this->_queryOptions = array();
$this->returnType = 'Array';
}
/**
* Helper function to create dbObject with Json return type
*
* @return dbObject
*/
public function JsonBuilder () {
$this->returnType = 'Json';
return $this;
}
/**
* Helper function to create dbObject with Array return type
* Added for consistency as thats default output type
*
* @return dbObject
*/
public function ArrayBuilder () {
$this->returnType = 'Array';
return $this;
}
/**
* Helper function to create dbObject with Object return type.
*
* @return dbObject
*/
public function ObjectBuilder () {
$this->returnType = 'Object';
return $this;
} }
/** /**
...@@ -358,11 +398,12 @@ class MysqliDb ...@@ -358,11 +398,12 @@ class MysqliDb
{ {
$res = $this->get ($tableName, 1, $columns); $res = $this->get ($tableName, 1, $columns);
if (is_object($res)) if ($res instanceof MysqliDb)
return $res; return $res;
else if (is_array ($res) && isset ($res[0]))
if (isset($res[0]))
return $res[0]; return $res[0];
else if ($res)
return $res;
return null; return null;
} }
...@@ -376,7 +417,7 @@ class MysqliDb ...@@ -376,7 +417,7 @@ class MysqliDb
*/ */
public function getValue($tableName, $column) public function getValue($tableName, $column)
{ {
$res = $this->get ($tableName, 1, "{$column} as retval"); $res = $this->ArrayBuilder()->get ($tableName, 1, "{$column} as retval");
if (isset($res[0]["retval"])) if (isset($res[0]["retval"]))
return $res[0]["retval"]; return $res[0]["retval"];
...@@ -801,9 +842,14 @@ class MysqliDb ...@@ -801,9 +842,14 @@ class MysqliDb
$this->totalCount = 0; $this->totalCount = 0;
$this->count = 0; $this->count = 0;
while ($stmt->fetch()) { while ($stmt->fetch()) {
$x = array(); if ($this->returnType == 'Object') {
foreach ($row as $key => $val) { $x = new stdClass ();
$x[$key] = $val; foreach ($row as $key => $val)
$x->$key = $val;
} else {
$x = array();
foreach ($row as $key => $val)
$x[$key] = $val;
} }
$this->count++; $this->count++;
array_push($results, $x); array_push($results, $x);
...@@ -817,6 +863,9 @@ class MysqliDb ...@@ -817,6 +863,9 @@ class MysqliDb
$totalCount = $stmt->fetch_row(); $totalCount = $stmt->fetch_row();
$this->totalCount = $totalCount[0]; $this->totalCount = $totalCount[0];
} }
if ($this->returnType == 'Json') {
return json_encode ($results);
}
return $results; return $results;
} }
......
...@@ -59,6 +59,7 @@ class dbObject { ...@@ -59,6 +59,7 @@ class dbObject {
public $isNew = true; public $isNew = true;
/** /**
* Return type: 'Array' to return results as array, 'Object' as object * Return type: 'Array' to return results as array, 'Object' as object
* 'Json' as json string
* *
* @var string * @var string
*/ */
......
...@@ -128,7 +128,6 @@ if ($db->update ('users', $data)) ...@@ -128,7 +128,6 @@ if ($db->update ('users', $data))
else else
echo 'update failed: ' . $db->getLastError(); echo 'update failed: ' . $db->getLastError();
``` ```
### Select Query ### Select Query
After any select/get function calls amount or returned rows After any select/get function calls amount or returned rows
is stored in $count variable is stored in $count variable
...@@ -165,7 +164,18 @@ or select one column value or function result ...@@ -165,7 +164,18 @@ or select one column value or function result
$count = $db->getValue ("users", "count(*)"); $count = $db->getValue ("users", "count(*)");
echo "{$count} users found"; echo "{$count} users found";
``` ```
### 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
```php
// Array return type
$= $db->getOne("users");
echo $u['login'];
// Object return type
$u = $db->ObjectBuilder()->getOne("users");
echo $u->login;
// Json return type
$json = $db->JsonBuilder()->getOne("users");
```
### Delete Query ### Delete Query
```php ```php
$db->where('id', 1); $db->where('id', 1);
......
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