]> git.mxchange.org Git - friendica.git/commitdiff
Add support for empty values array in Database\DBA
authorHypolite Petovan <hypolite@mrpetovan.com>
Tue, 24 Dec 2019 21:21:00 +0000 (16:21 -0500)
committerHypolite Petovan <hypolite@mrpetovan.com>
Tue, 24 Dec 2019 21:21:00 +0000 (16:21 -0500)
src/Database/DBA.php

index befb65bf9a904f1a31241d3b4c3c1e16b7940626..593ca0e5074e439492163f1f35d706b1b73ad5e6 100644 (file)
@@ -543,33 +543,38 @@ class DBA extends BaseObject
                                                $condition_string .= " AND ";
                                        }
                                        if (is_array($value)) {
-                                               /* Workaround for MySQL Bug #64791.
-                                                * Never mix data types inside any IN() condition.
-                                                * In case of mixed types, cast all as string.
-                                                * Logic needs to be consistent with DBA::p() data types.
-                                                */
-                                               $is_int = false;
-                                               $is_alpha = false;
-                                               foreach ($value as $single_value) {
-                                                       if (is_int($single_value)) {
-                                                               $is_int = true;
-                                                       } else {
-                                                               $is_alpha = true;
+                                               if (count($value)) {
+                                                       /* Workaround for MySQL Bug #64791.
+                                                        * Never mix data types inside any IN() condition.
+                                                        * In case of mixed types, cast all as string.
+                                                        * Logic needs to be consistent with DBA::p() data types.
+                                                        */
+                                                       $is_int = false;
+                                                       $is_alpha = false;
+                                                       foreach ($value as $single_value) {
+                                                               if (is_int($single_value)) {
+                                                                       $is_int = true;
+                                                               } else {
+                                                                       $is_alpha = true;
+                                                               }
                                                        }
-                                               }
 
-                                               if ($is_int && $is_alpha) {
-                                                       foreach ($value as &$ref) {
-                                                               if (is_int($ref)) {
-                                                                       $ref = (string)$ref;
+                                                       if ($is_int && $is_alpha) {
+                                                               foreach ($value as &$ref) {
+                                                                       if (is_int($ref)) {
+                                                                               $ref = (string)$ref;
+                                                                       }
                                                                }
+                                                               unset($ref); //Prevent accidental re-use.
                                                        }
-                                                       unset($ref); //Prevent accidental re-use.
-                                               }
 
-                                               $new_values = array_merge($new_values, array_values($value));
-                                               $placeholders = substr(str_repeat("?, ", count($value)), 0, -2);
-                                               $condition_string .= self::quoteIdentifier($field) . " IN (" . $placeholders . ")";
+                                                       $new_values = array_merge($new_values, array_values($value));
+                                                       $placeholders = substr(str_repeat("?, ", count($value)), 0, -2);
+                                                       $condition_string .= self::quoteIdentifier($field) . " IN (" . $placeholders . ")";
+                                               } else {
+                                                       // Empty value array isn't supported by IN and is logically equivalent to no match
+                                                       $condition_string .= "FALSE";
+                                               }
                                        } elseif (is_null($value)) {
                                                $condition_string .= self::quoteIdentifier($field) . " IS NULL";
                                        } else {