summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorBohwaZ <bohwaz@users.noreply.github.com>2018-08-01 17:45:20 -0400
committerAdam Baratz <adambaratz@php.net>2018-08-01 17:45:20 -0400
commit6f9ebe677bf31d0c64046f18e6b0ac75340cb93e (patch)
treefd0049c804cb37a7558c3ee8a42485d12a551d82 /ext
parent1a303f1a2b6ed8853fea3a2079999eef7f462d86 (diff)
downloadphp-git-6f9ebe677bf31d0c64046f18e6b0ac75340cb93e.tar.gz
Add \PDO::SQLITE_ATTR_READONLY_STATEMENT
This attribute is a boolean value. It is taken from the return value of sqlite3_stmt_readonly(), indicating if and only if the prepared statement makes no direct changes to the content of the database.
Diffstat (limited to 'ext')
-rw-r--r--ext/pdo_sqlite/pdo_sqlite.c1
-rw-r--r--ext/pdo_sqlite/php_pdo_sqlite_int.h1
-rw-r--r--ext/pdo_sqlite/sqlite_statement.c24
-rw-r--r--ext/pdo_sqlite/tests/pdo_sqlite_statement_getattribute.phpt20
4 files changed, 45 insertions, 1 deletions
diff --git a/ext/pdo_sqlite/pdo_sqlite.c b/ext/pdo_sqlite/pdo_sqlite.c
index 235191bebb..f91dd13982 100644
--- a/ext/pdo_sqlite/pdo_sqlite.c
+++ b/ext/pdo_sqlite/pdo_sqlite.c
@@ -75,6 +75,7 @@ PHP_MINIT_FUNCTION(pdo_sqlite)
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);
+ REGISTER_PDO_CLASS_CONST_LONG("SQLITE_ATTR_READONLY_STATEMENT", (zend_long)PDO_SQLITE_ATTR_READONLY_STATEMENT);
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 7e9a97cf88..2cd855e75b 100644
--- a/ext/pdo_sqlite/php_pdo_sqlite_int.h
+++ b/ext/pdo_sqlite/php_pdo_sqlite_int.h
@@ -76,6 +76,7 @@ extern const struct pdo_stmt_methods sqlite_stmt_methods;
enum {
PDO_SQLITE_ATTR_OPEN_FLAGS = PDO_ATTR_DRIVER_SPECIFIC,
+ PDO_SQLITE_ATTR_READONLY_STATEMENT
};
#endif
diff --git a/ext/pdo_sqlite/sqlite_statement.c b/ext/pdo_sqlite/sqlite_statement.c
index c82e2d34eb..4a9d2462bf 100644
--- a/ext/pdo_sqlite/sqlite_statement.c
+++ b/ext/pdo_sqlite/sqlite_statement.c
@@ -350,6 +350,28 @@ static int pdo_sqlite_stmt_cursor_closer(pdo_stmt_t *stmt)
return 1;
}
+static int pdo_sqlite_stmt_get_attribute(pdo_stmt_t *stmt, zend_long attr, zval *val)
+{
+ pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
+
+ switch (attr) {
+ case PDO_SQLITE_ATTR_READONLY_STATEMENT:
+ ZVAL_FALSE(val);
+
+#if SQLITE_VERSION_NUMBER >= 3007004
+ if (sqlite3_stmt_readonly(S->stmt)) {
+ ZVAL_TRUE(val);
+ }
+#endif
+ break;
+
+ default:
+ return 0;
+ }
+
+ return 1;
+}
+
const struct pdo_stmt_methods sqlite_stmt_methods = {
pdo_sqlite_stmt_dtor,
pdo_sqlite_stmt_execute,
@@ -358,7 +380,7 @@ const struct pdo_stmt_methods sqlite_stmt_methods = {
pdo_sqlite_stmt_get_col,
pdo_sqlite_stmt_param_hook,
NULL, /* set_attr */
- NULL, /* get_attr */
+ pdo_sqlite_stmt_get_attribute, /* get_attr */
pdo_sqlite_stmt_col_meta,
NULL, /* next_rowset */
pdo_sqlite_stmt_cursor_closer
diff --git a/ext/pdo_sqlite/tests/pdo_sqlite_statement_getattribute.phpt b/ext/pdo_sqlite/tests/pdo_sqlite_statement_getattribute.phpt
new file mode 100644
index 0000000000..559907c380
--- /dev/null
+++ b/ext/pdo_sqlite/tests/pdo_sqlite_statement_getattribute.phpt
@@ -0,0 +1,20 @@
+--TEST--
+PDO_sqlite: Testing PDOStatement::getAttribute()
+--SKIPIF--
+<?php if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; ?>
+--FILE--
+<?php
+
+$db = new PDO('sqlite::memory:');
+
+$st = $db->prepare('SELECT 1;');
+
+var_dump($st->getAttribute(PDO::SQLITE_ATTR_READONLY_STATEMENT));
+
+$st = $db->prepare('CREATE TABLE test (a TEXT);');
+
+var_dump($st->getAttribute(PDO::SQLITE_ATTR_READONLY_STATEMENT));
+?>
+--EXPECTF--
+bool(true)
+bool(false) \ No newline at end of file