Skip to content

Commit 3870e58

Browse files
author
Scott MacVicar
committed
Add SQLite3_Stmt::readOnly for checking if a statement is read only
1 parent 5eb2646 commit 3870e58

3 files changed

Lines changed: 81 additions & 6 deletions

File tree

NEWS

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
3-
?? ??? 20??, PHP 5.3.5
3+
?? ??? 2011, PHP 5.3.5
44
- Upgraded bundled Sqlite3 to version 3.7.4. (Ilia)
55
- Upgraded bundled PCRE to version 8.11. (Ilia)
66

@@ -79,8 +79,9 @@
7979
. Fixed bug #53515 (property_exists incorrect on ArrayObject null and 0
8080
values). (Felipe)
8181

82-
- SQLite extension:
82+
- SQLite3 extension:
8383
. Fixed memory leaked introduced by the NULL poisoning patch (Mateusz Kocielski, Pierre)
84+
. Add SQlite3_Stmt::readonly() for checking if a statement is read only. (Scott)
8485

8586
- Streams:
8687
. Implemented FR #26158 (open arbitrary file descriptor with fopen). (Gustavo)

ext/sqlite3/sqlite3.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,10 +1081,9 @@ static int php_sqlite3_stream_cast(php_stream *stream, int castas, void **ret TS
10811081

10821082
static int php_sqlite3_stream_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC)
10831083
{
1084-
/* TODO: fill in details based on Data: and Content-Length: headers, and/or data
1085-
* from curl_easy_getinfo().
1086-
* For now, return -1 to indicate that it doesn't make sense to stat this stream */
1087-
return -1;
1084+
php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
1085+
ssb->sb.st_size = sqlite3_stream->size;
1086+
return 0;
10881087
}
10891088

10901089
static php_stream_ops php_stream_sqlite3_ops = {
@@ -1234,6 +1233,27 @@ PHP_METHOD(sqlite3stmt, clear)
12341233
}
12351234
/* }}} */
12361235

1236+
/* {{{ proto bool SQLite3Stmt::readOnly()
1237+
Returns true if a statement is definitely read only */
1238+
PHP_METHOD(sqlite3stmt, readOnly)
1239+
{
1240+
php_sqlite3_stmt *stmt_obj;
1241+
zval *object = getThis();
1242+
stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1243+
1244+
if (zend_parse_parameters_none() == FAILURE) {
1245+
return;
1246+
}
1247+
1248+
#if SQLITE_VERSION_NUMBER >= 3007004
1249+
if (sqlite3_stmt_readonly(stmt_obj->stmt)) {
1250+
RETURN_TRUE;
1251+
}
1252+
#endif
1253+
RETURN_FALSE;
1254+
}
1255+
/* }}} */
1256+
12371257
static int register_bound_parameter_to_sqlite(struct php_sqlite3_bound_param *param, php_sqlite3_stmt *stmt TSRMLS_DC) /* {{{ */
12381258
{
12391259
HashTable *hash;
@@ -1804,6 +1824,7 @@ static zend_function_entry php_sqlite3_stmt_class_methods[] = {
18041824
PHP_ME(sqlite3stmt, execute, arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
18051825
PHP_ME(sqlite3stmt, bindParam, arginfo_sqlite3stmt_bindparam, ZEND_ACC_PUBLIC)
18061826
PHP_ME(sqlite3stmt, bindValue, arginfo_sqlite3stmt_bindvalue, ZEND_ACC_PUBLIC)
1827+
PHP_ME(sqlite3stmt, readOnly, arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
18071828
PHP_ME(sqlite3stmt, __construct, arginfo_sqlite3stmt_construct, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
18081829
{NULL, NULL, NULL}
18091830
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
SQLite3_stmt::readOnly check
3+
--SKIPIF--
4+
<?php require_once(dirname(__FILE__) . '/skipif.inc');
5+
$version = SQLite3::version();
6+
if ($version['versionNumber'] < 3007004) {
7+
die("skip");
8+
}
9+
?>
10+
--FILE--
11+
<?php
12+
13+
require_once(dirname(__FILE__) . '/new_db.inc');
14+
define('TIMENOW', time());
15+
16+
echo "Creating Table\n";
17+
var_dump($db->exec('CREATE TABLE test (time INTEGER, id STRING)'));
18+
19+
echo "INSERT into table\n";
20+
var_dump($db->exec("INSERT INTO test (time, id) VALUES (" . TIMENOW . ", 'a')"));
21+
var_dump($db->exec("INSERT INTO test (time, id) VALUES (" . TIMENOW . ", 'b')"));
22+
23+
echo "Checking select statement\n";
24+
$stmt = $db->prepare("SELECT * FROM test WHERE id = ? ORDER BY id ASC");
25+
var_dump($stmt->readOnly());
26+
27+
echo "Checking update statement\n";
28+
$stmt = $db->prepare("UPDATE test SET id = 'c' WHERE id = ?");
29+
var_dump($stmt->readOnly());
30+
31+
echo "Checking delete statement\n";
32+
$stmt = $db->prepare("DELETE FROM test");
33+
var_dump($stmt->readOnly());
34+
35+
echo "Closing database\n";
36+
var_dump($db->close());
37+
echo "Done\n";
38+
?>
39+
--EXPECTF--
40+
Creating Table
41+
bool(true)
42+
INSERT into table
43+
bool(true)
44+
bool(true)
45+
Checking select statement
46+
bool(true)
47+
Checking update statement
48+
bool(false)
49+
Checking delete statement
50+
bool(false)
51+
Closing database
52+
bool(true)
53+
Done

0 commit comments

Comments
 (0)