rebuild Model package for NoSQL drivers
This commit is contained in:
197
model/Model.php
197
model/Model.php
@ -62,41 +62,13 @@ abstract class Model
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $ident
|
||||
* @return string Quoted identifier.
|
||||
*/
|
||||
public function identify($ident)
|
||||
{
|
||||
return $this->db->quoteIdentifier($ident);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return string Quoted value.
|
||||
*/
|
||||
public function quote($value)
|
||||
{
|
||||
return $this->db->quote($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return object
|
||||
*/
|
||||
public function get($id)
|
||||
{
|
||||
$sql = 'SELECT * FROM :table WHERE :pk=?';
|
||||
return $this->fetch($sql, $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param array $on_duplicate
|
||||
* @return int Id of inserted row
|
||||
*/
|
||||
public function insert($data, $on_duplicate = array())
|
||||
public function insert($data)
|
||||
{
|
||||
$affected = $this->db->insert($this->table(), $data, $on_duplicate);
|
||||
$affected = $this->db->insert($this->table(), $data);
|
||||
return ($this->getInsertId()) ? $this->getInsertId() : $affected;
|
||||
}
|
||||
|
||||
@ -107,21 +79,8 @@ abstract class Model
|
||||
*/
|
||||
public function update($data, $where)
|
||||
{
|
||||
if (is_int($where) || $where === (string) (int) $where) {
|
||||
$where = $this->identify($this->key) . '=' . (int) $where;
|
||||
}
|
||||
return $this->db->update($this->table(), $data, $where);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id Int id
|
||||
* @return int Number of affected rows
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
$where = $this->identify($this->key) . '=' . (int) $id;
|
||||
return $this->db->delete($this->table(), $where);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
@ -133,112 +92,7 @@ abstract class Model
|
||||
}
|
||||
return $this->table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates order sql string
|
||||
*
|
||||
* @param array $params
|
||||
* @param array $sortable
|
||||
* @return string
|
||||
*/
|
||||
protected function order($params, $sortable = array('id'))
|
||||
{
|
||||
$sql = '';
|
||||
if (isset($params['sort'])) {
|
||||
$order = (isset($params['order']) && $params['order'] == 'desc') ? 'DESC' : 'ASC';
|
||||
if (in_array($params['sort'], $sortable)) {
|
||||
$sql = ' ORDER BY ' . $this->identify($params['sort']) . ' ' . $order;
|
||||
}
|
||||
}
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches using like
|
||||
*
|
||||
* @param array $params
|
||||
* @param array $searchable
|
||||
* @param string $table_prefix
|
||||
* @return string
|
||||
*/
|
||||
protected function search($params, $searchable = array('id'), $table_prefix = '')
|
||||
{
|
||||
$sql = '';
|
||||
if (isset($params['q']) && isset($params['qt']) && in_array($params['qt'], $searchable)) {
|
||||
if ($table_prefix) {
|
||||
$sql = $table_prefix . '.';
|
||||
}
|
||||
$sql .= $this->identify($params['qt']) . ' LIKE ' . $this->quote('%' . $params['q'] . '%');
|
||||
}
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 */
|
||||
|
||||
@ -280,4 +134,51 @@ abstract class Model
|
||||
}
|
||||
$this->caches_clean = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract methods
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return object
|
||||
*/
|
||||
abstract public function get($id);
|
||||
|
||||
/**
|
||||
* @param int $id Int id
|
||||
* @return int Number of affected rows
|
||||
*/
|
||||
abstract public function delete($id);
|
||||
|
||||
/**
|
||||
* Creates order sql string
|
||||
*
|
||||
* @param array $params
|
||||
* @param array $sortable
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function order($params, $sortable = array('id'));
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array $params
|
||||
* @param string $field
|
||||
* @param CacheKey $cache_key
|
||||
*/
|
||||
abstract protected function fetchField($data, $params = array(), $field, $cache_key = null);
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array $params
|
||||
* @param CacheKey $cache_key
|
||||
*/
|
||||
abstract protected function fetch($data, $params = array(), $cache_key = null);
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array $params
|
||||
* @param CacheKey $cache_key
|
||||
*/
|
||||
abstract protected function fetchAll($data, $params = array(), $cache_key = null);
|
||||
}
|
Reference in New Issue
Block a user