]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/Database.php
Replace BaseObject class with DI::* calls
[friendica.git] / src / Database / Database.php
index d13b52848d311ff0c49e305bd482c7937e3f0ebb..1dd3524ed61c694d2bc2f26303c475ca684325c2 100644 (file)
@@ -67,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']))
                {
@@ -90,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);
@@ -187,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;
        }
 
        /**
@@ -369,6 +373,7 @@ class Database
                                $connected = $this->connection->ping();
                                break;
                }
+
                return $connected;
        }
 
@@ -1372,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
@@ -1407,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));
        }
@@ -1465,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
         *
@@ -1478,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;
@@ -1488,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);