@@ -26,14 +26,14 @@ discard block |
||
| 26 | 26 | use OC\DB\ConnectionFactory; |
| 27 | 27 | |
| 28 | 28 | class Sqlite extends AbstractDatabase { |
| 29 | - public $dbprettyname = 'Sqlite'; |
|
| 29 | + public $dbprettyname = 'Sqlite'; |
|
| 30 | 30 | |
| 31 | - public function validate($config) { |
|
| 32 | - return array(); |
|
| 33 | - } |
|
| 31 | + public function validate($config) { |
|
| 32 | + return array(); |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - public function initialize($config) { |
|
| 36 | - /* |
|
| 35 | + public function initialize($config) { |
|
| 36 | + /* |
|
| 37 | 37 | * Web: When using web based installer its not possible to set dbname |
| 38 | 38 | * or dbtableprefix. Defaults used from ConnectionFactory and dbtype = 'sqlite' |
| 39 | 39 | * is written to config.php. |
@@ -44,32 +44,32 @@ discard block |
||
| 44 | 44 | * in connection factory configuration is obtained from config.php. |
| 45 | 45 | */ |
| 46 | 46 | |
| 47 | - $this->dbName = empty($config['dbname']) |
|
| 48 | - ? ConnectionFactory::DEFAULT_DBNAME |
|
| 49 | - : $config['dbname']; |
|
| 47 | + $this->dbName = empty($config['dbname']) |
|
| 48 | + ? ConnectionFactory::DEFAULT_DBNAME |
|
| 49 | + : $config['dbname']; |
|
| 50 | 50 | |
| 51 | - $this->tablePrefix = empty($config['dbtableprefix']) |
|
| 52 | - ? ConnectionFactory::DEFAULT_DBTABLEPREFIX |
|
| 53 | - : $config['dbtableprefix']; |
|
| 51 | + $this->tablePrefix = empty($config['dbtableprefix']) |
|
| 52 | + ? ConnectionFactory::DEFAULT_DBTABLEPREFIX |
|
| 53 | + : $config['dbtableprefix']; |
|
| 54 | 54 | |
| 55 | - if ($this->dbName !== ConnectionFactory::DEFAULT_DBNAME) { |
|
| 56 | - $this->config->setValue('dbname', $this->dbName); |
|
| 57 | - } |
|
| 55 | + if ($this->dbName !== ConnectionFactory::DEFAULT_DBNAME) { |
|
| 56 | + $this->config->setValue('dbname', $this->dbName); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - if ($this->tablePrefix !== ConnectionFactory::DEFAULT_DBTABLEPREFIX) { |
|
| 60 | - $this->config->setValue('dbtableprefix', $this->tablePrefix); |
|
| 61 | - } |
|
| 62 | - } |
|
| 59 | + if ($this->tablePrefix !== ConnectionFactory::DEFAULT_DBTABLEPREFIX) { |
|
| 60 | + $this->config->setValue('dbtableprefix', $this->tablePrefix); |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - public function setupDatabase($username) { |
|
| 65 | - $datadir = $this->config->getValue( |
|
| 66 | - 'datadirectory', |
|
| 67 | - \OC::$SERVERROOT . '/data' |
|
| 68 | - ); |
|
| 64 | + public function setupDatabase($username) { |
|
| 65 | + $datadir = $this->config->getValue( |
|
| 66 | + 'datadirectory', |
|
| 67 | + \OC::$SERVERROOT . '/data' |
|
| 68 | + ); |
|
| 69 | 69 | |
| 70 | - $sqliteFile = $datadir . '/' . $this->dbName . 'db'; |
|
| 71 | - if (file_exists($sqliteFile)) { |
|
| 72 | - unlink($sqliteFile); |
|
| 73 | - } |
|
| 74 | - } |
|
| 70 | + $sqliteFile = $datadir . '/' . $this->dbName . 'db'; |
|
| 71 | + if (file_exists($sqliteFile)) { |
|
| 72 | + unlink($sqliteFile); |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | 75 | } |
@@ -64,10 +64,10 @@ |
||
| 64 | 64 | public function setupDatabase($username) { |
| 65 | 65 | $datadir = $this->config->getValue( |
| 66 | 66 | 'datadirectory', |
| 67 | - \OC::$SERVERROOT . '/data' |
|
| 67 | + \OC::$SERVERROOT.'/data' |
|
| 68 | 68 | ); |
| 69 | 69 | |
| 70 | - $sqliteFile = $datadir . '/' . $this->dbName . 'db'; |
|
| 70 | + $sqliteFile = $datadir.'/'.$this->dbName.'db'; |
|
| 71 | 71 | if (file_exists($sqliteFile)) { |
| 72 | 72 | unlink($sqliteFile); |
| 73 | 73 | } |
@@ -38,218 +38,218 @@ |
||
| 38 | 38 | * Takes care of creating and configuring Doctrine connections. |
| 39 | 39 | */ |
| 40 | 40 | class ConnectionFactory { |
| 41 | - /** @var string default database name */ |
|
| 42 | - const DEFAULT_DBNAME = 'owncloud'; |
|
| 43 | - |
|
| 44 | - /** @var string default database table prefix */ |
|
| 45 | - const DEFAULT_DBTABLEPREFIX = 'oc_'; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var array |
|
| 49 | - * |
|
| 50 | - * Array mapping DBMS type to default connection parameters passed to |
|
| 51 | - * \Doctrine\DBAL\DriverManager::getConnection(). |
|
| 52 | - */ |
|
| 53 | - protected $defaultConnectionParams = [ |
|
| 54 | - 'mysql' => [ |
|
| 55 | - 'adapter' => AdapterMySQL::class, |
|
| 56 | - 'charset' => 'UTF8', |
|
| 57 | - 'driver' => 'pdo_mysql', |
|
| 58 | - 'wrapperClass' => Connection::class, |
|
| 59 | - ], |
|
| 60 | - 'oci' => [ |
|
| 61 | - 'adapter' => AdapterOCI8::class, |
|
| 62 | - 'charset' => 'AL32UTF8', |
|
| 63 | - 'driver' => 'oci8', |
|
| 64 | - 'wrapperClass' => OracleConnection::class, |
|
| 65 | - ], |
|
| 66 | - 'pgsql' => [ |
|
| 67 | - 'adapter' => AdapterPgSql::class, |
|
| 68 | - 'driver' => 'pdo_pgsql', |
|
| 69 | - 'wrapperClass' => Connection::class, |
|
| 70 | - ], |
|
| 71 | - 'sqlite3' => [ |
|
| 72 | - 'adapter' => AdapterSqlite::class, |
|
| 73 | - 'driver' => 'pdo_sqlite', |
|
| 74 | - 'wrapperClass' => Connection::class, |
|
| 75 | - ], |
|
| 76 | - ]; |
|
| 77 | - |
|
| 78 | - /** @var SystemConfig */ |
|
| 79 | - private $config; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * ConnectionFactory constructor. |
|
| 83 | - * |
|
| 84 | - * @param SystemConfig $systemConfig |
|
| 85 | - */ |
|
| 86 | - public function __construct(SystemConfig $systemConfig) { |
|
| 87 | - $this->config = $systemConfig; |
|
| 88 | - if ($this->config->getValue('mysql.utf8mb4', false)) { |
|
| 89 | - $this->defaultConnectionParams['mysql']['charset'] = 'utf8mb4'; |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @brief Get default connection parameters for a given DBMS. |
|
| 95 | - * @param string $type DBMS type |
|
| 96 | - * @throws \InvalidArgumentException If $type is invalid |
|
| 97 | - * @return array Default connection parameters. |
|
| 98 | - */ |
|
| 99 | - public function getDefaultConnectionParams($type) { |
|
| 100 | - $normalizedType = $this->normalizeType($type); |
|
| 101 | - if (!isset($this->defaultConnectionParams[$normalizedType])) { |
|
| 102 | - throw new \InvalidArgumentException("Unsupported type: $type"); |
|
| 103 | - } |
|
| 104 | - $result = $this->defaultConnectionParams[$normalizedType]; |
|
| 105 | - // \PDO::MYSQL_ATTR_FOUND_ROWS may not be defined, e.g. when the MySQL |
|
| 106 | - // driver is missing. In this case, we won't be able to connect anyway. |
|
| 107 | - if ($normalizedType === 'mysql' && defined('\PDO::MYSQL_ATTR_FOUND_ROWS')) { |
|
| 108 | - $result['driverOptions'] = array( |
|
| 109 | - \PDO::MYSQL_ATTR_FOUND_ROWS => true, |
|
| 110 | - ); |
|
| 111 | - } |
|
| 112 | - return $result; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * @brief Get default connection parameters for a given DBMS. |
|
| 117 | - * @param string $type DBMS type |
|
| 118 | - * @param array $additionalConnectionParams Additional connection parameters |
|
| 119 | - * @return \OC\DB\Connection |
|
| 120 | - */ |
|
| 121 | - public function getConnection($type, $additionalConnectionParams) { |
|
| 122 | - $normalizedType = $this->normalizeType($type); |
|
| 123 | - $eventManager = new EventManager(); |
|
| 124 | - switch ($normalizedType) { |
|
| 125 | - case 'mysql': |
|
| 126 | - $eventManager->addEventSubscriber( |
|
| 127 | - new SQLSessionInit("SET SESSION AUTOCOMMIT=1")); |
|
| 128 | - break; |
|
| 129 | - case 'oci': |
|
| 130 | - $eventManager->addEventSubscriber(new OracleSessionInit); |
|
| 131 | - // the driverOptions are unused in dbal and need to be mapped to the parameters |
|
| 132 | - if (isset($additionalConnectionParams['driverOptions'])) { |
|
| 133 | - $additionalConnectionParams = array_merge($additionalConnectionParams, $additionalConnectionParams['driverOptions']); |
|
| 134 | - } |
|
| 135 | - $host = $additionalConnectionParams['host']; |
|
| 136 | - $port = isset($additionalConnectionParams['port']) ? $additionalConnectionParams['port'] : null; |
|
| 137 | - $dbName = $additionalConnectionParams['dbname']; |
|
| 138 | - |
|
| 139 | - // we set the connect string as dbname and unset the host to coerce doctrine into using it as connect string |
|
| 140 | - if ($host === '') { |
|
| 141 | - $additionalConnectionParams['dbname'] = $dbName; // use dbname as easy connect name |
|
| 142 | - } else { |
|
| 143 | - $additionalConnectionParams['dbname'] = '//' . $host . (!empty($port) ? ":{$port}" : "") . '/' . $dbName; |
|
| 144 | - } |
|
| 145 | - unset($additionalConnectionParams['host']); |
|
| 146 | - break; |
|
| 147 | - |
|
| 148 | - case 'sqlite3': |
|
| 149 | - $journalMode = $additionalConnectionParams['sqlite.journal_mode']; |
|
| 150 | - $additionalConnectionParams['platform'] = new OCSqlitePlatform(); |
|
| 151 | - $eventManager->addEventSubscriber(new SQLiteSessionInit(true, $journalMode)); |
|
| 152 | - break; |
|
| 153 | - } |
|
| 154 | - /** @var Connection $connection */ |
|
| 155 | - $connection = DriverManager::getConnection( |
|
| 156 | - array_merge($this->getDefaultConnectionParams($type), $additionalConnectionParams), |
|
| 157 | - new Configuration(), |
|
| 158 | - $eventManager |
|
| 159 | - ); |
|
| 160 | - return $connection; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * @brief Normalize DBMS type |
|
| 165 | - * @param string $type DBMS type |
|
| 166 | - * @return string Normalized DBMS type |
|
| 167 | - */ |
|
| 168 | - public function normalizeType($type) { |
|
| 169 | - return $type === 'sqlite' ? 'sqlite3' : $type; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Checks whether the specified DBMS type is valid. |
|
| 174 | - * |
|
| 175 | - * @param string $type |
|
| 176 | - * @return bool |
|
| 177 | - */ |
|
| 178 | - public function isValidType($type) { |
|
| 179 | - $normalizedType = $this->normalizeType($type); |
|
| 180 | - return isset($this->defaultConnectionParams[$normalizedType]); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Create the connection parameters for the config |
|
| 185 | - * |
|
| 186 | - * @return array |
|
| 187 | - */ |
|
| 188 | - public function createConnectionParams() { |
|
| 189 | - $type = $this->config->getValue('dbtype', 'sqlite'); |
|
| 190 | - |
|
| 191 | - $connectionParams = [ |
|
| 192 | - 'user' => $this->config->getValue('dbuser', ''), |
|
| 193 | - 'password' => $this->config->getValue('dbpassword', ''), |
|
| 194 | - ]; |
|
| 195 | - $name = $this->config->getValue('dbname', self::DEFAULT_DBNAME); |
|
| 196 | - |
|
| 197 | - if ($this->normalizeType($type) === 'sqlite3') { |
|
| 198 | - $dataDir = $this->config->getValue("datadirectory", \OC::$SERVERROOT . '/data'); |
|
| 199 | - $connectionParams['path'] = $dataDir . '/' . $name . '.db'; |
|
| 200 | - } else { |
|
| 201 | - $host = $this->config->getValue('dbhost', ''); |
|
| 202 | - $connectionParams = array_merge($connectionParams, $this->splitHostFromPortAndSocket($host)); |
|
| 203 | - $connectionParams['dbname'] = $name; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - $connectionParams['tablePrefix'] = $this->config->getValue('dbtableprefix', self::DEFAULT_DBTABLEPREFIX); |
|
| 207 | - $connectionParams['sqlite.journal_mode'] = $this->config->getValue('sqlite.journal_mode', 'WAL'); |
|
| 208 | - |
|
| 209 | - //additional driver options, eg. for mysql ssl |
|
| 210 | - $driverOptions = $this->config->getValue('dbdriveroptions', null); |
|
| 211 | - if ($driverOptions) { |
|
| 212 | - $connectionParams['driverOptions'] = $driverOptions; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - // set default table creation options |
|
| 216 | - $connectionParams['defaultTableOptions'] = [ |
|
| 217 | - 'collate' => 'utf8_bin', |
|
| 218 | - 'tablePrefix' => $connectionParams['tablePrefix'] |
|
| 219 | - ]; |
|
| 220 | - |
|
| 221 | - if ($this->config->getValue('mysql.utf8mb4', false)) { |
|
| 222 | - $connectionParams['defaultTableOptions'] = [ |
|
| 223 | - 'collate' => 'utf8mb4_bin', |
|
| 224 | - 'charset' => 'utf8mb4', |
|
| 225 | - 'row_format' => 'compressed', |
|
| 226 | - 'tablePrefix' => $connectionParams['tablePrefix'] |
|
| 227 | - ]; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - return $connectionParams; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * @param string $host |
|
| 235 | - * @return array |
|
| 236 | - */ |
|
| 237 | - protected function splitHostFromPortAndSocket($host): array { |
|
| 238 | - $params = [ |
|
| 239 | - 'host' => $host, |
|
| 240 | - ]; |
|
| 241 | - |
|
| 242 | - $matches = []; |
|
| 243 | - if (preg_match('/^(.*):([^\]:]+)$/', $host, $matches)) { |
|
| 244 | - // Host variable carries a port or socket. |
|
| 245 | - $params['host'] = $matches[1]; |
|
| 246 | - if (is_numeric($matches[2])) { |
|
| 247 | - $params['port'] = (int) $matches[2]; |
|
| 248 | - } else { |
|
| 249 | - $params['unix_socket'] = $matches[2]; |
|
| 250 | - } |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - return $params; |
|
| 254 | - } |
|
| 41 | + /** @var string default database name */ |
|
| 42 | + const DEFAULT_DBNAME = 'owncloud'; |
|
| 43 | + |
|
| 44 | + /** @var string default database table prefix */ |
|
| 45 | + const DEFAULT_DBTABLEPREFIX = 'oc_'; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var array |
|
| 49 | + * |
|
| 50 | + * Array mapping DBMS type to default connection parameters passed to |
|
| 51 | + * \Doctrine\DBAL\DriverManager::getConnection(). |
|
| 52 | + */ |
|
| 53 | + protected $defaultConnectionParams = [ |
|
| 54 | + 'mysql' => [ |
|
| 55 | + 'adapter' => AdapterMySQL::class, |
|
| 56 | + 'charset' => 'UTF8', |
|
| 57 | + 'driver' => 'pdo_mysql', |
|
| 58 | + 'wrapperClass' => Connection::class, |
|
| 59 | + ], |
|
| 60 | + 'oci' => [ |
|
| 61 | + 'adapter' => AdapterOCI8::class, |
|
| 62 | + 'charset' => 'AL32UTF8', |
|
| 63 | + 'driver' => 'oci8', |
|
| 64 | + 'wrapperClass' => OracleConnection::class, |
|
| 65 | + ], |
|
| 66 | + 'pgsql' => [ |
|
| 67 | + 'adapter' => AdapterPgSql::class, |
|
| 68 | + 'driver' => 'pdo_pgsql', |
|
| 69 | + 'wrapperClass' => Connection::class, |
|
| 70 | + ], |
|
| 71 | + 'sqlite3' => [ |
|
| 72 | + 'adapter' => AdapterSqlite::class, |
|
| 73 | + 'driver' => 'pdo_sqlite', |
|
| 74 | + 'wrapperClass' => Connection::class, |
|
| 75 | + ], |
|
| 76 | + ]; |
|
| 77 | + |
|
| 78 | + /** @var SystemConfig */ |
|
| 79 | + private $config; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * ConnectionFactory constructor. |
|
| 83 | + * |
|
| 84 | + * @param SystemConfig $systemConfig |
|
| 85 | + */ |
|
| 86 | + public function __construct(SystemConfig $systemConfig) { |
|
| 87 | + $this->config = $systemConfig; |
|
| 88 | + if ($this->config->getValue('mysql.utf8mb4', false)) { |
|
| 89 | + $this->defaultConnectionParams['mysql']['charset'] = 'utf8mb4'; |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @brief Get default connection parameters for a given DBMS. |
|
| 95 | + * @param string $type DBMS type |
|
| 96 | + * @throws \InvalidArgumentException If $type is invalid |
|
| 97 | + * @return array Default connection parameters. |
|
| 98 | + */ |
|
| 99 | + public function getDefaultConnectionParams($type) { |
|
| 100 | + $normalizedType = $this->normalizeType($type); |
|
| 101 | + if (!isset($this->defaultConnectionParams[$normalizedType])) { |
|
| 102 | + throw new \InvalidArgumentException("Unsupported type: $type"); |
|
| 103 | + } |
|
| 104 | + $result = $this->defaultConnectionParams[$normalizedType]; |
|
| 105 | + // \PDO::MYSQL_ATTR_FOUND_ROWS may not be defined, e.g. when the MySQL |
|
| 106 | + // driver is missing. In this case, we won't be able to connect anyway. |
|
| 107 | + if ($normalizedType === 'mysql' && defined('\PDO::MYSQL_ATTR_FOUND_ROWS')) { |
|
| 108 | + $result['driverOptions'] = array( |
|
| 109 | + \PDO::MYSQL_ATTR_FOUND_ROWS => true, |
|
| 110 | + ); |
|
| 111 | + } |
|
| 112 | + return $result; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * @brief Get default connection parameters for a given DBMS. |
|
| 117 | + * @param string $type DBMS type |
|
| 118 | + * @param array $additionalConnectionParams Additional connection parameters |
|
| 119 | + * @return \OC\DB\Connection |
|
| 120 | + */ |
|
| 121 | + public function getConnection($type, $additionalConnectionParams) { |
|
| 122 | + $normalizedType = $this->normalizeType($type); |
|
| 123 | + $eventManager = new EventManager(); |
|
| 124 | + switch ($normalizedType) { |
|
| 125 | + case 'mysql': |
|
| 126 | + $eventManager->addEventSubscriber( |
|
| 127 | + new SQLSessionInit("SET SESSION AUTOCOMMIT=1")); |
|
| 128 | + break; |
|
| 129 | + case 'oci': |
|
| 130 | + $eventManager->addEventSubscriber(new OracleSessionInit); |
|
| 131 | + // the driverOptions are unused in dbal and need to be mapped to the parameters |
|
| 132 | + if (isset($additionalConnectionParams['driverOptions'])) { |
|
| 133 | + $additionalConnectionParams = array_merge($additionalConnectionParams, $additionalConnectionParams['driverOptions']); |
|
| 134 | + } |
|
| 135 | + $host = $additionalConnectionParams['host']; |
|
| 136 | + $port = isset($additionalConnectionParams['port']) ? $additionalConnectionParams['port'] : null; |
|
| 137 | + $dbName = $additionalConnectionParams['dbname']; |
|
| 138 | + |
|
| 139 | + // we set the connect string as dbname and unset the host to coerce doctrine into using it as connect string |
|
| 140 | + if ($host === '') { |
|
| 141 | + $additionalConnectionParams['dbname'] = $dbName; // use dbname as easy connect name |
|
| 142 | + } else { |
|
| 143 | + $additionalConnectionParams['dbname'] = '//' . $host . (!empty($port) ? ":{$port}" : "") . '/' . $dbName; |
|
| 144 | + } |
|
| 145 | + unset($additionalConnectionParams['host']); |
|
| 146 | + break; |
|
| 147 | + |
|
| 148 | + case 'sqlite3': |
|
| 149 | + $journalMode = $additionalConnectionParams['sqlite.journal_mode']; |
|
| 150 | + $additionalConnectionParams['platform'] = new OCSqlitePlatform(); |
|
| 151 | + $eventManager->addEventSubscriber(new SQLiteSessionInit(true, $journalMode)); |
|
| 152 | + break; |
|
| 153 | + } |
|
| 154 | + /** @var Connection $connection */ |
|
| 155 | + $connection = DriverManager::getConnection( |
|
| 156 | + array_merge($this->getDefaultConnectionParams($type), $additionalConnectionParams), |
|
| 157 | + new Configuration(), |
|
| 158 | + $eventManager |
|
| 159 | + ); |
|
| 160 | + return $connection; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * @brief Normalize DBMS type |
|
| 165 | + * @param string $type DBMS type |
|
| 166 | + * @return string Normalized DBMS type |
|
| 167 | + */ |
|
| 168 | + public function normalizeType($type) { |
|
| 169 | + return $type === 'sqlite' ? 'sqlite3' : $type; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Checks whether the specified DBMS type is valid. |
|
| 174 | + * |
|
| 175 | + * @param string $type |
|
| 176 | + * @return bool |
|
| 177 | + */ |
|
| 178 | + public function isValidType($type) { |
|
| 179 | + $normalizedType = $this->normalizeType($type); |
|
| 180 | + return isset($this->defaultConnectionParams[$normalizedType]); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Create the connection parameters for the config |
|
| 185 | + * |
|
| 186 | + * @return array |
|
| 187 | + */ |
|
| 188 | + public function createConnectionParams() { |
|
| 189 | + $type = $this->config->getValue('dbtype', 'sqlite'); |
|
| 190 | + |
|
| 191 | + $connectionParams = [ |
|
| 192 | + 'user' => $this->config->getValue('dbuser', ''), |
|
| 193 | + 'password' => $this->config->getValue('dbpassword', ''), |
|
| 194 | + ]; |
|
| 195 | + $name = $this->config->getValue('dbname', self::DEFAULT_DBNAME); |
|
| 196 | + |
|
| 197 | + if ($this->normalizeType($type) === 'sqlite3') { |
|
| 198 | + $dataDir = $this->config->getValue("datadirectory", \OC::$SERVERROOT . '/data'); |
|
| 199 | + $connectionParams['path'] = $dataDir . '/' . $name . '.db'; |
|
| 200 | + } else { |
|
| 201 | + $host = $this->config->getValue('dbhost', ''); |
|
| 202 | + $connectionParams = array_merge($connectionParams, $this->splitHostFromPortAndSocket($host)); |
|
| 203 | + $connectionParams['dbname'] = $name; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + $connectionParams['tablePrefix'] = $this->config->getValue('dbtableprefix', self::DEFAULT_DBTABLEPREFIX); |
|
| 207 | + $connectionParams['sqlite.journal_mode'] = $this->config->getValue('sqlite.journal_mode', 'WAL'); |
|
| 208 | + |
|
| 209 | + //additional driver options, eg. for mysql ssl |
|
| 210 | + $driverOptions = $this->config->getValue('dbdriveroptions', null); |
|
| 211 | + if ($driverOptions) { |
|
| 212 | + $connectionParams['driverOptions'] = $driverOptions; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + // set default table creation options |
|
| 216 | + $connectionParams['defaultTableOptions'] = [ |
|
| 217 | + 'collate' => 'utf8_bin', |
|
| 218 | + 'tablePrefix' => $connectionParams['tablePrefix'] |
|
| 219 | + ]; |
|
| 220 | + |
|
| 221 | + if ($this->config->getValue('mysql.utf8mb4', false)) { |
|
| 222 | + $connectionParams['defaultTableOptions'] = [ |
|
| 223 | + 'collate' => 'utf8mb4_bin', |
|
| 224 | + 'charset' => 'utf8mb4', |
|
| 225 | + 'row_format' => 'compressed', |
|
| 226 | + 'tablePrefix' => $connectionParams['tablePrefix'] |
|
| 227 | + ]; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + return $connectionParams; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * @param string $host |
|
| 235 | + * @return array |
|
| 236 | + */ |
|
| 237 | + protected function splitHostFromPortAndSocket($host): array { |
|
| 238 | + $params = [ |
|
| 239 | + 'host' => $host, |
|
| 240 | + ]; |
|
| 241 | + |
|
| 242 | + $matches = []; |
|
| 243 | + if (preg_match('/^(.*):([^\]:]+)$/', $host, $matches)) { |
|
| 244 | + // Host variable carries a port or socket. |
|
| 245 | + $params['host'] = $matches[1]; |
|
| 246 | + if (is_numeric($matches[2])) { |
|
| 247 | + $params['port'] = (int) $matches[2]; |
|
| 248 | + } else { |
|
| 249 | + $params['unix_socket'] = $matches[2]; |
|
| 250 | + } |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + return $params; |
|
| 254 | + } |
|
| 255 | 255 | } |
@@ -140,7 +140,7 @@ discard block |
||
| 140 | 140 | if ($host === '') { |
| 141 | 141 | $additionalConnectionParams['dbname'] = $dbName; // use dbname as easy connect name |
| 142 | 142 | } else { |
| 143 | - $additionalConnectionParams['dbname'] = '//' . $host . (!empty($port) ? ":{$port}" : "") . '/' . $dbName; |
|
| 143 | + $additionalConnectionParams['dbname'] = '//'.$host.(!empty($port) ? ":{$port}" : "").'/'.$dbName; |
|
| 144 | 144 | } |
| 145 | 145 | unset($additionalConnectionParams['host']); |
| 146 | 146 | break; |
@@ -195,8 +195,8 @@ discard block |
||
| 195 | 195 | $name = $this->config->getValue('dbname', self::DEFAULT_DBNAME); |
| 196 | 196 | |
| 197 | 197 | if ($this->normalizeType($type) === 'sqlite3') { |
| 198 | - $dataDir = $this->config->getValue("datadirectory", \OC::$SERVERROOT . '/data'); |
|
| 199 | - $connectionParams['path'] = $dataDir . '/' . $name . '.db'; |
|
| 198 | + $dataDir = $this->config->getValue("datadirectory", \OC::$SERVERROOT.'/data'); |
|
| 199 | + $connectionParams['path'] = $dataDir.'/'.$name.'.db'; |
|
| 200 | 200 | } else { |
| 201 | 201 | $host = $this->config->getValue('dbhost', ''); |
| 202 | 202 | $connectionParams = array_merge($connectionParams, $this->splitHostFromPortAndSocket($host)); |