Skip to content

Commit 1e8bfd7

Browse files
authored
Merge pull request #2053 from swiffer/sqlite-params
Support sqlite query parameters
2 parents acdd859 + b80ee4d commit 1e8bfd7

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

docs/en/configuration.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,18 @@ Declaring an SQLite database uses a simplified structure:
372372
adapter: sqlite
373373
memory: true # Setting memory to *any* value overrides name
374374
375+
Starting with PHP 8.1 the SQlite adapter supports ``cache`` and ``mode``
376+
query parameters by using the `URI scheme <https://www.sqlite.org/uri.html>`_ as long as ``open_basedir`` is unset.
377+
378+
.. code-block:: yaml
379+
380+
environments:
381+
testing:
382+
adapter: sqlite
383+
name: my_app
384+
mode: memory # Determines if the new database is opened read-only, read-write, read-write and created if it does not exist, or that the database is a pure in-memory database that never interacts with disk, respectively.
385+
cache: shared # Determines if the new database is opened using shared cache mode or with a private cache.
386+
375387
SQL Server
376388
`````````````````
377389

src/Phinx/Db/Adapter/SQLiteAdapter.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,26 @@ public function connect()
155155

156156
$options = $this->getOptions();
157157

158-
// use a memory database if the option was specified
159-
if (!empty($options['memory']) || $options['name'] === static::MEMORY) {
160-
$dsn = 'sqlite:' . static::MEMORY;
158+
if (PHP_VERSION_ID < 80100 && (!empty($options['mode']) || !empty($options['cache']))) {
159+
throw new RuntimeException('SQLite URI support requires PHP 8.1.');
160+
} elseif ((!empty($options['mode']) || !empty($options['cache'])) && !empty($options['memory'])) {
161+
throw new RuntimeException('Memory must not be set when cache or mode are.');
162+
} elseif (PHP_VERSION_ID >= 80100 && (!empty($options['mode']) || !empty($options['cache']))) {
163+
$params = [];
164+
if (!empty($options['cache'])) {
165+
$params[] = 'cache=' . $options['cache'];
166+
}
167+
if (!empty($options['mode'])) {
168+
$params[] = 'mode=' . $options['mode'];
169+
}
170+
$dsn = 'sqlite:file:' . $options['name'] ?? '' . '?' . implode('&', $params);
161171
} else {
162-
$dsn = 'sqlite:' . $options['name'] . $this->suffix;
172+
// use a memory database if the option was specified
173+
if (!empty($options['memory']) || $options['name'] === static::MEMORY) {
174+
$dsn = 'sqlite:' . static::MEMORY;
175+
} else {
176+
$dsn = 'sqlite:' . $options['name'] . $this->suffix;
177+
}
163178
}
164179

165180
$driverOptions = [];

0 commit comments

Comments
 (0)