Database query builder for UPDATE statements. See Query Builder for usage and examples.
Class declared in MODPATH/database/classes/kohana/database/query/builder/update.php on line 11.
$_as_object
$_force_execute
$_lifetime
$_limit
$_object_params
$_order_by
$_parameters
$_set
$_sql
$_table
$_type
$_whereSet the table for a update.
mixed
$table
= NULL - Table name or array($table, $alias) or objectvoid
public function __construct($table = NULL)
{
if ($table)
{
// Set the inital table name
$this->_table = $table;
}
// Start the query with no SQL
return parent::__construct(Database::UPDATE, '');
}
Compile the SQL query and return it.
object
$db
required - Database instancestring
public function compile(Database $db)
{
// Start an update query
$query = 'UPDATE '.$db->quote_table($this->_table);
// Add the columns to update
$query .= ' SET '.$this->_compile_set($db, $this->_set);
if ( ! empty($this->_where))
{
// Add selection conditions
$query .= ' WHERE '.$this->_compile_conditions($db, $this->_where);
}
if ( ! empty($this->_order_by))
{
// Add sorting
$query .= ' '.$this->_compile_order_by($db, $this->_order_by);
}
if ($this->_limit !== NULL)
{
// Add limiting
$query .= ' LIMIT '.$this->_limit;
}
$this->_sql = $query;
return parent::compile($db);
}
Reset the current builder status.
$this
public function reset()
{
$this->_table = NULL;
$this->_set =
$this->_where = array();
$this->_limit = NULL;
$this->_parameters = array();
$this->_sql = NULL;
return $this;
}
Set the values to update with an associative array.
array
$pairs
required - Associative (column => value) list$this
public function set(array $pairs)
{
foreach ($pairs as $column => $value)
{
$this->_set[] = array($column, $value);
}
return $this;
}
Sets the table to update.
mixed
$table
required - Table name or array($table, $alias) or object$this
public function table($table)
{
$this->_table = $table;
return $this;
}
Set the value of a single column.
mixed
$column
required - Table name or array($table, $alias) or objectmixed
$value
required - Column value$this
public function value($column, $value)
{
$this->_set[] = array($column, $value);
return $this;
}
Creates a new "AND WHERE" condition for the query.
mixed
$column
required - Column name or array($column, $alias) or objectstring
$op
required - Logic operatormixed
$value
required - Column value$this
public function and_where($column, $op, $value)
{
$this->_where[] = array('AND' => array($column, $op, $value));
return $this;
}
Closes an open "AND WHERE (...)" grouping.
$this
public function and_where_close()
{
$this->_where[] = array('AND' => ')');
return $this;
}
Opens a new "AND WHERE (...)" grouping.
$this
public function and_where_open()
{
$this->_where[] = array('AND' => '(');
return $this;
}
Return up to "LIMIT ..." results
integer
$number
required - Maximum results to return or NULL to reset$this
public function limit($number)
{
$this->_limit = $number;
return $this;
}
Creates a new "OR WHERE" condition for the query.
mixed
$column
required - Column name or array($column, $alias) or objectstring
$op
required - Logic operatormixed
$value
required - Column value$this
public function or_where($column, $op, $value)
{
$this->_where[] = array('OR' => array($column, $op, $value));
return $this;
}
Closes an open "OR WHERE (...)" grouping.
$this
public function or_where_close()
{
$this->_where[] = array('OR' => ')');
return $this;
}
Opens a new "OR WHERE (...)" grouping.
$this
public function or_where_open()
{
$this->_where[] = array('OR' => '(');
return $this;
}
Applies sorting with "ORDER BY ..."
mixed
$column
required - Column name or array($column, $alias) or objectstring
$direction
= NULL - Direction of sorting$this
public function order_by($column, $direction = NULL)
{
$this->_order_by[] = array($column, $direction);
return $this;
}
Alias of and_where()
mixed
$column
required - Column name or array($column, $alias) or objectstring
$op
required - Logic operatormixed
$value
required - Column value$this
public function where($column, $op, $value)
{
return $this->and_where($column, $op, $value);
}
Closes an open "AND WHERE (...)" grouping.
$this
public function where_close()
{
return $this->and_where_close();
}
Alias of and_where_open()
$this
public function where_open()
{
return $this->and_where_open();
}
Return the SQL query string.
string
final public function __toString()
{
try
{
// Return the SQL string
return $this->compile(Database::instance());
}
catch (Exception $e)
{
return Kohana_Exception::text($e);
}
}
Returns results as associative arrays
$this
public function as_assoc()
{
$this->_as_object = FALSE;
$this->_object_params = array();
return $this;
}
Returns results as objects
string
$class
= bool TRUE - Classname or TRUE for stdClassarray
$params
= NULL - $params$this
public function as_object($class = TRUE, array $params = NULL)
{
$this->_as_object = $class;
if ($params)
{
// Add object parameters
$this->_object_params = $params;
}
return $this;
}
Bind a variable to a parameter in the query.
string
$param
required - Parameter key to replacebyref mixed
$var
required - Variable to use$this
public function bind($param, & $var)
{
// Bind a value to a variable
$this->_parameters[$param] =& $var;
return $this;
}
Enables the query to be cached for a specified amount of time.
integer
$lifetime
= NULL - Number of seconds to cache, 0 deletes it from the cacheboolean
$force
= bool FALSE - Whether or not to execute the query during a cache hit$this
public function cached($lifetime = NULL, $force = FALSE)
{
if ($lifetime === NULL)
{
// Use the global setting
$lifetime = Kohana::$cache_life;
}
$this->_force_execute = $force;
$this->_lifetime = $lifetime;
return $this;
}
Execute the current query on the given database.
mixed
$db
= NULL - Database instance or name of instancestring
$as_object
= NULL - Result object classname, TRUE for stdClass or FALSE for arrayarray
$object_params
= NULL - Result object constructor argumentsobject
- Database_Result for SELECT queriesmixed
- The insert id for INSERT queriesinteger
- Number of affected rows for all other queriespublic function execute($db = NULL, $as_object = NULL, $object_params = NULL)
{
if ( ! is_object($db))
{
// Get the database instance
$db = Database::instance($db);
}
if ($as_object === NULL)
{
$as_object = $this->_as_object;
}
if ($object_params === NULL)
{
$object_params = $this->_object_params;
}
// Compile the SQL query
$sql = $this->compile($db);
if ($this->_lifetime !== NULL AND $this->_type === Database::SELECT)
{
// Set the cache key based on the database instance name and SQL
$cache_key = 'Database::query("'.$db.'", "'.$sql.'")';
// Read the cache first to delete a possible hit with lifetime <= 0
if (($result = Kohana::cache($cache_key, NULL, $this->_lifetime)) !== NULL
AND ! $this->_force_execute)
{
// Return a cached result
return new Database_Result_Cached($result, $sql, $as_object, $object_params);
}
}
// Execute the query
$result = $db->query($this->_type, $sql, $as_object, $object_params);
if (isset($cache_key) AND $this->_lifetime > 0)
{
// Cache the result array
Kohana::cache($cache_key, $result->as_array(), $this->_lifetime);
}
return $result;
}
Set the value of a parameter in the query.
string
$param
required - Parameter key to replacemixed
$value
required - Value to use$this
public function param($param, $value)
{
// Add or overload a new parameter
$this->_parameters[$param] = $value;
return $this;
}
Add multiple parameters to the query.
array
$params
required - List of parameters$this
public function parameters(array $params)
{
// Merge the new parameters in
$this->_parameters = $params + $this->_parameters;
return $this;
}
Get the type of the query.
integer
public function type()
{
return $this->_type;
}
Compiles an array of conditions into an SQL partial. Used for WHERE and HAVING.
object
$db
required - Database instancearray
$conditions
required - Condition statementsstring
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)) === FALSE)
{
// Quote the value, it is not a parameter
$min = $db->quote($min);
}
if ((is_string($max) AND array_key_exists($max, $this->_parameters)) === FALSE)
{
// Quote the value, it is not a parameter
$max = $db->quote($max);
}
// Quote the min and max value
$value = $min.' AND '.$max;
}
elseif ((is_string($value) AND array_key_exists($value, $this->_parameters)) === FALSE)
{
// Quote the value, it is not a parameter
$value = $db->quote($value);
}
if ($column)
{
if (is_array($column))
{
// Use the column name
$column = $db->quote_identifier(reset($column));
}
else
{
// Apply proper quoting to the column
$column = $db->quote_column($column);
}
}
// Append the statement to the query
$sql .= trim($column.' '.$op.' '.$value);
}
$last_condition = $condition;
}
}
return $sql;
}
Compiles an array of GROUP BY columns into an SQL partial.
object
$db
required - Database instancearray
$columns
required - $columnsstring
protected function _compile_group_by(Database $db, array $columns)
{
$group = array();
foreach ($columns as $column)
{
if (is_array($column))
{
// Use the column alias
$column = $db->quote_identifier(end($column));
}
else
{
// Apply proper quoting to the column
$column = $db->quote_column($column);
}
$group[] = $column;
}
return 'GROUP BY '.implode(', ', $group);
}
Compiles an array of JOIN statements into an SQL partial.
object
$db
required - Database instancearray
$joins
required - Join statementsstring
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);
}
Compiles an array of ORDER BY statements into an SQL partial.
object
$db
required - Database instancearray
$columns
required - Sorting columnsstring
protected function _compile_order_by(Database $db, array $columns)
{
$sort = array();
foreach ($columns as $group)
{
list ($column, $direction) = $group;
if (is_array($column))
{
// Use the column alias
$column = $db->quote_identifier(end($column));
}
else
{
// Apply proper quoting to the column
$column = $db->quote_column($column);
}
if ($direction)
{
// Make the direction uppercase
$direction = ' '.strtoupper($direction);
}
$sort[] = $column.$direction;
}
return 'ORDER BY '.implode(', ', $sort);
}
Compiles an array of set values into an SQL partial. Used for UPDATE.
object
$db
required - Database instancearray
$values
required - Updated valuesstring
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_column($column);
if ((is_string($value) AND array_key_exists($value, $this->_parameters)) === FALSE)
{
// Quote the value, it is not a parameter
$value = $db->quote($value);
}
$set[$column] = $column.' = '.$value;
}
return implode(', ', $set);
}