1
0
mirror of synced 2024-12-14 23:26:04 +03:00
This commit is contained in:
zYne 2007-01-14 23:02:50 +00:00
parent b441465ef7
commit 029b5d4043

View File

@ -18,10 +18,10 @@
* and is licensed under the LGPL. For more information, see * and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>. * <http://www.phpdoctrine.com>.
*/ */
Doctrine::autoload('Doctrine_Connection_Module');
/** /**
* Doctrine_Cache_Query_Sqlite * Doctrine_Cache_Query_Sqlite
* *
*
* @package Doctrine * @package Doctrine
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping * @category Object Relational Mapping
@ -30,45 +30,40 @@
* @version $Revision$ * @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi> * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/ */
class Doctrine_Cache_Query_Sqlite implements Countable class Doctrine_Cache_Query_Sqlite extends Doctrine_Connection_Module implements Countable
{ {
/** /**
* doctrine cache * doctrine cache
*/ */
const CACHE_TABLE = 'doctrine_query_cache'; const CACHE_TABLE = 'doctrine_query_cache';
/**
* @var Doctrine_Session $session the table object this cache container belongs to
*/
private $table;
/**
* @var PDO $dbh database handler
*/
private $dbh;
/** /**
* constructor * constructor
* *
* @param Doctrine_Connection|null $connection * @param Doctrine_Connection|null $conn
*/ */
public function __construct($connection = null) public function __construct($conn = null)
{ {
if ( ! ($connection instanceof Doctrine_Connection)) { parent::__construct($conn);
$connection = Doctrine_Manager::getInstance()->getCurrentConnection();
}
$this->session = $connection;
$dir = 'cache'; $dir = 'cache';
$this->path = $dir.DIRECTORY_SEPARATOR; $this->path = $dir . DIRECTORY_SEPARATOR;
$this->dbh = new PDO("sqlite::memory:");
try { try {
if ($this->session->getAttribute(Doctrine::ATTR_CREATE_TABLES) === true) { if ($this->session->getAttribute(Doctrine::ATTR_CREATE_TABLES) === true) {
$columns = array(); $columns = array();
$columns['query_md5'] = array('string', 32, 'notnull');
$columns['query_result'] = array('array', 100000, 'notnull');
$columns['expires'] = array('integer', 11, 'notnull');
$dataDict = new Doctrine_DataDict($this->dbh); $columns['query_md5'] = array('type' => 'string',
$dataDict->createTable(self::CACHE_TABLE, $columns); 'length' => 32,
'notnull' => true);
$columns['query_result'] = array('type' => 'array',
'length' => 100000,
'notnull' => true);
$columns['expires'] = array('type' => 'integer',
'length' => 11,
'notnull' => true);
$this->conn->createTable(self::CACHE_TABLE, $columns);
} }
} catch(PDOException $e) { } catch(PDOException $e) {
@ -85,10 +80,10 @@ class Doctrine_Cache_Query_Sqlite implements Countable
*/ */
public function store($query, array $result, $lifespan) public function store($query, array $result, $lifespan)
{ {
$sql = "INSERT INTO ".self::CACHE_TABLE." (query_md5, query_result, expires) VALUES (?,?,?)"; $sql = 'INSERT INTO ' . self::CACHE_TABLE . ' (query_md5, query_result, expires) VALUES (?,?,?)';
$stmt = $this->dbh->prepare($sql);
$params = array(md5($query), serialize($result), (time() + $lifespan)); $params = array(md5($query), serialize($result), (time() + $lifespan));
$stmt->execute($params);
$this->conn->execute($sql, $params);
} }
/** /**
* fetch * fetch
@ -98,11 +93,9 @@ class Doctrine_Cache_Query_Sqlite implements Countable
*/ */
public function fetch($md5) public function fetch($md5)
{ {
$sql = "SELECT query_result, expires FROM ".self::CACHE_TABLE." WHERE query_md5 = ?"; $sql = 'SELECT query_result, expires FROM ' . self::CACHE_TABLE . ' WHERE query_md5 = ?';
$stmt = $this->dbh->prepare($sql);
$params = array($md5); $result = $this->conn->fetchAssoc($sql, array($md5));
$stmt->execute($params);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return unserialize($result['query_result']); return unserialize($result['query_result']);
} }
/** /**
@ -113,9 +106,8 @@ class Doctrine_Cache_Query_Sqlite implements Countable
*/ */
public function deleteAll() public function deleteAll()
{ {
$sql = "DELETE FROM ".self::CACHE_TABLE; $sql = 'DELETE FROM '.self::CACHE_TABLE;
$stmt = $this->dbh->exec($sql); return $this->conn->exec($sql);
return $stmt->rowCount();
} }
/** /**
* deleteExpired * deleteExpired
@ -125,8 +117,9 @@ class Doctrine_Cache_Query_Sqlite implements Countable
*/ */
public function deleteExpired() public function deleteExpired()
{ {
$sql = "DELETE FROM ".self::CACHE_TABLE." WHERE expired < ?"; $sql = 'DELETE FROM ' . self::CACHE_TABLE . ' WHERE expired < ?';
$stmt = $this->dbh->prepare($sql); $stmt = $this->dbh->prepare($sql);
$stmt->execute(array(time())); $stmt->execute(array(time()));
} }
/** /**
@ -135,15 +128,13 @@ class Doctrine_Cache_Query_Sqlite implements Countable
* query was succesfully deleted * query was succesfully deleted
* *
* @param string $md5 * @param string $md5
* @return boolean * @return boolean whether or not the row was successfully deleted
*/ */
public function delete($md5) public function delete($md5)
{ {
$sql = "DELETE FROM ".self::CACHE_TABLE." WHERE query_md5 = ?"; $sql = 'DELETE FROM ' . self::CACHE_TABLE . ' WHERE query_md5 = ?';
$stmt = $this->dbh->prepare($sql);
$params = array($md5); return (bool) $this->conn->exec($sql, array($md5));
$stmt->execute($params);
return $stmt->rowCount();
} }
/** /**
* count * count
@ -152,7 +143,7 @@ class Doctrine_Cache_Query_Sqlite implements Countable
*/ */
public function count() public function count()
{ {
$stmt = $this->dbh->query("SELECT COUNT(*) FROM ".self::CACHE_TABLE); $stmt = $this->dbh->query('SELECT COUNT(*) FROM ' . self::CACHE_TABLE);
$data = $stmt->fetch(PDO::FETCH_NUM); $data = $stmt->fetch(PDO::FETCH_NUM);
// table has three columns so we have to divide the count by two // table has three columns so we have to divide the count by two