@@ -1057,13 +1057,36 @@ typedef struct {
1057
1057
sqlite3_blob * blob ;
1058
1058
size_t position ;
1059
1059
size_t size ;
1060
+ int flags ;
1060
1061
} php_stream_sqlite3_data ;
1061
1062
1062
1063
static size_t php_sqlite3_stream_write (php_stream * stream , const char * buf , size_t count )
1063
1064
{
1064
- /* php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract; */
1065
+ php_stream_sqlite3_data * sqlite3_stream = (php_stream_sqlite3_data * ) stream -> abstract ;
1065
1066
1066
- return 0 ;
1067
+ if (sqlite3_stream -> flags == 0 ) {
1068
+ php_error_docref (NULL , E_WARNING , "Can't write to blob stream: is open as read only" );
1069
+ return 0 ;
1070
+ }
1071
+
1072
+ if (sqlite3_stream -> position + count > sqlite3_stream -> size ) {
1073
+ php_error_docref (NULL , E_WARNING , "It is not possible to increase the size of a BLOB" );
1074
+ return 0 ;
1075
+ }
1076
+
1077
+ if (sqlite3_blob_write (sqlite3_stream -> blob , buf , count , sqlite3_stream -> position ) != SQLITE_OK ) {
1078
+ return 0 ;
1079
+ }
1080
+
1081
+ if (sqlite3_stream -> position + count >= sqlite3_stream -> size ) {
1082
+ stream -> eof = 1 ;
1083
+ sqlite3_stream -> position = sqlite3_stream -> size ;
1084
+ }
1085
+ else {
1086
+ sqlite3_stream -> position += count ;
1087
+ }
1088
+
1089
+ return count ;
1067
1090
}
1068
1091
1069
1092
static size_t php_sqlite3_stream_read (php_stream * stream , char * buf , size_t count )
@@ -1190,13 +1213,13 @@ static php_stream_ops php_stream_sqlite3_ops = {
1190
1213
NULL
1191
1214
};
1192
1215
1193
- /* {{{ proto resource SQLite3::openBlob(string table, string column, int rowid [, string dbname])
1216
+ /* {{{ proto resource SQLite3::openBlob(string table, string column, int rowid [, string dbname [, int flags] ])
1194
1217
Open a blob as a stream which we can read / write to. */
1195
1218
PHP_METHOD (sqlite3 , openBlob )
1196
1219
{
1197
1220
php_sqlite3_db_object * db_obj ;
1198
1221
zval * object = getThis ();
1199
- char * table , * column , * dbname = "main" ;
1222
+ char * table , * column , * dbname = "main" , * mode = "rb" ;
1200
1223
size_t table_len , column_len , dbname_len ;
1201
1224
zend_long rowid , flags = 0 ;
1202
1225
sqlite3_blob * blob = NULL ;
@@ -1207,7 +1230,7 @@ PHP_METHOD(sqlite3, openBlob)
1207
1230
1208
1231
SQLITE3_CHECK_INITIALIZED (db_obj , db_obj -> initialised , SQLite3 )
1209
1232
1210
- if (zend_parse_parameters (ZEND_NUM_ARGS (), "ssl|s " , & table , & table_len , & column , & column_len , & rowid , & dbname , & dbname_len ) == FAILURE ) {
1233
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "ssl|sl " , & table , & table_len , & column , & column_len , & rowid , & dbname , & dbname_len , & flags ) == FAILURE ) {
1211
1234
return ;
1212
1235
}
1213
1236
@@ -1218,10 +1241,15 @@ PHP_METHOD(sqlite3, openBlob)
1218
1241
1219
1242
sqlite3_stream = emalloc (sizeof (php_stream_sqlite3_data ));
1220
1243
sqlite3_stream -> blob = blob ;
1244
+ sqlite3_stream -> flags = flags ;
1221
1245
sqlite3_stream -> position = 0 ;
1222
1246
sqlite3_stream -> size = sqlite3_blob_bytes (blob );
1223
1247
1224
- stream = php_stream_alloc (& php_stream_sqlite3_ops , sqlite3_stream , 0 , "rb" );
1248
+ if (flags != 0 ) {
1249
+ mode = "r+b" ;
1250
+ }
1251
+
1252
+ stream = php_stream_alloc (& php_stream_sqlite3_ops , sqlite3_stream , 0 , mode );
1225
1253
1226
1254
if (stream ) {
1227
1255
php_stream_to_zval (stream , return_value );
@@ -1921,6 +1949,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_openblob, 0, 0, 3)
1921
1949
ZEND_ARG_INFO (0 , column )
1922
1950
ZEND_ARG_INFO (0 , rowid )
1923
1951
ZEND_ARG_INFO (0 , dbname )
1952
+ ZEND_ARG_INFO (0 , flags )
1924
1953
ZEND_END_ARG_INFO ()
1925
1954
1926
1955
ZEND_BEGIN_ARG_INFO_EX (arginfo_sqlite3_enableexceptions , 0 , 0 , 0 )
0 commit comments