]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/Database.php
Add missing copyright text
[friendica.git] / src / Database / Database.php
index 28283b1c8175d8273d980c0ffe17b021b8074cdc..a812efbdb9a9f09811118e389bd459fe96d7fc97 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
  *
@@ -21,9 +21,9 @@
 
 namespace Friendica\Database;
 
-use Friendica\Core\Config\Cache;
+use Friendica\Core\Config\ValueObject\Cache;
 use Friendica\Core\System;
-use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Network\HTTPException\ServiceUnavailableException;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Profiler;
 use mysqli;
@@ -49,7 +49,7 @@ class Database
        protected $connected = false;
 
        /**
-        * @var Cache
+        * @var \Friendica\Core\Config\ValueObject\Cache
         */
        protected $configCache;
        /**
@@ -434,7 +434,7 @@ class Database
        private function replaceParameters($sql, $args)
        {
                $offset = 0;
-               foreach ($args AS $param => $value) {
+               foreach ($args as $param => $value) {
                        if (is_int($args[$param]) || is_float($args[$param]) || is_bool($args[$param])) {
                                $replace = intval($args[$param]);
                        } elseif (is_null($args[$param])) {
@@ -476,7 +476,7 @@ class Database
                // Renumber the array keys to be sure that they fit
                $i    = 0;
                $args = [];
-               foreach ($params AS $param) {
+               foreach ($params as $param) {
                        // Avoid problems with some MySQL servers and boolean values. See issue #3645
                        if (is_bool($param)) {
                                $param = (int)$param;
@@ -520,7 +520,7 @@ class Database
                $called_from_e = ($called_from['function'] == 'e');
 
                if (!isset($this->connection)) {
-                       throw new InternalServerErrorException('The Connection is empty, although connected is set true.');
+                       throw new ServiceUnavailableException('The Connection is empty, although connected is set true.');
                }
 
                switch ($this->driver) {
@@ -549,12 +549,14 @@ class Database
                                        break;
                                }
 
-                               foreach ($args AS $param => $value) {
+                               foreach (array_keys($args) as $param) {
+                                       $data_type = PDO::PARAM_STR;
                                        if (is_int($args[$param])) {
                                                $data_type = PDO::PARAM_INT;
-                                       } else {
-                                               $data_type = PDO::PARAM_STR;
+                                       } elseif ($args[$param] !== null) {
+                                               $args[$param] = (string)$args[$param];
                                        }
+
                                        $stmt->bindParam($param, $args[$param], $data_type);
                                }
 
@@ -605,13 +607,16 @@ class Database
 
                                $param_types = '';
                                $values      = [];
-                               foreach ($args AS $param => $value) {
+                               foreach (array_keys($args) as $param) {
                                        if (is_int($args[$param])) {
                                                $param_types .= 'i';
                                        } elseif (is_float($args[$param])) {
                                                $param_types .= 'd';
                                        } elseif (is_string($args[$param])) {
                                                $param_types .= 's';
+                                       } elseif (is_object($args[$param]) && method_exists($args[$param], '__toString')) {
+                                               $param_types .= 's';
+                                               $args[$param] = (string)$args[$param];
                                        } else {
                                                $param_types .= 'b';
                                        }
@@ -909,7 +914,7 @@ class Database
        /**
         * Fetch a single row
         *
-        * @param PDOStatement|mysqli_stmt $stmt statement object
+        * @param bool|PDOStatement|mysqli_stmt $stmt statement object
         *
         * @return array|false current row
         */
@@ -958,7 +963,7 @@ class Database
                                $result = $stmt->result_metadata();
                                $fields = $result->fetch_fields();
 
-                               foreach ($cols_num AS $param => $col) {
+                               foreach ($cols_num as $param => $col) {
                                        $columns[$fields[$param]->name] = $col;
                                }
                }
@@ -969,7 +974,7 @@ class Database
        }
 
        /**
-        * Insert a row into a table
+        * Insert a row into a table. Field value objects will be cast as string.
         *
         * @param string|array $table          Table name or array [schema => table]
         * @param array        $param          parameter array
@@ -1244,9 +1249,9 @@ class Database
        }
 
        /**
-        * Updates rows
+        * Updates rows in the database. Field value objects will be cast as string.
         *
-        * Updates rows in the database. When $old_fields is set to an array,
+        * When $old_fields is set to an array,
         * the system will only do an update if the fields in that array changed.
         *
         * Attention:
@@ -1293,7 +1298,7 @@ class Database
                        }
                }
 
-               foreach ($old_fields AS $fieldname => $content) {
+               foreach ($old_fields as $fieldname => $content) {
                        if (isset($fields[$fieldname]) && !is_null($content) && ($fields[$fieldname] == $content)) {
                                unset($fields[$fieldname]);
                        }
@@ -1561,9 +1566,9 @@ class Database
                        }
                }
 
-               return $fields; 
+               return $fields;
        }
-       
+
        /**
         * Returns the error number of the last query
         *