Memcache, refactoring, View helpers, #16
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@124 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
@ -91,9 +91,10 @@ abstract class DbDriver
|
||||
/**
|
||||
* @param string $table
|
||||
* @param mixed $bind
|
||||
* @param mixed $on_duplicate
|
||||
* @return int Affected rows count
|
||||
*/
|
||||
public function insert($table, $bind)
|
||||
public function insert($table, $bind, $on_duplicate = array())
|
||||
{
|
||||
$columns = array();
|
||||
foreach ($bind as $col => $val) {
|
||||
|
@ -34,31 +34,6 @@ abstract class DbStatement
|
||||
$this->sql = $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $style
|
||||
* @return array
|
||||
*/
|
||||
public function fetchAll($style = Db::FETCH_OBJ)
|
||||
{
|
||||
$data = array();
|
||||
while ($row = $this->fetch($style)) {
|
||||
$data[] = $row;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
*/
|
||||
public function fetchField($field)
|
||||
{
|
||||
$row = $this->fetch(Db::FETCH_ASSOC);
|
||||
if (isset($row[$field])) {
|
||||
return $row[$field];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function bindParam($param, &$value)
|
||||
{
|
||||
if ($this->map === null) {
|
||||
@ -138,6 +113,31 @@ abstract class DbStatement
|
||||
{
|
||||
$this->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $style
|
||||
* @return array
|
||||
*/
|
||||
public function fetchAll($style = Db::FETCH_OBJ)
|
||||
{
|
||||
$data = array();
|
||||
while ($row = $this->fetch($style)) {
|
||||
$data[] = $row;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
*/
|
||||
public function fetchField($field)
|
||||
{
|
||||
$row = $this->fetch(Db::FETCH_ASSOC);
|
||||
if (isset($row[$field])) {
|
||||
return $row[$field];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Abstract methods */
|
||||
|
||||
|
135
model/Model.php
135
model/Model.php
@ -12,7 +12,7 @@
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
abstract class Model
|
||||
abstract class Model implements iCacheable
|
||||
{
|
||||
|
||||
/**
|
||||
@ -28,6 +28,20 @@ abstract class Model
|
||||
* @var Cache
|
||||
*/
|
||||
protected $cache;
|
||||
|
||||
/**
|
||||
* Custom expiration time for keys
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
protected $cache_keys = array();
|
||||
|
||||
/**
|
||||
* Caches to clean.
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
protected $caches_clean = array();
|
||||
|
||||
protected $table;
|
||||
|
||||
@ -35,7 +49,6 @@ abstract class Model
|
||||
|
||||
protected $key = 'id';
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->db = Db::connect($this->connection);
|
||||
@ -73,37 +86,21 @@ abstract class Model
|
||||
*/
|
||||
public function get($id)
|
||||
{
|
||||
$sql = 'SELECT * FROM ' . $this->table() . ' WHERE ' . $this->identify($this->key) . '=' . (int) $id;
|
||||
return $this->db->query($sql)->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return int Affect row or id of inserted field.
|
||||
*/
|
||||
public function save($data)
|
||||
{
|
||||
$key = isset($data[$this->key]) ? $data[$this->key] : false;
|
||||
$result = false;
|
||||
if ($key) {
|
||||
unset($data[$this->key]);
|
||||
$result = $this->update($data, $this->identify($this->key) . '=' . (int) $id);
|
||||
} else {
|
||||
$result = $this->insert($data);
|
||||
}
|
||||
return $result;
|
||||
$sql = 'SELECT * FROM ' . $this->table() . ' WHERE ' . $this->identify($this->key) . '=?';
|
||||
return $this->fetch($sql, $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param array $on_duplicate
|
||||
* @return int Id of inserted row
|
||||
*/
|
||||
public function insert($data)
|
||||
public function insert($data, $on_duplicate = array())
|
||||
{
|
||||
if (!$this->db->insert($this->table(false), $data)) {
|
||||
if (!$res = $this->db->insert($this->table(false), $data, $on_duplicate)) {
|
||||
return false;
|
||||
}
|
||||
return $this->getInsertId();
|
||||
return ($on_duplicate) ? $res : $this->getInsertId();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -146,11 +143,99 @@ abstract class Model
|
||||
/**
|
||||
* @return Cache
|
||||
*/
|
||||
protected function cache()
|
||||
public function getCache()
|
||||
{
|
||||
if (!$this->cache) {
|
||||
$this->cache = Cacher::get(Config::get(__CLASS__, 'MemcacheCache'));
|
||||
}
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CacheKey | CacheKeySet $cache
|
||||
*/
|
||||
protected function addCleanCache($cache)
|
||||
{
|
||||
$this->caches_clean[] = $cache;
|
||||
}
|
||||
|
||||
protected function cleanCaches()
|
||||
{
|
||||
// cleaning caches
|
||||
foreach ($this->caches_clean as $cache) {
|
||||
$cache->del();
|
||||
}
|
||||
$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;
|
||||
}
|
||||
}
|
@ -15,6 +15,28 @@
|
||||
class MySQLiDriver extends DbDriver
|
||||
{
|
||||
|
||||
public function insert($table, $bind, $on_duplicate = array())
|
||||
{
|
||||
$columns = array();
|
||||
foreach ($bind as $col => $val) {
|
||||
$columns[] = $this->quoteIdentifier($col);
|
||||
}
|
||||
$values = array_values($bind);
|
||||
|
||||
if ($on_duplicate) {
|
||||
$update = array();
|
||||
foreach ($on_duplicate as $col => $val) {
|
||||
$update[] = $this->quoteIdentifier($col) . '=' . $this->quote($val);
|
||||
}
|
||||
$on_duplicate = ' ON DUPLICATE KEY UPDATE ' . implode(', ', $update);
|
||||
}
|
||||
|
||||
$sql = 'INSERT INTO ' . $this->quoteIdentifier($table)
|
||||
. ' (' . implode(', ', $columns) . ') VALUES (' . $this->quote($values) . ')'
|
||||
. (($on_duplicate) ? $on_duplicate : '');
|
||||
return $this->query($sql)->affectedRows();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $sql
|
||||
* @return DbStatement
|
||||
@ -72,7 +94,6 @@ class MySQLiDriver extends DbDriver
|
||||
}
|
||||
|
||||
if (is_bool($value)) {
|
||||
var_dump($value);
|
||||
return (int) $value;
|
||||
}
|
||||
|
||||
|
@ -16,9 +16,15 @@
|
||||
class MySQLiStatement extends DbStatement
|
||||
{
|
||||
|
||||
/**
|
||||
* Fetches single row
|
||||
*
|
||||
* @param mixed $style
|
||||
* @return mixed
|
||||
*/
|
||||
public function fetch($style = Db::FETCH_OBJ)
|
||||
{
|
||||
if (! $this->result) {
|
||||
if (!$this->result) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -77,7 +83,13 @@ class MySQLiStatement extends DbStatement
|
||||
* @var MySQLi
|
||||
*/
|
||||
$mysqli = $this->driver->getConnection();
|
||||
$result = $mysqli->query($sql);
|
||||
if (DEBUG) {
|
||||
$profiler = Profiler::getInstance()->profilerQuery($sql);
|
||||
$result = $mysqli->query($sql);
|
||||
$profiler->end();
|
||||
} else {
|
||||
$result = $mysqli->query($sql);
|
||||
}
|
||||
if ($result === false) {
|
||||
throw new Exception($mysqli->error, $mysqli->errno);
|
||||
}
|
||||
|
Reference in New Issue
Block a user