blob: 129aad7363094b1b0fa358038b1755c6adeb6d68 [file] [log] [blame]
danielk1977b4e9af92007-05-01 17:49:49 +00001/*
2** 2007 May 1
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
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.
10**
11*************************************************************************
12**
drh16a9b832007-05-05 18:39:25 +000013** This file contains code used to implement incremental BLOB I/O.
danielk1977b4e9af92007-05-01 17:49:49 +000014*/
15
16#include "sqliteInt.h"
17#include "vdbeInt.h"
18
19#ifndef SQLITE_OMIT_INCRBLOB
20
21/*
22** Valid sqlite3_blob* handles point to Incrblob structures.
23*/
24typedef struct Incrblob Incrblob;
danielk1977b4e9af92007-05-01 17:49:49 +000025struct Incrblob {
26 int flags; /* Copy of "flags" passed to sqlite3_blob_open() */
27 int nByte; /* Size of open blob, in bytes */
28 int iOffset; /* Byte offset of blob in cursor data */
dan4e76cc32010-10-20 18:56:04 +000029 int iCol; /* Table column this handle is open on */
danielk1977b4e9af92007-05-01 17:49:49 +000030 BtCursor *pCsr; /* Cursor pointing at blob row */
31 sqlite3_stmt *pStmt; /* Statement holding cursor open */
drh605264d2007-08-21 15:13:19 +000032 sqlite3 *db; /* The associated database */
danielk1977b4e9af92007-05-01 17:49:49 +000033};
34
dan4e76cc32010-10-20 18:56:04 +000035
36static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
37 int rc; /* Error code */
38 char *zErr = 0; /* Error message */
39 Vdbe *v = (Vdbe *)p->pStmt;
40
41 v->aVar[0].u.i = iRow;
42 rc = sqlite3_step(p->pStmt);
43
44 if( rc==SQLITE_ROW ){
45 Vdbe *v = (Vdbe *)p->pStmt;
46 u32 type = v->apCsr[0]->aType[p->iCol];
47 if( type<12 ){
48 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
49 type==0?"null": type==7?"real": "integer"
50 );
51 rc = SQLITE_ERROR;
52 sqlite3_finalize(p->pStmt);
53 p->pStmt = 0;
54 }else{
55 p->iOffset = v->apCsr[0]->aOffset[p->iCol];
56 p->nByte = sqlite3VdbeSerialTypeLen(type);
57 p->pCsr = v->apCsr[0]->pCursor;
58 sqlite3BtreeEnterCursor(p->pCsr);
59 sqlite3BtreeCacheOverflow(p->pCsr);
60 sqlite3BtreeLeaveCursor(p->pCsr);
61 }
62 }
63
64 if( rc==SQLITE_ROW ){
65 rc = SQLITE_OK;
66 }else if( p->pStmt ){
67 rc = sqlite3_finalize(p->pStmt);
68 p->pStmt = 0;
69 if( rc==SQLITE_OK ){
70 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
71 rc = SQLITE_ERROR;
72 }else{
73 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
74 }
75 }
76
77 assert( rc!=SQLITE_OK || zErr==0 );
78 assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
79
80 *pzErr = zErr;
81 return rc;
82}
83
danielk1977b4e9af92007-05-01 17:49:49 +000084/*
85** Open a blob handle.
86*/
87int sqlite3_blob_open(
drh16a9b832007-05-05 18:39:25 +000088 sqlite3* db, /* The database connection */
89 const char *zDb, /* The attached database containing the blob */
90 const char *zTable, /* The table containing the blob */
91 const char *zColumn, /* The column containing the blob */
92 sqlite_int64 iRow, /* The row containing the glob */
danielk1977b4e9af92007-05-01 17:49:49 +000093 int flags, /* True -> read/write access, false -> read-only */
drh16a9b832007-05-05 18:39:25 +000094 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
danielk1977b4e9af92007-05-01 17:49:49 +000095){
danielk1977b4e9af92007-05-01 17:49:49 +000096 int nAttempt = 0;
97 int iCol; /* Index of zColumn in row-record */
98
99 /* This VDBE program seeks a btree cursor to the identified
100 ** db/table/row entry. The reason for using a vdbe program instead
101 ** of writing code to use the b-tree layer directly is that the
102 ** vdbe program will take advantage of the various transaction,
103 ** locking and error handling infrastructure built into the vdbe.
104 **
drh2d401ab2008-01-10 23:50:11 +0000105 ** After seeking the cursor, the vdbe executes an OP_ResultRow.
danielk1977b4e9af92007-05-01 17:49:49 +0000106 ** Code external to the Vdbe then "borrows" the b-tree cursor and
107 ** uses it to implement the blob_read(), blob_write() and
108 ** blob_bytes() functions.
109 **
110 ** The sqlite3_blob_close() function finalizes the vdbe program,
111 ** which closes the b-tree cursor and (possibly) commits the
112 ** transaction.
113 */
114 static const VdbeOpList openBlob[] = {
115 {OP_Transaction, 0, 0, 0}, /* 0: Start a transaction */
116 {OP_VerifyCookie, 0, 0, 0}, /* 1: Check the schema cookie */
danielk197796d48e92009-06-29 06:00:37 +0000117 {OP_TableLock, 0, 0, 0}, /* 2: Acquire a read or write lock */
danielk1977b4e9af92007-05-01 17:49:49 +0000118
danielk197796d48e92009-06-29 06:00:37 +0000119 /* One of the following two instructions is replaced by an OP_Noop. */
120 {OP_OpenRead, 0, 0, 0}, /* 3: Open cursor 0 for reading */
121 {OP_OpenWrite, 0, 0, 0}, /* 4: Open cursor 0 for read/write */
danielk1977b4e9af92007-05-01 17:49:49 +0000122
danielk197796d48e92009-06-29 06:00:37 +0000123 {OP_Variable, 1, 1, 1}, /* 5: Push the rowid to the stack */
dan4e76cc32010-10-20 18:56:04 +0000124 {OP_NotExists, 0, 10, 1}, /* 6: Seek the cursor */
danielk197796d48e92009-06-29 06:00:37 +0000125 {OP_Column, 0, 0, 1}, /* 7 */
126 {OP_ResultRow, 1, 0, 0}, /* 8 */
dan4e76cc32010-10-20 18:56:04 +0000127 {OP_Goto, 0, 5, 0}, /* 9 */
128 {OP_Close, 0, 0, 0}, /* 10 */
129 {OP_Halt, 0, 0, 0}, /* 11 */
danielk1977b4e9af92007-05-01 17:49:49 +0000130 };
131
132 Vdbe *v = 0;
danielk19778cbadb02007-05-03 16:31:26 +0000133 int rc = SQLITE_OK;
drh6ee700c2009-06-01 19:53:30 +0000134 char *zErr = 0;
135 Table *pTab;
136 Parse *pParse;
dan4e76cc32010-10-20 18:56:04 +0000137 Incrblob *pBlob;
danielk1977b4e9af92007-05-01 17:49:49 +0000138
dan4e76cc32010-10-20 18:56:04 +0000139 flags = !!flags; /* flags = (flags ? 1 : 0); */
drh28414ee2009-05-09 15:17:47 +0000140 *ppBlob = 0;
dan4e76cc32010-10-20 18:56:04 +0000141
drh605264d2007-08-21 15:13:19 +0000142 sqlite3_mutex_enter(db->mutex);
dan4e76cc32010-10-20 18:56:04 +0000143
144 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
drh6ee700c2009-06-01 19:53:30 +0000145 pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
dan4e76cc32010-10-20 18:56:04 +0000146 if( pParse==0 || pBlob==0 ){
147 assert( db->mallocFailed );
drh6ee700c2009-06-01 19:53:30 +0000148 goto blob_open_out;
149 }
dan4e76cc32010-10-20 18:56:04 +0000150
danielk1977b4e9af92007-05-01 17:49:49 +0000151 do {
drh6ee700c2009-06-01 19:53:30 +0000152 memset(pParse, 0, sizeof(Parse));
153 pParse->db = db;
danielk1977b4e9af92007-05-01 17:49:49 +0000154
drhff0587c2007-08-29 17:43:19 +0000155 sqlite3BtreeEnterAll(db);
drh6ee700c2009-06-01 19:53:30 +0000156 pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
danielk197736961ed2008-04-24 09:49:55 +0000157 if( pTab && IsVirtual(pTab) ){
158 pTab = 0;
drh6ee700c2009-06-01 19:53:30 +0000159 sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
danielk197736961ed2008-04-24 09:49:55 +0000160 }
161#ifndef SQLITE_OMIT_VIEW
162 if( pTab && pTab->pSelect ){
163 pTab = 0;
drh6ee700c2009-06-01 19:53:30 +0000164 sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
danielk197736961ed2008-04-24 09:49:55 +0000165 }
166#endif
danielk1977b4e9af92007-05-01 17:49:49 +0000167 if( !pTab ){
drh6ee700c2009-06-01 19:53:30 +0000168 if( pParse->zErrMsg ){
169 sqlite3DbFree(db, zErr);
170 zErr = pParse->zErrMsg;
171 pParse->zErrMsg = 0;
danielk19778cbadb02007-05-03 16:31:26 +0000172 }
danielk19778cbadb02007-05-03 16:31:26 +0000173 rc = SQLITE_ERROR;
drhff0587c2007-08-29 17:43:19 +0000174 sqlite3BtreeLeaveAll(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000175 goto blob_open_out;
176 }
177
178 /* Now search pTab for the exact column. */
179 for(iCol=0; iCol < pTab->nCol; iCol++) {
180 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
181 break;
182 }
183 }
184 if( iCol==pTab->nCol ){
drh6ee700c2009-06-01 19:53:30 +0000185 sqlite3DbFree(db, zErr);
186 zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
danielk1977b4e9af92007-05-01 17:49:49 +0000187 rc = SQLITE_ERROR;
drhff0587c2007-08-29 17:43:19 +0000188 sqlite3BtreeLeaveAll(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000189 goto blob_open_out;
190 }
191
danielk1977f1819242007-05-03 18:14:10 +0000192 /* If the value is being opened for writing, check that the
dan1da40a32009-09-19 17:00:31 +0000193 ** column is not indexed, and that it is not part of a foreign key.
194 ** It is against the rules to open a column to which either of these
195 ** descriptions applies for writing. */
danielk1977f1819242007-05-03 18:14:10 +0000196 if( flags ){
dan1da40a32009-09-19 17:00:31 +0000197 const char *zFault = 0;
danielk1977f1819242007-05-03 18:14:10 +0000198 Index *pIdx;
dan1da40a32009-09-19 17:00:31 +0000199#ifndef SQLITE_OMIT_FOREIGN_KEY
200 if( db->flags&SQLITE_ForeignKeys ){
dan13167002009-10-02 06:35:06 +0000201 /* Check that the column is not part of an FK child key definition. It
202 ** is not necessary to check if it is part of a parent key, as parent
203 ** key columns must be indexed. The check below will pick up this
204 ** case. */
dan1da40a32009-09-19 17:00:31 +0000205 FKey *pFKey;
206 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
207 int j;
208 for(j=0; j<pFKey->nCol; j++){
209 if( pFKey->aCol[j].iFrom==iCol ){
210 zFault = "foreign key";
211 }
212 }
213 }
214 }
215#endif
danielk1977f1819242007-05-03 18:14:10 +0000216 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
217 int j;
218 for(j=0; j<pIdx->nColumn; j++){
219 if( pIdx->aiColumn[j]==iCol ){
dan1da40a32009-09-19 17:00:31 +0000220 zFault = "indexed";
danielk1977f1819242007-05-03 18:14:10 +0000221 }
222 }
223 }
dan1da40a32009-09-19 17:00:31 +0000224 if( zFault ){
225 sqlite3DbFree(db, zErr);
226 zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
227 rc = SQLITE_ERROR;
dan1da40a32009-09-19 17:00:31 +0000228 sqlite3BtreeLeaveAll(db);
229 goto blob_open_out;
230 }
danielk1977f1819242007-05-03 18:14:10 +0000231 }
232
danielk1977b4e9af92007-05-01 17:49:49 +0000233 v = sqlite3VdbeCreate(db);
234 if( v ){
235 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
236 sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
237
238 /* Configure the OP_Transaction */
239 sqlite3VdbeChangeP1(v, 0, iDb);
danielk197796d48e92009-06-29 06:00:37 +0000240 sqlite3VdbeChangeP2(v, 0, flags);
danielk1977b4e9af92007-05-01 17:49:49 +0000241
242 /* Configure the OP_VerifyCookie */
243 sqlite3VdbeChangeP1(v, 1, iDb);
244 sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
245
drhff0587c2007-08-29 17:43:19 +0000246 /* Make sure a mutex is held on the table to be accessed */
drhfb982642007-08-30 01:19:59 +0000247 sqlite3VdbeUsesBtree(v, iDb);
drhff0587c2007-08-29 17:43:19 +0000248
danielk197796d48e92009-06-29 06:00:37 +0000249 /* Configure the OP_TableLock instruction */
danc548b782010-06-19 19:06:33 +0000250#ifdef SQLITE_OMIT_SHARED_CACHE
251 sqlite3VdbeChangeToNoop(v, 2, 1);
252#else
danielk197796d48e92009-06-29 06:00:37 +0000253 sqlite3VdbeChangeP1(v, 2, iDb);
254 sqlite3VdbeChangeP2(v, 2, pTab->tnum);
255 sqlite3VdbeChangeP3(v, 2, flags);
256 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
danc548b782010-06-19 19:06:33 +0000257#endif
danielk197796d48e92009-06-29 06:00:37 +0000258
danielk1977f1819242007-05-03 18:14:10 +0000259 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
danielk197796d48e92009-06-29 06:00:37 +0000260 ** parameter of the other to pTab->tnum. */
261 sqlite3VdbeChangeToNoop(v, 4 - flags, 1);
262 sqlite3VdbeChangeP2(v, 3 + flags, pTab->tnum);
263 sqlite3VdbeChangeP3(v, 3 + flags, iDb);
danielk1977b4e9af92007-05-01 17:49:49 +0000264
danielk1977d336e222009-02-20 10:58:41 +0000265 /* Configure the number of columns. Configure the cursor to
danielk1977b4e9af92007-05-01 17:49:49 +0000266 ** think that the table has one more column than it really
267 ** does. An OP_Column to retrieve this imaginary column will
268 ** always return an SQL NULL. This is useful because it means
269 ** we can invoke OP_Column to fill in the vdbe cursors type
270 ** and offset cache without causing any IO.
271 */
danielk197796d48e92009-06-29 06:00:37 +0000272 sqlite3VdbeChangeP4(v, 3+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
273 sqlite3VdbeChangeP2(v, 7, pTab->nCol);
drh17435752007-08-16 04:30:38 +0000274 if( !db->mallocFailed ){
dane0af83a2009-09-08 19:15:01 +0000275 sqlite3VdbeMakeReady(v, 1, 1, 1, 0, 0, 0);
danielk197792d4d7a2007-05-04 12:05:56 +0000276 }
danielk1977b4e9af92007-05-01 17:49:49 +0000277 }
drhff0587c2007-08-29 17:43:19 +0000278
danielk1977b4e9af92007-05-01 17:49:49 +0000279 pBlob->flags = flags;
danielk1977b4e9af92007-05-01 17:49:49 +0000280 pBlob->pStmt = (sqlite3_stmt *)v;
dan4e76cc32010-10-20 18:56:04 +0000281 pBlob->iCol = iCol;
drh605264d2007-08-21 15:13:19 +0000282 pBlob->db = db;
dan4e76cc32010-10-20 18:56:04 +0000283 sqlite3BtreeLeaveAll(db);
284 v = 0;
285 if( db->mallocFailed ){
286 goto blob_open_out;
287 }
dan4e76cc32010-10-20 18:56:04 +0000288 sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
289 rc = blobSeekToRow(pBlob, iRow, &zErr);
290 } while( (++nAttempt)<5 && rc==SQLITE_SCHEMA );
danielk1977b4e9af92007-05-01 17:49:49 +0000291
292blob_open_out:
dan4e76cc32010-10-20 18:56:04 +0000293 if( rc==SQLITE_OK && db->mallocFailed==0 ){
294 *ppBlob = (sqlite3_blob *)pBlob;
295 }else{
296 if( v ) sqlite3VdbeFinalize(v);
297 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
298 sqlite3DbFree(db, pBlob);
danielk1977b4e9af92007-05-01 17:49:49 +0000299 }
dan4e76cc32010-10-20 18:56:04 +0000300 sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
drh6ee700c2009-06-01 19:53:30 +0000301 sqlite3DbFree(db, zErr);
302 sqlite3StackFree(db, pParse);
drh605264d2007-08-21 15:13:19 +0000303 rc = sqlite3ApiExit(db, rc);
304 sqlite3_mutex_leave(db->mutex);
305 return rc;
danielk1977b4e9af92007-05-01 17:49:49 +0000306}
307
308/*
drh16a9b832007-05-05 18:39:25 +0000309** Close a blob handle that was previously created using
310** sqlite3_blob_open().
danielk1977b4e9af92007-05-01 17:49:49 +0000311*/
312int sqlite3_blob_close(sqlite3_blob *pBlob){
313 Incrblob *p = (Incrblob *)pBlob;
drh605264d2007-08-21 15:13:19 +0000314 int rc;
drhd9da78a2009-03-24 15:08:09 +0000315 sqlite3 *db;
drh605264d2007-08-21 15:13:19 +0000316
drh28414ee2009-05-09 15:17:47 +0000317 if( p ){
318 db = p->db;
319 sqlite3_mutex_enter(db->mutex);
320 rc = sqlite3_finalize(p->pStmt);
321 sqlite3DbFree(db, p);
322 sqlite3_mutex_leave(db->mutex);
323 }else{
324 rc = SQLITE_OK;
325 }
drh605264d2007-08-21 15:13:19 +0000326 return rc;
danielk1977b4e9af92007-05-01 17:49:49 +0000327}
328
drh605264d2007-08-21 15:13:19 +0000329/*
330** Perform a read or write operation on a blob
331*/
drhee858132007-05-08 20:37:38 +0000332static int blobReadWrite(
danielk1977dcbb5d32007-05-04 18:36:44 +0000333 sqlite3_blob *pBlob,
334 void *z,
335 int n,
336 int iOffset,
337 int (*xCall)(BtCursor*, u32, u32, void*)
338){
339 int rc;
340 Incrblob *p = (Incrblob *)pBlob;
drh605264d2007-08-21 15:13:19 +0000341 Vdbe *v;
drh28414ee2009-05-09 15:17:47 +0000342 sqlite3 *db;
danielk1977dcbb5d32007-05-04 18:36:44 +0000343
drh413c3d32010-02-23 20:11:56 +0000344 if( p==0 ) return SQLITE_MISUSE_BKPT;
drh28414ee2009-05-09 15:17:47 +0000345 db = p->db;
drh605264d2007-08-21 15:13:19 +0000346 sqlite3_mutex_enter(db->mutex);
drh605264d2007-08-21 15:13:19 +0000347 v = (Vdbe*)p->pStmt;
danielk1977a8b30182008-10-02 14:49:01 +0000348
349 if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
350 /* Request is out of range. Return a transient error. */
351 rc = SQLITE_ERROR;
352 sqlite3Error(db, SQLITE_ERROR, 0);
dan4e76cc32010-10-20 18:56:04 +0000353 }else if( v==0 ){
danielk1977a8b30182008-10-02 14:49:01 +0000354 /* If there is no statement handle, then the blob-handle has
355 ** already been invalidated. Return SQLITE_ABORT in this case.
356 */
drh605264d2007-08-21 15:13:19 +0000357 rc = SQLITE_ABORT;
danielk1977dcbb5d32007-05-04 18:36:44 +0000358 }else{
drh605264d2007-08-21 15:13:19 +0000359 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
360 ** returned, clean-up the statement handle.
361 */
362 assert( db == v->db );
drhff0587c2007-08-29 17:43:19 +0000363 sqlite3BtreeEnterCursor(p->pCsr);
drh605264d2007-08-21 15:13:19 +0000364 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
drhff0587c2007-08-29 17:43:19 +0000365 sqlite3BtreeLeaveCursor(p->pCsr);
drh605264d2007-08-21 15:13:19 +0000366 if( rc==SQLITE_ABORT ){
367 sqlite3VdbeFinalize(v);
368 p->pStmt = 0;
369 }else{
370 db->errCode = rc;
371 v->rc = rc;
372 }
danielk1977dcbb5d32007-05-04 18:36:44 +0000373 }
drh605264d2007-08-21 15:13:19 +0000374 rc = sqlite3ApiExit(db, rc);
375 sqlite3_mutex_leave(db->mutex);
376 return rc;
danielk1977dcbb5d32007-05-04 18:36:44 +0000377}
378
danielk1977b4e9af92007-05-01 17:49:49 +0000379/*
380** Read data from a blob handle.
381*/
382int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
danielk1977dcbb5d32007-05-04 18:36:44 +0000383 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
danielk1977b4e9af92007-05-01 17:49:49 +0000384}
385
386/*
387** Write data to a blob handle.
388*/
389int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
danielk1977dcbb5d32007-05-04 18:36:44 +0000390 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
danielk1977b4e9af92007-05-01 17:49:49 +0000391}
392
393/*
394** Query a blob handle for the size of the data.
drh605264d2007-08-21 15:13:19 +0000395**
396** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
397** so no mutex is required for access.
danielk1977b4e9af92007-05-01 17:49:49 +0000398*/
399int sqlite3_blob_bytes(sqlite3_blob *pBlob){
400 Incrblob *p = (Incrblob *)pBlob;
drh28414ee2009-05-09 15:17:47 +0000401 return p ? p->nByte : 0;
danielk1977b4e9af92007-05-01 17:49:49 +0000402}
403
dan4e76cc32010-10-20 18:56:04 +0000404/*
405** Move an existing blob handle to point to a different row of the same
406** database table.
407**
408** If an error occurs, or if the specified row does not exist or does not
409** contain a blob or text value, then an error code is returned and the
410** database handle error code and message set. If this happens, then all
411** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
412** immediately return SQLITE_ABORT.
413*/
414int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
415 int rc;
416 Incrblob *p = (Incrblob *)pBlob;
417 sqlite3 *db;
418
419 if( p==0 ) return SQLITE_MISUSE_BKPT;
420 db = p->db;
421 sqlite3_mutex_enter(db->mutex);
422
423 if( p->pStmt==0 ){
424 /* If there is no statement handle, then the blob-handle has
425 ** already been invalidated. Return SQLITE_ABORT in this case.
426 */
427 rc = SQLITE_ABORT;
428 }else{
429 char *zErr;
430 rc = blobSeekToRow(p, iRow, &zErr);
431 if( rc!=SQLITE_OK ){
432 sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
433 sqlite3DbFree(db, zErr);
434 }
435 assert( rc!=SQLITE_SCHEMA );
436 }
437
438 rc = sqlite3ApiExit(db, rc);
439 sqlite3_mutex_leave(db->mutex);
440 return rc;
441}
442
danielk1977b4e9af92007-05-01 17:49:49 +0000443#endif /* #ifndef SQLITE_OMIT_INCRBLOB */