Skip to content

Commit 626ee74

Browse files
author
BohwaZ
committed
Change flags to use SQLITE3_OPEN_READ* constants instead of a fake-boolean, add tests on errors
1 parent 208aea1 commit 626ee74

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

ext/sqlite3/sqlite3.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ static size_t php_sqlite3_stream_write(php_stream *stream, const char *buf, size
10641064
{
10651065
php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
10661066

1067-
if (sqlite3_stream->flags == 0) {
1067+
if (sqlite3_stream->flags & SQLITE_OPEN_READONLY) {
10681068
php_error_docref(NULL, E_WARNING, "Can't write to blob stream: is open as read only");
10691069
return 0;
10701070
}
@@ -1221,7 +1221,7 @@ PHP_METHOD(sqlite3, openBlob)
12211221
zval *object = getThis();
12221222
char *table, *column, *dbname = "main", *mode = "rb";
12231223
size_t table_len, column_len, dbname_len;
1224-
zend_long rowid, flags = 0;
1224+
zend_long rowid, flags = SQLITE_OPEN_READONLY, sqlite_flags = 0;
12251225
sqlite3_blob *blob = NULL;
12261226
php_stream_sqlite3_data *sqlite3_stream;
12271227
php_stream *stream;
@@ -1234,7 +1234,9 @@ PHP_METHOD(sqlite3, openBlob)
12341234
return;
12351235
}
12361236

1237-
if (sqlite3_blob_open(db_obj->db, dbname, table, column, rowid, flags, &blob) != SQLITE_OK) {
1237+
sqlite_flags = (flags & SQLITE_OPEN_READWRITE) ? 1 : 0;
1238+
1239+
if (sqlite3_blob_open(db_obj->db, dbname, table, column, rowid, sqlite_flags, &blob) != SQLITE_OK) {
12381240
php_sqlite3_error(db_obj, "Unable to open blob: %s", sqlite3_errmsg(db_obj->db));
12391241
RETURN_FALSE;
12401242
}
@@ -1245,7 +1247,7 @@ PHP_METHOD(sqlite3, openBlob)
12451247
sqlite3_stream->position = 0;
12461248
sqlite3_stream->size = sqlite3_blob_bytes(blob);
12471249

1248-
if (flags != 0) {
1250+
if (sqlite_flags != 0) {
12491251
mode = "r+b";
12501252
}
12511253

ext/sqlite3/tests/sqlite3_30_blobopen.phpt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,20 @@ $stream = $db->openBlob('test', 'data', 1);
2525
var_dump($stream);
2626
echo "Stream Contents\n";
2727
var_dump(stream_get_contents($stream));
28+
echo "Writing to read-only stream\n";
29+
var_dump(fwrite($stream, 'ABCD'));
2830
echo "Closing Stream\n";
2931
var_dump(fclose($stream));
3032
echo "Opening stream in write mode\n";
31-
$stream = $db->openBlob('test', 'data', 1, 'main', 1);
33+
$stream = $db->openBlob('test', 'data', 1, 'main', SQLITE3_OPEN_READWRITE);
3234
var_dump($stream);
3335
echo "Writing to blob\n";
3436
var_dump(fwrite($stream, 'ABCD'));
3537
echo "Stream Contents\n";
3638
fseek($stream, 0);
3739
var_dump(stream_get_contents($stream));
40+
echo "Expanding blob size\n";
41+
var_dump(fwrite($stream, 'ABCD ABCD ABCD'));
3842
echo "Closing Stream\n";
3943
var_dump(fclose($stream));
4044
echo "Closing database\n";
@@ -53,6 +57,10 @@ bool(true)
5357
resource(%d) of type (stream)
5458
Stream Contents
5559
string(9) "TEST TEST"
60+
Writing to read-only stream
61+
62+
Warning: fwrite(): Can't write to blob stream: is open as read only in %s on line %d
63+
int(0)
5664
Closing Stream
5765
bool(true)
5866
Opening stream in write mode
@@ -61,6 +69,10 @@ Writing to blob
6169
int(4)
6270
Stream Contents
6371
string(9) "ABCD TEST"
72+
Expanding blob size
73+
74+
Warning: fwrite(): It is not possible to increase the size of a BLOB in %s on line %d
75+
int(0)
6476
Closing Stream
6577
bool(true)
6678
Closing database

0 commit comments

Comments
 (0)