Skip to content

Commit b2c6ff5

Browse files
committed
enable to specify sqlite3 DSN (file:/)
1 parent a06c20a commit b2c6ff5

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

ext/pdo_sqlite/sqlite_driver.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ static const struct pdo_dbh_methods sqlite_methods = {
734734

735735
static char *make_filename_safe(const char *filename)
736736
{
737-
if (*filename && memcmp(filename, ":memory:", sizeof(":memory:"))) {
737+
if (*filename && strncasecmp(filename, "file:", 5) != 0 && memcmp(filename, ":memory:", sizeof(":memory:"))) {
738738
char *fullpath = expand_filepath(filename, NULL);
739739

740740
if (!fullpath) {
@@ -806,7 +806,7 @@ static int pdo_sqlite_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{
806806

807807
flags = pdo_attr_lval(driver_options, PDO_SQLITE_ATTR_OPEN_FLAGS, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
808808

809-
i = sqlite3_open_v2(filename, &H->db, flags, NULL);
809+
i = sqlite3_open_v2(filename, &H->db, flags | SQLITE_OPEN_URI, NULL);
810810

811811
efree(filename);
812812

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
PDO_sqlite: Testing filename uri
3+
--SKIPIF--
4+
<?php if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; ?>
5+
--FILE--
6+
<?php
7+
8+
// create with default read-write|create mode
9+
$filename = "file:" . __DIR__ . DIRECTORY_SEPARATOR . "pdo_sqlite_filename_uri.db";
10+
11+
$db = new PDO('sqlite:' . $filename);
12+
13+
var_dump($db->exec('CREATE TABLE test1 (id INT);'));
14+
15+
// create with readonly mode
16+
$filename = "file:" . __DIR__ . DIRECTORY_SEPARATOR . "pdo_sqlite_filename_uri.db?mode=ro";
17+
18+
$db = new PDO('sqlite:' . $filename);
19+
20+
var_dump($db->exec('CREATE TABLE test2 (id INT);'));
21+
22+
?>
23+
--CLEAN--
24+
<?php
25+
$filename = __DIR__ . DIRECTORY_SEPARATOR . "pdo_sqlite_filename_uri.db";
26+
if (file_exists($filename)) {
27+
unlink($filename);
28+
}
29+
?>
30+
--EXPECTF--
31+
int(0)
32+
33+
Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in %s
34+
Stack trace:
35+
%s
36+
#1 {main}
37+
thrown in %s

0 commit comments

Comments
 (0)