diff --git a/suda/src/database/statement/ReadStatement.php b/suda/src/database/statement/ReadStatement.php index 3c1e5466..61ec4210 100644 --- a/suda/src/database/statement/ReadStatement.php +++ b/suda/src/database/statement/ReadStatement.php @@ -1,4 +1,5 @@ prepareWhereString($where, $whereBinder); - $this->where = 'WHERE '. $where; + $this->where = 'WHERE ' . $where; $this->binder = $this->mergeBinder($this->binder, $whereBinder); } @@ -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; } @@ -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); } @@ -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; } @@ -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; } @@ -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); diff --git a/tests/src/database/StatementTest.php b/tests/src/database/StatementTest.php index e9ee0f53..85060b60 100644 --- a/tests/src/database/StatementTest.php +++ b/tests/src/database/StatementTest.php @@ -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() ); @@ -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()