add support for escaping like parameters when using the query builder

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2017-01-16 15:12:30 +01:00
parent 83f3990e06
commit a65652fc1e
No known key found for this signature in database
GPG Key ID: 50F2B59C6DEBBCFE
4 changed files with 62 additions and 8 deletions

View 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
*/

View File

@ -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 '\\'";
}
}

View 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);
}

View 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`'))