From 15672b6928289bedf08aa2f9b130ed81e9779830 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 13 Mar 2012 10:13:57 -0400 Subject: [PATCH] Implemented Insert method in query builder --- sys/db/query_builder.php | 31 ++++++++++++++++++++++++++----- tests/test_dbs/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/sys/db/query_builder.php b/sys/db/query_builder.php index a3ce8b7..663ace9 100644 --- a/sys/db/query_builder.php +++ b/sys/db/query_builder.php @@ -443,11 +443,21 @@ class Query_Builder { * * @param string $table * @param mixed $data - * @return + * @return mixed */ 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()) { + // No use duplicating logic! + if ( ! empty($data)) + { + $this->set($data); + } + $sql = 'UPDATE '.$this->quote_ident($table). ' SET '. $this->set_string; $params = array_values($this->set_array); @@ -535,10 +551,11 @@ class Query_Builder { /** * String together the sql statements for sending to the db * - * @param $type + * @param string $type + * @param string $table * @return $string */ - private function _compile($type="select") + private function _compile($type="select", $table="") { $sql = ''; @@ -569,7 +586,11 @@ class Query_Builder { break; 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; case "delete": diff --git a/tests/test_dbs/FB_TEST_DB.FDB b/tests/test_dbs/FB_TEST_DB.FDB index eddeef569d897a8c40f877152765c0db52f50aa5..2a3cf1b0ed8bbae7069fc328fdc70eb7207d5237 100755 GIT binary patch delta 719 zcmZ9Jze^lJ6vyA}&7Nj`&W)1TB+8yE9(Nb?4vfJf;VP@JL=o!?f;JXbA&>}*#WWi5 zNAU^?h+?oZiGr6V30eomHi+OqAlg|;W^ZP=kR5j3e&@}6-gn<5Ni|8TF@v$nZ4O$G z>_{?JrEy09e9hc@g$>T+n;I>$SR$32#h>IKCb_MN+ z7|~T8%igx2`w)An+>hP#sf1$mhX>V>E&b0Y+JO&D4*;imQ@8S9^J{cf(nXd+S7cSt zX+Or^M?rlN4jc5(6uoTGUG}Kc(xROXZ2p5mA4CrQUeINPF?&agWqdjMD}}zb(2PCg zv}d7CJHK-VdNSakvx2S%wBMP3{T`?2Hx}*uIBiO`@}?BPOWXY()Uwv5lx`wOY3fa5e@!hog+vCz<$HuLB&s?LBC delta 690 zcmZ9Jze^lJ6oB9C?w#JuySs@-8zbzpr{?aA-anq|_xPCEZ{B+w$59+da}35RdwKZx zND;e+8hJbiFr!}phaLR@csqJC@LMDYBshAlG=b?jp1TNE&hxucNv5Quq^qQ-WJ<}j zl5I-1E19WD?nyRVPx<`VWiQU=-=V7TT19vdb8CsV89g1BSROV-2!o;s2i%9xJcMK6 z!%yB1w>%5Wdhb%_cAZakex>)H@FM)lgm9awFZej*1t(d6Yb|qt=>=dJ3?I^ZfB}6U zz{ixk6W#0^oeg7wf~Qz!Rq74#WwdV`+VVuHrIY~PO a{U?4E@j2Ce>>=Ib2&Dt;s(&rvM%Q0yuyiW`