Redis implementation, #16
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@149 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
@ -151,6 +151,9 @@ abstract class DbStatement
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function fetchPairs()
|
||||
{
|
||||
$data = array();
|
||||
@ -179,4 +182,4 @@ abstract class DbStatement
|
||||
* @return bool
|
||||
*/
|
||||
abstract protected function driverExecute($sql);
|
||||
}
|
||||
}
|
||||
|
175
model/Model.php
175
model/Model.php
@ -12,7 +12,7 @@
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
abstract class Model implements iCacheable
|
||||
abstract class Model
|
||||
{
|
||||
|
||||
/**
|
||||
@ -45,13 +45,12 @@ abstract class Model implements iCacheable
|
||||
|
||||
protected $table;
|
||||
|
||||
protected $connection = 'default';
|
||||
protected $key = 'id';
|
||||
|
||||
|
||||
protected $key = 'id';
|
||||
|
||||
public function __construct()
|
||||
public function __construct($connection = 'default')
|
||||
{
|
||||
$this->db = Db::connect($this->connection);
|
||||
$this->db = Db::connect($connection);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -59,7 +58,7 @@ abstract class Model implements iCacheable
|
||||
*/
|
||||
public function getInsertId()
|
||||
{
|
||||
return $this->db->getInsertId($this->table(false), $this->key);
|
||||
return $this->db->getInsertId($this->table(), $this->key);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -86,7 +85,7 @@ abstract class Model implements iCacheable
|
||||
*/
|
||||
public function get($id)
|
||||
{
|
||||
$sql = 'SELECT * FROM ' . $this->table() . ' WHERE ' . $this->identify($this->key) . '=?';
|
||||
$sql = 'SELECT * FROM :table WHERE :pk=?';
|
||||
return $this->fetch($sql, $id);
|
||||
}
|
||||
|
||||
@ -97,7 +96,7 @@ abstract class Model implements iCacheable
|
||||
*/
|
||||
public function insert($data, $on_duplicate = array())
|
||||
{
|
||||
$affected = $this->db->insert($this->table(false), $data, $on_duplicate);
|
||||
$affected = $this->db->insert($this->table(), $data, $on_duplicate);
|
||||
return ($this->getInsertId()) ? $this->getInsertId() : $affected;
|
||||
}
|
||||
|
||||
@ -111,33 +110,28 @@ abstract class Model implements iCacheable
|
||||
if (is_int($where)) {
|
||||
$where = $this->identify($this->key) . '=' . (int) $where;
|
||||
}
|
||||
return $this->db->update($this->table(false), $data, $where);
|
||||
return $this->db->update($this->table(), $data, $where);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|array $where Int or array ids
|
||||
* @param int $id Int id
|
||||
* @return int Number of affected rows
|
||||
*/
|
||||
public function delete($where)
|
||||
public function delete($id)
|
||||
{
|
||||
if (is_array($where)) {
|
||||
$where = $this->identify($this->key) . ' IN (' . $this->quote($where) . ')';
|
||||
} else {
|
||||
$where = $this->identify($this->key) . '=' . (int) $where;
|
||||
}
|
||||
return $this->db->delete($this->table(false), $where);
|
||||
$where = $this->identify($this->key) . '=' . (int) $id;
|
||||
return $this->db->delete($this->table(), $where);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param bool $autoindent
|
||||
* @return string
|
||||
*/
|
||||
protected function table($autoindent = true)
|
||||
protected function table()
|
||||
{
|
||||
if (!$this->table) {
|
||||
$this->table = substr(strtolower(get_class($this)), 0, -5/*strlen('Model')*/);
|
||||
}
|
||||
return $autoindent ? $this->identify($this->table) : $this->table;
|
||||
return $this->table;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -180,6 +174,75 @@ abstract class Model implements iCacheable
|
||||
}
|
||||
|
||||
/**
|
||||
* This method appends to params table and primary key.
|
||||
* So they can be accessed thru `:table` and `:pk` placeholders.
|
||||
*
|
||||
* @return DbStatement
|
||||
*/
|
||||
protected function query($sql, $params = array())
|
||||
{
|
||||
if (!is_array($params)) {
|
||||
$params = array($params);
|
||||
}
|
||||
$params = array(
|
||||
'table' => new DbExpr($this->identify($this->table())),
|
||||
'pk' => new DbExpr($this->identify($this->key)),
|
||||
) + $params;
|
||||
return $this->db->query($sql, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array $params
|
||||
* @param string $field
|
||||
* @param CacheKey $cache_key
|
||||
*/
|
||||
protected function fetchField($sql, $params = array(), $field, $cache_key = null)
|
||||
{
|
||||
if (!$cache_key || !$result = $cache_key->get()) {
|
||||
$result = $this->query($sql, $params)->fetchField($field);
|
||||
if ($cache_key) {
|
||||
$cache_key->set($result);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array $params
|
||||
* @param CacheKey $cache_key
|
||||
*/
|
||||
protected function fetch($sql, $params = array(), $cache_key = null)
|
||||
{
|
||||
if (!$cache_key || !$result = $cache_key->get()) {
|
||||
$result = $this->query($sql, $params)->fetch();
|
||||
if ($cache_key) {
|
||||
$cache_key->set($result);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array $params
|
||||
* @param CacheKey $cache_key
|
||||
*/
|
||||
protected function fetchAll($sql, $params = array(), $cache_key = null)
|
||||
{
|
||||
if (!$cache_key || !$result = $cache_key->get()) {
|
||||
$result = $this->query($sql, $params)->fetchAll();
|
||||
if ($cache_key) {
|
||||
$cache_key->set($result);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/* Cache workaround */
|
||||
|
||||
/**
|
||||
* @return Cache
|
||||
*/
|
||||
public function getCache()
|
||||
@ -191,29 +254,18 @@ abstract class Model implements iCacheable
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return int
|
||||
*/
|
||||
public function getKeyExpire($key)
|
||||
{
|
||||
return (isset($this->cache_keys[$key])) ? ($this->cache_keys[$key] * 60) : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $params
|
||||
* @return CacheKey
|
||||
*/
|
||||
protected function cacheKey($name, $params = array())
|
||||
{
|
||||
if (substr(strtolower($name), -3, 3) == 'set') {
|
||||
return new CacheKeySet($name, $params, $this);
|
||||
}
|
||||
return new CacheKey($name, $params, $this);
|
||||
$expire = (isset($this->cache_keys[$name])) ? ($this->cache_keys[$name] * 60) : 0;
|
||||
return new CacheKey($this->getCache(), $name, $params, $expire);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CacheKey | CacheKeySet $cache
|
||||
* @param CacheKey $cache
|
||||
*/
|
||||
protected function addCleanCache($cache)
|
||||
{
|
||||
@ -228,53 +280,4 @@ abstract class Model implements iCacheable
|
||||
}
|
||||
$this->caches_clean = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array $params
|
||||
* @param string $field
|
||||
* @param CacheKey | CacheKeySet $cache_key
|
||||
*/
|
||||
protected function fetchField($sql, $params = array(), $field, $cache_key = null)
|
||||
{
|
||||
if (!$cache_key || !$result = $cache_key->get()) {
|
||||
$result = $this->db->query($sql, $params)->fetchField($field);
|
||||
if ($cache_key) {
|
||||
$cache_key->set($result);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array $params
|
||||
* @param CacheKey | CacheKeySet $cache_key
|
||||
*/
|
||||
protected function fetch($sql, $params = array(), $cache_key = null)
|
||||
{
|
||||
if (!$cache_key || !$result = $cache_key->get()) {
|
||||
$result = $this->db->query($sql, $params)->fetch();
|
||||
if ($cache_key) {
|
||||
$cache_key->set($result);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array $params
|
||||
* @param CacheKey | CacheKeySet $cache_key
|
||||
*/
|
||||
protected function fetchAll($sql, $params = array(), $cache_key = null)
|
||||
{
|
||||
if (!$cache_key || !$result = $cache_key->get()) {
|
||||
$result = $this->db->query($sql, $params)->fetchAll();
|
||||
if ($cache_key) {
|
||||
$cache_key->set($result);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user