123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <?php
- /**
- * Database query wrapper. See [Parameterized Statements](database/query/parameterized) for usage and examples.
- *
- * @package Kohana/Database
- * @category Query
- * @author Kohana Team
- * @copyright (c) Kohana Team
- * @license https://koseven.ga/LICENSE.md
- */
- class Kohana_Database_Query {
- // Query type
- protected $_type;
- // Execute the query during a cache hit
- protected $_force_execute = FALSE;
- // Cache lifetime
- protected $_lifetime = NULL;
- // SQL statement
- protected $_sql;
- // Quoted query parameters
- protected $_parameters = [];
- // Return results as associative arrays or objects
- protected $_as_object = FALSE;
- // Parameters for __construct when using object results
- protected $_object_params = [];
- /**
- * Creates a new SQL query of the specified type.
- *
- * @param integer $type query type: Database::SELECT, Database::INSERT, etc
- * @param string $sql query string
- * @return void
- */
- public function __construct($type, $sql)
- {
- $this->_type = $type;
- $this->_sql = $sql;
- }
- /**
- * Return the SQL query string.
- *
- * @return string
- */
- public function __toString()
- {
- try
- {
- // Return the SQL string
- return $this->compile(Database::instance());
- }
- catch (Exception $e)
- {
- return Kohana_Exception::text($e);
- }
- }
- /**
- * Get the type of the query.
- *
- * @return integer
- */
- public function type()
- {
- return $this->_type;
- }
- /**
- * Enables the query to be cached for a specified amount of time.
- *
- * @param integer $lifetime number of seconds to cache, 0 deletes it from the cache
- * @param boolean $force whether or not to execute the query during a cache hit
- * @return $this
- * @uses Kohana::$cache_life
- */
- 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;
- }
- /**
- * Returns results as associative arrays
- *
- * @return $this
- */
- public function as_assoc()
- {
- $this->_as_object = FALSE;
- $this->_object_params = [];
- return $this;
- }
- /**
- * Returns results as objects
- *
- * @param string $class classname or TRUE for stdClass
- * @param array $params
- * @return $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;
- }
- /**
- * Set the value of a parameter in the query.
- *
- * @param string $param parameter key to replace
- * @param mixed $value value to use
- * @return $this
- */
- public function param($param, $value)
- {
- // Add or overload a new parameter
- $this->_parameters[$param] = $value;
- return $this;
- }
- /**
- * Bind a variable to a parameter in the query.
- *
- * @param string $param parameter key to replace
- * @param mixed $var variable to use
- * @return $this
- */
- public function bind($param, & $var)
- {
- // Bind a value to a variable
- $this->_parameters[$param] =& $var;
- return $this;
- }
- /**
- * Add multiple parameters to the query.
- *
- * @param array $params list of parameters
- * @return $this
- */
- public function parameters(array $params)
- {
- // Merge the new parameters in
- $this->_parameters = $params + $this->_parameters;
- return $this;
- }
- /**
- * Compile the SQL query and return it. Replaces any parameters with their
- * given values.
- *
- * @param mixed $db Database instance or name of instance
- * @return string
- */
- public function compile($db = NULL)
- {
- if ( ! is_object($db))
- {
- // Get the database instance
- $db = Database::instance($db);
- }
- // Import the SQL locally
- $sql = $this->_sql;
- if ( ! empty($this->_parameters))
- {
- // Quote all of the values
- $values = array_map([$db, 'quote'], $this->_parameters);
- // Replace the values in the SQL
- $sql = strtr($sql, $values);
- }
- return $sql;
- }
- /**
- * Execute the current query on the given database.
- *
- * @param mixed $db Database instance or name of instance
- * @param string result object classname, TRUE for stdClass or FALSE for array
- * @param array result object constructor arguments
- * @return object Database_Result for SELECT queries
- * @return mixed the insert id for INSERT queries
- * @return integer number of affected rows for all other queries
- */
- public 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;
- }
- } // End Database_Query
|