From 78e5d3e103c2171c46d2148c3ffcacbf829edf85 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Wed, 21 Sep 2016 11:12:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D1=81=D1=88=D0=B8=D1=80=D0=B5?= =?UTF-8?q?=D0=BD=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=BE=D0=BD=D0=B0?= =?UTF-8?q?=D0=BB=20Yii2=20QueryBuilder:=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D1=8C=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=BE=D0=BD=D0=B0?= =?UTF-8?q?=D0=BB=20ORDER=20BY=20FIELD=20=D0=B2=D1=81=D1=82=D1=80=D0=BE?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=B2=D0=BD=D1=83=D1=82=D1=80=D1=8C=20=D0=B1?= =?UTF-8?q?=D0=B8=D0=BB=D0=B4=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/config/config.php | 3 ++ common/db/mysql/QueryBuilder.php | 39 +++++++++++++++++++ common/db/mysql/Schema.php | 12 ++++++ .../common/unit/db/mysql/QueryBuilderTest.php | 16 ++++++++ 4 files changed, 70 insertions(+) create mode 100644 common/db/mysql/QueryBuilder.php create mode 100644 common/db/mysql/Schema.php create mode 100644 tests/codeception/common/unit/db/mysql/QueryBuilderTest.php diff --git a/common/config/config.php b/common/config/config.php index 3df1fbb..92f07f9 100644 --- a/common/config/config.php +++ b/common/config/config.php @@ -12,6 +12,9 @@ return [ 'username' => getenv('MYSQL_USER'), 'password' => getenv('MYSQL_PASSWORD'), 'charset' => 'utf8', + 'schemaMap' => [ + 'mysql' => common\db\mysql\Schema::class, + ], ], 'mailer' => [ 'class' => yii\swiftmailer\Mailer::class, diff --git a/common/db/mysql/QueryBuilder.php b/common/db/mysql/QueryBuilder.php new file mode 100644 index 0000000..f467396 --- /dev/null +++ b/common/db/mysql/QueryBuilder.php @@ -0,0 +1,39 @@ + $direction) { + if ($direction instanceof Expression) { + $orders[] = $direction->expression; + } elseif (is_array($direction)) { + // This is new feature + if (empty($direction)) { + continue; + } + + $fieldValues = []; + foreach($direction as $fieldValue) { + $fieldValues[] = $this->db->quoteValue($fieldValue); + } + + $orders[] = 'FIELD(' . $this->db->quoteColumnName($name) . ',' . implode(',', $fieldValues) . ')'; + // End of new feature + } else { + $orders[] = $this->db->quoteColumnName($name) . ($direction === SORT_DESC ? ' DESC' : ''); + } + } + + return 'ORDER BY ' . implode(', ', $orders); + } + +} diff --git a/common/db/mysql/Schema.php b/common/db/mysql/Schema.php new file mode 100644 index 0000000..301d828 --- /dev/null +++ b/common/db/mysql/Schema.php @@ -0,0 +1,12 @@ +db); + } + +} diff --git a/tests/codeception/common/unit/db/mysql/QueryBuilderTest.php b/tests/codeception/common/unit/db/mysql/QueryBuilderTest.php new file mode 100644 index 0000000..77a3d96 --- /dev/null +++ b/tests/codeception/common/unit/db/mysql/QueryBuilderTest.php @@ -0,0 +1,16 @@ +db); + $result = $queryBuilder->buildOrderBy(['dummy' => ['first', 'second']]); + $this->assertEquals("ORDER BY FIELD(`dummy`,'first','second')", $result); + } + +}