Skip to content

Commit

Permalink
添加:SQL多字段排序
Browse files Browse the repository at this point in the history
  • Loading branch information
dxkite committed Jul 7, 2019
1 parent 65908cd commit 19e5028
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
47 changes: 39 additions & 8 deletions suda/src/database/statement/ReadStatement.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace suda\database\statement;

use function func_get_args;
Expand All @@ -8,6 +9,10 @@
use suda\database\middleware\Middleware;
use suda\database\exception\SQLException;

/**
* Class ReadStatement
* @package suda\database\statement
*/
class ReadStatement extends QueryStatement
{
use PrepareTrait;
Expand All @@ -26,18 +31,39 @@ class ReadStatement extends QueryStatement
*/
protected $table;

/**
* @var string
*/
protected $distinct = '';

/**
* @var string
*/
protected $select = '*';

/**
* @var string
*/
protected $where = '';

/**
* @var string
*/
protected $groupBy = '';

/**
* @var string
*/
protected $having = '';

/**
* @var string
*/
protected $orderBy = '';

/**
* @var string
*/
protected $limit = '';

/**
Expand Down Expand Up @@ -145,7 +171,7 @@ protected function whereArray(array $where, array $binders)
protected function whereStringArray(string $where, array $whereBinder)
{
list($where, $whereBinder) = $this->prepareWhereString($where, $whereBinder);
$this->where = 'WHERE '. $where;
$this->where = 'WHERE ' . $where;
$this->binder = $this->mergeBinder($this->binder, $whereBinder);
}

Expand All @@ -157,7 +183,7 @@ protected function whereStringArray(string $where, array $whereBinder)
*/
public function groupBy(string $what)
{
$this->groupBy = 'GROUP BY '. $what;
$this->groupBy = 'GROUP BY ' . $what;
return $this;
}

Expand Down Expand Up @@ -201,7 +227,7 @@ protected function havingStringArray(string $having, array $havingBinder)
{

list($having, $havingBinder) = $this->prepareWhereString($having, $havingBinder);
$this->having = 'HAVING '. $having;
$this->having = 'HAVING ' . $having;
$this->binder = $this->mergeBinder($this->binder, $havingBinder);
}

Expand All @@ -214,7 +240,12 @@ protected function havingStringArray(string $having, array $havingBinder)
*/
public function orderBy(string $what, string $order = 'ASC')
{
$this->orderBy = 'ORDER BY '. $what.' '. $order;
$order = strtoupper($order);
if (strlen($this->orderBy) > 0) {
$this->orderBy .= ',`' . $what . '` ' . $order;
} else {
$this->orderBy = 'ORDER BY `' . $what . '` ' . $order;
}
return $this;
}

Expand All @@ -227,7 +258,7 @@ public function orderBy(string $what, string $order = 'ASC')
*/
public function limit(int $start, int $length = null)
{
$this->limit = 'LIMIT '. $start . ($length !== null ? ',' .$length :'');
$this->limit = 'LIMIT ' . $start . ($length !== null ? ',' . $length : '');
return $this;
}

Expand All @@ -252,11 +283,11 @@ public function page(int $page, int $length)
*
* @return Query
*/
protected function prepareQuery():Query
protected function prepareQuery(): Query
{
$where = [$this->where,$this->groupBy,$this->having,$this->orderBy,$this->limit];
$where = [$this->where, $this->groupBy, $this->having, $this->orderBy, $this->limit];
$condition = implode(' ', array_filter(array_map('trim', $where), 'strlen'));
$select = [$this->distinct,$this->select];
$select = [$this->distinct, $this->select];
$selection = implode(' ', array_filter(array_map('trim', $select), 'strlen'));
$string = sprintf("SELECT %s FROM %s %s", $selection, $this->table, $condition);
return new Query($string, $this->binder);
Expand Down
14 changes: 12 additions & 2 deletions tests/src/database/StatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ public function testMySQLBuilder()
);

$this->assertEquals(
'SELECT `id`,`name` FROM user_table WHERE name like :name ORDER BY id ASC LIMIT 0,10',
'SELECT `id`,`name` FROM user_table WHERE name like :name ORDER BY `id` ASC LIMIT 0,10',
$table->read('id', 'name')->where('name like :name', ['name' => 'dxkite'])->page(1, 10)->orderBy('id', 'ASC')->getString()
);

$this->assertEquals(
'SELECT DISTINCT `id`,`name` FROM user_table WHERE name like :name ORDER BY id ASC LIMIT 0,10',
'SELECT DISTINCT `id`,`name` FROM user_table WHERE name like :name ORDER BY `id` ASC LIMIT 0,10',
$table->read('id', 'name')->distinct()->where('name like :name', ['name' => 'dxkite'])->page(1, 10)->orderBy('id', 'ASC')->getString()
);

Expand Down Expand Up @@ -145,6 +145,16 @@ public function testMySQLBuilder()
'UPDATE user_table SET id = id + 1 WHERE `name`=:_name_31',
$whereIn->getString()
);

$this->assertEquals(
'SELECT `id`,`name` FROM user_table WHERE id in (:_0_32,:_0_33) HAVING name like :_0_34 ORDER BY `id` DESC,`name` ASC LIMIT 0,10',
$table->read('id', 'name')
->where('id in (?)', new ArrayObject([ 1, 2]))
->page(1, 10)
->having('name like ?', 'dxkite')
->orderBy('id','desc')
->orderBy('name','asc')->getString()
);
}

public function testStuctSet()
Expand Down

0 comments on commit 19e5028

Please sign in to comment.