Skip to content

Commit

Permalink
代码规范
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Feb 4, 2023
1 parent 1f14dea commit 3b84e09
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 187 deletions.
71 changes: 35 additions & 36 deletions src/db/BaseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ abstract class BaseQuery
*/
public function __construct(ConnectionInterface $connection)
{
$this->connection = $connection;

$this->prefix = $this->connection->getConfig('prefix');
$this->connection = $connection;
$this->prefix = $this->connection->getConfig('prefix');
}

/**
Expand Down Expand Up @@ -378,7 +377,7 @@ public function field(string|array|Raw|bool $field)
if (true === $field) {
// 获取全部字段
$fields = $this->getTableFields();
$field = $fields ?: ['*'];
$field = $fields ?: ['*'];
}

if (isset($this->options['field'])) {
Expand Down Expand Up @@ -409,7 +408,7 @@ public function withoutField(array|string $field)

// 字段排除
$fields = $this->getTableFields();
$field = $fields ? array_diff($fields, $field) : $field;
$field = $fields ? array_diff($fields, $field) : $field;

if (isset($this->options['field'])) {
$field = array_merge((array) $this->options['field'], $field);
Expand Down Expand Up @@ -443,7 +442,7 @@ public function tableField(string|array|bool $field, string $tableName, string $
if (true === $field) {
// 获取全部字段
$fields = $this->getTableFields($tableName);
$field = $fields ?: ['*'];
$field = $fields ?: ['*'];
}

// 添加统一的前缀
Expand Down Expand Up @@ -490,8 +489,8 @@ public function data(array $data)
public function removeOption(string $option = '')
{
if ('' === $option) {
$this->options = [];
$this->bind = [];
$this->options = [];
$this->bind = [];
} elseif (isset($this->options[$option])) {
unset($this->options[$option]);
}
Expand Down Expand Up @@ -670,7 +669,7 @@ public function order(string|array|Raw $field, string $order = '')
public function paginate(int|array $listRows = null, int|bool $simple = false): Paginator
{
if (is_int($simple)) {
$total = $simple;
$total = $simple;
$simple = false;
}

Expand All @@ -682,26 +681,24 @@ public function paginate(int|array $listRows = null, int|bool $simple = false):
];

if (is_array($listRows)) {
$config = array_merge($defaultConfig, $listRows);
$listRows = intval($config['list_rows']);
$config = array_merge($defaultConfig, $listRows);
$listRows = intval($config['list_rows']);
} else {
$config = $defaultConfig;
$listRows = intval($listRows ?: $config['list_rows']);
$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;

$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['cache'], $this->options['limit'], $this->options['page'], $this->options['field']);

$bind = $this->bind;
$total = $this->count();
$bind = $this->bind;
$total = $this->count();
if ($total > 0) {
$results = $this->options($options)->bind($bind)->page($page, $listRows)->select();
} else {
Expand All @@ -712,10 +709,10 @@ public function paginate(int|array $listRows = null, int|bool $simple = false):
}
}
} elseif ($simple) {
$results = $this->limit(($page - 1) * $listRows, $listRows + 1)->select();
$total = null;
$results = $this->limit(($page - 1) * $listRows, $listRows + 1)->select();
$total = null;
} else {
$results = $this->page($page, $listRows)->select();
$results = $this->page($page, $listRows)->select();
}

$this->removeOption('limit');
Expand Down Expand Up @@ -744,15 +741,15 @@ public function paginateX(int|array $listRows = null, string $key = null, string
'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 = 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();
$key = $key ?: $this->getPk();
$options = $this->getOptions();

if (is_null($sort)) {
$order = $options['order'] ?? '';
Expand Down Expand Up @@ -856,11 +853,11 @@ public function cache($key = true, $expire = null, $tag = null, bool $always = f

if ($key instanceof \DateTimeInterface || $key instanceof \DateInterval || (is_int($key) && is_null($expire))) {
$expire = $key;
$key = true;
$key = true;
}

$this->options['cache'] = [$key, $expire, $tag];
$this->options['cache_always'] = $always;
$this->options['cache'] = [$key, $expire, $tag];
$this->options['cache_always'] = $always;

return $this;
}
Expand Down Expand Up @@ -969,8 +966,8 @@ public function sequence(string $sequence = null)
*/
public function json(array $json = [], bool $assoc = false)
{
$this->options['json'] = $json;
$this->options['json_assoc'] = $assoc;
$this->options['json'] = $json;
$this->options['json_assoc'] = $assoc;

return $this;
}
Expand Down Expand Up @@ -1329,9 +1326,11 @@ public function parseOptions(): array
if (isset($options['page'])) {
// 根据页数计算limit
[$page, $listRows] = $options['page'];
$page = $page > 0 ? $page : 1;
$listRows = $listRows ?: (is_numeric($options['limit']) ? $options['limit'] : 20);
$offset = $listRows * ($page - 1);

$page = $page > 0 ? $page : 1;
$listRows = $listRows ?: (is_numeric($options['limit']) ? $options['limit'] : 20);
$offset = $listRows * ($page - 1);

$options['limit'] = $offset . ',' . $listRows;
}

