From a39e48078ce6aba98a506cb19bdf61254e57b3e9 Mon Sep 17 00:00:00 2001 From: Sam Reed Date: Mon, 2 May 2011 20:14:17 +0000 Subject: [PATCH] Add makeInsertOptions Allow Sqlite to OR IGNORE on UPDATE or INSERT --- includes/db/Database.php | 12 ++++++++++- includes/db/DatabaseSqlite.php | 38 +++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/includes/db/Database.php b/includes/db/Database.php index a3d8a3a5cf..6ef582baa5 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -1257,6 +1257,14 @@ abstract class DatabaseBase implements DatabaseType { return !$indexInfo[0]->Non_unique; } + /** + * @param $options array + * @return string + */ + function makeInsertOptions( $options ) { + return implode( ' ', $options ); + } + /** * INSERT wrapper, inserts an array into a table * @@ -1285,6 +1293,8 @@ abstract class DatabaseBase implements DatabaseType { $options = array( $options ); } + $options = $this->makeInsertOptions( $options ); + if ( isset( $a[0] ) && is_array( $a[0] ) ) { $multi = true; $keys = array_keys( $a[0] ); @@ -1293,7 +1303,7 @@ abstract class DatabaseBase implements DatabaseType { $keys = array_keys( $a ); } - $sql = 'INSERT ' . implode( ' ', $options ) . + $sql = 'INSERT ' . $options . " INTO $table (" . implode( ',', $keys ) . ') VALUES '; if ( $multi ) { diff --git a/includes/db/DatabaseSqlite.php b/includes/db/DatabaseSqlite.php index 6861a63729..782ea4a705 100644 --- a/includes/db/DatabaseSqlite.php +++ b/includes/db/DatabaseSqlite.php @@ -363,22 +363,44 @@ class DatabaseSqlite extends DatabaseBase { } /** - * Based on generic method (parent) with some prior SQLite-sepcific adjustments + * @param $options array + * @return string */ - function insert( $table, $a, $fname = 'DatabaseSqlite::insert', $options = array() ) { - if ( !count( $a ) ) { - return true; - } - if ( !is_array( $options ) ) { - $options = array( $options ); - } + function makeUpdateOptions( $options ) { + $options = self::fixIgnore( $options ); + return parent::makeUpdateOptions( $options ); + } + /** + * @param $options array + * @return array + */ + static function fixIgnore( $options ) { # SQLite uses OR IGNORE not just IGNORE foreach ( $options as $k => $v ) { if ( $v == 'IGNORE' ) { $options[$k] = 'OR IGNORE'; } } + return $options; + } + + /** + * @param $options array + * @return string + */ + function makeInsertOptions( &$options ) { + $options = self::fixIgnore( $options ); + return parent::makeInsertOptions( $options ); + } + + /** + * Based on generic method (parent) with some prior SQLite-sepcific adjustments + */ + function insert( $table, $a, $fname = 'DatabaseSqlite::insert', $options = array() ) { + if ( !count( $a ) ) { + return true; + } # SQLite can't handle multi-row inserts, so divide up into multiple single-row inserts if ( isset( $a[0] ) && is_array( $a[0] ) ) { -- 2.20.1