2016-04-12 19:31:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace b8;
|
2016-04-20 12:30:26 +02:00
|
|
|
|
2016-04-12 19:31:39 +02:00
|
|
|
use b8\Exception\HttpException;
|
2016-04-20 12:30:26 +02:00
|
|
|
use b8\Database;
|
2016-04-12 19:31:39 +02:00
|
|
|
|
|
|
|
abstract class Store
|
|
|
|
{
|
2016-04-20 12:30:26 +02:00
|
|
|
protected $modelName = null;
|
|
|
|
protected $tableName = null;
|
|
|
|
protected $primaryKey = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \b8\Model
|
|
|
|
*/
|
|
|
|
abstract public function getByPrimaryKey($key, $useConnection = 'read');
|
|
|
|
|
|
|
|
public function getWhere(
|
|
|
|
$where = [],
|
|
|
|
$limit = 25,
|
|
|
|
$offset = 0,
|
|
|
|
$joins = [],
|
|
|
|
$order = [],
|
|
|
|
$manualJoins = [],
|
|
|
|
$group = null,
|
|
|
|
$manualWheres = [],
|
|
|
|
$whereType = 'AND'
|
|
|
|
) {
|
2017-01-29 03:49:43 +01:00
|
|
|
$query = 'SELECT * FROM {{' . $this->tableName . '}}';
|
|
|
|
$countQuery = 'SELECT COUNT(*) AS {{count}} FROM {{' . $this->tableName . '}}';
|
2016-04-20 12:30:26 +02:00
|
|
|
|
|
|
|
$wheres = [];
|
|
|
|
$params = [];
|
|
|
|
foreach ($where as $key => $value) {
|
|
|
|
$key = $this->fieldCheck($key);
|
|
|
|
|
|
|
|
if (!is_array($value)) {
|
|
|
|
$params[] = $value;
|
|
|
|
$wheres[] = $key . ' = ?';
|
|
|
|
} else {
|
|
|
|
if (isset($value['operator'])) {
|
|
|
|
if (is_array($value['value'])) {
|
|
|
|
if ($value['operator'] == 'between') {
|
|
|
|
$params[] = $value['value'][0];
|
|
|
|
$params[] = $value['value'][1];
|
|
|
|
$wheres[] = $key . ' BETWEEN ? AND ?';
|
|
|
|
} elseif ($value['operator'] == 'IN') {
|
|
|
|
$in = [];
|
|
|
|
|
|
|
|
foreach ($value['value'] as $item) {
|
|
|
|
$params[] = $item;
|
|
|
|
$in[] = '?';
|
|
|
|
}
|
|
|
|
|
|
|
|
$wheres[] = $key . ' IN (' . implode(', ', $in) . ') ';
|
|
|
|
} else {
|
|
|
|
$ors = [];
|
|
|
|
foreach ($value['value'] as $item) {
|
|
|
|
if ($item == 'null') {
|
|
|
|
switch ($value['operator']) {
|
|
|
|
case '!=':
|
|
|
|
$ors[] = $key . ' IS NOT NULL';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '==':
|
|
|
|
default:
|
|
|
|
$ors[] = $key . ' IS NULL';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$params[] = $item;
|
2017-01-29 03:49:43 +01:00
|
|
|
$ors[] = $key . ' ' . $value['operator'] . ' ?';
|
2016-04-20 12:30:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$wheres[] = '(' . implode(' OR ', $ors) . ')';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($value['operator'] == 'like') {
|
|
|
|
$params[] = '%' . $value['value'] . '%';
|
|
|
|
$wheres[] = $key . ' ' . $value['operator'] . ' ?';
|
|
|
|
} else {
|
|
|
|
if ($value['value'] === 'null') {
|
|
|
|
switch ($value['operator']) {
|
|
|
|
case '!=':
|
|
|
|
$wheres[] = $key . ' IS NOT NULL';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '==':
|
|
|
|
default:
|
|
|
|
$wheres[] = $key . ' IS NULL';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$params[] = $value['value'];
|
|
|
|
$wheres[] = $key . ' ' . $value['operator'] . ' ?';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-01-29 03:49:43 +01:00
|
|
|
$wheres[] = $key . ' IN (' . implode(', ', array_map([Database::getConnection('read'), 'quote'], $value)) . ')';
|
2016-04-20 12:30:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($joins)) {
|
|
|
|
foreach ($joins as $table => $join) {
|
2017-01-29 03:49:43 +01:00
|
|
|
$query .= ' LEFT JOIN {{' . $table . '}} AS ' . $join['alias'] . ' ON ' . $join['on'] . ' ';
|
|
|
|
$countQuery .= ' LEFT JOIN {{' . $table . '}} AS ' . $join['alias'] . ' ON ' . $join['on'] . ' ';
|
2016-04-20 12:30:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($manualJoins)) {
|
|
|
|
foreach ($manualJoins as $join) {
|
|
|
|
$query .= ' ' . $join . ' ';
|
|
|
|
$countQuery .= ' ' . $join . ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$hasWhere = false;
|
|
|
|
if (count($wheres)) {
|
|
|
|
$hasWhere = true;
|
|
|
|
$query .= ' WHERE (' . implode(' ' . $whereType . ' ', $wheres) . ')';
|
|
|
|
$countQuery .= ' WHERE (' . implode(' ' . $whereType . ' ', $wheres) . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($manualWheres)) {
|
|
|
|
foreach ($manualWheres as $where) {
|
|
|
|
if (!$hasWhere) {
|
|
|
|
$hasWhere = true;
|
|
|
|
$query .= ' WHERE ';
|
|
|
|
$countQuery .= ' WHERE ';
|
|
|
|
} else {
|
|
|
|
$query .= ' ' . $where['type'] . ' ';
|
|
|
|
$countQuery .= ' ' . $where['type'] . ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
$query .= ' ' . $where['query'];
|
|
|
|
$countQuery .= ' ' . $where['query'];
|
|
|
|
|
|
|
|
if (isset($where['params'])) {
|
|
|
|
foreach ($where['params'] as $param) {
|
|
|
|
$params[] = $param;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_null($group)) {
|
|
|
|
$query .= ' GROUP BY ' . $group . ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($order)) {
|
|
|
|
$orders = [];
|
|
|
|
if (is_string($order) && $order == 'rand') {
|
|
|
|
$query .= ' ORDER BY RAND() ';
|
|
|
|
} else {
|
|
|
|
foreach ($order as $key => $value) {
|
|
|
|
$orders[] = $this->fieldCheck($key) . ' ' . $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query .= ' ORDER BY ' . implode(', ', $orders);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($limit) {
|
|
|
|
$query .= ' LIMIT ' . $limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($offset) {
|
|
|
|
$query .= ' OFFSET ' . $offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-01-29 03:49:43 +01:00
|
|
|
$stmt = Database::getConnection('read')->prepareCommon($countQuery);
|
2016-04-20 12:30:26 +02:00
|
|
|
$stmt->execute($params);
|
|
|
|
$res = $stmt->fetch(\PDO::FETCH_ASSOC);
|
2017-01-29 03:49:43 +01:00
|
|
|
$count = (int)$res['count'];
|
2016-04-20 12:30:26 +02:00
|
|
|
} catch (\PDOException $ex) {
|
|
|
|
$count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-01-29 03:49:43 +01:00
|
|
|
$stmt = Database::getConnection('read')->prepareCommon($query);
|
2016-04-20 12:30:26 +02:00
|
|
|
$stmt->execute($params);
|
|
|
|
$res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
|
|
|
$rtn = [];
|
|
|
|
|
|
|
|
foreach ($res as $data) {
|
|
|
|
$rtn[] = new $this->modelName($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ['items' => $rtn, 'count' => $count];
|
|
|
|
} catch (\PDOException $ex) {
|
|
|
|
throw $ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function save(Model $obj, $saveAllColumns = false)
|
|
|
|
{
|
|
|
|
if (!isset($this->primaryKey)) {
|
|
|
|
throw new HttpException\BadRequestException('Save not implemented for this store.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!($obj instanceof $this->modelName)) {
|
|
|
|
throw new HttpException\BadRequestException(get_class($obj) . ' is an invalid model type for this store.');
|
|
|
|
}
|
2016-04-12 19:31:39 +02:00
|
|
|
|
|
|
|
$data = $obj->getDataArray();
|
|
|
|
|
2016-04-20 12:30:26 +02:00
|
|
|
if (isset($data[$this->primaryKey])) {
|
|
|
|
$rtn = $this->saveByUpdate($obj, $saveAllColumns);
|
|
|
|
} else {
|
|
|
|
$rtn = $this->saveByInsert($obj, $saveAllColumns);
|
|
|
|
}
|
2016-04-12 19:31:39 +02:00
|
|
|
|
2016-04-20 12:30:26 +02:00
|
|
|
return $rtn;
|
|
|
|
}
|
2016-04-12 19:31:39 +02:00
|
|
|
|
|
|
|
public function saveByUpdate(Model $obj, $saveAllColumns = false)
|
|
|
|
{
|
|
|
|
$rtn = null;
|
|
|
|
$data = $obj->getDataArray();
|
|
|
|
$modified = ($saveAllColumns) ? array_keys($data) : $obj->getModified();
|
|
|
|
|
2016-04-20 12:30:26 +02:00
|
|
|
$updates = [];
|
|
|
|
$update_params = [];
|
|
|
|
foreach ($modified as $key) {
|
2016-04-21 16:17:36 +02:00
|
|
|
$updates[] = $key . ' = :' . $key;
|
2016-04-20 12:30:26 +02:00
|
|
|
$update_params[] = [$key, $data[$key]];
|
2016-04-12 19:31:39 +02:00
|
|
|
}
|
|
|
|
|
2016-04-20 12:30:26 +02:00
|
|
|
if (count($updates)) {
|
2017-01-29 03:49:43 +01:00
|
|
|
$qs = 'UPDATE {{' . $this->tableName . '}} SET ' . implode(', ', $updates) . ' WHERE {{' . $this->primaryKey . '}} = :primaryKey';
|
|
|
|
$q = Database::getConnection('write')->prepareCommon($qs);
|
2016-04-12 19:31:39 +02:00
|
|
|
|
2016-04-20 12:30:26 +02:00
|
|
|
foreach ($update_params as $update_param) {
|
2016-04-12 19:31:39 +02:00
|
|
|
$q->bindValue(':' . $update_param[0], $update_param[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$q->bindValue(':primaryKey', $data[$this->primaryKey]);
|
|
|
|
$q->execute();
|
|
|
|
|
|
|
|
$rtn = $this->getByPrimaryKey($data[$this->primaryKey], 'write');
|
2016-04-20 12:30:26 +02:00
|
|
|
} else {
|
2016-04-12 19:31:39 +02:00
|
|
|
$rtn = $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function saveByInsert(Model $obj, $saveAllColumns = false)
|
|
|
|
{
|
|
|
|
$rtn = null;
|
|
|
|
$data = $obj->getDataArray();
|
|
|
|
$modified = ($saveAllColumns) ? array_keys($data) : $obj->getModified();
|
|
|
|
|
2016-04-20 12:30:26 +02:00
|
|
|
$cols = [];
|
|
|
|
$values = [];
|
|
|
|
$qParams = [];
|
|
|
|
foreach ($modified as $key) {
|
|
|
|
$cols[] = $key;
|
|
|
|
$values[] = ':' . $key;
|
2016-04-12 19:31:39 +02:00
|
|
|
$qParams[':' . $key] = $data[$key];
|
|
|
|
}
|
|
|
|
|
2016-04-20 12:30:26 +02:00
|
|
|
if (count($cols)) {
|
2017-01-29 03:49:43 +01:00
|
|
|
$qs = 'INSERT INTO {{' . $this->tableName . '}} (' . implode(', ', $cols) . ') VALUES (' . implode(', ',
|
2016-04-20 12:30:26 +02:00
|
|
|
$values) . ')';
|
2017-01-29 03:49:43 +01:00
|
|
|
$q = Database::getConnection('write')->prepareCommon($qs);
|
2016-04-12 19:31:39 +02:00
|
|
|
|
2016-04-20 12:30:26 +02:00
|
|
|
if ($q->execute($qParams)) {
|
2016-04-12 19:31:39 +02:00
|
|
|
$id = !empty($data[$this->primaryKey]) ? $data[$this->primaryKey] : Database::getConnection('write')->lastInsertId();
|
|
|
|
$rtn = $this->getByPrimaryKey($id, 'write');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
2016-04-20 12:30:26 +02:00
|
|
|
public function delete(Model $obj)
|
|
|
|
{
|
|
|
|
if (!isset($this->primaryKey)) {
|
|
|
|
throw new HttpException\BadRequestException('Delete not implemented for this store.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!($obj instanceof $this->modelName)) {
|
|
|
|
throw new HttpException\BadRequestException(get_class($obj) . ' is an invalid model type for this store.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = $obj->getDataArray();
|
|
|
|
|
2017-01-29 03:49:43 +01:00
|
|
|
$q = Database::getConnection('write')->prepareCommon('DELETE FROM {{' . $this->tableName . '}} WHERE {{' . $this->primaryKey . '}} = :primaryKey');
|
2016-04-20 12:30:26 +02:00
|
|
|
$q->bindValue(':primaryKey', $data[$this->primaryKey]);
|
|
|
|
$q->execute();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function fieldCheck($field)
|
|
|
|
{
|
|
|
|
if (empty($field)) {
|
|
|
|
throw new HttpException('You cannot have an empty field name.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($field, '.') === false) {
|
2017-02-11 19:35:17 +01:00
|
|
|
return '{{' . $this->tableName . '}}.{{' . $field . '}}';
|
2016-04-20 12:30:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $field;
|
|
|
|
}
|
2016-04-21 19:05:32 +02:00
|
|
|
}
|