Created
April 22, 2012 06:49
-
-
Save skowron-line/2462105 to your computer and use it in GitHub Desktop.
Extension for kohana 3.2 DB class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php defined('SYSPATH') or die('No direct access allowed.'); | |
/** | |
* | |
* @author skowron-line | |
*/ | |
class DB extends Kohana_DB { | |
/** | |
* | |
* @var string cache driver | |
*/ | |
public static $_cache_driver = 'file'; | |
/** | |
* | |
* @param string $table | |
* @param array $values | |
* @return object | |
*/ | |
public static function _update($table, $values) | |
{ | |
return parent::update($table)->set($values); | |
} | |
/** | |
* | |
* @param string $table | |
* @param array $values | |
* @return object | |
*/ | |
public static function _insert($table, $values) | |
{ | |
return parent::insert($table, array_keys($values))->values(array_values($values)); | |
} | |
/** | |
* | |
* @param object query to execute | |
* @param mixed time to cache | |
* @return object | |
*/ | |
public static function _query($sql, $lifetime = null) | |
{ | |
$type = ''; | |
$cache = Cache::instance(self::$_cache_driver); | |
switch(substr(strtolower($sql), 0, 6)) | |
{ | |
/** | |
* Dirty way | |
*/ | |
case '(selec': | |
case 'select': | |
if($lifetime != null) | |
{ | |
if($result = $cache->get($sql)) | |
{ | |
return $result; | |
} | |
$result = parent::query(Database::SELECT, $sql)->execute(); | |
$cache->set($sql, $result, $lifetime); | |
return $result; | |
} | |
return parent::query(Database::SELECT, $sql)->execute(); | |
break; | |
case 'update': | |
$type = Database::UPDATE; | |
break; | |
case 'delete': | |
$type = Database::DELETE; | |
break; | |
case 'insert': | |
list($id) = parent::query(Database::INSERT, $sql)->execute(); | |
return $id; | |
break; | |
} | |
return parent::query($type, $sql)->execute(); | |
} | |
/** | |
* | |
* @return boolean | |
*/ | |
public static function clear_cache() | |
{ | |
return Cache::instance(self::$_cache_driver)->delete_all(); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment