Commit a71834da authored by Alexander Butenko's avatar Alexander Butenko

Small refactoring of the Query builder part

parent dd7376b4
......@@ -459,7 +459,6 @@ class MysqliDb
// Did the user call the "where" method?
if (!empty($this->_where)) {
// if update data was passed, filter through and create the SQL query, accordingly.
if ($hasTableData) {
$pos = strpos($this->_query, 'UPDATE');
......@@ -471,10 +470,9 @@ class MysqliDb
$this->_query .= $prop . $value['[I]'] . ", ";
else {
$this->_query .= $value['[F]'][0] . ", ";
if (is_array ($value['[F]'][1])) {
foreach ($value['[F]'][1] as $key => $val) {
if (!empty($val['[F]'][1]) && is_array ($value['[F]'][1])) {
foreach ($value['[F]'][1] as $val)
$this->_paramTypeList .= $this->_determineType($val);
}
}
}
} else {
......@@ -555,33 +553,24 @@ class MysqliDb
// Determine if is INSERT query
if ($hasTableData) {
$pos = strpos($this->_query, 'INSERT');
if ($pos !== false) {
//is insert statement
$keys = array_keys($tableData);
$values = array_values($tableData);
$this->_query .= '(' . implode($keys, ', ') . ')';
$this->_query .= '(' . implode(array_keys($tableData), ', ') . ')';
$this->_query .= ' VALUES(';
// wrap values in quotes if needed
foreach ($values as $key => $val) {
if (is_array($val)) {
if (!empty($val['[I]']))
$this->_query .= $keys[$key].$val['[I]'].", ";
else {
$this->_query .= $val['[F]'][0].", ";
if (is_array ($val['[F]'][1])) {
foreach ($val['[F]'][1] as $key => $value) {
$this->_paramTypeList .= $this->_determineType($value);
}
}
foreach ($tableData as $key => $val) {
if (!is_array ($val)) {
$this->_paramTypeList .= $this->_determineType($val);
$this->_query .= '?, ';
} else if (!empty($val['[I]'])) {
$this->_query .= $key . $val['[I]'] . ", ";
} else {
$this->_query .= $val['[F]'][0] . ", ";
if (!empty($val['[F]'][1]) && is_array ($val['[F]'][1])) {
foreach ($val['[F]'][1] as $value)
$this->_paramTypeList .= $this->_determineType($value);
}
continue;
}
$values[$key] = "'{$val}'";
$this->_paramTypeList .= $this->_determineType($val);
$this->_query .= '?, ';
}
$this->_query = rtrim($this->_query, ', ');
$this->_query .= ')';
......@@ -602,14 +591,12 @@ class MysqliDb
// Prepare table data bind parameters
if ($hasTableData) {
$this->_bindParams[0] = $this->_paramTypeList;
foreach ($tableData as $prop => $val) {
if (!is_array($tableData[$prop])) {
array_push($this->_bindParams, $tableData[$prop]);
continue;
}
if (is_array($tableData[$prop]['[F]'][1])) {
foreach ($tableData[$prop]['[F]'][1] as $val)
foreach ($tableData as $val) {
if (!is_array ($val)) {
array_push ($this->_bindParams, $val);
} else if (!empty($val['[F]'][1]) && is_array ($val['[F]'][1])) {
// collect func() arguments
foreach ($val['[F]'][1] as $val)
array_push($this->_bindParams, $val);
}
}
......
<?php
require_once ("MysqliDb.php");
error_reporting(E_ALL);
$db = new Mysqlidb('localhost', 'root', '', 'testdb');
if(!$db) die("Database error");
$tables = Array (
'users' => Array (
'login' => 'char(10) not null',
'customerId' => 'int(10) not null',
'firstName' => 'char(10) not null',
'lastName' => 'char(10) not null',
'password' => 'text not null',
'createdAt' => 'datetime',
'expires' => 'datetime',
'loginCount' => 'int(10) default 0'
)
);
$data = Array (
Array ('login' => 'user1',
'customerId' => 10,
'firstName' => 'John',
'lastName' => 'Doe',
'password' => $db->func('SHA1(?)',Array ("secretpassword+salt")),
'createdAt' => $db->now(),
'expires' => $db->now('+1Y'),
'loginCount' => $db->inc()
),
Array ('login' => 'user2',
'customerId' => 10,
'firstName' => 'Mike',
'lastName' => 'B',
'password' => $db->func('SHA1(?)',Array ("secretpassword2+salt")),
'createdAt' => $db->now(),
'expires' => $db->now('+1Y'),
'loginCount' => $db->inc(2)
),
Array ('login' => 'user3',
'customerId' => 11,
'firstName' => 'Pete',
'lastName' => 'D',
'password' => $db->func('SHA1(?)',Array ("secretpassword2+salt")),
'createdAt' => $db->now(),
'expires' => $db->now('+1Y'),
'loginCount' => $db->inc(3)
)
);
function createTable ($name, $data) {
global $db;
//$q = "CREATE TABLE $name (id INT(9) UNSIGNED PRIMARY KEY NOT NULL";
$q = "CREATE TABLE $name (id INT(9) UNSIGNED PRIMARY KEY AUTO_INCREMENT";
foreach ($data as $k => $v) {
$q .= ", $k $v";
}
$q .= ")";
$db->rawQuery($q);
}
foreach ($tables as $name => $fields) {
$db->rawQuery("DROP TABLE $name");
createTable ($name, $fields);
}
foreach ($data as $d) {
$id = $db->insert("users", $d);
if ($id)
$d['id'] = $id;
else {
echo "failed to insert: ".$db->getLastQuery() ."\n". $db->getLastError();
}
}
$db->orderBy("id","asc");
$users = $db->get("users");
if ($db->count != 3) {
echo "Invalid total insert count";
exit;
}
// TODO
//$db->where("createdAt", Array (">" => $db->interval("-1h")));
//$users = $db->get("users");
//print_r ($users);
$db->where("firstname", Array("LIKE" => '%John%'));
$users = $db->get("users");
if ($db->count != 1) {
echo "Invalid insert count in LIKE: ".$db->count;
print_r ($users);
echo $db->getLastQuery();
exit;
}
// FIXME ADD IN and BETWEEN CHECKS
$db->groupBy("customerId");
$cnt = $db->get ("users", null, "customerId, count(id) as cnt");
if ($db->count != 2) {
echo "Invalid records count with group by";
}
$upData = Array (
'expires' => $db->now("+5M","expires"),
'loginCount' => $db->inc()
);
$db->where ("id", 1);
$cnt = $db->update("users", $upData);
echo "all done\n";
$db->where ("id", 1);
$db->getOne("users");
echo "cnt=".$db->count;
//print_r($db->rawQuery("CALL simpleproc(?)",Array("test")));
?>
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