method shortening

This commit is contained in:
Timothy Warren 2014-04-24 21:29:40 -04:00
parent 2ae38bea88
commit 315dc5e1c5
2 changed files with 55 additions and 68 deletions

View File

@ -201,20 +201,15 @@ abstract class Abstract_Query_Builder implements Query_Builder_Interface {
foreach($arg as $k => $v) foreach($arg as $k => $v)
{ {
switch($val_type) if (in_array($val_type, array(self::KEY, self::VALUE)))
{
$var[] = ($val_type === self::KEY)
? $k
: $v;
}
else
{ {
case self::KEY:
$var[] = $k;
break;
case self::VALUE:
$var[] = $v;
break;
default:
case self::BOTH:
$var[$k] = $v; $var[$k] = $v;
// break;
} }
} }
@ -257,10 +252,7 @@ abstract class Abstract_Query_Builder implements Query_Builder_Interface {
$sql = $this->_compile($type, $table); $sql = $this->_compile($type, $table);
// Reset the query builder for the next query // Reset the query builder for the next query
if ($reset) if ($reset) $this->reset_query();
{
$this->reset_query();
}
return $sql; return $sql;
} }

View File

@ -27,6 +27,41 @@ use \Query\Driver\Driver_Interface;
*/ */
class Query_Builder extends Abstract_Query_Builder { class Query_Builder extends Abstract_Query_Builder {
/**
* String class values to be reset
*
* @var array
*/
private $string_vars = array(
'select_string',
'from_string',
'set_string',
'order_string',
'group_string',
'limit',
'offset',
'explain',
);
/**
* Array class variables to be reset
*
* @var array
*/
private $array_vars = array(
'set_array_keys',
'order_array',
'group_array',
'values',
'where_values',
'query_map',
'having_map'
);
// --------------------------------------------------------------------------
// ! Methods
// --------------------------------------------------------------------------
/** /**
* Constructor * Constructor
* *
@ -642,16 +677,10 @@ class Query_Builder extends Abstract_Query_Builder {
public function get($table='', $limit=FALSE, $offset=FALSE) public function get($table='', $limit=FALSE, $offset=FALSE)
{ {
// Set the table // Set the table
if ( ! empty($table)) if ( ! empty($table)) $this->from($table);
{
$this->from($table);
}
// Set the limit, if it exists // Set the limit, if it exists
if (is_int($limit)) if (is_int($limit)) $this->limit($limit, $offset);
{
$this->limit($limit, $offset);
}
return $this->_run("get", $table); return $this->_run("get", $table);
} }
@ -703,14 +732,9 @@ class Query_Builder extends Abstract_Query_Builder {
public function count_all_results($table='') public function count_all_results($table='')
{ {
// Set the table // Set the table
if ( ! empty($table)) if ( ! empty($table)) $this->from($table);
{
$this->from($table);
}
$result = $this->_run('get', $table); $result = $this->_run('get', $table);
$rows = $result->fetchAll(); $rows = $result->fetchAll();
return (int) count($rows); return (int) count($rows);
@ -728,10 +752,7 @@ class Query_Builder extends Abstract_Query_Builder {
public function insert($table, $data=array()) public function insert($table, $data=array())
{ {
// No use duplicating logic! // No use duplicating logic!
if ( ! empty($data)) if ( ! empty($data)) $this->set($data);
{
$this->set($data);
}
return $this->_run("insert", $table); return $this->_run("insert", $table);
} }
@ -767,10 +788,7 @@ class Query_Builder extends Abstract_Query_Builder {
public function update($table, $data=array()) public function update($table, $data=array())
{ {
// No use duplicating logic! // No use duplicating logic!
if ( ! empty($data)) if ( ! empty($data)) $this->set($data);
{
$this->set($data);
}
return $this->_run("update", $table); return $this->_run("update", $table);
} }
@ -787,10 +805,7 @@ class Query_Builder extends Abstract_Query_Builder {
public function delete($table, $where='') public function delete($table, $where='')
{ {
// Set the where clause // Set the where clause
if ( ! empty($where)) if ( ! empty($where)) $this->where($where);
{
$this->where($where);
}
return $this->_run("delete", $table); return $this->_run("delete", $table);
} }
@ -811,10 +826,7 @@ class Query_Builder extends Abstract_Query_Builder {
public function get_compiled_select($table='', $reset=TRUE) public function get_compiled_select($table='', $reset=TRUE)
{ {
// Set the table // Set the table
if ( ! empty($table)) if ( ! empty($table)) $this->from($table);
{
$this->from($table);
}
return $this->_get_compile('select', $table, $reset); return $this->_get_compile('select', $table, $reset);
} }
@ -874,30 +886,13 @@ class Query_Builder extends Abstract_Query_Builder {
public function reset_query() public function reset_query()
{ {
// Reset strings and booleans // Reset strings and booleans
foreach(array( foreach($this->string_vars as $var)
'select_string',
'from_string',
'set_string',
'order_string',
'group_string',
'limit',
'offset',
'explain',
) as $var)
{ {
$this->$var = NULL; $this->$var = NULL;
} }
// Reset arrays // Reset arrays
foreach(array( foreach($this->array_vars as $var)
'set_array_keys',
'order_array',
'group_array',
'values',
'where_values',
'query_map',
'having_map'
) as $var)
{ {
$this->$var = array(); $this->$var = array();
} }