Some refactoring to eliminate some redundancy

This commit is contained in:
Timothy Warren 2012-10-30 14:37:32 +00:00
parent ee6f946782
commit 3de38cd953
2 changed files with 28 additions and 44 deletions

View File

@ -116,7 +116,7 @@ class Query_Builder {
* @var array * @var array
*/ */
private $values = array(); private $values = array();
/** /**
* Values to apply to where clauses in prepared statements * Values to apply to where clauses in prepared statements
* *
@ -1094,23 +1094,15 @@ class Query_Builder {
$this->limit($limit, $offset); $this->limit($limit, $offset);
} }
$sql = $this->_compile();
// Do prepared statements for anything involving a "where" clause // Do prepared statements for anything involving a "where" clause
if ( ! empty($this->query_map) || ! empty($this->having_map)) if ( ! empty($this->query_map) || ! empty($this->having_map))
{ {
$result = $this->_run($sql); return $this->_run("get", $table);
} }
else else
{ {
// Otherwise, a simple query will do. return $this->_run("get", $table, TRUE);
$result = $this->query($sql);
} }
// Reset for next query
$this->reset_query();
return $result;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -1165,22 +1157,17 @@ class Query_Builder {
$this->from($table); $this->from($table);
} }
$sql = $this->_compile();
// Do prepared statements for anything involving a "where" clause // Do prepared statements for anything involving a "where" clause
if ( ! empty($this->query_map)) if ( ! empty($this->query_map))
{ {
$result = $this->_run($sql); $result = $this->_run('get', $table);
} }
else else
{ {
// Otherwise, a simple query will do. // 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(); $rows = $result->fetchAll();
return (int) count($rows); return (int) count($rows);
@ -1203,12 +1190,7 @@ class Query_Builder {
$this->set($data); $this->set($data);
} }
$sql = $this->_compile("insert", $table); return $this->_run("insert", $table);
$res = $this->_run($sql);
$this->reset_query();
return $res;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -1228,13 +1210,7 @@ class Query_Builder {
$this->set($data); $this->set($data);
} }
$sql = $this->_compile('update', $table); return $this->_run("update", $table);
$res = $this->_run($sql);
$this->reset_query();
// Run the query
return $res;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -1254,14 +1230,7 @@ class Query_Builder {
$this->where($where); $this->where($where);
} }
// Create the SQL and parameters return $this->_run("delete", $table);
$sql = $this->_compile("delete", $table);
$res = $this->_run($sql);
$this->reset_query();
// Delete the table rows, and return the result
return $res;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -1393,19 +1362,33 @@ class Query_Builder {
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Executes the compiled query * Executes the compiled query
* *
* @param string $sql * @param string type
* @param string table
* @return mixed * @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); $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) switch($type)
{ {
default: default:
case "get":
$sql = "SELECT * FROM {$this->from_string}"; $sql = "SELECT * FROM {$this->from_string}";
// Set the select string // Set the select string

Binary file not shown.