Created
November 13, 2013 21:43
-
-
Save skowron-line/7456932 to your computer and use it in GitHub Desktop.
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 { | |
public static $_cache_driver = 'file'; | |
public static $_log_query = false; | |
private static $_transaction = false; | |
/** | |
* | |
* @param type $table | |
* @param type $values | |
* @return type | |
*/ | |
public static function _update($table, $values) | |
{ | |
return parent::update($table)->set($values); | |
} | |
public static function start_transaction() | |
{ | |
$exec = new Database_Query(null, 'START TRANSACTION'); | |
$exec->execute(); | |
self::$_transaction = true; | |
} | |
public static function commit() | |
{ | |
$exec = new Database_Query(null, 'COMMIT'); | |
$exec->execute(); | |
self::$_transaction = false; | |
} | |
public static function rollback() | |
{ | |
$exec = new Database_Query(null, 'ROLLBACK'); | |
$exec->execute(); | |
self::$_transaction = false; | |
} | |
/** | |
* | |
* @param type $table | |
* @param type $values | |
* @return type | |
* TODO add before insert action | |
*/ | |
public static function _insert($table, $values) | |
{ | |
return parent::insert($table, array_keys($values))->values(array_values($values)); | |
} | |
public static function _query($sql, $lifetime = null) | |
{ | |
$type = ''; | |
if(self::$_log_query == true) | |
{ | |
kohana::$log->add(Log::INFO, $sql); | |
} | |
switch(substr(strtolower($sql), 0, 6)) | |
{ | |
case 'select': | |
if($lifetime != null) | |
{ | |
$cache = Cache::instance(self::$_cache_driver); | |
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; | |
} | |
try { | |
return parent::query($type, $sql)->execute(); | |
} | |
catch(Database_Exception $e) | |
{ | |
if(self::$_transaction == true) | |
{ | |
self::rollback(); | |
} | |
} | |
} | |
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