]> source.dussan.org Git - nextcloud-server.git/commitdiff
add support for escaping like parameters when using the query builder
authorRobin Appelman <robin@icewind.nl>
Mon, 16 Jan 2017 14:12:30 +0000 (15:12 +0100)
committerRobin Appelman <robin@icewind.nl>
Thu, 30 Mar 2017 09:09:22 +0000 (11:09 +0200)
Signed-off-by: Robin Appelman <robin@icewind.nl>
lib/private/DB/QueryBuilder/ExpressionBuilder/OCIExpressionBuilder.php
lib/private/DB/QueryBuilder/ExpressionBuilder/SqliteExpressionBuilder.php [new file with mode: 0644]
lib/private/DB/QueryBuilder/QueryBuilder.php
tests/lib/DB/QueryBuilder/ExpressionBuilderTest.php

index 69c34947feb54607afa455882d1a608ae71ee383..179ce72e8e155863a7980c28969632961901dd86 100644 (file)
@@ -153,6 +153,13 @@ class OCIExpressionBuilder extends ExpressionBuilder {
                return parent::castColumn($column, $type);
        }
 
+       /**
+        * @inheritdoc
+        */
+       public function like($x, $y, $type = null) {
+               return parent::like($x, $y, $type) . " ESCAPE '\\'";
+       }
+
        /**
         * @inheritdoc
         */
diff --git a/lib/private/DB/QueryBuilder/ExpressionBuilder/SqliteExpressionBuilder.php b/lib/private/DB/QueryBuilder/ExpressionBuilder/SqliteExpressionBuilder.php
new file mode 100644 (file)
index 0000000..fa7c7fa
--- /dev/null
@@ -0,0 +1,37 @@
+<?php
+/**
+ * @author Robin Appelman <robin@icewind.nl>
+ *
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OC\DB\QueryBuilder\ExpressionBuilder;
+
+
+use OC\DB\QueryBuilder\QueryFunction;
+use OCP\DB\QueryBuilder\ILiteral;
+use OCP\DB\QueryBuilder\IParameter;
+use OCP\DB\QueryBuilder\IQueryBuilder;
+use OCP\DB\QueryBuilder\IQueryFunction;
+
+class SqliteExpressionBuilder extends ExpressionBuilder {
+       /**
+        * @inheritdoc
+        */
+       public function like($x, $y, $type = null) {
+               return parent::like($x, $y, $type) . " ESCAPE '\\'";
+       }
+}
index d5dd9cf03661c012e304d6f9c2f6972b42a61420..a434140806e8faab783e9e3f98e05889fab640c5 100644 (file)
@@ -26,11 +26,15 @@ namespace OC\DB\QueryBuilder;
 
 use Doctrine\DBAL\Platforms\MySqlPlatform;
 use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
+use Doctrine\DBAL\Platforms\SqlitePlatform;
 use OC\DB\OracleConnection;
 use OC\DB\QueryBuilder\ExpressionBuilder\ExpressionBuilder;
 use OC\DB\QueryBuilder\ExpressionBuilder\MySqlExpressionBuilder;
 use OC\DB\QueryBuilder\ExpressionBuilder\OCIExpressionBuilder;
 use OC\DB\QueryBuilder\ExpressionBuilder\PgSqlExpressionBuilder;
+use OC\DB\QueryBuilder\ExpressionBuilder\SqliteExpressionBuilder;
+use OC\DB\QueryBuilder\FunctionBuilder\FunctionBuilder;
+use OC\DB\QueryBuilder\FunctionBuilder\SqliteFunctionBuilder;
 use OC\SystemConfig;
 use OCP\DB\QueryBuilder\IQueryBuilder;
 use OCP\DB\QueryBuilder\IQueryFunction;
@@ -110,6 +114,8 @@ class QueryBuilder implements IQueryBuilder {
                        return new PgSqlExpressionBuilder($this->connection);
                } else if ($this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
                        return new MySqlExpressionBuilder($this->connection);
+               } else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
+                       return new SqliteExpressionBuilder($this->connection);
                } else {
                        return new ExpressionBuilder($this->connection);
                }
index 4122f300c86eb361c1a6a6f770d52e407dc89fe5..ff58b3f6e0bfed3a92e9327598aec77d187eea77 100644 (file)
@@ -352,24 +352,26 @@ class ExpressionBuilderTest extends TestCase {
                return [
                        ['eq', '5', IQueryBuilder::PARAM_STR, false, 3],
                        ['eq', '5', IQueryBuilder::PARAM_STR, true, 1],
-                       ['neq', '5', IQueryBuilder::PARAM_STR, false, 6],
-                       ['neq', '5', IQueryBuilder::PARAM_STR, true, 4],
+                       ['neq', '5', IQueryBuilder::PARAM_STR, false, 8],
+                       ['neq', '5', IQueryBuilder::PARAM_STR, true, 6],
                        ['lt', '5', IQueryBuilder::PARAM_STR, false, 3],
                        ['lt', '5', IQueryBuilder::PARAM_STR, true, 1],
                        ['lte', '5', IQueryBuilder::PARAM_STR, false, 6],
                        ['lte', '5', IQueryBuilder::PARAM_STR, true, 4],
-                       ['gt', '5', IQueryBuilder::PARAM_STR, false, 3],
+                       ['gt', '5', IQueryBuilder::PARAM_STR, false, 5],
                        ['gt', '5', IQueryBuilder::PARAM_STR, true, 1],
-                       ['gte', '5', IQueryBuilder::PARAM_STR, false, 6],
+                       ['gte', '5', IQueryBuilder::PARAM_STR, false, 8],
                        ['gte', '5', IQueryBuilder::PARAM_STR, true, 4],
                        ['like', '%5%', IQueryBuilder::PARAM_STR, false, 3],
                        ['like', '%5%', IQueryBuilder::PARAM_STR, true, 1],
-                       ['notLike', '%5%', IQueryBuilder::PARAM_STR, false, 6],
-                       ['notLike', '%5%', IQueryBuilder::PARAM_STR, true, 4],
+                       ['like', 'under_%', IQueryBuilder::PARAM_STR, false, 2],
+                       ['like', 'under\_%', IQueryBuilder::PARAM_STR, false, 1],
+                       ['notLike', '%5%', IQueryBuilder::PARAM_STR, false, 8],
+                       ['notLike', '%5%', IQueryBuilder::PARAM_STR, true, 6],
                        ['in', ['5'], IQueryBuilder::PARAM_STR_ARRAY, false, 3],
                        ['in', ['5'], IQueryBuilder::PARAM_STR_ARRAY, true, 1],
-                       ['notIn', ['5'], IQueryBuilder::PARAM_STR_ARRAY, false, 6],
-                       ['notIn', ['5'], IQueryBuilder::PARAM_STR_ARRAY, true, 4],
+                       ['notIn', ['5'], IQueryBuilder::PARAM_STR_ARRAY, false, 8],
+                       ['notIn', ['5'], IQueryBuilder::PARAM_STR_ARRAY, true, 6],
                ];
        }
 
@@ -392,6 +394,8 @@ class ExpressionBuilderTest extends TestCase {
                $this->createConfig($appId, 7, 4);
                $this->createConfig($appId, 8, 5);
                $this->createConfig($appId, 9, 6);
+               $this->createConfig($appId, 10, 'under_score');
+               $this->createConfig($appId, 11, 'underscore');
 
                $query = $this->connection->getQueryBuilder();
                $query->select($query->createFunction('COUNT(*) AS `count`'))