diff options
author | Robin Appelman <robin@icewind.nl> | 2017-01-16 15:12:30 +0100 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2017-03-30 11:09:22 +0200 |
commit | a65652fc1efe158bd097d573f012167cfcd26a62 (patch) | |
tree | 3dff9d54a80ceab4fff65590121c86bf7994992c | |
parent | 83f3990e069e466741b35ecc5ef730972d22f67d (diff) | |
download | nextcloud-server-a65652fc1efe158bd097d573f012167cfcd26a62.tar.gz nextcloud-server-a65652fc1efe158bd097d573f012167cfcd26a62.zip |
add support for escaping like parameters when using the query builder
Signed-off-by: Robin Appelman <robin@icewind.nl>
4 files changed, 62 insertions, 8 deletions
diff --git a/lib/private/DB/QueryBuilder/ExpressionBuilder/OCIExpressionBuilder.php b/lib/private/DB/QueryBuilder/ExpressionBuilder/OCIExpressionBuilder.php index 69c34947feb..179ce72e8e1 100644 --- a/lib/private/DB/QueryBuilder/ExpressionBuilder/OCIExpressionBuilder.php +++ b/lib/private/DB/QueryBuilder/ExpressionBuilder/OCIExpressionBuilder.php @@ -156,6 +156,13 @@ class OCIExpressionBuilder extends ExpressionBuilder { /** * @inheritdoc */ + public function like($x, $y, $type = null) { + return parent::like($x, $y, $type) . " ESCAPE '\\'"; + } + + /** + * @inheritdoc + */ public function iLike($x, $y, $type = null) { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); diff --git a/lib/private/DB/QueryBuilder/ExpressionBuilder/SqliteExpressionBuilder.php b/lib/private/DB/QueryBuilder/ExpressionBuilder/SqliteExpressionBuilder.php new file mode 100644 index 00000000000..fa7c7fab6e5 --- /dev/null +++ b/lib/private/DB/QueryBuilder/ExpressionBuilder/SqliteExpressionBuilder.php @@ -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 '\\'"; + } +} diff --git a/lib/private/DB/QueryBuilder/QueryBuilder.php b/lib/private/DB/QueryBuilder/QueryBuilder.php index d5dd9cf0366..a434140806e 100644 --- a/lib/private/DB/QueryBuilder/QueryBuilder.php +++ b/lib/private/DB/QueryBuilder/QueryBuilder.php @@ -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); } diff --git a/tests/lib/DB/QueryBuilder/ExpressionBuilderTest.php b/tests/lib/DB/QueryBuilder/ExpressionBuilderTest.php index 4122f300c86..ff58b3f6e0b 100644 --- a/tests/lib/DB/QueryBuilder/ExpressionBuilderTest.php +++ b/tests/lib/DB/QueryBuilder/ExpressionBuilderTest.php @@ -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`')) |