]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/Database.php
Replace BaseObject class with DI::* calls
[friendica.git] / src / Database / Database.php
index d3ec4d585a1f71a3606d47c2af9036723be74fad..1dd3524ed61c694d2bc2f26303c475ca684325c2 100644 (file)
@@ -4,6 +4,7 @@ namespace Friendica\Database;
 
 use Friendica\Core\Config\Cache\ConfigCache;
 use Friendica\Core\System;
+use Friendica\Network\HTTPException\InternalServerErrorException;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Profiler;
 use mysqli;
@@ -66,7 +67,7 @@ class Database
        {
                // Use environment variables for mysql if they are set beforehand
                if (!empty($server['MYSQL_HOST'])
-                   && !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
+                   && (!empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER'])))
                    && $server['MYSQL_PASSWORD'] !== false
                    && !empty($server['MYSQL_DATABASE']))
                {
@@ -89,9 +90,12 @@ class Database
        public function connect()
        {
                if (!is_null($this->connection) && $this->connected()) {
-                       return true;
+                       return $this->connected;
                }
 
+               // Reset connected state
+               $this->connected = false;
+
                $port       = 0;
                $serveraddr = trim($this->configCache->get('database', 'hostname'));
                $serverdata = explode(':', $serveraddr);
@@ -126,7 +130,7 @@ class Database
                                $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
                                $this->connected = true;
                        } catch (PDOException $e) {
-                               /// @TODO At least log exception, don't ignore it!
+                               $this->connected = false;
                        }
                }
 
@@ -186,19 +190,20 @@ class Database
         */
        public function disconnect()
        {
-               if (is_null($this->connection)) {
-                       return;
+               if (!is_null($this->connection)) {
+                       switch ($this->driver) {
+                               case 'pdo':
+                                       $this->connection = null;
+                                       break;
+                               case 'mysqli':
+                                       $this->connection->close();
+                                       $this->connection = null;
+                                       break;
+                       }
                }
 
-               switch ($this->driver) {
-                       case 'pdo':
-                               $this->connection = null;
-                               break;
-                       case 'mysqli':
-                               $this->connection->close();
-                               $this->connection = null;
-                               break;
-               }
+               $this->driver    = null;
+               $this->connected = false;
        }
 
        /**
@@ -368,6 +373,7 @@ class Database
                                $connected = $this->connection->ping();
                                break;
                }
+
                return $connected;
        }
 
@@ -484,6 +490,10 @@ class Database
                // We are having an own error logging in the function "e"
                $called_from_e = ($called_from['function'] == 'e');
 
+               if (!isset($this->connection)) {
+                       throw new InternalServerErrorException('The Connection is empty, although connected is set true.');
+               }
+
                switch ($this->driver) {
                        case 'pdo':
                                // If there are no arguments we use "query"
@@ -1367,10 +1377,10 @@ class Database
         *
         * @brief Retrieve a single record from a table
         *
-        * @param string $table
-        * @param array  $fields
-        * @param array  $condition
-        * @param array  $params
+        * @param string|array $table
+        * @param array        $fields
+        * @param array        $condition
+        * @param array        $params
         *
         * @return bool|array
         * @throws \Exception
@@ -1402,7 +1412,7 @@ class Database
         * @throws \Exception
         * @see   self::select
         */
-       public function selectToArray(string $table, array $fields = [], array $condition = [], array $params = [])
+       public function selectToArray($table, array $fields = [], array $condition = [], array $params = [])
        {
                return $this->toArray($this->select($table, $fields, $condition, $params));
        }
@@ -1460,6 +1470,7 @@ class Database
         *
         * @param string|array $table     Table name or array [schema => table]
         * @param array        $condition Array of fields for condition
+        * @param array        $params    Array of several parameters
         *
         * @return int
         *
@@ -1473,7 +1484,7 @@ class Database
         * $count = DBA::count($table, $condition);
         * @throws \Exception
         */
-       public function count($table, array $condition = [])
+       public function count($table, array $condition = [], array $params = [])
        {
                if (empty($table)) {
                        return false;
@@ -1483,7 +1494,15 @@ class Database
 
                $condition_string = DBA::buildCondition($condition);
 
-               $sql = "SELECT COUNT(*) AS `count` FROM " . $table_string . $condition_string;
+               if (empty($params['expression'])) {
+                       $expression = '*';
+               } elseif (!empty($params['distinct'])) {
+                       $expression = "DISTINCT " . DBA::quoteIdentifier($params['expression']);
+               } else {
+                       $expression = DBA::quoteIdentifier($params['expression']);
+               }
+
+               $sql = "SELECT COUNT(" . $expression . ") AS `count` FROM " . $table_string . $condition_string;
 
                $row = $this->fetchFirst($sql, $condition);