Skip to content

Commit ed2f651

Browse files
BohwaZkrakjoe
authored andcommitted
Add support for SQLite open flags
1 parent fafd67c commit ed2f651

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ PHP NEWS
4343
. Fixed bug #74631 (PDO_PCO with PHP-FPM: OCI environment initialized
4444
before PHP-FPM sets it up). (Ingmar Runge)
4545

46+
- PDO SQLite
47+
. Add support for additional open flags
48+
4649
- phar:
4750
. Fixed bug #74991 (include_path has a 4096 char limit in some cases).
4851
(bwbroersma)

ext/pdo_sqlite/pdo_sqlite.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ PHP_MINIT_FUNCTION(pdo_sqlite)
7373
REGISTER_PDO_CLASS_CONST_LONG("SQLITE_DETERMINISTIC", (zend_long)SQLITE_DETERMINISTIC);
7474
#endif
7575

76+
REGISTER_PDO_CLASS_CONST_LONG("SQLITE_ATTR_OPEN_FLAGS", (zend_long)PDO_SQLITE_ATTR_OPEN_FLAGS);
77+
REGISTER_PDO_CLASS_CONST_LONG("SQLITE_OPEN_READONLY", (zend_long)SQLITE_OPEN_READONLY);
78+
REGISTER_PDO_CLASS_CONST_LONG("SQLITE_OPEN_READWRITE", (zend_long)SQLITE_OPEN_READWRITE);
79+
REGISTER_PDO_CLASS_CONST_LONG("SQLITE_OPEN_CREATE", (zend_long)SQLITE_OPEN_CREATE);
80+
7681
return php_pdo_register_driver(&pdo_sqlite_driver);
7782
}
7883
/* }}} */

ext/pdo_sqlite/php_pdo_sqlite_int.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,9 @@ extern int _pdo_sqlite_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file,
7575
#define pdo_sqlite_error_stmt(s) _pdo_sqlite_error(stmt->dbh, stmt, __FILE__, __LINE__)
7676

7777
extern struct pdo_stmt_methods sqlite_stmt_methods;
78+
79+
enum {
80+
PDO_SQLITE_ATTR_OPEN_FLAGS = PDO_ATTR_DRIVER_SPECIFIC,
81+
};
82+
7883
#endif

ext/pdo_sqlite/sqlite_driver.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ static int pdo_sqlite_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{
791791
{
792792
pdo_sqlite_db_handle *H;
793793
int i, ret = 0;
794-
zend_long timeout = 60;
794+
zend_long timeout = 60, flags;
795795
char *filename;
796796

797797
H = pecalloc(1, sizeof(pdo_sqlite_db_handle), dbh->is_persistent);
@@ -809,7 +809,14 @@ static int pdo_sqlite_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{
809809
goto cleanup;
810810
}
811811

812+
flags = pdo_attr_lval(driver_options, PDO_SQLITE_ATTR_OPEN_FLAGS, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
813+
814+
#if SQLITE_VERSION_NUMBER >= 3005000
815+
i = sqlite3_open_v2(filename, &H->db, flags, NULL);
816+
#else
812817
i = sqlite3_open(filename, &H->db);
818+
#endif
819+
813820
efree(filename);
814821

815822
if (i != SQLITE_OK) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
PDO_sqlite: Testing open flags
3+
--SKIPIF--
4+
<?php if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; ?>
5+
--FILE--
6+
<?php
7+
8+
$filename = tempnam(sys_get_temp_dir(), 'sqlite');
9+
10+
// Default open flag is read-write|create
11+
$db = new PDO('sqlite:' . $filename, null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
12+
13+
var_dump($db->exec('CREATE TABLE test1 (id INT);'));
14+
15+
try {
16+
$db = new PDO('sqlite:' . $filename, null, null, [PDO::SQLITE_ATTR_OPEN_FLAGS => PDO::SQLITE_OPEN_READONLY, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
17+
18+
var_dump($db->exec('CREATE TABLE test2 (id INT);'));
19+
}
20+
finally {
21+
// Cleanup
22+
unlink($filename);
23+
}
24+
25+
?>
26+
--EXPECTF--
27+
int(0)
28+
29+
Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in %s
30+
Stack trace:
31+
%s
32+
#1 {main}
33+
thrown in %s

0 commit comments

Comments
 (0)