Implemented Insert method in query builder

This commit is contained in:
Timothy Warren 2012-03-13 10:13:57 -04:00
parent 46ca22d912
commit 15672b6928
2 changed files with 26 additions and 5 deletions

View File

@ -443,11 +443,21 @@ class Query_Builder {
* *
* @param string $table * @param string $table
* @param mixed $data * @param mixed $data
* @return * @return mixed
*/ */
public function insert($table, $data=array()) public function insert($table, $data=array())
{ {
// @todo implement insert method // No use duplicating logic!
if ( ! empty($data))
{
$this->set($data);
}
$params = array_values($this->set_array);
$sql = $this->_compile("insert", $table);
return $this->db->prepare_execute($sql, $params);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -461,6 +471,12 @@ class Query_Builder {
*/ */
public function update($table, $data=array()) public function update($table, $data=array())
{ {
// No use duplicating logic!
if ( ! empty($data))
{
$this->set($data);
}
$sql = 'UPDATE '.$this->quote_ident($table). ' SET '. $this->set_string; $sql = 'UPDATE '.$this->quote_ident($table). ' SET '. $this->set_string;
$params = array_values($this->set_array); $params = array_values($this->set_array);
@ -535,10 +551,11 @@ class Query_Builder {
/** /**
* String together the sql statements for sending to the db * String together the sql statements for sending to the db
* *
* @param $type * @param string $type
* @param string $table
* @return $string * @return $string
*/ */
private function _compile($type="select") private function _compile($type="select", $table="")
{ {
$sql = ''; $sql = '';
@ -569,7 +586,11 @@ class Query_Builder {
break; break;
case "insert": case "insert":
// @todo Implement insert statements $param_count = count($this->set_array);
$params = array_file(0, $param_count, '?');
$sql = 'INSERT (' . implode(', ', $this->set_array_keys) .
') INTO '. $this->quote_ident($table) .
'VALUES ('.implode(', ', $params).')';
break; break;
case "delete": case "delete":

Binary file not shown.