Special-case empty arrays and single-element arrays in database wrappers.
authorBrion Vibber <brion@users.mediawiki.org>
Thu, 15 Nov 2007 18:35:58 +0000 (18:35 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Thu, 15 Nov 2007 18:35:58 +0000 (18:35 +0000)
* single-element arrays were inefficient ("field IN (42)"), now using straight matches ("field=42")
* empty arrays caused an error ("IN ()" is invalid syntax), now will evaluate to 0 (false)

includes/Database.php

index 4455228..f611f8e 100644 (file)
@@ -1564,7 +1564,15 @@ class Database {
                        } elseif ( ($mode == LIST_SET) && is_numeric( $field ) ) {
                                $list .= "$value";
                        } elseif ( ($mode == LIST_AND || $mode == LIST_OR) && is_array($value) ) {
-                               $list .= $field." IN (".$this->makeList($value).") ";
+                               if( count( $value ) == 0 ) {
+                                       // Empty input... or should this throw an error?
+                                       $list .= '0';
+                               } elseif( count( $value ) == 1 ) {
+                                       // Special-case single values, as IN isn't terribly efficient
+                                       $list .= $field." = ".$this->addQuotes( $value[0] );
+                               } else {
+                                       $list .= $field." IN (".$this->makeList($value).") ";
+                               }
                        } elseif( is_null($value) ) {
                                if ( $mode == LIST_AND || $mode == LIST_OR ) {
                                        $list .= "$field IS ";