From 9ca9ae5836cf885ceeb5dfabc74c8f8575d17ff2 Mon Sep 17 00:00:00 2001 From: Sam Reed Date: Wed, 25 Aug 2010 01:03:43 +0000 Subject: [PATCH] Create insertOnDupeUpdate, called by insert Will be using soon --- includes/db/Database.php | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/includes/db/Database.php b/includes/db/Database.php index 5106bb1666..618789b06f 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -1097,7 +1097,7 @@ abstract class DatabaseBase { } return !$indexInfo[0]->Non_unique; } - + /** * INSERT wrapper, inserts an array into a table * @@ -1115,6 +1115,28 @@ abstract class DatabaseBase { * @return bool */ function insert( $table, $a, $fname = 'Database::insert', $options = array() ) { + return this->insertOnDupeUpdate( $table, $a, $fname, $options ); + } + + /** + * INSERT ... ON DUPE UPDATE wrapper, inserts an array into a table, optionally updating if + * duplicate primary key found + * + * $a may be a single associative array, or an array of these with numeric keys, for + * multi-row insert. + * + * Usually aborts on failure + * If errors are explicitly ignored, returns success + * + * @param $table String: table name (prefix auto-added) + * @param $a Array: Array of rows to insert + * @param $fname String: Calling function name (use __METHOD__) for logs/profiling + * @param $options Mixed: Associative array of options + * @param $onDupeUpdate Array: Associative array of fields to update on duplicate + * + * @return bool + */ + function insertOnDupeUpdate( $table, $a, $fname = 'Database::insertOnDupeUpdate', $options = array(), $onDupeUpdate = array() ) { # No rows to insert, easy just return now if ( !count( $a ) ) { return true; @@ -1148,6 +1170,11 @@ abstract class DatabaseBase { } else { $sql .= '(' . $this->makeList( $a ) . ')'; } + + if ( count( $onDupeUpdate ) ) { + $sql .= ' ON DUPLICATE KEY UPDATE ' . $this->makeList( $onDupeUpdate ); + } + return (bool)$this->query( $sql, $fname ); } -- 2.20.1