Modules
Kohana_Database_Query
Database query wrapper. See Prepared Statements for usage and examples.
Class declared in MODPATH/database/classes/kohana/database/query.php on line 11.
Constants
- None
Properties
Properties
protected
$_as_objectprotected
$_lifetimeprotected
$_object_paramsprotected
$_parametersprotected
$_sqlprotected
$_type
Methods
public __construct( integer $type , string $sql ) (defined in Kohana_Database_Query)
Creates a new SQL query of the specified type.
Parameters
-
integer$type required - Query type: Database::SELECT, Database::INSERT, etc -
string$sql required - Query string
Return Values
void
Source Code
public function __construct($type, $sql)
{
$this->_type = $type;
$this->_sql = $sql;
}
final public __toString( ) (defined in Kohana_Database_Query)
Return the SQL query string.
Return Values
string
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( ) (defined in Kohana_Database_Query)
Returns results as associative arrays
Return Values
$this
Source Code
public function as_assoc()
{
$this->_as_object = FALSE;
$this->_object_params = array();
return $this;
}
public as_object( [ string $class = bool TRUE , $params = NULL ] ) (defined in Kohana_Database_Query)
Returns results as objects
Parameters
-
string$class = bool TRUE - Classname or TRUE for stdClass -
unknown$params = NULL
Return Values
$this
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( string $param , mixed & $var ) (defined in Kohana_Database_Query)
Bind a variable to a parameter in the query.
Parameters
-
string$param required - Parameter key to replace -
byref mixed$var required - Variable to use
Return Values
$this
Source Code
public function bind($param, & $var)
{
// Bind a value to a variable
$this->_parameters[$param] =& $var;
return $this;
}
public cached( [ integer $lifetime = NULL ] ) (defined in Kohana_Database_Query)
Enables the query to be cached for a specified amount of time.
Parameters
-
integer$lifetime = NULL - Number of seconds to cache or null for default
Tags
Return Values
$this
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( object $db ) (defined in Kohana_Database_Query)
Compile the SQL query and return it. Replaces any parameters with their given values.
Parameters
-
object$db required - Database instance
Return Values
string
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( [ mixed $db = NULL ] ) (defined in Kohana_Database_Query)
Execute the current query on the given database.
Parameters
-
mixed$db = NULL - Database instance or name of instance
Return Values
object- Database_Result for SELECT queriesmixed- The insert id for INSERT queriesinteger- Number of affected rows for all other queries
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( string $param , mixed $value ) (defined in Kohana_Database_Query)
Set the value of a parameter in the query.
Parameters
-
string$param required - Parameter key to replace -
mixed$value required - Value to use
Return Values
$this
Source Code
public function param($param, $value)
{
// Add or overload a new parameter
$this->_parameters[$param] = $value;
return $this;
}
public parameters( array $params ) (defined in Kohana_Database_Query)
Add multiple parameters to the query.
Parameters
-
array$params required - List of parameters
Return Values
$this
Source Code
public function parameters(array $params)
{
// Merge the new parameters in
$this->_parameters = $params + $this->_parameters;
return $this;
}
public type( ) (defined in Kohana_Database_Query)
Get the type of the query.
Return Values
integer
Source Code
public function type()
{
return $this->_type;
}