From 00987feda1b9d4c4d19b41291275e00d5d519f9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Fri, 5 Jul 2013 14:05:42 +0200 Subject: [PATCH 1/2] fix insertIfNotExist return value, update doc and corresponding test --- lib/db.php | 7 ++----- tests/lib/db.php | 28 ++++++++++++++-------------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/lib/db.php b/lib/db.php index 4d6788f2bd..d5ef424876 100644 --- a/lib/db.php +++ b/lib/db.php @@ -702,7 +702,7 @@ class OC_DB { * @brief Insert a row if a matching row doesn't exists. * @param string $table. The table to insert into in the form '*PREFIX*tableName' * @param array $input. An array of fieldname/value pairs - * @returns The return value from PDOStatementWrapper->execute() + * @returns int number of updated rows */ public static function insertIfNotExist($table, $input) { self::connect(); @@ -736,7 +736,7 @@ class OC_DB { . implode('`,`', array_keys($input)) . '`) VALUES(' . str_repeat('?,', count($input)-1).'? ' . ')'; } else { - return true; + return 0; //no rows updated } } elseif( $type == 'pgsql' || $type == 'oci' || $type == 'mysql' || $type == 'mssql') { $query = 'INSERT INTO `' .$table . '` (`' @@ -757,9 +757,6 @@ class OC_DB { } catch(PDOException $e) { OC_Template::printExceptionErrorPage( $e ); } - if ($result === 0) { - return true; - } return $result; } diff --git a/tests/lib/db.php b/tests/lib/db.php index 0ba7d5d4b0..69e3542f92 100644 --- a/tests/lib/db.php +++ b/tests/lib/db.php @@ -40,7 +40,7 @@ class Test_DB extends PHPUnit_Framework_TestCase { $this->assertFalse((bool)$row); //PDO returns false, MDB2 returns null $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (?,?)'); $result = $query->execute(array('fullname test', 'uri_1')); - $this->assertEquals('1', $result); + $this->assertEquals(1, $result); $query = OC_DB::prepare('SELECT `fullname`,`uri` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?'); $result = $query->execute(array('uri_1')); $this->assertTrue((bool)$result); @@ -57,7 +57,7 @@ class Test_DB extends PHPUnit_Framework_TestCase { public function testNOW() { $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (NOW(),?)'); $result = $query->execute(array('uri_2')); - $this->assertEquals('1', $result); + $this->assertEquals(1, $result); $query = OC_DB::prepare('SELECT `fullname`,`uri` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?'); $result = $query->execute(array('uri_2')); $this->assertTrue((bool)$result); @@ -66,7 +66,7 @@ class Test_DB extends PHPUnit_Framework_TestCase { public function testUNIX_TIMESTAMP() { $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (UNIX_TIMESTAMP(),?)'); $result = $query->execute(array('uri_3')); - $this->assertEquals('1', $result); + $this->assertEquals(1, $result); $query = OC_DB::prepare('SELECT `fullname`,`uri` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?'); $result = $query->execute(array('uri_3')); $this->assertTrue((bool)$result); @@ -74,11 +74,11 @@ class Test_DB extends PHPUnit_Framework_TestCase { public function testinsertIfNotExist() { $categoryentries = array( - array('user' => 'test', 'type' => 'contact', 'category' => 'Family'), - array('user' => 'test', 'type' => 'contact', 'category' => 'Friends'), - array('user' => 'test', 'type' => 'contact', 'category' => 'Coworkers'), - array('user' => 'test', 'type' => 'contact', 'category' => 'Coworkers'), - array('user' => 'test', 'type' => 'contact', 'category' => 'School'), + 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) { @@ -88,13 +88,13 @@ class Test_DB extends PHPUnit_Framework_TestCase { 'type' => $entry['type'], 'category' => $entry['category'], )); - $this->assertTrue((bool)$result); + $this->assertEquals($entry['expectedResult'], $result); } $query = OC_DB::prepare('SELECT * FROM `*PREFIX*'.$this->table3.'`'); $result = $query->execute(); $this->assertTrue((bool)$result); - $this->assertEquals('4', $result->numRows()); + $this->assertEquals(4, $result->numRows()); } public function testinsertIfNotExistDontOverwrite() { @@ -105,14 +105,14 @@ class Test_DB extends PHPUnit_Framework_TestCase { // 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); + $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); $row = $result->fetchRow(); $this->assertArrayHasKey('carddata', $row); $this->assertEquals($carddata, $row['carddata']); - $this->assertEquals('1', $result->numRows()); + $this->assertEquals(1, $result->numRows()); // Try to insert a new row $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table2, @@ -120,7 +120,7 @@ class Test_DB extends PHPUnit_Framework_TestCase { 'fullname' => $fullname, 'uri' => $uri, )); - $this->assertTrue((bool)$result); + $this->assertEquals(0, $result); $query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?'); $result = $query->execute(array($uri)); @@ -130,7 +130,7 @@ class Test_DB extends PHPUnit_Framework_TestCase { // Test that previously inserted data isn't overwritten $this->assertEquals($carddata, $row['carddata']); // And that a new row hasn't been inserted. - $this->assertEquals('1', $result->numRows()); + $this->assertEquals(1, $result->numRows()); } } From 212ea0508d05f881d01df4f5b2ed38133bdff359 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Fri, 5 Jul 2013 15:39:57 +0200 Subject: [PATCH 2/2] update comment --- lib/db.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/db.php b/lib/db.php index d5ef424876..efea9e40a8 100644 --- a/lib/db.php +++ b/lib/db.php @@ -1047,7 +1047,7 @@ class PDOStatementWrapper{ } /** - * make execute return the result instead of a bool + * make execute return the result or updated row count instead of a bool */ public function execute($input=array()) { if(OC_Config::getValue( "log_query", false)) {