* @since 1.22
*/
public function upsert(
- $table, array $rows, array $uniqueIndexes, array $set, $fname = 'DatabaseBase::upsert'
+ $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__
) {
if ( !count( $rows ) ) {
return true; // nothing to do
$useTrx = !$this->mTrxLevel;
if ( $useTrx ) {
- $this->begin();
+ $this->begin( $fname );
}
try {
# Update any existing conflicting row(s)
$ok = $this->insert( $table, $rows, $fname, array( 'IGNORE' ) ) && $ok;
} catch ( Exception $e ) {
if ( $useTrx ) {
- $this->rollback();
+ $this->rollback( $fname );
}
throw $e;
}
if ( $useTrx ) {
- $this->commit();
+ $this->commit( $fname );
}
return $ok;
* @return bool
*/
public function upsert(
- $table, array $rows, array $uniqueIndexes, array $set, $fname = 'DatabaseMysql::upsert'
+ $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__
) {
if ( !count( $rows ) ) {
return true; // nothing to do
);
}
+ /**
+ * @dataProvider provideUpsert
+ */
+ function testUpsert( $sql, $sqlText ) {
+ $this->database->upsert(
+ $sql['table'],
+ $sql['rows'],
+ $sql['uniqueIndexes'],
+ $sql['set'],
+ __METHOD__
+ );
+ $this->assertLastSql( $sqlText );
+ }
+
+ public static function provideUpsert() {
+ return array(
+ array(
+ array(
+ 'table' => 'upsert_table',
+ 'rows' => array( 'field' => 'text', 'field2' => 'text2' ),
+ 'uniqueIndexes' => array( 'field' ),
+ 'set' => array( 'field' => 'set' ),
+ ),
+ "BEGIN; " .
+ "UPDATE upsert_table " .
+ "SET field = 'set' " .
+ "WHERE ((field = 'text')); " .
+ "INSERT IGNORE INTO upsert_table " .
+ "(field,field2) " .
+ "VALUES ('text','text2'); " .
+ "COMMIT"
+ ),
+ );
+ }
+
/**
* @dataProvider provideDeleteJoin
*/