com php-src: Add support for SQLite open flags: NEWS ext/pdo_sqlite/pdo_sqlite.c ext/pdo_sqlite/php_pdo _sqlite_int.h ext/pdo_sqlite/sqlite_driver.c
ex t/pdo_sqlite/tests/pdo_sqlite_open_flags.phpt
| From: | Joe Watkins | Date: | Wed, 06 Sep 2017 08:56:02 +0000 |
| Subject: | com php-src: Add support for SQLite open flags: NEWS ext/pdo_sqlite/pdo_sqlite.c ext/pdo_sqlite/php_pdo _sqlite_int.h ext/pdo_sqlite/sqlite_driver.c ex t/pdo_sqlite/tests/pdo_sqlite_open_flags.phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-99281@lists.php.net to get a copy of this message | ||
Commit: ed2f6510da7c68b5690c60344cbb9fe73241592a
Author: BohwaZ <bohwaz@github.com> Mon, 4 Sep 2017 11:02:12 +1200
Committer: Joe Watkins <krakjoe@php.net> Wed, 6 Sep 2017 09:56:02 +0100
Parents: fafd67cb71e2d47f7cae3a74d9371fe6cda55442
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=ed2f6510da7c68b5690c60344cbb9fe73241592a
Log:
Add support for SQLite open flags
Changed paths:
M NEWS
M ext/pdo_sqlite/pdo_sqlite.c
M ext/pdo_sqlite/php_pdo_sqlite_int.h
M ext/pdo_sqlite/sqlite_driver.c
A ext/pdo_sqlite/tests/pdo_sqlite_open_flags.phpt
Diff:
diff --git a/NEWS b/NEWS
index aa87b85..a13a638 100644
--- a/NEWS
+++ b/NEWS
@@ -43,6 +43,9 @@ PHP NEWS
. Fixed bug #74631 (PDO_PCO with PHP-FPM: OCI environment initialized
before PHP-FPM sets it up). (Ingmar Runge)
+- PDO SQLite
+ . Add support for additional open flags
+
- phar:
. Fixed bug #74991 (include_path has a 4096 char limit in some cases).
(bwbroersma)
diff --git a/ext/pdo_sqlite/pdo_sqlite.c b/ext/pdo_sqlite/pdo_sqlite.c
index d0eb182..60e1a07 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 a390d64..1c90416 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 e234406..1b4feca 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 0000000..2248324
--- /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