From ab620fe84a36cade780e611c764ee0a41950ecf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20D=C3=B6ring?= Date: Fri, 19 Aug 2016 14:46:41 +0200 Subject: [PATCH 1/4] Fixed: SQL order by ignoring raw expressions. --- src/Query/Sql/Select.php | 4 ++++ src/Translator/Mysql.php | 8 ++++++++ tests/Query/Sql/Select.php | 11 +++++++++++ tests/Translator/Mysql.php | 6 ++++++ 4 files changed, 29 insertions(+) diff --git a/src/Query/Sql/Select.php b/src/Query/Sql/Select.php index 78d5845..4f59cb8 100644 --- a/src/Query/Sql/Select.php +++ b/src/Query/Sql/Select.php @@ -239,6 +239,10 @@ public function orderBy($columns, $direction = 'asc') { $columns = $this->stringArgumentToArray($columns); } + elseif ($columns instanceof Expression) + { + $this->orders[] = [$columns, $direction]; return $this; + } foreach ($columns as $key => $column) { diff --git a/src/Translator/Mysql.php b/src/Translator/Mysql.php index 25ef3d9..0d85549 100644 --- a/src/Translator/Mysql.php +++ b/src/Translator/Mysql.php @@ -615,6 +615,14 @@ protected function translateOrderBy() foreach ($this->attr('orders') as $column => $direction) { + // in case a raw value is given we had to + // put the column / raw value an direction inside another + // array because we cannot make objects to array keys. + if (is_array($direction)) + { + list($column, $direction) = $direction; + } + $build .= $this->escape($column) . ' ' . $direction . ', '; } diff --git a/tests/Query/Sql/Select.php b/tests/Query/Sql/Select.php index 0c6a352..5063fd4 100644 --- a/tests/Query/Sql/Select.php +++ b/tests/Query/Sql/Select.php @@ -119,6 +119,17 @@ public function testOrderBy() $this->assertAttributes($this->createQuery()->orderBy(array('firstname' => 'asc', 'lastname' => 'desc')), array('orders' => array('firstname' => 'asc', 'lastname' => 'desc'))); } + /** + * Select::orderBy + */ + public function testOrderByRaw() + { + $raw = new Expression('language <> de'); + + // simple + $this->assertAttributes($this->createQuery()->orderBy($raw), array('orders' => array([$raw, 'asc']))); + } + /** * Select::groupBy */ diff --git a/tests/Translator/Mysql.php b/tests/Translator/Mysql.php index 8703d84..0f8803f 100644 --- a/tests/Translator/Mysql.php +++ b/tests/Translator/Mysql.php @@ -362,6 +362,12 @@ public function testSelectOrderBy() { return $q->table('phpunit')->select()->orderBy(array('u.firstname' => 'asc', 'u.lastname' => 'desc')); }); + + // raw sorting + $this->assertQueryTranslation('select * from `phpunit` order by firstname <> nick asc', array(), function($q) + { + return $q->table('phpunit')->select()->orderBy(new Expression("firstname <> nick")); + }); } /** From 28122b835b5a26248cc246ea2814987e0557a4fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20D=C3=B6ring?= Date: Fri, 19 Aug 2016 14:49:03 +0200 Subject: [PATCH 2/4] Fixed php53 compatibility --- src/Query/Sql/Select.php | 2 +- tests/Query/Sql/Select.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Query/Sql/Select.php b/src/Query/Sql/Select.php index 4f59cb8..c50b215 100644 --- a/src/Query/Sql/Select.php +++ b/src/Query/Sql/Select.php @@ -241,7 +241,7 @@ public function orderBy($columns, $direction = 'asc') } elseif ($columns instanceof Expression) { - $this->orders[] = [$columns, $direction]; return $this; + $this->orders[] = array($columns, $direction); return $this; } foreach ($columns as $key => $column) diff --git a/tests/Query/Sql/Select.php b/tests/Query/Sql/Select.php index 5063fd4..2ceaaf4 100644 --- a/tests/Query/Sql/Select.php +++ b/tests/Query/Sql/Select.php @@ -127,7 +127,7 @@ public function testOrderByRaw() $raw = new Expression('language <> de'); // simple - $this->assertAttributes($this->createQuery()->orderBy($raw), array('orders' => array([$raw, 'asc']))); + $this->assertAttributes($this->createQuery()->orderBy($raw), array('orders' => array(array($raw, 'asc')))); } /** From e8853c267b680d8f92dfb7fd5090f99bfa5f00fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20D=C3=B6ring?= Date: Fri, 19 Aug 2016 15:38:38 +0200 Subject: [PATCH 3/4] Added php7 --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 4a351e6..e42b336 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,7 @@ php: - 5.4 - 5.5 - 5.6 + - 7.0 - hhvm - nightly From d065a22faa97ecf7813a6614213eba1833345b4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20D=C3=B6ring?= Date: Fri, 19 Aug 2016 15:50:40 +0200 Subject: [PATCH 4/4] Fixed php 5 bug --- src/Translator/Mysql.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Translator/Mysql.php b/src/Translator/Mysql.php index 0d85549..2e9a3e2 100644 --- a/src/Translator/Mysql.php +++ b/src/Translator/Mysql.php @@ -620,7 +620,10 @@ protected function translateOrderBy() // array because we cannot make objects to array keys. if (is_array($direction)) { - list($column, $direction) = $direction; + // This only works in php 7 the php 5 fix is below + //list($column, $direction) = $direction; + $column = $direction[0]; + $direction = $direction[1]; } $build .= $this->escape($column) . ' ' . $direction . ', ';