Skip to content

Commit

Permalink
Add support for SQLite open flags
Browse files Browse the repository at this point in the history
  • Loading branch information
BohwaZ authored and krakjoe committed Sep 6, 2017
1 parent fafd67c commit ed2f651
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions ext/pdo_sqlite/pdo_sqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
/* }}} */
Expand Down
5 changes: 5 additions & 0 deletions ext/pdo_sqlite/php_pdo_sqlite_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 8 additions & 1 deletion ext/pdo_sqlite/sqlite_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
33 changes: 33 additions & 0 deletions ext/pdo_sqlite/tests/pdo_sqlite_open_flags.phpt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ed2f651

Please sign in to comment.