From 3de38cd95313b3e22a879e11baf96e30bb24bbfd Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 30 Oct 2012 14:37:32 +0000 Subject: [PATCH] Some refactoring to eliminate some redundancy --- classes/query_builder.php | 72 +++++++++++++--------------------- tests/db_files/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes 2 files changed, 28 insertions(+), 44 deletions(-) diff --git a/classes/query_builder.php b/classes/query_builder.php index 01adc1a..29fbd27 100644 --- a/classes/query_builder.php +++ b/classes/query_builder.php @@ -116,7 +116,7 @@ class Query_Builder { * @var array */ private $values = array(); - + /** * Values to apply to where clauses in prepared statements * @@ -1094,23 +1094,15 @@ class Query_Builder { $this->limit($limit, $offset); } - $sql = $this->_compile(); - // Do prepared statements for anything involving a "where" clause if ( ! empty($this->query_map) || ! empty($this->having_map)) { - $result = $this->_run($sql); + return $this->_run("get", $table); } else { - // Otherwise, a simple query will do. - $result = $this->query($sql); + return $this->_run("get", $table, TRUE); } - - // Reset for next query - $this->reset_query(); - - return $result; } // -------------------------------------------------------------------------- @@ -1165,22 +1157,17 @@ class Query_Builder { $this->from($table); } - $sql = $this->_compile(); - // Do prepared statements for anything involving a "where" clause if ( ! empty($this->query_map)) { - $result = $this->_run($sql); + $result = $this->_run('get', $table); } else { // Otherwise, a simple query will do. - $result = $this->query($sql); + $result = $this->_run('get', $table, TRUE); } - // Reset for next query - $this->reset_query(); - $rows = $result->fetchAll(); return (int) count($rows); @@ -1203,12 +1190,7 @@ class Query_Builder { $this->set($data); } - $sql = $this->_compile("insert", $table); - $res = $this->_run($sql); - - $this->reset_query(); - - return $res; + return $this->_run("insert", $table); } // -------------------------------------------------------------------------- @@ -1228,13 +1210,7 @@ class Query_Builder { $this->set($data); } - $sql = $this->_compile('update', $table); - $res = $this->_run($sql); - - $this->reset_query(); - - // Run the query - return $res; + return $this->_run("update", $table); } // -------------------------------------------------------------------------- @@ -1254,14 +1230,7 @@ class Query_Builder { $this->where($where); } - // Create the SQL and parameters - $sql = $this->_compile("delete", $table); - $res = $this->_run($sql); - - $this->reset_query(); - - // Delete the table rows, and return the result - return $res; + return $this->_run("delete", $table); } // -------------------------------------------------------------------------- @@ -1393,19 +1362,33 @@ class Query_Builder { } // -------------------------------------------------------------------------- - + /** * Executes the compiled query * - * @param string $sql + * @param string type + * @param string table * @return mixed */ - private function _run($sql) + private function _run($type, $table, $simple=FALSE) { + $sql = $this->_compile($type, $table); $vals = array_merge($this->values, (array) $this->where_values); - return $this->prepare_execute($sql, $vals); + + if ($simple) + { + $res = $this->query($sql); + } + else + { + $res = $this->prepare_execute($sql, $vals); + } + + $this->reset_query(); + + return $res; } - + // -------------------------------------------------------------------------- /** @@ -1460,6 +1443,7 @@ class Query_Builder { switch($type) { default: + case "get": $sql = "SELECT * FROM {$this->from_string}"; // Set the select string diff --git a/tests/db_files/FB_TEST_DB.FDB b/tests/db_files/FB_TEST_DB.FDB index 0bd4c45c02339cd7618514cfa88918e4bb5a7c33..f4b1802155963271c83123447be71f99724a9112 100644 GIT binary patch delta 2101 zcmb7FO=whC6h8O8c{49_61^nS8mu9sZ=3`PW}3v-mCdH@qSgu}TNM=-N=9Y`H-!!^ z-rCT@9n4sQ3=$+*X+d2Tp#>z?{MFnbKg1lednC- zey?24m&^ID1ss@so`v?1SaxHJ?<)bo&oluXvG_N@x1ao(yFY9kBslatrVT;YmEhOR zv0t`cl%W)%bVBKdG7(BIl*v%0Lg|OH@0Ls_q_}z88%bo`c$L;<;NcuZpVEjH!bDuBFPh2PzPi{t4b!82A z=41rPw2dJr4w^a3&h0rv&h<6S?A;Q9b};^){(oFMdvSmV$Iq4u2Pt5pj)59(U{3s` z?)`_E6DEkSjt?qB>^w z4R)b?R~;(q&_5JJb?6xXFG%fwh(jL{2nHM1IPwXYsc~tw(yDRC-nzzd5C_tX1k6B1 z)d|Sn{w^T8#--8}JNDF=0w(^&V0+pGFrT@2x)^j_(Zxt|G4da)jRx-6GS$Y$&kwC* z-~^z4e~dZT;RP#D~9TN%!E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