- <?php defined('SYSPATH') or die('No direct script access.');
- class Musterdb_Database_Query extends Kohana_Database_Query
- {
- protected $_db;
- protected $_config;
- // for execute of prepared statement
- /**
- * Execute the current query on the given database.
- *
- * @param mixed Database instance or name of instance
- * @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 config($config = NULL)
- {
- $this->_config = $config;
- return $this;
- }
- public function execute($db = 'default')
- {
- $config = $this->_config; // picks up optional $config parameter
- if ( ! is_object($db))
- {
- // Get the database instance
- $db = Database::instance($db);
- }
- $db->is_query(); // notify to use quote
- // 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);
- }
- }
- // Execute the query
- $result = $db->query($this->_type, $sql, $this->_as_object,$config); // adds optional config parameter
- if (isset($cache_key))
- {
- // Cache the result array
- Kohana::cache($cache_key, $result->as_array(), $this->_lifetime);
- }
- return $result;
- }
- # new function
- public function prepare($db = 'default',$bindings = NULL,$binding_datatypes = array())
- {
- $config = $this->_config;
- if ( ! is_object($db))
- {
- // Get the database instance
- $db = Database::instance($db);
- }
- $this->_db = $db;
- $db->is_prepare(); // notify not to use quote
- // Compile the SQL query
- $sql = $this->compile($db);
- // Execute the query
- $result = $db->prepare($this->_type, $sql, $this->_as_object,$config);
- if (!empty($bindings))
- {
- if ($this->_type === Database::SELECT)
- $statement = $result->statement();
- else
- $statement = $result;
- foreach ($bindings as $param => &$value)
- {
- if (!isset($binding_datatype[$param]))
- $statement->bindParam($param,&$value);
- else
- $statement->bindParam($param,&$value,$binding_datatypes[$param]);
- }
- }
- return $result;
- }
- }