diff options
author | Bharat Mediratta <bharat@menalto.com> | 2009-11-24 19:20:36 -0800 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2009-11-24 19:20:36 -0800 |
commit | 9b6663f87a7e679ffba691cf516191fc840cf978 (patch) | |
tree | 20cf9f3aaf93b4ba69d282dcf10d259db4a752de /system/libraries/Database_Query.php | |
parent | 82ee5f9d338017c69331b2907f37a468ced8c66e (diff) |
Update to Kohana r4684 which is now Kohana 2.4 and has substantial
changes.
Diffstat (limited to 'system/libraries/Database_Query.php')
-rw-r--r-- | system/libraries/Database_Query.php | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/system/libraries/Database_Query.php b/system/libraries/Database_Query.php new file mode 100644 index 00000000..d9399d66 --- /dev/null +++ b/system/libraries/Database_Query.php @@ -0,0 +1,95 @@ +<?php defined('SYSPATH') or die('No direct script access.'); +/** + * Database query wrapper. + * + * $Id: Database_Query.php 4679 2009-11-10 01:45:52Z isaiah $ + * + * @package Kohana + * @author Kohana Team + * @copyright (c) 2008-2009 Kohana Team + * @license http://kohanaphp.com/license + */ +class Database_Query_Core { + + protected $sql; + protected $params; + protected $ttl = FALSE; + + public function __construct($sql = NULL) + { + $this->sql = $sql; + } + + public function __toString() + { + // Return the SQL of this query + return $this->sql; + } + + public function sql($sql) + { + $this->sql = $sql; + + return $this; + } + + public function value($key, $value) + { + $this->params[$key] = $value; + + return $this; + } + + public function bind($key, & $value) + { + $this->params[$key] =& $value; + + return $this; + } + + public function execute($db = 'default') + { + if ( ! is_object($db)) + { + // Get the database instance + $db = Database::instance($db); + } + + // Import the SQL locally + $sql = $this->sql; + + if ( ! empty($this->params)) + { + // Quote all of the values + $params = array_map(array($db, 'quote'), $this->params); + + // Replace the values in the SQL + $sql = strtr($sql, $params); + } + + if ($this->ttl !== FALSE) + { + // Load the result from the cache + return $db->query_cache($sql, $this->ttl); + } + else + { + // Load the result (no caching) + return $db->query($sql); + } + } + + /** + * Set caching for the query + * + * @param mixed Time-to-live (FALSE to disable, NULL for Cache default, seconds otherwise) + * @return Database_Query + */ + public function cache($ttl = NULL) + { + $this->ttl = $ttl; + + return $this; + } + +} // End Database_Query
\ No newline at end of file |