Database_Query_Builder_Join
› Kohana_Database_Query_Builder_Join
› Database_Query_Builder
› Kohana_Database_Query_Builder
› Database_Query
› Kohana_Database_Query
Class Contents
Constants
- None
Properties
- None
Class declared in MODPATH/database/classes/database/query/builder/join.php on line 3.
Methods
public __construct( )
› Kohana_Database_Query_Builder_Join
Source Code
public function __construct($table, $type = NULL)
{
// Set the table to JOIN on
$this->_table = $table;
if ($type !== NULL)
{
// Set the JOIN type
$this->_type = (string) $type;
}
}
public compile( )
› Kohana_Database_Query_Builder_Join
Source Code
public function compile(Database $db)
{
if ($this->_type)
{
$sql = strtoupper($this->_type).' JOIN';
}
else
{
$sql = 'JOIN';
}
// Quote the table name that is being joined
$sql .= ' '.$db->quote_table($this->_table).' ON ';
$conditions = array();
foreach ($this->_on as $condition)
{
// Split the condition
list($c1, $op, $c2) = $condition;
if ($op)
{
// Make the operator uppercase and spaced
$op = ' '.strtoupper($op);
}
// Quote each of the identifiers used for the condition
$conditions[] = $db->quote_identifier($c1).$op.' '.$db->quote_identifier($c2);
}
// Concat the conditions "... AND ..."
$sql .= '('.implode(' AND ', $conditions).')';
return $sql;
}
public on( )
› Kohana_Database_Query_Builder_Join
Source Code
public function on($c1, $op, $c2)
{
$this->_on[] = array($c1, $op, $c2);
return $this;
}
public reset( )
› Kohana_Database_Query_Builder_Join
Source Code
public function reset()
{
$this->_type =
$this->_table = NULL;
$this->_on = array();
}
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);
}
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 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;
}