Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/private/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,13 @@ protected function save() {
$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,
Expand Down Expand Up @@ -679,7 +680,7 @@ public function tagAs($objid, $tag) {
$tagId = $tag;
}
try {
\OCP\DB::insertIfNotExist(self::RELATION_TABLE,
\OC::$server->getDatabaseConnection()->insertIfNotExist(self::RELATION_TABLE,
array(
'objid' => $objid,
'categoryid' => $tagId,
Expand Down
17 changes: 0 additions & 17 deletions lib/public/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,6 @@ static public function prepare( $query, $limit=null, $offset=null ) {
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()
Expand Down
138 changes: 138 additions & 0 deletions tests/lib/DB/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,142 @@ public function testSetValuesSameNoError() {

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

}
134 changes: 0 additions & 134 deletions tests/lib/DB/LegacyDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,140 +134,6 @@ public function testLastInsertId() {
// now we can check if the two ids are in correct order
$this->assertGreaterThan($id1, $id2);
}

public function testinsertIfNotExist() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there tests for the connection version of it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered if IDBConnection::insertIfNotExist() has tests

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find one ... let me then migrate those tests over to the IDBConnection.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migrated them to the new class.

$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}";
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/DB/MDB2SchemaReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function testRead() {
$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());
Expand Down
1 change: 1 addition & 0 deletions tests/lib/DB/testschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<notnull>true</notnull>
<primary>true</primary>
<length>4</length>
<autoincrement>1</autoincrement>
</field>
<field>
<name>integerfield_default</name>
Expand Down