Skip to content

Commit 260a304

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

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

ext/pdo_sqlite/sqlite_driver.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,20 @@ static const struct pdo_dbh_methods sqlite_methods = {
734734

735735
static char *make_filename_safe(const char *filename)
736736
{
737+
if (*filename && strncasecmp(filename, "file:", 5) == 0) {
738+
char *fullpath = expand_filepath(filename+5, NULL);
739+
740+
if (!fullpath) {
741+
return NULL;
742+
}
743+
744+
if (php_check_open_basedir(fullpath)) {
745+
efree(fullpath);
746+
return NULL;
747+
}
748+
efree(fullpath);
749+
return estrdup(filename);
750+
}
737751
if (*filename && memcmp(filename, ":memory:", sizeof(":memory:"))) {
738752
char *fullpath = expand_filepath(filename, NULL);
739753

@@ -806,7 +820,7 @@ static int pdo_sqlite_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{
806820

807821
flags = pdo_attr_lval(driver_options, PDO_SQLITE_ATTR_OPEN_FLAGS, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
808822

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

811825
efree(filename);
812826

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)