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