Browse Source
Merge pull request #3089 from nextcloud/cache-move-single-query
Merge pull request #3089 from nextcloud/cache-move-single-query
Move all children of a folder in a single querypull/4166/head
committed by
GitHub
18 changed files with 542 additions and 28 deletions
-
6lib/composer/composer/autoload_classmap.php
-
6lib/composer/composer/autoload_static.php
-
1lib/private/DB/AdapterOCI8.php
-
1lib/private/DB/AdapterSqlite.php
-
7lib/private/DB/QueryBuilder/ExpressionBuilder/OCIExpressionBuilder.php
-
38lib/private/DB/QueryBuilder/ExpressionBuilder/SqliteExpressionBuilder.php
-
56lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php
-
30lib/private/DB/QueryBuilder/FunctionBuilder/OCIFunctionBuilder.php
-
30lib/private/DB/QueryBuilder/FunctionBuilder/PgSqlFunctionBuilder.php
-
30lib/private/DB/QueryBuilder/FunctionBuilder/SqliteFunctionBuilder.php
-
36lib/private/DB/QueryBuilder/QueryBuilder.php
-
3lib/private/DB/SQLiteSessionInit.php
-
45lib/private/Files/Cache/Cache.php
-
62lib/public/DB/QueryBuilder/IFunctionBuilder.php
-
19lib/public/DB/QueryBuilder/IQueryBuilder.php
-
106tests/lib/DB/QueryBuilder/ExpressionBuilderDBTest.php
-
20tests/lib/DB/QueryBuilder/ExpressionBuilderTest.php
-
74tests/lib/DB/QueryBuilder/FunctionBuilderTest.php
@ -0,0 +1,38 @@ |
|||||
|
<?php |
||||
|
/** |
||||
|
* @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> |
||||
|
* |
||||
|
* @license GNU AGPL version 3 or any later version |
||||
|
* |
||||
|
* This program is free software: you can redistribute it and/or modify |
||||
|
* it under the terms of the GNU Affero General Public License as |
||||
|
* published by the Free Software Foundation, either version 3 of the |
||||
|
* License, or (at your option) any later version. |
||||
|
* |
||||
|
* 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 |
||||
|
* 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 '\\'"; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,56 @@ |
|||||
|
<?php |
||||
|
/** |
||||
|
* @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> |
||||
|
* |
||||
|
* @license GNU AGPL version 3 or any later version |
||||
|
* |
||||
|
* This program is free software: you can redistribute it and/or modify |
||||
|
* it under the terms of the GNU Affero General Public License as |
||||
|
* published by the Free Software Foundation, either version 3 of the |
||||
|
* License, or (at your option) any later version. |
||||
|
* |
||||
|
* 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 |
||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
|
* |
||||
|
*/ |
||||
|
|
||||
|
namespace OC\DB\QueryBuilder\FunctionBuilder; |
||||
|
|
||||
|
use OC\DB\QueryBuilder\QueryFunction; |
||||
|
use OC\DB\QueryBuilder\QuoteHelper; |
||||
|
use OCP\DB\QueryBuilder\IFunctionBuilder; |
||||
|
|
||||
|
class FunctionBuilder implements IFunctionBuilder { |
||||
|
/** @var QuoteHelper */ |
||||
|
protected $helper; |
||||
|
|
||||
|
/** |
||||
|
* ExpressionBuilder constructor. |
||||
|
* |
||||
|
* @param QuoteHelper $helper |
||||
|
*/ |
||||
|
public function __construct(QuoteHelper $helper) { |
||||
|
$this->helper = $helper; |
||||
|
} |
||||
|
|
||||
|
public function md5($input) { |
||||
|
return new QueryFunction('MD5(' . $this->helper->quoteColumnName($input) . ')'); |
||||
|
} |
||||
|
|
||||
|
public function concat($x, $y) { |
||||
|
return new QueryFunction('CONCAT(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')'); |
||||
|
} |
||||
|
|
||||
|
public function substring($input, $start, $length = null) { |
||||
|
if ($length) { |
||||
|
return new QueryFunction('SUBSTR(' . $this->helper->quoteColumnName($input) . ', ' . $this->helper->quoteColumnName($start) . ', ' . $this->helper->quoteColumnName($length) . ')'); |
||||
|
} else { |
||||
|
return new QueryFunction('SUBSTR(' . $this->helper->quoteColumnName($input) . ', ' . $this->helper->quoteColumnName($start) . ')'); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,30 @@ |
|||||
|
<?php |
||||
|
/** |
||||
|
* @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> |
||||
|
* |
||||
|
* @license GNU AGPL version 3 or any later version |
||||
|
* |
||||
|
* This program is free software: you can redistribute it and/or modify |
||||
|
* it under the terms of the GNU Affero General Public License as |
||||
|
* published by the Free Software Foundation, either version 3 of the |
||||
|
* License, or (at your option) any later version. |
||||
|
* |
||||
|
* 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 |
||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
|
* |
||||
|
*/ |
||||
|
|
||||
|
namespace OC\DB\QueryBuilder\FunctionBuilder; |
||||
|
|
||||
|
use OC\DB\QueryBuilder\QueryFunction; |
||||
|
|
||||
|
class OCIFunctionBuilder extends FunctionBuilder { |
||||
|
public function md5($input) { |
||||
|
return new QueryFunction('LOWER(DBMS_OBFUSCATION_TOOLKIT.md5 (input => UTL_RAW.cast_to_raw(' . $this->helper->quoteColumnName($input) .')))'); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,30 @@ |
|||||
|
<?php |
||||
|
/** |
||||
|
* @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> |
||||
|
* |
||||
|
* @license GNU AGPL version 3 or any later version |
||||
|
* |
||||
|
* This program is free software: you can redistribute it and/or modify |
||||
|
* it under the terms of the GNU Affero General Public License as |
||||
|
* published by the Free Software Foundation, either version 3 of the |
||||
|
* License, or (at your option) any later version. |
||||
|
* |
||||
|
* 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 |
||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
|
* |
||||
|
*/ |
||||
|
|
||||
|
namespace OC\DB\QueryBuilder\FunctionBuilder; |
||||
|
|
||||
|
use OC\DB\QueryBuilder\QueryFunction; |
||||
|
|
||||
|
class PgSqlFunctionBuilder extends FunctionBuilder { |
||||
|
public function concat($x, $y) { |
||||
|
return new QueryFunction($this->helper->quoteColumnName($x) . ' || ' . $this->helper->quoteColumnName($y)); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,30 @@ |
|||||
|
<?php |
||||
|
/** |
||||
|
* @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> |
||||
|
* |
||||
|
* @license GNU AGPL version 3 or any later version |
||||
|
* |
||||
|
* This program is free software: you can redistribute it and/or modify |
||||
|
* it under the terms of the GNU Affero General Public License as |
||||
|
* published by the Free Software Foundation, either version 3 of the |
||||
|
* License, or (at your option) any later version. |
||||
|
* |
||||
|
* 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 |
||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
|
* |
||||
|
*/ |
||||
|
|
||||
|
namespace OC\DB\QueryBuilder\FunctionBuilder; |
||||
|
|
||||
|
use OC\DB\QueryBuilder\QueryFunction; |
||||
|
|
||||
|
class SqliteFunctionBuilder extends FunctionBuilder { |
||||
|
public function concat($x, $y) { |
||||
|
return new QueryFunction($this->helper->quoteColumnName($x) . ' || ' . $this->helper->quoteColumnName($y)); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,62 @@ |
|||||
|
<?php |
||||
|
/** |
||||
|
* @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> |
||||
|
* |
||||
|
* @license GNU AGPL version 3 or any later version |
||||
|
* |
||||
|
* This program is free software: you can redistribute it and/or modify |
||||
|
* it under the terms of the GNU Affero General Public License as |
||||
|
* published by the Free Software Foundation, either version 3 of the |
||||
|
* License, or (at your option) any later version. |
||||
|
* |
||||
|
* 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 |
||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
|
* |
||||
|
*/ |
||||
|
|
||||
|
namespace OCP\DB\QueryBuilder; |
||||
|
|
||||
|
/** |
||||
|
* This class provides a builder for sql some functions |
||||
|
* |
||||
|
* @since 12.0.0 |
||||
|
*/ |
||||
|
interface IFunctionBuilder { |
||||
|
/** |
||||
|
* Calculates the MD5 hash of a given input |
||||
|
* |
||||
|
* @param mixed $input The input to be hashed |
||||
|
* |
||||
|
* @return IQueryFunction |
||||
|
* @since 12.0.0 |
||||
|
*/ |
||||
|
public function md5($input); |
||||
|
|
||||
|
/** |
||||
|
* Combines two input strings |
||||
|
* |
||||
|
* @param mixed $x The first input string |
||||
|
* @param mixed $y The seccond input string |
||||
|
* |
||||
|
* @return IQueryFunction |
||||
|
* @since 12.0.0 |
||||
|
*/ |
||||
|
public function concat($x, $y); |
||||
|
|
||||
|
/** |
||||
|
* Takes a substring from the input string |
||||
|
* |
||||
|
* @param mixed $input The input string |
||||
|
* @param mixed $start The start of the substring, note that counting starts at 1 |
||||
|
* @param mixed $length The length of the substring |
||||
|
* |
||||
|
* @return IQueryFunction |
||||
|
* @since 12.0.0 |
||||
|
*/ |
||||
|
public function substring($input, $start, $length = null); |
||||
|
} |
||||
@ -0,0 +1,106 @@ |
|||||
|
<?php |
||||
|
/** |
||||
|
* @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> |
||||
|
* |
||||
|
* @license GNU AGPL version 3 or any later version |
||||
|
* |
||||
|
* This program is free software: you can redistribute it and/or modify |
||||
|
* it under the terms of the GNU Affero General Public License as |
||||
|
* published by the Free Software Foundation, either version 3 of the |
||||
|
* License, or (at your option) any later version. |
||||
|
* |
||||
|
* 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 |
||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
|
* |
||||
|
*/ |
||||
|
|
||||
|
namespace Test\DB\QueryBuilder; |
||||
|
|
||||
|
use OC\DB\QueryBuilder\Literal; |
||||
|
use Test\TestCase; |
||||
|
|
||||
|
/** |
||||
|
* @group DB |
||||
|
*/ |
||||
|
class ExpressionBuilderDBTest extends TestCase { |
||||
|
/** @var \Doctrine\DBAL\Connection|\OCP\IDBConnection */ |
||||
|
protected $connection; |
||||
|
|
||||
|
protected function setUp() { |
||||
|
parent::setUp(); |
||||
|
|
||||
|
$this->connection = \OC::$server->getDatabaseConnection(); |
||||
|
} |
||||
|
|
||||
|
public function likeProvider() { |
||||
|
$connection = \OC::$server->getDatabaseConnection(); |
||||
|
|
||||
|
return [ |
||||
|
['foo', 'bar', false], |
||||
|
['foo', 'foo', true], |
||||
|
['foo', 'f%', true], |
||||
|
['foo', '%o', true], |
||||
|
['foo', '%', true], |
||||
|
['foo', 'fo_', true], |
||||
|
['foo', 'foo_', false], |
||||
|
['foo', $connection->escapeLikeParameter('fo_'), false], |
||||
|
['foo', $connection->escapeLikeParameter('f%'), false], |
||||
|
]; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @dataProvider likeProvider |
||||
|
* |
||||
|
* @param string $param1 |
||||
|
* @param string $param2 |
||||
|
* @param boolean $match |
||||
|
*/ |
||||
|
public function testLike($param1, $param2, $match) { |
||||
|
$query = $this->connection->getQueryBuilder(); |
||||
|
|
||||
|
$query->select(new Literal('1')) |
||||
|
->from('users') |
||||
|
->where($query->expr()->like($query->createNamedParameter($param1), $query->createNamedParameter($param2))); |
||||
|
|
||||
|
$this->assertEquals($match, $query->execute()->fetchColumn()); |
||||
|
} |
||||
|
|
||||
|
public function ilikeProvider() { |
||||
|
$connection = \OC::$server->getDatabaseConnection(); |
||||
|
|
||||
|
return [ |
||||
|
['foo', 'bar', false], |
||||
|
['foo', 'foo', true], |
||||
|
['foo', 'Foo', true], |
||||
|
['foo', 'f%', true], |
||||
|
['foo', '%o', true], |
||||
|
['foo', '%', true], |
||||
|
['foo', 'fo_', true], |
||||
|
['foo', 'foo_', false], |
||||
|
['foo', $connection->escapeLikeParameter('fo_'), false], |
||||
|
['foo', $connection->escapeLikeParameter('f%'), false], |
||||
|
]; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @dataProvider ilikeProvider |
||||
|
* |
||||
|
* @param string $param1 |
||||
|
* @param string $param2 |
||||
|
* @param boolean $match |
||||
|
*/ |
||||
|
public function testILike($param1, $param2, $match) { |
||||
|
$query = $this->connection->getQueryBuilder(); |
||||
|
|
||||
|
$query->select(new Literal('1')) |
||||
|
->from('users') |
||||
|
->where($query->expr()->iLike($query->createNamedParameter($param1), $query->createNamedParameter($param2))); |
||||
|
|
||||
|
$this->assertEquals($match, $query->execute()->fetchColumn()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,74 @@ |
|||||
|
<?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 Test\DB\QueryBuilder; |
||||
|
|
||||
|
use OC\DB\QueryBuilder\Literal; |
||||
|
use Test\TestCase; |
||||
|
|
||||
|
/** |
||||
|
* Class FunctionBuilderTest |
||||
|
* |
||||
|
* @group DB |
||||
|
* |
||||
|
* @package Test\DB\QueryBuilder |
||||
|
*/ |
||||
|
class FunctionBuilderTest extends TestCase { |
||||
|
/** @var \Doctrine\DBAL\Connection|\OCP\IDBConnection */ |
||||
|
protected $connection; |
||||
|
|
||||
|
protected function setUp() { |
||||
|
parent::setUp(); |
||||
|
|
||||
|
$this->connection = \OC::$server->getDatabaseConnection(); |
||||
|
} |
||||
|
|
||||
|
public function testConcat() { |
||||
|
$query = $this->connection->getQueryBuilder(); |
||||
|
|
||||
|
$query->select($query->func()->concat($query->createNamedParameter('foo'), new Literal("'bar'"))); |
||||
|
|
||||
|
$this->assertEquals('foobar', $query->execute()->fetchColumn()); |
||||
|
} |
||||
|
|
||||
|
public function testMd5() { |
||||
|
$query = $this->connection->getQueryBuilder(); |
||||
|
|
||||
|
$query->select($query->func()->md5($query->createNamedParameter('foobar'))); |
||||
|
|
||||
|
$this->assertEquals(md5('foobar'), $query->execute()->fetchColumn()); |
||||
|
} |
||||
|
|
||||
|
public function testSubstring() { |
||||
|
$query = $this->connection->getQueryBuilder(); |
||||
|
|
||||
|
$query->select($query->func()->substring($query->createNamedParameter('foobar'), new Literal(2), $query->createNamedParameter(2))); |
||||
|
|
||||
|
$this->assertEquals('oo', $query->execute()->fetchColumn()); |
||||
|
} |
||||
|
|
||||
|
public function testSubstringNoLength() { |
||||
|
$query = $this->connection->getQueryBuilder(); |
||||
|
|
||||
|
$query->select($query->func()->substring($query->createNamedParameter('foobar'), new Literal(2))); |
||||
|
|
||||
|
$this->assertEquals('oobar', $query->execute()->fetchColumn()); |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue