]> source.dussan.org Git - nextcloud-server.git/commitdiff
Move creating the database connection to the server container
authorRobin Appelman <icewind@owncloud.com>
Wed, 10 Sep 2014 11:24:49 +0000 (13:24 +0200)
committerRobin Appelman <icewind@owncloud.com>
Wed, 22 Oct 2014 10:29:53 +0000 (12:29 +0200)
lib/private/appframework/db/db.php
lib/private/db.php
lib/private/db/connectionfactory.php
lib/private/server.php

index fc77a38f814a845de773bb7153ff6c2f2708f39a..91572ad9e5a4298c599b45c0c8f0ba4699275646 100644 (file)
@@ -30,28 +30,40 @@ use \OCP\IDb;
  * Small Facade for being able to inject the database connection for tests
  */
 class Db implements IDb {
+       /**
+        * @var \OCP\IDBConnection
+        */
+       protected $connection;
 
+       /**
+        * @param \OCP\IDBConnection $connection
+        */
+       public function __construct($connection) {
+               $this->connection = $connection;
+       }
 
-    /**
-     * Used to abstract the owncloud database access away
-     * @param string $sql the sql query with ? placeholder for params
-     * @param int $limit the maximum number of rows
-     * @param int $offset from which row we want to start
-     * @return \OC_DB_StatementWrapper prepared SQL query
-     */
-    public function prepareQuery($sql, $limit=null, $offset=null){
-        return \OCP\DB::prepare($sql, $limit, $offset);
-    }
-
-
-    /**
-     * Used to get the id of the just inserted element
-     * @param string $tableName the name of the table where we inserted the item
-     * @return int the id of the inserted element
-     */
-    public function getInsertId($tableName){
-        return \OCP\DB::insertid($tableName);
-    }
+       /**
+        * Used to abstract the owncloud database access away
+        *
+        * @param string $sql the sql query with ? placeholder for params
+        * @param int $limit the maximum number of rows
+        * @param int $offset from which row we want to start
+        * @return \OC_DB_StatementWrapper prepared SQL query
+        */
+       public function prepareQuery($sql, $limit = null, $offset = null) {
+               return $this->connection->prepare($sql, $limit, $offset);
+       }
+
+
+       /**
+        * Used to get the id of the just inserted element
+        *
+        * @param string $tableName the name of the table where we inserted the item
+        * @return int the id of the inserted element
+        */
+       public function getInsertId($tableName) {
+               return $this->connection->lastInsertId($tableName);
+       }
 
 
 }
index 381ed93e81efcebef6d074bf83f204a48e9f2b88..59d61ffa297320431fb7b480a74d20e0fe678982 100644 (file)
@@ -46,66 +46,6 @@ class OC_DB {
         */
        static private $connection; //the preferred connection to use, only Doctrine
 
-       /**
-        * connects to the database
-        * @return boolean|null true if connection can be established or false on error
-        *
-        * Connects to the database as specified in config.php
-        */
-       public static function connect() {
-               if(self::$connection) {
-                       return true;
-               }
-
-               $type = OC_Config::getValue('dbtype', 'sqlite');
-               $factory = new \OC\DB\ConnectionFactory();
-               if (!$factory->isValidType($type)) {
-                       return false;
-               }
-
-               $connectionParams = array(
-                       'user' => OC_Config::getValue('dbuser', ''),
-                       'password' => OC_Config::getValue('dbpassword', ''),
-               );
-               $name = OC_Config::getValue('dbname', 'owncloud');
-
-               if ($factory->normalizeType($type) === 'sqlite3') {
-                       $datadir = OC_Config::getValue("datadirectory", OC::$SERVERROOT.'/data');
-                       $connectionParams['path'] = $datadir.'/'.$name.'.db';
-               } else {
-                       $host = OC_Config::getValue('dbhost', '');
-                       if (strpos($host, ':')) {
-                               // Host variable may carry a port or socket.
-                               list($host, $portOrSocket) = explode(':', $host, 2);
-                               if (ctype_digit($portOrSocket)) {
-                                       $connectionParams['port'] = $portOrSocket;
-                               } else {
-                                       $connectionParams['unix_socket'] = $portOrSocket;
-                               }
-                       }
-                       $connectionParams['host'] = $host;
-                       $connectionParams['dbname'] = $name;
-               }
-
-               $connectionParams['tablePrefix'] = OC_Config::getValue('dbtableprefix', 'oc_');
-
-               try {
-                       self::$connection = $factory->getConnection($type, $connectionParams);
-                       self::$connection->getConfiguration()->setSQLLogger(\OC::$server->getQueryLogger());
-               } catch(\Doctrine\DBAL\DBALException $e) {
-                       OC_Log::write('core', $e->getMessage(), OC_Log::FATAL);
-                       OC_User::setUserId(null);
-
-                       // send http status 503
-                       header('HTTP/1.1 503 Service Temporarily Unavailable');
-                       header('Status: 503 Service Temporarily Unavailable');
-                       OC_Template::printErrorPage('Failed to connect to database');
-                       die();
-               }
-
-               return true;
-       }
-
        /**
         * The existing database connection is closed and connected again
         */
@@ -116,11 +56,10 @@ class OC_DB {
        }
 
        /**
-        * @return \OC\DB\Connection
+        * @return \OCP\IDBConnection
         */
        static public function getConnection() {
-               self::connect();
-               return self::$connection;
+               return \OC::$server->getDatabaseConnection();
        }
 
        /**
index a5260c1a4c5517dec228646763122ddd59ac9b9f..1f676f1fca20b58ce12a4ea684f4c8d334e24d8f 100644 (file)
@@ -118,4 +118,41 @@ class ConnectionFactory {
                $normalizedType = $this->normalizeType($type);
                return isset($this->defaultConnectionParams[$normalizedType]);
        }
+
+       /**
+        * Create the connection parameters for the config
+        *
+        * @param \OCP\IConfig $config
+        * @return array
+        */
+       public function createConnectionParams($config) {
+               $type = $config->getSystemValue('dbtype', 'sqlite');
+
+               $connectionParams = array(
+                       'user' => $config->getSystemValue('dbuser', ''),
+                       'password' => $config->getSystemValue('dbpassword', ''),
+               );
+               $name = $config->getSystemValue('dbname', 'owncloud');
+
+               if ($this->normalizeType($type) === 'sqlite3') {
+                       $datadir = $config->getSystemValue("datadirectory", \OC::$SERVERROOT . '/data');
+                       $connectionParams['path'] = $datadir . '/' . $name . '.db';
+               } else {
+                       $host = $config->getSystemValue('dbhost', '');
+                       if (strpos($host, ':')) {
+                               // Host variable may carry a port or socket.
+                               list($host, $portOrSocket) = explode(':', $host, 2);
+                               if (ctype_digit($portOrSocket)) {
+                                       $connectionParams['port'] = $portOrSocket;
+                               } else {
+                                       $connectionParams['unix_socket'] = $portOrSocket;
+                               }
+                       }
+                       $connectionParams['host'] = $host;
+                       $connectionParams['dbname'] = $name;
+               }
+
+               $connectionParams['tablePrefix'] = $config->getSystemValue('dbtableprefix', 'oc_');
+               return $connectionParams;
+       }
 }
index 26d540ab23900968edf3387a2ebe49b8bc1313f5..b0d63af15545b93f15e298bf18c185a4d6597960 100644 (file)
@@ -217,8 +217,25 @@ class Server extends SimpleContainer implements IServerContainer {
                $this->registerService('Crypto', function ($c) {
                        return new Crypto(\OC::$server->getConfig(), \OC::$server->getSecureRandom());
                });
+               $this->registerService('DatabaseConnection', function ($c) {
+                       /**
+                        * @var Server $c
+                        */
+                       $factory = new \OC\DB\ConnectionFactory();
+                       $type = $c->getConfig()->getSystemValue('dbtype', 'sqlite');
+                       if (!$factory->isValidType($type)) {
+                               throw new \DatabaseException('Invalid database type');
+                       }
+                       $connectionParams = $factory->createConnectionParams($c->getConfig());
+                       $connection = $factory->getConnection($type, $connectionParams);
+                       $connection->getConfiguration()->setSQLLogger($c->getQueryLogger());
+                       return $connection;
+               });
                $this->registerService('Db', function ($c) {
-                       return new Db();
+                       /**
+                        * @var Server $c
+                        */
+                       return new Db($c->getDatabaseConnection());
                });
                $this->registerService('HTTPHelper', function (SimpleContainer $c) {
                        $config = $c->query('AllConfig');
@@ -469,7 +486,7 @@ class Server extends SimpleContainer implements IServerContainer {
         * @return \OCP\IDBConnection
         */
        function getDatabaseConnection() {
-               return \OC_DB::getConnection();
+               return $this->query('DatabaseConnection');
        }
 
        /**