]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/Database.php
Merge pull request #11267 from MrPetovan/task/11264-mastodon-api-version
[friendica.git] / src / Database / Database.php
index 0e1cffb4ac95204ea2ea8d557917e5737a6d6d4a..f478d7993a1844dfac0f49c76da20f1c6bcef8a3 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -138,6 +138,7 @@ class Database
                        try {
                                $this->connection = @new PDO($connect, $user, $pass, [PDO::ATTR_PERSISTENT => $persistent]);
                                $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->pdo_emulate_prepares);
+                               $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
                                $this->connected = true;
                        } catch (PDOException $e) {
                                $this->connected = false;
@@ -1367,6 +1368,45 @@ class Database
                return $this->toArray($this->select($table, $fields, $condition, $params));
        }
 
+       /**
+        * Escape fields, adding special treatment for "group by" handling
+        *
+        * @param array $fields 
+        * @param array $options 
+        * @return array 
+        */
+       private function escapeFields(array $fields, array $options)
+       {
+               // In the case of a "GROUP BY" we have to add all the ORDER fields to the fieldlist.
+               // This needs to done to apply the "ANY_VALUE(...)" treatment from below to them.
+               // Otherwise MySQL would report errors.
+               if (!empty($options['group_by']) && !empty($options['order'])) {
+                       foreach ($options['order'] as $key => $field) {
+                               if (!is_int($key)) {
+                                       if (!in_array($key, $fields)) {
+                                               $fields[] = $key;
+                                       }
+                               } else {
+                                       if (!in_array($field, $fields)) {
+                                               $fields[] = $field;
+                                       }
+                               }
+                       }
+               }
+
+               array_walk($fields, function(&$value, $key) use ($options)
+               {
+                       $field = $value;
+                       $value = '`' . str_replace('`', '``', $value) . '`';
+
+                       if (!empty($options['group_by']) && !in_array($field, $options['group_by'])) {
+                               $value = 'ANY_VALUE(' . $value . ') AS ' . $value;
+                       }
+               });
+
+               return $fields;
+       }
+
        /**
         * Select rows from a table
         *
@@ -1403,7 +1443,8 @@ class Database
                }
 
                if (count($fields) > 0) {
-                       $select_string = implode(', ', array_map([DBA::class, 'quoteIdentifier'], $fields));
+                       $fields = $this->escapeFields($fields, $params);
+                       $select_string = implode(', ', $fields);
                } else {
                        $select_string = '*';
                }