Merge pull request #8786 from nextcloud/remove-insert-if-not-exists

Use insertIfNotExist of the new interface
This commit is contained in:
Morris Jobke 2018-03-12 18:44:16 +01:00 committed by GitHub
commit fac0a11576
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 143 additions and 154 deletions

View File

@ -491,12 +491,13 @@ class Tags implements \OCP\ITags {
$tags = $this->tags;
// For some reason this is needed or array_search(i) will return 0..?
ksort($tags);
$dbConnection = \OC::$server->getDatabaseConnection();
foreach(self::$relations as $relation) {
$tagId = $this->getTagId($relation['tag']);
\OCP\Util::writeLog('core', __METHOD__ . 'catid, ' . $relation['tag'] . ' ' . $tagId, \OCP\Util::DEBUG);
if($tagId) {
try {
\OCP\DB::insertIfNotExist(self::RELATION_TABLE,
$dbConnection->insertIfNotExist(self::RELATION_TABLE,
array(
'objid' => $relation['objid'],
'categoryid' => $tagId,
@ -679,7 +680,7 @@ class Tags implements \OCP\ITags {
$tagId = $tag;
}
try {
\OCP\DB::insertIfNotExist(self::RELATION_TABLE,
\OC::$server->getDatabaseConnection()->insertIfNotExist(self::RELATION_TABLE,
array(
'objid' => $objid,
'categoryid' => $tagId,

View File

@ -61,23 +61,6 @@ class DB {
return \OC_DB::prepare($query, $limit, $offset);
}
/**
* Insert a row if the matching row does not exists.
*
* @param string $table The table name (will replace *PREFIX* with the actual prefix)
* @param array $input data that should be inserted into the table (column name => value)
* @param array|null $compare List of values that should be checked for "if not exists"
* If this is null or an empty array, all keys of $input will be compared
* @return int number of inserted rows
* @throws \Doctrine\DBAL\DBALException
* @deprecated 8.1.0 use insertIfNotExist() of \OCP\IDBConnection - \OC::$server->getDatabaseConnection()
* @since 5.0.0 - parameter $compare was added in 8.1.0
*
*/
public static function insertIfNotExist($table, $input, array $compare = null) {
return \OC::$server->getDatabaseConnection()->insertIfNotExist($table, $input, $compare);
}
/**
* Start a transaction
* @deprecated 8.1.0 use beginTransaction() of \OCP\IDBConnection - \OC::$server->getDatabaseConnection()

View File

@ -198,4 +198,142 @@ class ConnectionTest extends \Test\TestCase {
$this->addToAssertionCount(1);
}
public function testInsertIfNotExist() {
$this->makeTestTable();
$categoryEntries = [
['user' => 'test', 'category' => 'Family', 'expectedResult' => 1],
['user' => 'test', 'category' => 'Friends', 'expectedResult' => 1],
['user' => 'test', 'category' => 'Coworkers', 'expectedResult' => 1],
['user' => 'test', 'category' => 'Coworkers', 'expectedResult' => 0],
['user' => 'test', 'category' => 'School', 'expectedResult' => 1],
['user' => 'test2', 'category' => 'Coworkers2', 'expectedResult' => 1],
['user' => 'test2', 'category' => 'Coworkers2', 'expectedResult' => 0],
['user' => 'test2', 'category' => 'School2', 'expectedResult' => 1],
['user' => 'test2', 'category' => 'Coworkers', 'expectedResult' => 1],
];
foreach($categoryEntries as $entry) {
$result = $this->connection->insertIfNotExist('*PREFIX*table',
[
'textfield' => $entry['user'],
'clobfield' => $entry['category'],
]);
$this->assertEquals($entry['expectedResult'], $result);
}
$query = $this->connection->prepare('SELECT * FROM `*PREFIX*table`');
$result = $query->execute();
$this->assertTrue((bool)$result);
$this->assertEquals(7, count($query->fetchAll()));
}
public function testInsertIfNotExistNull() {
$this->makeTestTable();
$categoryEntries = [
['addressbookid' => 123, 'fullname' => null, 'expectedResult' => 1],
['addressbookid' => 123, 'fullname' => null, 'expectedResult' => 0],
['addressbookid' => 123, 'fullname' => 'test', 'expectedResult' => 1],
];
foreach($categoryEntries as $entry) {
$result = $this->connection->insertIfNotExist('*PREFIX*table',
[
'integerfield_default' => $entry['addressbookid'],
'clobfield' => $entry['fullname'],
]);
$this->assertEquals($entry['expectedResult'], $result);
}
$query = $this->connection->prepare('SELECT * FROM `*PREFIX*table`');
$result = $query->execute();
$this->assertTrue((bool)$result);
$this->assertEquals(2, count($query->fetchAll()));
}
public function testInsertIfNotExistDonTOverwrite() {
$this->makeTestTable();
$fullName = 'fullname test';
$uri = 'uri_1';
// Normal test to have same known data inserted.
$query = $this->connection->prepare('INSERT INTO `*PREFIX*table` (`textfield`, `clobfield`) VALUES (?, ?)');
$result = $query->execute([$fullName, $uri]);
$this->assertEquals(1, $result);
$query = $this->connection->prepare('SELECT `textfield`, `clobfield` FROM `*PREFIX*table` WHERE `clobfield` = ?');
$result = $query->execute([$uri]);
$this->assertTrue($result);
$rowset = $query->fetchAll();
$this->assertEquals(1, count($rowset));
$this->assertArrayHasKey('textfield', $rowset[0]);
$this->assertEquals($fullName, $rowset[0]['textfield']);
// Try to insert a new row
$result = $this->connection->insertIfNotExist('*PREFIX*table',
[
'textfield' => $fullName,
'clobfield' => $uri,
]);
$this->assertEquals(0, $result);
$query = $this->connection->prepare('SELECT `textfield`, `clobfield` FROM `*PREFIX*table` WHERE `clobfield` = ?');
$result = $query->execute([$uri]);
$this->assertTrue($result);
// Test that previously inserted data isn't overwritten
// And that a new row hasn't been inserted.
$rowset = $query->fetchAll();
$this->assertEquals(1, count($rowset));
$this->assertArrayHasKey('textfield', $rowset[0]);
$this->assertEquals($fullName, $rowset[0]['textfield']);
}
public function testInsertIfNotExistsViolating() {
$this->makeTestTable();
$result = $this->connection->insertIfNotExist('*PREFIX*table',
[
'textfield' => md5('welcome.txt'),
'clobfield' => $this->getUniqueID()
]);
$this->assertEquals(1, $result);
$result = $this->connection->insertIfNotExist('*PREFIX*table',
[
'textfield' => md5('welcome.txt'),
'clobfield' => $this->getUniqueID()
],['textfield']);
$this->assertEquals(0, $result);
}
public function insertIfNotExistsViolatingThrows() {
return [
[null],
[['clobfield']],
];
}
/**
* @dataProvider insertIfNotExistsViolatingThrows
* @expectedException \Doctrine\DBAL\Exception\UniqueConstraintViolationException
*
* @param array $compareKeys
*/
public function testInsertIfNotExistsViolatingThrows($compareKeys) {
$this->makeTestTable();
$result = $this->connection->insertIfNotExist('*PREFIX*table',
[
'integerfield' => 1,
'clobfield' => $this->getUniqueID()
]);
$this->assertEquals(1, $result);
$result = $this->connection->insertIfNotExist('*PREFIX*table',
[
'integerfield' => 1,
'clobfield' => $this->getUniqueID()
], $compareKeys);
$this->assertEquals(0, $result);
}
}

View File

@ -135,140 +135,6 @@ class LegacyDBTest extends \Test\TestCase {
$this->assertGreaterThan($id1, $id2);
}
public function testinsertIfNotExist() {
$categoryEntries = array(
array('user' => 'test', 'type' => 'contact', 'category' => 'Family', 'expectedResult' => 1),
array('user' => 'test', 'type' => 'contact', 'category' => 'Friends', 'expectedResult' => 1),
array('user' => 'test', 'type' => 'contact', 'category' => 'Coworkers', 'expectedResult' => 1),
array('user' => 'test', 'type' => 'contact', 'category' => 'Coworkers', 'expectedResult' => 0),
array('user' => 'test', 'type' => 'contact', 'category' => 'School', 'expectedResult' => 1),
);
foreach($categoryEntries as $entry) {
$result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table3,
array(
'uid' => $entry['user'],
'type' => $entry['type'],
'category' => $entry['category'],
));
$this->assertEquals($entry['expectedResult'], $result);
}
$query = OC_DB::prepare('SELECT * FROM `*PREFIX*'.$this->table3.'`');
$result = $query->execute();
$this->assertTrue((bool)$result);
$this->assertEquals(4, count($result->fetchAll()));
}
public function testInsertIfNotExistNull() {
$categoryEntries = array(
array('addressbookid' => 123, 'fullname' => null, 'expectedResult' => 1),
array('addressbookid' => 123, 'fullname' => null, 'expectedResult' => 0),
array('addressbookid' => 123, 'fullname' => 'test', 'expectedResult' => 1),
);
foreach($categoryEntries as $entry) {
$result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table2,
array(
'addressbookid' => $entry['addressbookid'],
'fullname' => $entry['fullname'],
));
$this->assertEquals($entry['expectedResult'], $result);
}
$query = OC_DB::prepare('SELECT * FROM `*PREFIX*'.$this->table2.'`');
$result = $query->execute();
$this->assertTrue((bool)$result);
$this->assertEquals(2, count($result->fetchAll()));
}
public function testInsertIfNotExistDonTOverwrite() {
$fullName = 'fullname test';
$uri = 'uri_1';
$carddata = 'This is a vCard';
// Normal test to have same known data inserted.
$query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`, `uri`, `carddata`) VALUES (?, ?, ?)');
$result = $query->execute(array($fullName, $uri, $carddata));
$this->assertEquals(1, $result);
$query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?');
$result = $query->execute(array($uri));
$this->assertTrue((bool)$result);
$rowset = $result->fetchAll();
$this->assertEquals(1, count($rowset));
$this->assertArrayHasKey('carddata', $rowset[0]);
$this->assertEquals($carddata, $rowset[0]['carddata']);
// Try to insert a new row
$result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table2,
array(
'fullname' => $fullName,
'uri' => $uri,
));
$this->assertEquals(0, $result);
$query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?');
$result = $query->execute(array($uri));
$this->assertTrue((bool)$result);
// Test that previously inserted data isn't overwritten
// And that a new row hasn't been inserted.
$rowset = $result->fetchAll();
$this->assertEquals(1, count($rowset));
$this->assertArrayHasKey('carddata', $rowset[0]);
$this->assertEquals($carddata, $rowset[0]['carddata']);
}
public function testInsertIfNotExistsViolating() {
$result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table5,
array(
'storage' => 1,
'path_hash' => md5('welcome.txt'),
'etag' => $this->getUniqueID()
));
$this->assertEquals(1, $result);
$result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table5,
array(
'storage' => 1,
'path_hash' => md5('welcome.txt'),
'etag' => $this->getUniqueID()
),['storage', 'path_hash']);
$this->assertEquals(0, $result);
}
public function insertIfNotExistsViolatingThrows() {
return [
[null],
[['etag']],
];
}
/**
* @dataProvider insertIfNotExistsViolatingThrows
* @expectedException \Doctrine\DBAL\Exception\UniqueConstraintViolationException
*
* @param array $compareKeys
*/
public function testInsertIfNotExistsViolatingThrows($compareKeys) {
$result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table5,
array(
'storage' => 1,
'path_hash' => md5('welcome.txt'),
'etag' => $this->getUniqueID()
));
$this->assertEquals(1, $result);
$result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table5,
array(
'storage' => 1,
'path_hash' => md5('welcome.txt'),
'etag' => $this->getUniqueID()
), $compareKeys);
$this->assertEquals(0, $result);
}
public function testUtf8Data() {
$table = "*PREFIX*{$this->table2}";
$expected = "Ћö雙喜\xE2\x80\xA2";

View File

@ -54,7 +54,7 @@ class MDB2SchemaReaderTest extends TestCase {
$this->assertCount(8, $table->getColumns());
$this->assertEquals(4, $table->getColumn('integerfield')->getLength());
$this->assertFalse($table->getColumn('integerfield')->getAutoincrement());
$this->assertTrue($table->getColumn('integerfield')->getAutoincrement());
$this->assertEquals(0, $table->getColumn('integerfield')->getDefault());
$this->assertTrue($table->getColumn('integerfield')->getNotnull());
$this->assertInstanceOf('Doctrine\DBAL\Types\IntegerType', $table->getColumn('integerfield')->getType());

View File

@ -19,6 +19,7 @@
<notnull>true</notnull>
<primary>true</primary>
<length>4</length>
<autoincrement>1</autoincrement>
</field>
<field>
<name>integerfield_default</name>