Database_Query
Kohana_Database_Query

Class Contents

Class declared in MODPATH/database/classes/database/query.php on line 3.

Methods

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;
}

public type( )
Kohana_Database_Query

Source Code
public function type()
{
	return $this->_type;
}