summaryrefslogtreecommitdiff
path: root/ext/pdo_sqlite
diff options
context:
space:
mode:
authorBohwaZ <bohwaz@github.com>2017-09-04 11:02:12 +1200
committerJoe Watkins <krakjoe@php.net>2017-09-06 09:56:02 +0100
commited2f6510da7c68b5690c60344cbb9fe73241592a (patch)
treec655116faa99c0e05f35c7167f493100f14c343a /ext/pdo_sqlite
parentfafd67cb71e2d47f7cae3a74d9371fe6cda55442 (diff)
downloadphp-git-ed2f6510da7c68b5690c60344cbb9fe73241592a.tar.gz
Add support for SQLite open flags
Diffstat (limited to 'ext/pdo_sqlite')
-rw-r--r--ext/pdo_sqlite/pdo_sqlite.c5
-rw-r--r--ext/pdo_sqlite/php_pdo_sqlite_int.h5
-rw-r--r--ext/pdo_sqlite/sqlite_driver.c9
-rw-r--r--ext/pdo_sqlite/tests/pdo_sqlite_open_flags.phpt33
4 files changed, 51 insertions, 1 deletions
diff --git a/ext/pdo_sqlite/pdo_sqlite.c b/ext/pdo_sqlite/pdo_sqlite.c
index d0eb1823d6..60e1a07afb 100644
--- a/ext/pdo_sqlite/pdo_sqlite.c
+++ b/ext/pdo_sqlite/pdo_sqlite.c
@@ -73,6 +73,11 @@ PHP_MINIT_FUNCTION(pdo_sqlite)
REGISTER_PDO_CLASS_CONST_LONG("SQLITE_DETERMINISTIC", (zend_long)SQLITE_DETERMINISTIC);
#endif
+ REGISTER_PDO_CLASS_CONST_LONG("SQLITE_ATTR_OPEN_FLAGS", (zend_long)PDO_SQLITE_ATTR_OPEN_FLAGS);
+ REGISTER_PDO_CLASS_CONST_LONG("SQLITE_OPEN_READONLY", (zend_long)SQLITE_OPEN_READONLY);
+ REGISTER_PDO_CLASS_CONST_LONG("SQLITE_OPEN_READWRITE", (zend_long)SQLITE_OPEN_READWRITE);
+ REGISTER_PDO_CLASS_CONST_LONG("SQLITE_OPEN_CREATE", (zend_long)SQLITE_OPEN_CREATE);
+
return php_pdo_register_driver(&pdo_sqlite_driver);
}
/* }}} */
diff --git a/ext/pdo_sqlite/php_pdo_sqlite_int.h b/ext/pdo_sqlite/php_pdo_sqlite_int.h
index a390d6497f..1c90416b9f 100644
--- a/ext/pdo_sqlite/php_pdo_sqlite_int.h
+++ b/ext/pdo_sqlite/php_pdo_sqlite_int.h
@@ -75,4 +75,9 @@ extern int _pdo_sqlite_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file,
#define pdo_sqlite_error_stmt(s) _pdo_sqlite_error(stmt->dbh, stmt, __FILE__, __LINE__)
extern struct pdo_stmt_methods sqlite_stmt_methods;
+
+enum {
+ PDO_SQLITE_ATTR_OPEN_FLAGS = PDO_ATTR_DRIVER_SPECIFIC,
+};
+
#endif
diff --git a/ext/pdo_sqlite/sqlite_driver.c b/ext/pdo_sqlite/sqlite_driver.c
index e234406be9..1b4fecae81 100644
--- a/ext/pdo_sqlite/sqlite_driver.c
+++ b/ext/pdo_sqlite/sqlite_driver.c
@@ -791,7 +791,7 @@ static int pdo_sqlite_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{
{
pdo_sqlite_db_handle *H;
int i, ret = 0;
- zend_long timeout = 60;
+ zend_long timeout = 60, flags;
char *filename;
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) /* {{
goto cleanup;
}
+ flags = pdo_attr_lval(driver_options, PDO_SQLITE_ATTR_OPEN_FLAGS, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
+
+#if SQLITE_VERSION_NUMBER >= 3005000
+ i = sqlite3_open_v2(filename, &H->db, flags, NULL);
+#else
i = sqlite3_open(filename, &H->db);
+#endif
+
efree(filename);
if (i != SQLITE_OK) {
diff --git a/ext/pdo_sqlite/tests/pdo_sqlite_open_flags.phpt b/ext/pdo_sqlite/tests/pdo_sqlite_open_flags.phpt
new file mode 100644
index 0000000000..2248324d0c
--- /dev/null
+++ b/ext/pdo_sqlite/tests/pdo_sqlite_open_flags.phpt
@@ -0,0 +1,33 @@
+--TEST--
+PDO_sqlite: Testing open flags
+--SKIPIF--
+<?php if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; ?>
+--FILE--
+<?php
+
+$filename = tempnam(sys_get_temp_dir(), 'sqlite');
+
+// Default open flag is read-write|create
+$db = new PDO('sqlite:' . $filename, null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
+
+var_dump($db->exec('CREATE TABLE test1 (id INT);'));
+
+try {
+ $db = new PDO('sqlite:' . $filename, null, null, [PDO::SQLITE_ATTR_OPEN_FLAGS => PDO::SQLITE_OPEN_READONLY, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
+
+ var_dump($db->exec('CREATE TABLE test2 (id INT);'));
+}
+finally {
+ // Cleanup
+ unlink($filename);
+}
+
+?>
+--EXPECTF--
+int(0)
+
+Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in %s
+Stack trace:
+%s
+#1 {main}
+ thrown in %s