Expand Down
11 changes: 4 additions & 7 deletions src/db/builder/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,8 @@ public function insert(Query $query): string
*/
public function insertAll(Query $query, array $dataSet, bool $replace = false): string
{
$options = $query->getOptions();

// 获取绑定信息
$bind = $query->getFieldsBindType();
$options = $query->getOptions();
$bind = $query->getFieldsBindType();

// 获取合法的字段
if (empty($options['field']) || '*' == $options['field']) {
Expand Down Expand Up @@ -215,9 +213,8 @@ public function insertAll(Query $query, array $dataSet, bool $replace = false):
*/
public function update(Query $query): string
{
$options = $query->getOptions();

$data = $this->parseData($query, $options['data']);
$options = $query->getOptions();
$data = $this->parseData($query, $options['data']);

if (empty($data)) {
return '';
Expand Down
8 changes: 4 additions & 4 deletions src/db/builder/Oracle.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ protected function parseLimit(Query $query, string $limit): string
* 设置锁机制.
*
* @param Query $query 查询对象
* @param bool|false $lock
* @param bool|string $lock
*
* @return string
*/
protected function parseLock(Query $query, $lock = false): string
protected function parseLock(Query $query, bool|string $lock = false): string
{
if (!$lock) {
return '';
Expand All @@ -69,14 +69,14 @@ protected function parseLock(Query $query, $lock = false): string
* 字段和表名处理.
*
* @param Query $query 查询对象
* @param string $key
* @param string|int|Raw $key
* @param bool $strict
*
* @throws Exception
*
* @return string
*/
public function parseKey(Query $query, $key, bool $strict = false): string
public function parseKey(Query $query, string|int|Raw $key, bool $strict = false): string
{
if (is_int($key)) {
return (string) $key;
Expand Down
4 changes: 2 additions & 2 deletions src/db/builder/Pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ public function parseLimit(Query $query, string $limit): string
* 字段和表名处理.
*
* @param Query $query 查询对象
* @param mixed $key 字段名
* @param string|int|Raw $key 字段名
* @param bool $strict 严格检测
*
* @return string
*/
public function parseKey(Query $query, $key, bool $strict = false): string
public function parseKey(Query $query, string|int|Raw $key, bool $strict = false): string
{
if (is_int($key)) {
return (string) $key;
Expand Down
6 changes: 3 additions & 3 deletions src/db/builder/Sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ protected function parseRand(Query $query): string
* 字段和表名处理.
*
* @param Query $query 查询对象
* @param mixed $key 字段名
* @param string|int|Raw $key 字段名
* @param bool $strict 严格检测
*
* @return string
*/
public function parseKey(Query $query, $key, bool $strict = false): string
public function parseKey(Query $query, string|int|Raw $key, bool $strict = false): string
{
if (is_int($key)) {
return (string) $key;
Expand Down Expand Up @@ -111,7 +111,7 @@ public function parseKey(Query $query, $key, bool $strict = false): string
*
* @return string
*/
protected function parseLock(Query $query, $lock = false): string
protected function parseLock(Query $query, bool|string $lock = false): string
{
return '';
}
Expand Down
4 changes: 2 additions & 2 deletions src/db/builder/Sqlsrv.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ protected function parseRand(Query $query): string
* 字段和表名处理.
*
* @param Query $query 查询对象
* @param mixed $key 字段名
* @param string|int|Raw $key 字段名
* @param bool $strict 严格检测
*
* @return string
*/
public function parseKey(Query $query, $key, bool $strict = false): string
public function parseKey(Query $query, string|int|Raw $key, bool $strict = false): string
{
if (is_int($key)) {
return (string) $key;
Expand Down
Loading

0 comments on commit 3b84e09

Please sign in to comment.