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