<?php
<liu21st@gmail.com>
declare (strict_types = 1);
namespace think\db;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException as Exception;
use think\db\exception\ModelNotFoundException;
use think\helper\Str;
use think\Model;
use think\Paginator;
abstract class BaseQuery
{
use concern\TimeFieldQuery;
use concern\AggregateQuery;
use concern\ModelRelationQuery;
use concern\ResultOperation;
use concern\Transaction;
use concern\WhereQuery;
protected $connection;
protected $name = '';
protected $pk;
protected $autoinc;
protected $prefix = '';
protected $options = [];
public function __construct(ConnectionInterface $connection)
{
$this->connection = $connection;
$this->prefix = $this->connection->getConfig('prefix');
}
public function __call(string $method, array $args)
{
if (strtolower(substr($method, 0, 5)) == 'getby') {
$field = Str::snake(substr($method, 5));
return $this->where($field, '=', $args[0])->find();
} elseif (strtolower(substr($method, 0, 10)) == 'getfieldby') {
$name = Str::snake(substr($method, 10));
return $this->where($name, '=', $args[0])->value($args[1]);
} elseif (strtolower(substr($method, 0, 7)) == 'whereor') {
$name = Str::snake(substr($method, 7));
array_unshift($args, $name);
return call_user_func_array([$this, 'whereOr'], $args);
} elseif (strtolower(substr($method, 0, 5)) == 'where') {
$name = Str::snake(substr($method, 5));
array_unshift($args, $name);
return call_user_func_array([$this, 'where'], $args);
} elseif ($this->model && method_exists($this->model, 'scope' . $method)) {
$method = 'scope' . $method;
array_unshift($args, $this);
call_user_func_array([$this->model, $method], $args);
return $this;
} else {
throw new Exception('method not exist:' . static::class . '->' . $method);
}
}
public function newQuery(): BaseQuery
{
$query = new static($this->connection);
if ($this->model) {
$query->model($this->model);
}
if (isset($this->options['table'])) {
$query->table($this->options['table']);
} else {
$query->name($this->name);
}
if (isset($this->options['json'])) {
$query->json($this->options['json'], $this->options['json_assoc']);
}
if (isset($this->options['field_type'])) {
$query->setFieldType($this->options['field_type']);
}
return $query;
}
public function getConnection()
{
return $this->connection;
}
public function name(string $name)
{
$this->name = $name;
return $this;
}
public function getName(): string
{
return $this->name ?: $this->model->getName();
}
public function getConfig(string $name = '')
{
return $this->connection->getConfig($name);
}
public function getTable(string $name = '')
{
if (empty($name) && isset($this->options['table'])) {
return $this->options['table'];
}
$name = $name ?: $this->name;
return $this->prefix . Str::snake($name);
}
public function setFieldType(array $type)
{
$this->options['field_type'] = $type;
return $this;
}
public function getLastSql(): string
{
return $this->connection->getLastSql();
}
public function getNumRows(): int
{
return $this->connection->getNumRows();
}
public function getLastInsID(string $sequence = null)
{
return $this->connection->getLastInsID($this, $sequence);
}
public function value(string $field, $default = null)
{
return $this->connection->value($this, $field, $default);
}
public function column(string $field, string $key = ''): array
{
return $this->connection->column($this, $field, $key);
}
public function union($union, bool $all = false)
{
$this->options['union']['type'] = $all ? 'UNION ALL' : 'UNION';
if (is_array($union)) {
$this->options['union'] = array_merge($this->options['union'], $union);
} else {
$this->options['union'][] = $union;
}
return $this;
}
public function unionAll($union)
{
return $this->union($union, true);
}
public function field($field)
{
if (empty($field)) {
return $this;
} elseif ($field instanceof Raw) {
$this->options['field'][] = $field;
return $this;
}
if (is_string($field)) {
if (preg_match('/[\<\'\"\(]/', $field)) {
return $this->fieldRaw($field);
}
$field = array_map('trim', explode(',', $field));
}
if (true === $field) {
$fields = $this->getTableFields();
$field = $fields ?: ['*'];
}
if (isset($this->options['field'])) {
$field = array_merge((array) $this->options['field'], $field);
}
$this->options['field'] = array_unique($field);
return $this;
}
public function withoutField($field)
{
if (empty($field)) {
return $this;
}
if (is_string($field)) {
$field = array_map('trim', explode(',', $field));
}
$fields = $this->getTableFields();
$field = $fields ? array_diff($fields, $field) : $field;
if (isset($this->options['field'])) {
$field = array_merge((array) $this->options['field'], $field);
}
$this->options['field'] = array_unique($field);
return $this;
}
public function tableField($field, string $tableName, string $prefix = '', string $alias = '')
{
if (empty($field)) {
return $this;
}
if (is_string($field)) {
$field = array_map('trim', explode(',', $field));
}
if (true === $field) {
$fields = $this->getTableFields($tableName);
$field = $fields ?: ['*'];
}
$prefix = $prefix ?: $tableName;
foreach ($field as $key => &$val) {
if (is_numeric($key) && $alias) {
$field[$prefix . '.' . $val] = $alias . $val;
unset($field[$key]);
} elseif (is_numeric($key)) {
$val = $prefix . '.' . $val;
}
}
if (isset($this->options['field'])) {
$field = array_merge((array) $this->options['field'], $field);
}
$this->options['field'] = array_unique($field);
return $this;
}
public function data(array $data)
{
$this->options['data'] = $data;
return $this;
}
public function removeOption(string $option = '')
{
if ('' === $option) {
$this->options = [];
$this->bind = [];
} elseif (isset($this->options[$option])) {
unset($this->options[$option]);
}
return $this;
}
public function limit(int $offset, int $length = null)
{
$this->options['limit'] = $offset . ($length ? ',' . $length : '');
return $this;
}
public function page(int $page, int $listRows = null)
{
$this->options['page'] = [$page, $listRows];
return $this;
}
public function table($table)
{
if (is_string($table)) {
if (strpos($table, ')')) {
} elseif (false === strpos($table, ',')) {
if (strpos($table, ' ')) {
[$item, $alias] = explode(' ', $table);
$table = [];
$this->alias([$item => $alias]);
$table[$item] = $alias;
}
} else {
$tables = explode(',', $table);
$table = [];
foreach ($tables as $item) {
$item = trim($item);
if (strpos($item, ' ')) {
[$item, $alias] = explode(' ', $item);
$this->alias([$item => $alias]);
$table[$item] = $alias;
} else {
$table[] = $item;
}
}
}
} elseif (is_array($table)) {
$tables = $table;
$table = [];
foreach ($tables as $key => $val) {
if (is_numeric($key)) {
$table[] = $val;
} else {
$this->alias([$key => $val]);
$table[$key] = $val;
}
}
}
$this->options['table'] = $table;
return $this;
}
public function order($field, string $order = '')
{
if (empty($field)) {
return $this;
} elseif ($field instanceof Raw) {
$this->options['order'][] = $field;
return $this;
}
if (is_string($field)) {
if (!empty($this->options['via'])) {
$field = $this->options['via'] . '.' . $field;
}
if (strpos($field, ',')) {
$field = array_map('trim', explode(',', $field));
} else {
$field = empty($order) ? $field : [$field => $order];
}
} elseif (!empty($this->options['via'])) {
foreach ($field as $key => $val) {
if (is_numeric($key)) {
$field[$key] = $this->options['via'] . '.' . $val;
} else {
$field[$this->options['via'] . '.' . $key] = $val;
unset($field[$key]);
}
}
}
if (!isset($this->options['order'])) {
$this->options['order'] = [];
}
if (is_array($field)) {
$this->options['order'] = array_merge($this->options['order'], $field);
} else {
$this->options['order'][] = $field;
}
return $this;
}
public function paginate($listRows = null, $simple = false): Paginator
{
if (is_int($simple)) {
$total = $simple;
$simple = false;
}
$defaultConfig = [
'query' => [], 'fragment' => '', 'var_page' => 'page', 'list_rows' => 15, ];
if (is_array($listRows)) {
$config = array_merge($defaultConfig, $listRows);
$listRows = intval($config['list_rows']);
} else {
$config = $defaultConfig;
$listRows = intval($listRows ?: $config['list_rows']);
}
$page = isset($config['page']) ? (int) $config['page'] : Paginator::getCurrentPage($config['var_page']);
$page = $page < 1 ? 1 : $page;
$config['path'] = $config['path'] ?? Paginator::getCurrentPath();
if (!isset($total) && !$simple) {
$options = $this->getOptions();
unset($this->options['order'], $this->options['limit'], $this->options['page'], $this->options['field']);
$bind = $this->bind;
$total = $this->count();
$results = $this->options($options)->bind($bind)->page($page, $listRows)->select();
} elseif ($simple) {
$results = $this->limit(($page - 1) * $listRows, $listRows + 1)->select();
$total = null;
} else {
$results = $this->page($page, $listRows)->select();
}
$this->removeOption('limit');
$this->removeOption('page');
return Paginator::make($results, $listRows, $page, $total, $simple, $config);
}
public function paginateX($listRows = null, string $key = null, string $sort = null): Paginator
{
$defaultConfig = [
'query' => [], 'fragment' => '', 'var_page' => 'page', 'list_rows' => 15, ];
$config = is_array($listRows) ? array_merge($defaultConfig, $listRows) : $defaultConfig;
$listRows = is_int($listRows) ? $listRows : (int) $config['list_rows'];
$page = isset($config['page']) ? (int) $config['page'] : Paginator::getCurrentPage($config['var_page']);
$page = $page < 1 ? 1 : $page;
$config['path'] = $config['path'] ?? Paginator::getCurrentPath();
$key = $key ?: $this->getPk();
$options = $this->getOptions();
if (is_null($sort)) {
$order = $options['order'] ?? '';
if (!empty($order)) {
$sort = $order[$key] ?? 'desc';
} else {
$this->order($key, 'desc');
$sort = 'desc';
}
} else {
$this->order($key, $sort);
}
$newOption = $options;
unset($newOption['field'], $newOption['page']);
$data = $this->newQuery()
->options($newOption)
->field($key)
->where(true)
->order($key, $sort)
->limit(1)
->find();
$result = $data[$key];
if (is_numeric($result)) {
$lastId = 'asc' == $sort ? ($result - 1) + ($page - 1) * $listRows : ($result + 1) - ($page - 1) * $listRows;
} else {
throw new Exception('not support type');
}
$results = $this->when($lastId, function ($query) use ($key, $sort, $lastId) {
$query->where($key, 'asc' == $sort ? '>' : '<', $lastId);
})
->limit($listRows)
->select();
$this->options($options);
return Paginator::make($results, $listRows, $page, null, true, $config);
}
public function more(int $limit, $lastId = null, string $key = null, string $sort = null): array
{
$key = $key ?: $this->getPk();
if (is_null($sort)) {
$order = $this->getOptions('order');
if (!empty($order)) {
$sort = $order[$key] ?? 'desc';
} else {
$this->order($key, 'desc');
$sort = 'desc';
}
} else {
$this->order($key, $sort);
}
$result = $this->when($lastId, function ($query) use ($key, $sort, $lastId) {
$query->where($key, 'asc' == $sort ? '>' : '<', $lastId);
})->limit($limit)->select();
$last = $result->last();
$result->first();
return [
'data' => $result,
'lastId' => $last[$key],
];
}
public function cache($key = true, $expire = null, $tag = null)
{
if (false === $key || !$this->getConnection()->getCache()) {
return $this;
}
if ($key instanceof \DateTimeInterface || $key instanceof \DateInterval || (is_int($key) && is_null($expire))) {
$expire = $key;
$key = true;
}
$this->options['cache'] = [$key, $expire, $tag];
return $this;
}
public function lock($lock = false)
{
$this->options['lock'] = $lock;
if ($lock) {
$this->options['master'] = true;
}
return $this;
}
public function alias($alias)
{
if (is_array($alias)) {
$this->options['alias'] = $alias;
} else {
$table = $this->getTable();
$this->options['alias'][$table] = $alias;
}
return $this;
}
public function master(bool $readMaster = true)
{
$this->options['master'] = $readMaster;
return $this;
}
public function strict(bool $strict = true)
{
$this->options['strict'] = $strict;
return $this;
}
public function sequence(string $sequence = null)
{
$this->options['sequence'] = $sequence;
return $this;
}
public function json(array $json = [], bool $assoc = false)
{
$this->options['json'] = $json;
$this->options['json_assoc'] = $assoc;
return $this;
}
public function pk($pk)
{
$this->pk = $pk;
return $this;
}
protected function options(array $options)
{
$this->options = $options;
return $this;
}
public function getOptions(string $name = '')
{
if ('' === $name) {
return $this->options;
}
return $this->options[$name] ?? null;
}
public function setOption(string $option, $value)
{
$this->options[$option] = $value;
return $this;
}
public function via(string $via = '')
{
$this->options['via'] = $via;
return $this;
}
public function save(array $data = [], bool $forceInsert = false)
{
if ($forceInsert) {
return $this->insert($data);
}
$this->options['data'] = array_merge($this->options['data'] ?? [], $data);
if (!empty($this->options['where'])) {
$isUpdate = true;
} else {
$isUpdate = $this->parseUpdateData($this->options['data']);
}
return $isUpdate ? $this->update() : $this->insert();
}
public function insert(array $data = [], bool $getLastInsID = false)
{
if (!empty($data)) {
$this->options['data'] = $data;
}
return $this->connection->insert($this, $getLastInsID);
}
public function insertGetId(array $data)
{
return $this->insert($data, true);
}
public function insertAll(array $dataSet = [], int $limit = 0): int
{
if (empty($dataSet)) {
$dataSet = $this->options['data'] ?? [];
}
if (empty($limit) && !empty($this->options['limit']) && is_numeric($this->options['limit'])) {
$limit = (int) $this->options['limit'];
}
return $this->connection->insertAll($this, $dataSet, $limit);
}
public function selectInsert(array $fields, string $table): int
{
return $this->connection->selectInsert($this, $fields, $table);
}
public function update(array $data = []): int
{
if (!empty($data)) {
$this->options['data'] = array_merge($this->options['data'] ?? [], $data);
}
if (empty($this->options['where'])) {
$this->parseUpdateData($this->options['data']);
}
if (empty($this->options['where']) && $this->model) {
$this->where($this->model->getWhere());
}
if (empty($this->options['where'])) {
throw new Exception('miss update condition');
}
return $this->connection->update($this);
}
public function delete($data = null): int
{
if (!is_null($data) && true !== $data) {
$this->parsePkWhere($data);
}
if (empty($this->options['where']) && $this->model) {
$this->where($this->model->getWhere());
}
if (true !== $data && empty($this->options['where'])) {
throw new Exception('delete without condition');
}
if (!empty($this->options['soft_delete'])) {
list($field, $condition) = $this->options['soft_delete'];
if ($condition) {
unset($this->options['soft_delete']);
$this->options['data'] = [$field => $condition];
return $this->connection->update($this);
}
}
$this->options['data'] = $data;
return $this->connection->delete($this);
}
public function select($data = null): Collection
{
if (!is_null($data)) {
$this->parsePkWhere($data);
}
$resultSet = $this->connection->select($this);
if (!empty($this->options['fail']) && count($resultSet) == 0) {
$this->throwNotFound();
}
if (!empty($this->model)) {
$resultSet = $this->resultSetToModelCollection($resultSet);
} else {
$this->resultSet($resultSet);
}
return $resultSet;
}
public function find($data = null)
{
if (!is_null($data)) {
$this->parsePkWhere($data);
}
if (empty($this->options['where']) && empty($this->options['order'])) {
$result = [];
} else {
$result = $this->connection->find($this);
}
if (empty($result)) {
return $this->resultToEmpty();
}
if (!empty($this->model)) {
$this->resultToModel($result, $this->options);
} else {
$this->result($result);
}
return $result;
}
public function parseOptions(): array
{
$options = $this->getOptions();
if (empty($options['table'])) {
$options['table'] = $this->getTable();
}
if (!isset($options['where'])) {
$options['where'] = [];
} elseif (isset($options['view'])) {
$this->parseView($options);
}
if (!isset($options['field'])) {
$options['field'] = '*';
}
foreach (['data', 'order', 'join', 'union'] as $name) {
if (!isset($options[$name])) {
$options[$name] = [];
}
}
if (!isset($options['strict'])) {
$options['strict'] = $this->connection->getConfig('fields_strict');
}
foreach (['master', 'lock', 'fetch_sql', 'array', 'distinct', 'procedure'] as $name) {
if (!isset($options[$name])) {
$options[$name] = false;
}
}
foreach (['group', 'having', 'limit', 'force', 'comment', 'partition', 'duplicate', 'extra'] as $name) {
if (!isset($options[$name])) {
$options[$name] = '';
}
}
if (isset($options['page'])) {
[$page, $listRows] = $options['page'];
$page = $page > 0 ? $page : 1;
$listRows = $listRows ?: (is_numeric($options['limit']) ? $options['limit'] : 20);
$offset = $listRows * ($page - 1);
$options['limit'] = $offset . ',' . $listRows;
}
$this->options = $options;
return $options;
}
public function parseUpdateData(&$data): bool
{
$pk = $this->getPk();
$isUpdate = false;
if (is_string($pk) && isset($data[$pk])) {
$this->where($pk, '=', $data[$pk]);
$this->options['key'] = $data[$pk];
unset($data[$pk]);
$isUpdate = true;
} elseif (is_array($pk)) {
foreach ($pk as $field) {
if (isset($data[$field])) {
$this->where($field, '=', $data[$field]);
$isUpdate = true;
} else {
throw new Exception('miss complex primary data');
}
unset($data[$field]);
}
}
return $isUpdate;
}
public function parsePkWhere($data): void
{
$pk = $this->getPk();
if (is_string($pk)) {
if (empty($this->options['table'])) {
$this->options['table'] = $this->getTable();
}
$table = is_array($this->options['table']) ? key($this->options['table']) : $this->options['table'];
if (!empty($this->options['alias'][$table])) {
$alias = $this->options['alias'][$table];
}
$key = isset($alias) ? $alias . '.' . $pk : $pk;
if (is_array($data)) {
$this->where($key, 'in', $data);
} else {
$this->where($key, '=', $data);
$this->options['key'] = $data;
}
}
}
protected function getModelUpdateCondition(array $options)
{
return $options['where']['AND'] ?? null;
}
}