@@ -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 & SQLITE_OPEN_READONLY ) {
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,15 +1213,15 @@ 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
- zend_long rowid , flags = 0 ;
1224
+ zend_long rowid , flags = SQLITE_OPEN_READONLY , sqlite_flags = 0 ;
1202
1225
sqlite3_blob * blob = NULL ;
1203
1226
php_stream_sqlite3_data * sqlite3_stream ;
1204
1227
php_stream * stream ;
@@ -1207,21 +1230,28 @@ 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
1214
- 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 ) {
1215
1240
php_sqlite3_error (db_obj , "Unable to open blob: %s" , sqlite3_errmsg (db_obj -> db ));
1216
1241
RETURN_FALSE ;
1217
1242
}
1218
1243
1219
1244
sqlite3_stream = emalloc (sizeof (php_stream_sqlite3_data ));
1220
1245
sqlite3_stream -> blob = blob ;
1246
+ sqlite3_stream -> flags = flags ;
1221
1247
sqlite3_stream -> position = 0 ;
1222
1248
sqlite3_stream -> size = sqlite3_blob_bytes (blob );
1223
1249
1224
- stream = php_stream_alloc (& php_stream_sqlite3_ops , sqlite3_stream , 0 , "rb" );
1250
+ if (sqlite_flags != 0 ) {
1251
+ mode = "r+b" ;
1252
+ }
1253
+
1254
+ stream = php_stream_alloc (& php_stream_sqlite3_ops , sqlite3_stream , 0 , mode );
1225
1255
1226
1256
if (stream ) {
1227
1257
php_stream_to_zval (stream , return_value );
@@ -1921,6 +1951,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_openblob, 0, 0, 3)
1921
1951
ZEND_ARG_INFO (0 , column )
1922
1952
ZEND_ARG_INFO (0 , rowid )
1923
1953
ZEND_ARG_INFO (0 , dbname )
1954
+ ZEND_ARG_INFO (0 , flags )
1924
1955
ZEND_END_ARG_INFO ()
1925
1956
1926
1957
ZEND_BEGIN_ARG_INFO_EX (arginfo_sqlite3_enableexceptions , 0 , 0 , 0 )
0 commit comments