]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/Database.php
Merge branch 'no-term3' into no-term2
[friendica.git] / src / Database / Database.php
index 14e186a72f63a4cbf94d31869da5b81e2f5d52eb..db906bd01e05e40675e049c6adc45802f333798b 100644 (file)
@@ -1,4 +1,23 @@
 <?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
 
 namespace Friendica\Database;
 
@@ -16,8 +35,6 @@ use PDOStatement;
 use Psr\Log\LoggerInterface;
 
 /**
- * @class MySQL database class
- *
  * This class is for the low level database stuff that does driver specific things.
  */
 class Database
@@ -40,6 +57,7 @@ class Database
        /** @var PDO|mysqli */
        protected $connection;
        protected $driver;
+       private $emulate_prepares = false;
        private $error          = false;
        private $errorno        = 0;
        private $affected_rows  = 0;
@@ -113,6 +131,8 @@ class Database
                        return false;
                }
 
+               $this->emulate_prepares = (bool)$this->configCache->get('database', 'emulate_prepares');
+
                if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
                        $this->driver = 'pdo';
                        $connect      = "mysql:host=" . $server . ";dbname=" . $db;
@@ -411,8 +431,10 @@ class Database
        {
                $offset = 0;
                foreach ($args AS $param => $value) {
-                       if (is_int($args[$param]) || is_float($args[$param])) {
+                       if (is_int($args[$param]) || is_float($args[$param]) || is_bool($args[$param])) {
                                $replace = intval($args[$param]);
+                       } elseif (is_null($args[$param])) {
+                               $replace = 'NULL';
                        } else {
                                $replace = "'" . $this->escape($args[$param]) . "'";
                        }
@@ -498,8 +520,8 @@ class Database
                switch ($this->driver) {
                        case 'pdo':
                                // If there are no arguments we use "query"
-                               if (count($args) == 0) {
-                                       if (!$retval = $this->connection->query($sql)) {
+                               if ($this->emulate_prepares || count($args) == 0) {
+                                       if (!$retval = $this->connection->query($this->replaceParameters($sql, $args))) {
                                                $errorInfo     = $this->connection->errorInfo();
                                                $this->error   = $errorInfo[2];
                                                $this->errorno = $errorInfo[1];
@@ -545,7 +567,7 @@ class Database
                                $can_be_prepared = in_array($command, ['select', 'update', 'insert', 'delete']);
 
                                // The fallback routine is called as well when there are no arguments
-                               if (!$can_be_prepared || (count($args) == 0)) {
+                               if ($this->emulate_prepares || !$can_be_prepared || (count($args) == 0)) {
                                        $retval = $this->connection->query($this->replaceParameters($sql, $args));
                                        if ($this->connection->errno) {
                                                $this->error   = $this->connection->error;
@@ -1245,7 +1267,7 @@ class Database
 
                        if ((count($command['conditions']) > 1) || is_int($first_key)) {
                                $sql = "DELETE FROM " . DBA::quoteIdentifier($command['table']) . " " . $condition_string;
-                               $this->logger->debug($this->replaceParameters($sql, $conditions));
+                               $this->logger->info($this->replaceParameters($sql, $conditions), ['callstack' => System::callstack(6), 'internal_callstack' => $callstack]);
 
                                if (!$this->e($sql, $conditions)) {
                                        if ($do_transaction) {
@@ -1275,7 +1297,7 @@ class Database
                                        $sql = "DELETE FROM " . DBA::quoteIdentifier($table) . " WHERE " . DBA::quoteIdentifier($field) . " IN (" .
                                               substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
 
-                                       $this->logger->debug($this->replaceParameters($sql, $field_values));
+                                       $this->logger->info($this->replaceParameters($sql, $field_values), ['callstack' => System::callstack(6), 'internal_callstack' => $callstack]);
 
                                        if (!$this->e($sql, $field_values)) {
                                                if ($do_transaction) {