Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 2.48 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. class Musterdb_Database_Query extends Kohana_Database_Query
  3. {      
  4.         protected $_db;
  5.         protected $_config;
  6.         // for execute of prepared statement   
  7.         /**
  8.          * Execute the current query on the given database.
  9.          *
  10.          * @param   mixed  Database instance or name of instance
  11.          * @return  object   Database_Result for SELECT queries
  12.          * @return  mixed    the insert id for INSERT queries
  13.          * @return  integer  number of affected rows for all other queries
  14.          */
  15.         public function config($config = NULL)
  16.         {
  17.                 $this->_config = $config;
  18.                 return $this;
  19.         }
  20.         public function execute($db = 'default')
  21.         {
  22.                 $config = $this->_config; // picks up optional $config parameter
  23.                 if ( ! is_object($db))
  24.                 {
  25.                         // Get the database instance
  26.                         $db = Database::instance($db);
  27.                 }
  28.                 $db->is_query(); // notify to use quote
  29.                 // Compile the SQL query
  30.                 $sql = $this->compile($db);
  31.  
  32.                 if ( ! empty($this->_lifetime) AND $this->_type === Database::SELECT)
  33.                 {
  34.                         // Set the cache key based on the database instance name and SQL
  35.                         $cache_key = 'Database::query("'.$db.'", "'.$sql.'")';
  36.  
  37.                         if ($result = Kohana::cache($cache_key, NULL, $this->_lifetime))
  38.                         {
  39.                                 // Return a cached result
  40.                                 return new Database_Result_Cached($result, $sql, $this->_as_object);
  41.                         }
  42.                 }
  43.  
  44.                 // Execute the query
  45.                 $result = $db->query($this->_type, $sql, $this->_as_object,$config); // adds optional config parameter
  46.  
  47.                 if (isset($cache_key))
  48.                 {
  49.                         // Cache the result array
  50.                         Kohana::cache($cache_key, $result->as_array(), $this->_lifetime);
  51.                 }
  52.  
  53.                 return $result;
  54.         }
  55.         # new function
  56.         public function prepare($db = 'default',$bindings = NULL,$binding_datatypes = array()) 
  57.         {              
  58.                 $config = $this->_config;
  59.                 if ( ! is_object($db))         
  60.                 {                      
  61.                         // Get the database instance                   
  62.                         $db = Database::instance($db);         
  63.                 }              
  64.                 $this->_db = $db;              
  65.                 $db->is_prepare(); // notify not to use quote
  66.                 // Compile the SQL query               
  67.                 $sql = $this->compile($db);
  68.                 // Execute the query
  69.                 $result = $db->prepare($this->_type, $sql, $this->_as_object,$config);
  70.                 if (!empty($bindings))
  71.                 {
  72.                         if ($this->_type === Database::SELECT)
  73.                                 $statement = $result->statement();
  74.                         else
  75.                                 $statement = $result;
  76.                         foreach ($bindings as $param => &$value)
  77.                         {
  78.                                 if (!isset($binding_datatype[$param]))
  79.                                         $statement->bindParam($param,&$value);
  80.                                 else
  81.                                         $statement->bindParam($param,&$value,$binding_datatypes[$param]);
  82.                         }
  83.                 }
  84.                
  85.                 return $result;
  86.         }
  87. }