abstract Database_Query_Builder_Where
› Kohana_Database_Query_Builder_Where
› Database_Query_Builder
› Kohana_Database_Query_Builder
› Database_Query
› Kohana_Database_Query
Class Contents
Constants
- None
Properties
- None
Methods
- and_where()
- and_where_close()
- and_where_open()
- limit()
- or_where()
- or_where_close()
- or_where_open()
- order_by()
- where()
- where_close()
- where_open()
- _compile_conditions()
- _compile_join()
- _compile_order_by()
- _compile_set()
- reset()
- __construct()
- __toString()
- as_assoc()
- as_object()
- bind()
- cached()
- compile()
- execute()
- param()
- parameters()
- type()
Class declared in MODPATH/database/classes/database/query/builder/where.php on line 3.
Methods
public and_where( )
› Kohana_Database_Query_Builder_Where
Source Code
public function and_where($column, $op, $value)
{
$this->_where[] = array('AND' => array($column, $op, $value));
return $this;
}
public and_where_close( )
› Kohana_Database_Query_Builder_Where
Source Code
public function and_where_close()
{
$this->_where[] = array('AND' => ')');
return $this;
}
public and_where_open( )
› Kohana_Database_Query_Builder_Where
Source Code
public function and_where_open()
{
$this->_where[] = array('AND' => '(');
return $this;
}
public limit( )
› Kohana_Database_Query_Builder_Where
Source Code
public function limit($number)
{
$this->_limit = (int) $number;
return $this;
}
public or_where( )
› Kohana_Database_Query_Builder_Where
Source Code
public function or_where($column, $op, $value)
{
$this->_where[] = array('OR' => array($column, $op, $value));
return $this;
}
public or_where_close( )
› Kohana_Database_Query_Builder_Where
Source Code
public function or_where_close()
{
$this->_where[] = array('OR' => ')');
return $this;
}
public or_where_open( )
› Kohana_Database_Query_Builder_Where
Source Code
public function or_where_open()
{
$this->_where[] = array('OR' => '(');
return $this;
}
public order_by( )
› Kohana_Database_Query_Builder_Where
Source Code
public function order_by($column, $direction = NULL)
{
$this->_order_by[] = array($column, $direction);
return $this;
}
public where( )
› Kohana_Database_Query_Builder_Where
Source Code
public function where($column, $op, $value)
{
return $this->and_where($column, $op, $value);
}
public where_close( )
› Kohana_Database_Query_Builder_Where
Source Code
public function where_close()
{
return $this->and_where_close();
}
public where_open( )
› Kohana_Database_Query_Builder_Where
Source Code
public function where_open()
{
return $this->and_where_open();
}
protected _compile_conditions( )
› Kohana_Database_Query_Builder
Source Code
protected function _compile_conditions(Database $db, array $conditions)
{
$last_condition = NULL;
$sql = '';
foreach ($conditions as $group)
{
// Process groups of conditions
foreach ($group as $logic => $condition)
{
if ($condition === '(')
{
if ( ! empty($sql) AND $last_condition !== '(')
{
// Include logic operator
$sql .= ' '.$logic.' ';
}
$sql .= '(';
}
elseif ($condition === ')')
{
$sql .= ')';
}
else
{
if ( ! empty($sql) AND $last_condition !== '(')
{
// Add the logic operator
$sql .= ' '.$logic.' ';
}
// Split the condition
list($column, $op, $value) = $condition;
if ($value === NULL)
{
if ($op === '=')
{
// Convert "val = NULL" to "val IS NULL"
$op = 'IS';
}
elseif ($op === '!=')
{
// Convert "val != NULL" to "valu IS NOT NULL"
$op = 'IS NOT';
}
}
// Database operators are always uppercase
$op = strtoupper($op);
if ($op === 'BETWEEN' AND is_array($value))
{
// BETWEEN always has exactly two arguments
list($min, $max) = $value;
if (is_string($min) AND array_key_exists($min, $this->_parameters))
{
// Set the parameter as the minimum
$min = $this->_parameters[$min];
}
if (is_string($max) AND array_key_exists($max, $this->_parameters))
{
// Set the parameter as the maximum
$max = $this->_parameters[$max];
}
// Quote the min and max value
$value = $db->quote($min).' AND '.$db->quote($max);
}
else
{
if (is_string($value) AND array_key_exists($value, $this->_parameters))
{
// Set the parameter as the value
$value = $this->_parameters[$value];
}
// Quote the entire value normally
$value = $db->quote($value);
}
if ($column)
{
// Apply proper quoting to the column
$column = $db->quote_identifier($column);
}
// Append the statement to the query
$sql .= trim($column.' '.$op.' '.$value);
}
$last_condition = $condition;
}
}
return $sql;
}
protected _compile_join( )
› Kohana_Database_Query_Builder
Source Code
protected function _compile_join(Database $db, array $joins)
{
$statements = array();
foreach ($joins as $join)
{
// Compile each of the join statements
$statements[] = $join->compile($db);
}
return implode(' ', $statements);
}
protected _compile_order_by( )
› Kohana_Database_Query_Builder
Source Code
protected function _compile_order_by(Database $db, array $columns)
{
$sort = array();
foreach ($columns as $group)
{
list ($column, $direction) = $group;
if ($direction)
{
// Make the direction uppercase
$direction = strtoupper($direction);
}
if ($column)
{
// Quote the column, if it has a value
$column = $db->quote_identifier($column);
}
$sort[] = trim($column.' '.$direction);
}
return 'ORDER BY '.implode(', ', $sort);
}
protected _compile_set( )
› Kohana_Database_Query_Builder
Source Code
protected function _compile_set(Database $db, array $values)
{
$set = array();
foreach ($values as $group)
{
// Split the set
list ($column, $value) = $group;
// Quote the column name
$column = $db->quote_identifier($column);
if (is_string($value) AND array_key_exists($value, $this->_parameters))
{
// Use the parameter value
$value = $this->_parameters[$value];
}
$set[$column] = $column.' = '.$db->quote($value);
}
return implode(', ', $set);
}
abstract public reset( )
› Kohana_Database_Query_Builder
Source Code
abstract public function reset();
public __construct( )
› Kohana_Database_Query
Source Code
public function __construct($type, $sql)
{
$this->_type = $type;
$this->_sql = $sql;
}
final public __toString( )
› Kohana_Database_Query
Source Code
final public function __toString()
{
try
{
// Return the SQL string
return $this->compile(Database::instance());
}
catch (Exception $e)
{
return Kohana::exception_text($e);
}
}
public as_assoc( )
› Kohana_Database_Query
Source Code
public function as_assoc()
{
$this->_as_object = FALSE;
$this->_object_params = array();
return $this;
}
public as_object( )
› Kohana_Database_Query
Source Code
public function as_object($class = TRUE, array $params = NULL)
{
$this->_as_object = $class;
if ($params)
{
// Add object parameters
$this->_object_params = $params;
}
return $this;
}
public bind( )
› Kohana_Database_Query
Source Code
public function bind($param, & $var)
{
// Bind a value to a variable
$this->_parameters[$param] =& $var;
return $this;
}
public cached( )
› Kohana_Database_Query
Source Code
public function cached($lifetime = NULL)
{
if ($lifetime === NULL)
{
// Use the global setting
$lifetime = Kohana::$cache_life;
}
$this->_lifetime = $lifetime;
return $this;
}
public compile( )
› Kohana_Database_Query
Source Code
public function compile(Database $db)
{
// Import the SQL locally
$sql = $this->_sql;
if ( ! empty($this->_parameters))
{
// Quote all of the values
$values = array_map(array($db, 'quote'), $this->_parameters);
// Replace the values in the SQL
$sql = strtr($sql, $values);
}
return $sql;
}
public execute( )
› Kohana_Database_Query
Source Code
public function execute($db = NULL)
{
if ( ! is_object($db))
{
// Get the database instance
$db = Database::instance($db);
}
// Compile the SQL query
$sql = $this->compile($db);
if ( ! empty($this->_lifetime) AND $this->_type === Database::SELECT)
{
// Set the cache key based on the database instance name and SQL
$cache_key = 'Database::query("'.$db.'", "'.$sql.'")';
if ($result = Kohana::cache($cache_key, NULL, $this->_lifetime))
{
// Return a cached result
return new Database_Result_Cached($result, $sql, $this->_as_object, $this->_object_params);
}
}
// Execute the query
$result = $db->query($this->_type, $sql, $this->_as_object, $this->_object_params);
if (isset($cache_key))
{
// Cache the result array
Kohana::cache($cache_key, $result->as_array(), $this->_lifetime);
}
return $result;
}
public param( )
› Kohana_Database_Query
Source Code
public function param($param, $value)
{
// Add or overload a new parameter
$this->_parameters[$param] = $value;
return $this;
}
public parameters( )
› Kohana_Database_Query
Source Code
public function parameters(array $params)
{
// Merge the new parameters in
$this->_parameters = $params + $this->_parameters;
return $this;
}