]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/DBA.php
Merge pull request #12946 from friendica/api-permission
[friendica.git] / src / Database / DBA.php
index cb501596878a1dc4e9c290699007655e6063ce50..e29cd30386848b465afc2f9446dc3f0c9864758e 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -77,7 +77,7 @@ class DBA
         *
         * @return string with either "pdo" or "mysqli"
         */
-       public static function getDriver()
+       public static function getDriver(): string
        {
                return DI::dba()->getDriver();
        }
@@ -218,7 +218,7 @@ class DBA
        /**
         * Check if data exists
         *
-        * @param string $table     Table name or array [schema => table]
+        * @param string $table     Table name in format schema.table (where schema is optional)
         * @param array  $condition Array of fields for condition
         * @return boolean Are there rows for that condition?
         * @throws \Exception
@@ -289,7 +289,7 @@ class DBA
        /**
         * Insert a row into a table
         *
-        * @param string $table          Table name or array [schema => table]
+        * @param string $table          Table name in format schema.table (where schema is optional)
         * @param array  $param          parameter array
         * @param int    $duplicate_mode What to do on a duplicated entry
         * @return boolean was the insert successful?
@@ -304,7 +304,7 @@ class DBA
         * Inserts a row with the provided data in the provided table.
         * If the data corresponds to an existing row through a UNIQUE or PRIMARY index constraints, it updates the row instead.
         *
-        * @param string $table Table name or array [schema => table]
+        * @param string $table Table name in format schema.table (where schema is optional)
         * @param array  $param parameter array
         * @return boolean was the insert successful?
         * @throws \Exception
@@ -329,7 +329,7 @@ class DBA
         *
         * This function can be extended in the future to accept a table array as well.
         *
-        * @param string $table Table name or array [schema => table]
+        * @param string $table Table name in format schema.table (where schema is optional)
         * @return boolean was the lock successful?
         * @throws \Exception
         */
@@ -414,7 +414,7 @@ class DBA
         * Only set $old_fields to a boolean value when you are sure that you will update a single row.
         * When you set $old_fields to "true" then $fields must contain all relevant fields!
         *
-        * @param string        $table      Table name or array [schema => table]
+        * @param string        $table      Table name in format schema.table (where schema is optional)
         * @param array         $fields     contains the fields that are updated
         * @param array         $condition  condition array with the key values
         * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate, false = don't update identical fields)
@@ -431,7 +431,7 @@ class DBA
        /**
         * Retrieve a single record from a table and returns it in an associative array
         *
-        * @param string|array $table     Table name or array [schema => table]
+        * @param string|array $table     Table name in format schema.table (where schema is optional)
         * @param array        $fields
         * @param array        $condition
         * @param array        $params
@@ -447,16 +447,16 @@ class DBA
        /**
         * Select rows from a table and fills an array with the data
         *
-        * @param string|array $table     Table name or array [schema => table]
-        * @param array        $fields    Array of selected fields, empty for all
-        * @param array        $condition Array of fields for condition
-        * @param array        $params    Array of several parameters
+        * @param string $table     Table name in format schema.table (where schema is optional)
+        * @param array  $fields    Array of selected fields, empty for all
+        * @param array  $condition Array of fields for condition
+        * @param array  $params    Array of several parameters
         *
         * @return array Data array
         * @throws \Exception
         * @see   self::select
         */
-       public static function selectToArray($table, array $fields = [], array $condition = [], array $params = [])
+       public static function selectToArray(string $table, array $fields = [], array $condition = [], array $params = [])
        {
                return DI::dba()->selectToArray($table, $fields, $condition, $params);
        }
@@ -464,7 +464,7 @@ class DBA
        /**
         * Select rows from a table
         *
-        * @param string $table     Table name or array [schema => table]
+        * @param string $table     Table name in format schema.table (where schema is optional)
         * @param array  $fields    Array of selected fields, empty for all
         * @param array  $condition Array of fields for condition
         * @param array  $params    Array of several parameters
@@ -492,7 +492,7 @@ class DBA
        /**
         * Counts the rows from a table satisfying the provided condition
         *
-        * @param string $table     Table name or array [schema => table]
+        * @param string $table     Table name in format schema.table (where schema is optional)
         * @param array  $condition array of fields for condition
         * @param array  $params    Array of several parameters
         *
@@ -517,7 +517,7 @@ class DBA
         * Build the table query substring from one or more tables, with or without a schema.
         *
         * Expected formats:
-        * - table
+        * - [table]
         * - [table1, table2, ...]
         * - [schema1 => table1, schema2 => table2, table3, ...]
         *
@@ -526,28 +526,25 @@ class DBA
         */
        public static function buildTableString(array $tables): string
        {
-               $quotedTables = [];
-
-               foreach ($tables as $schema => $table) {
-                       if (is_numeric($schema)) {
-                               $quotedTables[] = self::quoteIdentifier($table);
-                       } else {
-                               $quotedTables[] = self::quoteIdentifier($schema) . '.' . self::quoteIdentifier($table);
-                       }
-               }
-
-               return implode(', ', $quotedTables);
+               // Quote each entry
+               return implode(',', array_map([self::class, 'quoteIdentifier'], $tables));
        }
 
        /**
-        * Escape an identifier (table or field name)
+        * Escape an identifier (table or field name) optional with a schema like ((schema.)table.)field
         *
-        * @param $identifier
-        * @return string
+        * @param string $identifier Table, field name
+        * @return string Quotes table or field name
         */
        public static function quoteIdentifier(string $identifier): string
        {
-               return '`' . str_replace('`', '``', $identifier) . '`';
+               return implode(
+                       '.',
+                       array_map(
+                               function (string $identifier) { return '`' . str_replace('`', '``', $identifier) . '`'; },
+                               explode('.', $identifier)
+                       )
+               );
        }
 
        /**
@@ -571,7 +568,7 @@ class DBA
        public static function buildCondition(array &$condition = []): string
        {
                $condition = self::collapseCondition($condition);
-               
+
                $condition_string = '';
                if (count($condition) > 0) {
                        $condition_string = " WHERE (" . array_shift($condition) . ")";
@@ -720,7 +717,7 @@ class DBA
        {
                $groupby_string = '';
                if (!empty($params['group_by'])) {
-                       $groupby_string = " GROUP BY " . implode(', ', array_map(['self', 'quoteIdentifier'], $params['group_by']));
+                       $groupby_string = " GROUP BY " . implode(', ', array_map([self::class, 'quoteIdentifier'], $params['group_by']));
                }
 
                $order_string = '';