4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 ******************************************************************************
13 ** An SQL function that uses the incremental BLOB I/O mechanism of SQLite
14 ** to read or write part of a blob. This is intended for debugging use
17 ** readblob(SCHEMA,TABLE,COLUMN,ROWID,OFFSET,N)
19 ** Returns N bytes of the blob starting at OFFSET.
21 ** writeblob(SCHEMA,TABLE,COLUMN,ROWID,OFFSET,NEWDATA)
23 ** NEWDATA must be a blob. The content of NEWDATA overwrites the
24 ** existing BLOB data at SCHEMA.TABLE.COLUMN for row ROWID beginning
25 ** at OFFSET bytes into the blob.
27 #include "sqlite3ext.h"
28 SQLITE_EXTENSION_INIT1
32 static void readblobFunc(
33 sqlite3_context
*context
,
37 sqlite3_blob
*pBlob
= 0;
48 zSchema
= (const char*)sqlite3_value_text(argv
[0]);
49 zTable
= (const char*)sqlite3_value_text(argv
[1]);
51 sqlite3_result_error(context
, "bad table name", -1);
54 zColumn
= (const char*)sqlite3_value_text(argv
[2]);
56 sqlite3_result_error(context
, "bad column name", -1);
59 iRowid
= sqlite3_value_int64(argv
[3]);
60 iOfst
= sqlite3_value_int(argv
[4]);
61 nData
= sqlite3_value_int(argv
[5]);
62 if( nData
<=0 ) return;
63 aData
= sqlite3_malloc64( nData
+1 );
65 sqlite3_result_error_nomem(context
);
68 db
= sqlite3_context_db_handle(context
);
69 rc
= sqlite3_blob_open(db
, zSchema
, zTable
, zColumn
, iRowid
, 0, &pBlob
);
72 sqlite3_result_error(context
, "cannot open BLOB pointer", -1);
75 rc
= sqlite3_blob_read(pBlob
, aData
, nData
, iOfst
);
76 sqlite3_blob_close(pBlob
);
79 sqlite3_result_error(context
, "BLOB read failed", -1);
81 sqlite3_result_blob(context
, aData
, nData
, sqlite3_free
);
85 static void writeblobFunc(
86 sqlite3_context
*context
,
90 sqlite3_blob
*pBlob
= 0;
101 zSchema
= (const char*)sqlite3_value_text(argv
[0]);
102 zTable
= (const char*)sqlite3_value_text(argv
[1]);
104 sqlite3_result_error(context
, "bad table name", -1);
107 zColumn
= (const char*)sqlite3_value_text(argv
[2]);
109 sqlite3_result_error(context
, "bad column name", -1);
112 iRowid
= sqlite3_value_int64(argv
[3]);
113 iOfst
= sqlite3_value_int(argv
[4]);
114 if( sqlite3_value_type(argv
[5])!=SQLITE_BLOB
){
115 sqlite3_result_error(context
, "6th argument must be a BLOB", -1);
118 nData
= sqlite3_value_bytes(argv
[5]);
119 aData
= (unsigned char *)sqlite3_value_blob(argv
[5]);
120 db
= sqlite3_context_db_handle(context
);
121 rc
= sqlite3_blob_open(db
, zSchema
, zTable
, zColumn
, iRowid
, 1, &pBlob
);
123 sqlite3_result_error(context
, "cannot open BLOB pointer", -1);
126 rc
= sqlite3_blob_write(pBlob
, aData
, nData
, iOfst
);
127 sqlite3_blob_close(pBlob
);
129 sqlite3_result_error(context
, "BLOB write failed", -1);
135 __declspec(dllexport
)
137 int sqlite3_blobio_init(
140 const sqlite3_api_routines
*pApi
143 SQLITE_EXTENSION_INIT2(pApi
);
144 (void)pzErrMsg
; /* Unused parameter */
145 rc
= sqlite3_create_function(db
, "readblob", 6, SQLITE_UTF8
, 0,
148 rc
= sqlite3_create_function(db
, "writeblob", 6, SQLITE_UTF8
, 0,
149 writeblobFunc
, 0, 0);