Commit 7c6b2f4b authored by Alexander Butenko's avatar Alexander Butenko

insert/update Functions calls draft

parent f171a2cd
...@@ -422,6 +422,14 @@ class MysqliDb ...@@ -422,6 +422,14 @@ class MysqliDb
$pos = strpos($this->_query, 'UPDATE'); $pos = strpos($this->_query, 'UPDATE');
if ($pos !== false) { if ($pos !== false) {
foreach ($tableData as $prop => $value) { foreach ($tableData as $prop => $value) {
if (is_array($value)) {
$this->_query .= $prop ." = ";
if (!empty($value['[I]']))
$this->_query .= $prop.$value['[I]'].", ";
else
$this->_query .= $value['[F]'].", ";
continue;
}
// determines what data type the item is, for binding purposes. // determines what data type the item is, for binding purposes.
$this->_paramTypeList .= $this->_determineType($value); $this->_paramTypeList .= $this->_determineType($value);
...@@ -498,19 +506,22 @@ class MysqliDb ...@@ -498,19 +506,22 @@ class MysqliDb
//is insert statement //is insert statement
$keys = array_keys($tableData); $keys = array_keys($tableData);
$values = array_values($tableData); $values = array_values($tableData);
$num = count($keys);
// wrap values in quotes $this->_query .= '(' . implode($keys, ', ') . ')';
$this->_query .= ' VALUES(';
// wrap values in quotes if needed
foreach ($values as $key => $val) { foreach ($values as $key => $val) {
if (is_array($val)) {
if (!empty($val['[I]']))
$this->_query .= $keys[$key].$val['[I]'].", ";
else
$this->_query .= $val['[F]'].", ";
continue;
}
$values[$key] = "'{$val}'"; $values[$key] = "'{$val}'";
$this->_paramTypeList .= $this->_determineType($val); $this->_paramTypeList .= $this->_determineType($val);
}
$this->_query .= '(' . implode($keys, ', ') . ')';
$this->_query .= ' VALUES(';
while ($num !== 0) {
$this->_query .= '?, '; $this->_query .= '?, ';
$num--;
} }
$this->_query = rtrim($this->_query, ', '); $this->_query = rtrim($this->_query, ', ');
$this->_query .= ')'; $this->_query .= ')';
...@@ -529,7 +540,8 @@ class MysqliDb ...@@ -529,7 +540,8 @@ class MysqliDb
if ($hasTableData) { if ($hasTableData) {
$this->_bindParams[0] = $this->_paramTypeList; $this->_bindParams[0] = $this->_paramTypeList;
foreach ($tableData as $prop => $val) { foreach ($tableData as $prop => $val) {
array_push($this->_bindParams, $tableData[$prop]); if (!is_array($tableData[$prop]))
array_push($this->_bindParams, $tableData[$prop]);
} }
} }
// Prepare where condition bind parameters // Prepare where condition bind parameters
...@@ -652,4 +664,72 @@ class MysqliDb ...@@ -652,4 +664,72 @@ class MysqliDb
return $this->_mysqli->error; return $this->_mysqli->error;
} }
/* Helper functions */
/**
* Method returns generated interval function as a string
*
* @param string interval in the formats:
* "1", "-1d" or "- 1 day" -- For interval - 1 day
* Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear
* Default null;
* @param string Initial date
*
* @return string
*/
public function interval ($diff, $func = "NOW()") {
$types = Array ("s" => "second", "m" => "minute", "h" => "hour", "d" => "day", "M" => "month", "Y" => "year");
$incr = '+';
$items = '';
$type = 'd';
if ($diff && preg_match('/([+-]?) ?([0-9]+) ?([a-zA-Z]?)/',$diff, $matches)) {
if (!empty ($matches[1])) $incr = $matches[1];
if (!empty ($matches[2])) $items = $matches[2];
if (!empty ($matches[3])) $type = $matches[3];
if (!in_array($type, array_keys($types)))
trigger_error ("invalid interval type in '{$diff}'");
$func .= " ".$incr ." interval ". $items ." ".$types[$type] . " ";
}
return $func;
}
/**
* Method returns generated interval function as an insert/update function
*
* @param string interval in the formats:
* "1", "-1d" or "- 1 day" -- For interval - 1 day
* Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear
* Default null;
* @param string Initial date
*
* @return array
*/
public function now ($diff = null, $func = "NOW()") {
return Array ("[F]" => $this->interval($diff, $func));
}
/**
* Method generates incremental function call
* @param int increment amount. 1 by default
*/
public function inc($num = 1) {
return Array ("[I]" => "+" . (int)$num);
}
/**
* Method generates decrimental function call
* @param int increment amount. 1 by default
*/
public function dec ($num = 1) {
return Array ("[I]" => "-" . (int)$num);
}
/**
* Method generates user defined function call
* @param string user function body
*/
public function func ($expr) {
return Array ("[F]" => $expr);
}
} // END class } // END class
...@@ -19,6 +19,9 @@ $data = array( ...@@ -19,6 +19,9 @@ $data = array(
'login' => 'admin', 'login' => 'admin',
'firstName' => 'John', 'firstName' => 'John',
'lastName' => 'Doe', 'lastName' => 'Doe',
'fullName' => $db->func("CONCAT(firstName," ",lastName)"),
'createdAt' => $db->now(),
'expires' => $db->now("+1y")
); );
$id = $db->insert('users', $data) $id = $db->insert('users', $data)
...@@ -58,7 +61,8 @@ echo $user['id']; ...@@ -58,7 +61,8 @@ echo $user['id'];
```php ```php
$data = array ( $data = array (
'firstName' => 'Bobby', 'firstName' => 'Bobby',
'lastName' => 'Tables' 'lastName' => 'Tables',
'editCount' => $db->inc()
); );
$db->where('id', 1); $db->where('id', 1);
if($db->update('users', $data)) echo 'successfully updated'; if($db->update('users', $data)) echo 'successfully updated';
......
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