diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2015-08-10 21:41:32 +0200 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2015-08-10 21:41:32 +0200 |
commit | bfb9a8e58e1a8d613a7eaf0c3422e079abfcacb9 (patch) | |
tree | 985841ed4957413e205a668e99071203325b505f /tests | |
parent | c2856c05aa9cbdc3adddea127a8588183647ee0a (diff) | |
parent | e6eb74958fd9779362fbde788cbfac982783abc0 (diff) | |
download | nextcloud-server-bfb9a8e58e1a8d613a7eaf0c3422e079abfcacb9.tar.gz nextcloud-server-bfb9a8e58e1a8d613a7eaf0c3422e079abfcacb9.zip |
Merge pull request #18175 from owncloud/automatic-db-prefix-query-builder
Automatic db prefix query builder
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/db/querybuilder/querybuildertest.php | 98 | ||||
-rw-r--r-- | tests/lib/repair/cleantags.php | 34 | ||||
-rw-r--r-- | tests/lib/share/share.php | 4 |
3 files changed, 83 insertions, 53 deletions
diff --git a/tests/lib/db/querybuilder/querybuildertest.php b/tests/lib/db/querybuilder/querybuildertest.php index 02e516b7386..75e62ba944e 100644 --- a/tests/lib/db/querybuilder/querybuildertest.php +++ b/tests/lib/db/querybuilder/querybuildertest.php @@ -253,8 +253,8 @@ class QueryBuilderTest extends \Test\TestCase { public function dataDelete() { return [ - ['data', null, ['table' => '`data`', 'alias' => null], '`data`'], - ['data', 't', ['table' => '`data`', 'alias' => 't'], '`data` t'], + ['data', null, ['table' => '`*PREFIX*data`', 'alias' => null], '`*PREFIX*data`'], + ['data', 't', ['table' => '`*PREFIX*data`', 'alias' => 't'], '`*PREFIX*data` t'], ]; } @@ -282,8 +282,8 @@ class QueryBuilderTest extends \Test\TestCase { public function dataUpdate() { return [ - ['data', null, ['table' => '`data`', 'alias' => null], '`data`'], - ['data', 't', ['table' => '`data`', 'alias' => 't'], '`data` t'], + ['data', null, ['table' => '`*PREFIX*data`', 'alias' => null], '`*PREFIX*data`'], + ['data', 't', ['table' => '`*PREFIX*data`', 'alias' => 't'], '`*PREFIX*data` t'], ]; } @@ -311,7 +311,7 @@ class QueryBuilderTest extends \Test\TestCase { public function dataInsert() { return [ - ['data', ['table' => '`data`'], '`data`'], + ['data', ['table' => '`*PREFIX*data`'], '`*PREFIX*data`'], ]; } @@ -338,16 +338,16 @@ class QueryBuilderTest extends \Test\TestCase { public function dataFrom() { return [ - ['data', null, null, null, [['table' => '`data`', 'alias' => null]], '`data`'], - ['data', 't', null, null, [['table' => '`data`', 'alias' => 't']], '`data` t'], + ['data', null, null, null, [['table' => '`*PREFIX*data`', 'alias' => null]], '`*PREFIX*data`'], + ['data', 't', null, null, [['table' => '`*PREFIX*data`', 'alias' => 't']], '`*PREFIX*data` t'], ['data1', null, 'data2', null, [ - ['table' => '`data1`', 'alias' => null], - ['table' => '`data2`', 'alias' => null] - ], '`data1`, `data2`'], + ['table' => '`*PREFIX*data1`', 'alias' => null], + ['table' => '`*PREFIX*data2`', 'alias' => null] + ], '`*PREFIX*data1`, `*PREFIX*data2`'], ['data', 't1', 'data', 't2', [ - ['table' => '`data`', 'alias' => 't1'], - ['table' => '`data`', 'alias' => 't2'] - ], '`data` t1, `data` t2'], + ['table' => '`*PREFIX*data`', 'alias' => 't1'], + ['table' => '`*PREFIX*data`', 'alias' => 't2'] + ], '`*PREFIX*data` t1, `*PREFIX*data` t2'], ]; } @@ -382,18 +382,18 @@ class QueryBuilderTest extends \Test\TestCase { return [ [ 'd1', 'data2', null, null, - ['d1' => [['joinType' => 'inner', 'joinTable' => '`data2`', 'joinAlias' => null, 'joinCondition' => null]]], - '`data1` d1 INNER JOIN `data2` ON ' + ['d1' => [['joinType' => 'inner', 'joinTable' => '`*PREFIX*data2`', 'joinAlias' => null, 'joinCondition' => null]]], + '`*PREFIX*data1` d1 INNER JOIN `*PREFIX*data2` ON ' ], [ 'd1', 'data2', 'd2', null, - ['d1' => [['joinType' => 'inner', 'joinTable' => '`data2`', 'joinAlias' => 'd2', 'joinCondition' => null]]], - '`data1` d1 INNER JOIN `data2` d2 ON ' + ['d1' => [['joinType' => 'inner', 'joinTable' => '`*PREFIX*data2`', 'joinAlias' => 'd2', 'joinCondition' => null]]], + '`*PREFIX*data1` d1 INNER JOIN `*PREFIX*data2` d2 ON ' ], [ 'd1', 'data2', 'd2', 'd1.`field1` = d2.`field2`', - ['d1' => [['joinType' => 'inner', 'joinTable' => '`data2`', 'joinAlias' => 'd2', 'joinCondition' => 'd1.`field1` = d2.`field2`']]], - '`data1` d1 INNER JOIN `data2` d2 ON d1.`field1` = d2.`field2`' + ['d1' => [['joinType' => 'inner', 'joinTable' => '`*PREFIX*data2`', 'joinAlias' => 'd2', 'joinCondition' => 'd1.`field1` = d2.`field2`']]], + '`*PREFIX*data1` d1 INNER JOIN `*PREFIX*data2` d2 ON d1.`field1` = d2.`field2`' ], ]; @@ -463,18 +463,18 @@ class QueryBuilderTest extends \Test\TestCase { return [ [ 'd1', 'data2', null, null, - ['d1' => [['joinType' => 'left', 'joinTable' => '`data2`', 'joinAlias' => null, 'joinCondition' => null]]], - '`data1` d1 LEFT JOIN `data2` ON ' + ['d1' => [['joinType' => 'left', 'joinTable' => '`*PREFIX*data2`', 'joinAlias' => null, 'joinCondition' => null]]], + '`*PREFIX*data1` d1 LEFT JOIN `*PREFIX*data2` ON ' ], [ 'd1', 'data2', 'd2', null, - ['d1' => [['joinType' => 'left', 'joinTable' => '`data2`', 'joinAlias' => 'd2', 'joinCondition' => null]]], - '`data1` d1 LEFT JOIN `data2` d2 ON ' + ['d1' => [['joinType' => 'left', 'joinTable' => '`*PREFIX*data2`', 'joinAlias' => 'd2', 'joinCondition' => null]]], + '`*PREFIX*data1` d1 LEFT JOIN `*PREFIX*data2` d2 ON ' ], [ 'd1', 'data2', 'd2', 'd1.`field1` = d2.`field2`', - ['d1' => [['joinType' => 'left', 'joinTable' => '`data2`', 'joinAlias' => 'd2', 'joinCondition' => 'd1.`field1` = d2.`field2`']]], - '`data1` d1 LEFT JOIN `data2` d2 ON d1.`field1` = d2.`field2`' + ['d1' => [['joinType' => 'left', 'joinTable' => '`*PREFIX*data2`', 'joinAlias' => 'd2', 'joinCondition' => 'd1.`field1` = d2.`field2`']]], + '`*PREFIX*data1` d1 LEFT JOIN `*PREFIX*data2` d2 ON d1.`field1` = d2.`field2`' ], ]; } @@ -513,18 +513,18 @@ class QueryBuilderTest extends \Test\TestCase { return [ [ 'd1', 'data2', null, null, - ['d1' => [['joinType' => 'right', 'joinTable' => '`data2`', 'joinAlias' => null, 'joinCondition' => null]]], - '`data1` d1 RIGHT JOIN `data2` ON ' + ['d1' => [['joinType' => 'right', 'joinTable' => '`*PREFIX*data2`', 'joinAlias' => null, 'joinCondition' => null]]], + '`*PREFIX*data1` d1 RIGHT JOIN `*PREFIX*data2` ON ' ], [ 'd1', 'data2', 'd2', null, - ['d1' => [['joinType' => 'right', 'joinTable' => '`data2`', 'joinAlias' => 'd2', 'joinCondition' => null]]], - '`data1` d1 RIGHT JOIN `data2` d2 ON ' + ['d1' => [['joinType' => 'right', 'joinTable' => '`*PREFIX*data2`', 'joinAlias' => 'd2', 'joinCondition' => null]]], + '`*PREFIX*data1` d1 RIGHT JOIN `*PREFIX*data2` d2 ON ' ], [ 'd1', 'data2', 'd2', 'd1.`field1` = d2.`field2`', - ['d1' => [['joinType' => 'right', 'joinTable' => '`data2`', 'joinAlias' => 'd2', 'joinCondition' => 'd1.`field1` = d2.`field2`']]], - '`data1` d1 RIGHT JOIN `data2` d2 ON d1.`field1` = d2.`field2`' + ['d1' => [['joinType' => 'right', 'joinTable' => '`*PREFIX*data2`', 'joinAlias' => 'd2', 'joinCondition' => 'd1.`field1` = d2.`field2`']]], + '`*PREFIX*data1` d1 RIGHT JOIN `*PREFIX*data2` d2 ON d1.`field1` = d2.`field2`' ], ]; } @@ -591,7 +591,7 @@ class QueryBuilderTest extends \Test\TestCase { ); $this->assertSame( - 'UPDATE `data` SET ' . $expectedQuery, + 'UPDATE `*PREFIX*data` SET ' . $expectedQuery, $this->queryBuilder->getSQL() ); } @@ -774,7 +774,7 @@ class QueryBuilderTest extends \Test\TestCase { ); $this->assertSame( - 'INSERT INTO `data` ' . $expectedQuery, + 'INSERT INTO `*PREFIX*data` ' . $expectedQuery, $this->queryBuilder->getSQL() ); } @@ -799,7 +799,7 @@ class QueryBuilderTest extends \Test\TestCase { ); $this->assertSame( - 'INSERT INTO `data` ' . $expectedQuery, + 'INSERT INTO `*PREFIX*data` ' . $expectedQuery, $this->queryBuilder->getSQL() ); } @@ -996,4 +996,34 @@ class QueryBuilderTest extends \Test\TestCase { $this->queryBuilder->getSQL() ); } + + public function dataGetTableName() { + return [ + ['*PREFIX*table', null, '`*PREFIX*table`'], + ['*PREFIX*table', true, '`*PREFIX*table`'], + ['*PREFIX*table', false, '`*PREFIX*table`'], + + ['table', null, '`*PREFIX*table`'], + ['table', true, '`*PREFIX*table`'], + ['table', false, '`table`'], + ]; + } + + /** + * @dataProvider dataGetTableName + * + * @param string $tableName + * @param bool $automatic + * @param string $expected + */ + public function testGetTableName($tableName, $automatic, $expected) { + if ($automatic !== null) { + $this->queryBuilder->automaticTablePrefix($automatic); + } + + $this->assertSame( + $expected, + $this->invokePrivate($this->queryBuilder, 'getTableName', [$tableName]) + ); + } } diff --git a/tests/lib/repair/cleantags.php b/tests/lib/repair/cleantags.php index 2f6d0879642..a511daa03d6 100644 --- a/tests/lib/repair/cleantags.php +++ b/tests/lib/repair/cleantags.php @@ -40,13 +40,13 @@ class CleanTags extends \Test\TestCase { protected function cleanUpTables() { $qb = $this->connection->getQueryBuilder(); - $qb->delete('*PREFIX*vcategory') + $qb->delete('vcategory') ->execute(); - $qb->delete('*PREFIX*vcategory_to_object') + $qb->delete('vcategory_to_object') ->execute(); - $qb->delete('*PREFIX*filecache') + $qb->delete('filecache') ->execute(); } @@ -61,20 +61,20 @@ class CleanTags extends \Test\TestCase { $this->addTagEntry(9999999, $cat3, 'contacts'); // Retained $this->addTagEntry($this->getFileID(), $cat3 + 1, 'files'); // Deleted: Category is NULL - $this->assertEntryCount('*PREFIX*vcategory_to_object', 4, 'Assert tag entries count before repair step'); - $this->assertEntryCount('*PREFIX*vcategory', 4, 'Assert tag categories count before repair step'); + $this->assertEntryCount('vcategory_to_object', 4, 'Assert tag entries count before repair step'); + $this->assertEntryCount('vcategory', 4, 'Assert tag categories count before repair step'); self::invokePrivate($this->repair, 'deleteOrphanFileEntries'); - $this->assertEntryCount('*PREFIX*vcategory_to_object', 3, 'Assert tag entries count after cleaning file entries'); - $this->assertEntryCount('*PREFIX*vcategory', 4, 'Assert tag categories count after cleaning file entries'); + $this->assertEntryCount('vcategory_to_object', 3, 'Assert tag entries count after cleaning file entries'); + $this->assertEntryCount('vcategory', 4, 'Assert tag categories count after cleaning file entries'); self::invokePrivate($this->repair, 'deleteOrphanTagEntries'); - $this->assertEntryCount('*PREFIX*vcategory_to_object', 2, 'Assert tag entries count after cleaning tag entries'); - $this->assertEntryCount('*PREFIX*vcategory', 4, 'Assert tag categories count after cleaning tag entries'); + $this->assertEntryCount('vcategory_to_object', 2, 'Assert tag entries count after cleaning tag entries'); + $this->assertEntryCount('vcategory', 4, 'Assert tag categories count after cleaning tag entries'); self::invokePrivate($this->repair, 'deleteOrphanCategoryEntries'); - $this->assertEntryCount('*PREFIX*vcategory_to_object', 2, 'Assert tag entries count after cleaning category entries'); - $this->assertEntryCount('*PREFIX*vcategory', 2, 'Assert tag categories count after cleaning category entries'); + $this->assertEntryCount('vcategory_to_object', 2, 'Assert tag entries count after cleaning category entries'); + $this->assertEntryCount('vcategory', 2, 'Assert tag categories count after cleaning category entries'); } /** @@ -100,7 +100,7 @@ class CleanTags extends \Test\TestCase { */ protected function addTagCategory($category, $type) { $qb = $this->connection->getQueryBuilder(); - $qb->insert('*PREFIX*vcategory') + $qb->insert('vcategory') ->values([ 'uid' => $qb->createNamedParameter('TestRepairCleanTags'), 'category' => $qb->createNamedParameter($category), @@ -108,7 +108,7 @@ class CleanTags extends \Test\TestCase { ]) ->execute(); - return (int) $this->getLastInsertID('*PREFIX*vcategory', 'id'); + return (int) $this->getLastInsertID('vcategory', 'id'); } /** @@ -119,7 +119,7 @@ class CleanTags extends \Test\TestCase { */ protected function addTagEntry($objectId, $category, $type) { $qb = $this->connection->getQueryBuilder(); - $qb->insert('*PREFIX*vcategory_to_object') + $qb->insert('vcategory_to_object') ->values([ 'objid' => $qb->createNamedParameter($objectId, \PDO::PARAM_INT), 'categoryid' => $qb->createNamedParameter($category, \PDO::PARAM_INT), @@ -141,21 +141,21 @@ class CleanTags extends \Test\TestCase { // We create a new file entry and delete it after the test again $fileName = $this->getUniqueID('TestRepairCleanTags', 12); - $qb->insert('*PREFIX*filecache') + $qb->insert('filecache') ->values([ 'path' => $qb->createNamedParameter($fileName), 'path_hash' => $qb->createNamedParameter(md5($fileName)), ]) ->execute(); $fileName = $this->getUniqueID('TestRepairCleanTags', 12); - $qb->insert('*PREFIX*filecache') + $qb->insert('filecache') ->values([ 'path' => $qb->createNamedParameter($fileName), 'path_hash' => $qb->createNamedParameter(md5($fileName)), ]) ->execute(); - $this->createdFile = (int) $this->getLastInsertID('*PREFIX*filecache', 'fileid'); + $this->createdFile = (int) $this->getLastInsertID('filecache', 'fileid'); return $this->createdFile; } diff --git a/tests/lib/share/share.php b/tests/lib/share/share.php index b6d3e16826d..ef0d9822085 100644 --- a/tests/lib/share/share.php +++ b/tests/lib/share/share.php @@ -1288,7 +1288,7 @@ class Test_Share extends \Test\TestCase { // Find the share ID in the db $qb = $connection->getQueryBuilder(); $qb->select('id') - ->from('*PREFIX*share') + ->from('share') ->where($qb->expr()->eq('item_type', $qb->createParameter('type'))) ->andWhere($qb->expr()->eq('item_source', $qb->createParameter('source'))) ->andWhere($qb->expr()->eq('uid_owner', $qb->createParameter('owner'))) @@ -1309,7 +1309,7 @@ class Test_Share extends \Test\TestCase { // Fetch the hash from the database $qb = $connection->getQueryBuilder(); $qb->select('share_with') - ->from('*PREFIX*share') + ->from('share') ->where($qb->expr()->eq('id', $qb->createParameter('id'))) ->setParameter('id', $id); $hash = $qb->execute()->fetch()['share_with']; |