Add makeInsertOptions
authorSam Reed <reedy@users.mediawiki.org>
Mon, 2 May 2011 20:14:17 +0000 (20:14 +0000)
committerSam Reed <reedy@users.mediawiki.org>
Mon, 2 May 2011 20:14:17 +0000 (20:14 +0000)
Allow Sqlite to OR IGNORE on UPDATE or INSERT

includes/db/Database.php
includes/db/DatabaseSqlite.php

index a3d8a3a..6ef582b 100644 (file)
@@ -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 ) {
index 6861a63..782ea4a 100644 (file)
@@ -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] ) ) {