/** * Insert a row if a matching row doesn't exists. * @param $table string The table name (will replace *PREFIX*) to perform the replace on. * @param $input array * * The input array if in the form: * * array ( 'id' => array ( 'value' => 6, * 'key' => true * ), * 'name' => array ('value' => 'Stoyan'), * 'family' => array ('value' => 'Stefanov'), * 'birth_date' => array ('value' => '1975-06-20') * ); * @return bool * */ public static function insertIfNotExist($table, $input) { return \OC_DB::insertIfNotExist($table, $input); }
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->assertTrue($result); $query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM *PREFIX*' . $this->table2 . ' WHERE `uri` = ?'); $result = $query->execute(array($uri)); $this->assertTrue($result); $row = $result->fetchRow(); $this->assertArrayHasKey('carddata', $row); $this->assertEqual($carddata, $row['carddata']); $this->assertEqual('1', $result->numRows()); // Try to insert a new row $result = OC_DB::insertIfNotExist('*PREFIX*' . $this->table2, array('fullname' => $fullname, 'uri' => $uri)); $this->assertTrue($result); $query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM *PREFIX*' . $this->table2 . ' WHERE `uri` = ?'); $result = $query->execute(array($uri)); $this->assertTrue($result); $row = $result->fetchRow(); $this->assertArrayHasKey('carddata', $row); // Test that previously inserted data isn't overwritten $this->assertEqual($carddata, $row['carddata']); // And that a new row hasn't been inserted. $this->assertEqual('1', $result->numRows()); }