blob: 44f0e6baf62f8fde3f515ce4ce6e456a4e7eed13 [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"
dan55388072011-09-20 15:53:02 +000018#include "btreeInt.h"
danielk1977b4e9af92007-05-01 17:49:49 +000019
20#ifndef SQLITE_OMIT_INCRBLOB
21
22/*
23** Valid sqlite3_blob* handles point to Incrblob structures.
24*/
25typedef struct Incrblob Incrblob;
danielk1977b4e9af92007-05-01 17:49:49 +000026struct Incrblob {
27 int flags; /* Copy of "flags" passed to sqlite3_blob_open() */
28 int nByte; /* Size of open blob, in bytes */
29 int iOffset; /* Byte offset of blob in cursor data */
dan4e76cc32010-10-20 18:56:04 +000030 int iCol; /* Table column this handle is open on */
danielk1977b4e9af92007-05-01 17:49:49 +000031 BtCursor *pCsr; /* Cursor pointing at blob row */
32 sqlite3_stmt *pStmt; /* Statement holding cursor open */
drh605264d2007-08-21 15:13:19 +000033 sqlite3 *db; /* The associated database */
danielk1977b4e9af92007-05-01 17:49:49 +000034};
35
dan4e76cc32010-10-20 18:56:04 +000036
dane3d82a82010-10-26 11:56:57 +000037/*
38** This function is used by both blob_open() and blob_reopen(). It seeks
39** the b-tree cursor associated with blob handle p to point to row iRow.
40** If successful, SQLITE_OK is returned and subsequent calls to
41** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
42**
43** If an error occurs, or if the specified row does not exist or does not
44** contain a value of type TEXT or BLOB in the column nominated when the
45** blob handle was opened, then an error code is returned and *pzErr may
46** be set to point to a buffer containing an error message. It is the
47** responsibility of the caller to free the error message buffer using
48** sqlite3DbFree().
49**
50** If an error does occur, then the b-tree cursor is closed. All subsequent
51** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
52** immediately return SQLITE_ABORT.
53*/
dan4e76cc32010-10-20 18:56:04 +000054static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
55 int rc; /* Error code */
56 char *zErr = 0; /* Error message */
57 Vdbe *v = (Vdbe *)p->pStmt;
58
dane3d82a82010-10-26 11:56:57 +000059 /* Set the value of the SQL statements only variable to integer iRow.
60 ** This is done directly instead of using sqlite3_bind_int64() to avoid
61 ** triggering asserts related to mutexes.
62 */
63 assert( v->aVar[0].flags&MEM_Int );
dan4e76cc32010-10-20 18:56:04 +000064 v->aVar[0].u.i = iRow;
dan4e76cc32010-10-20 18:56:04 +000065
dane3d82a82010-10-26 11:56:57 +000066 rc = sqlite3_step(p->pStmt);
dan4e76cc32010-10-20 18:56:04 +000067 if( rc==SQLITE_ROW ){
dan4e76cc32010-10-20 18:56:04 +000068 u32 type = v->apCsr[0]->aType[p->iCol];
69 if( type<12 ){
70 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
71 type==0?"null": type==7?"real": "integer"
72 );
73 rc = SQLITE_ERROR;
74 sqlite3_finalize(p->pStmt);
75 p->pStmt = 0;
76 }else{
77 p->iOffset = v->apCsr[0]->aOffset[p->iCol];
78 p->nByte = sqlite3VdbeSerialTypeLen(type);
79 p->pCsr = v->apCsr[0]->pCursor;
80 sqlite3BtreeEnterCursor(p->pCsr);
81 sqlite3BtreeCacheOverflow(p->pCsr);
82 sqlite3BtreeLeaveCursor(p->pCsr);
83 }
84 }
85
86 if( rc==SQLITE_ROW ){
87 rc = SQLITE_OK;
88 }else if( p->pStmt ){
89 rc = sqlite3_finalize(p->pStmt);
90 p->pStmt = 0;
91 if( rc==SQLITE_OK ){
92 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
93 rc = SQLITE_ERROR;
94 }else{
95 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
96 }
97 }
98
99 assert( rc!=SQLITE_OK || zErr==0 );
100 assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
101
102 *pzErr = zErr;
103 return rc;
104}
105
danielk1977b4e9af92007-05-01 17:49:49 +0000106/*
107** Open a blob handle.
108*/
109int sqlite3_blob_open(
drh16a9b832007-05-05 18:39:25 +0000110 sqlite3* db, /* The database connection */
111 const char *zDb, /* The attached database containing the blob */
112 const char *zTable, /* The table containing the blob */
113 const char *zColumn, /* The column containing the blob */
114 sqlite_int64 iRow, /* The row containing the glob */
danielk1977b4e9af92007-05-01 17:49:49 +0000115 int flags, /* True -> read/write access, false -> read-only */
drh16a9b832007-05-05 18:39:25 +0000116 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
danielk1977b4e9af92007-05-01 17:49:49 +0000117){
danielk1977b4e9af92007-05-01 17:49:49 +0000118 int nAttempt = 0;
119 int iCol; /* Index of zColumn in row-record */
120
121 /* This VDBE program seeks a btree cursor to the identified
122 ** db/table/row entry. The reason for using a vdbe program instead
123 ** of writing code to use the b-tree layer directly is that the
124 ** vdbe program will take advantage of the various transaction,
125 ** locking and error handling infrastructure built into the vdbe.
126 **
drh2d401ab2008-01-10 23:50:11 +0000127 ** After seeking the cursor, the vdbe executes an OP_ResultRow.
danielk1977b4e9af92007-05-01 17:49:49 +0000128 ** Code external to the Vdbe then "borrows" the b-tree cursor and
129 ** uses it to implement the blob_read(), blob_write() and
130 ** blob_bytes() functions.
131 **
132 ** The sqlite3_blob_close() function finalizes the vdbe program,
133 ** which closes the b-tree cursor and (possibly) commits the
134 ** transaction.
135 */
136 static const VdbeOpList openBlob[] = {
137 {OP_Transaction, 0, 0, 0}, /* 0: Start a transaction */
138 {OP_VerifyCookie, 0, 0, 0}, /* 1: Check the schema cookie */
danielk197796d48e92009-06-29 06:00:37 +0000139 {OP_TableLock, 0, 0, 0}, /* 2: Acquire a read or write lock */
danielk1977b4e9af92007-05-01 17:49:49 +0000140
danielk197796d48e92009-06-29 06:00:37 +0000141 /* One of the following two instructions is replaced by an OP_Noop. */
142 {OP_OpenRead, 0, 0, 0}, /* 3: Open cursor 0 for reading */
143 {OP_OpenWrite, 0, 0, 0}, /* 4: Open cursor 0 for read/write */
danielk1977b4e9af92007-05-01 17:49:49 +0000144
danielk197796d48e92009-06-29 06:00:37 +0000145 {OP_Variable, 1, 1, 1}, /* 5: Push the rowid to the stack */
dan4e76cc32010-10-20 18:56:04 +0000146 {OP_NotExists, 0, 10, 1}, /* 6: Seek the cursor */
danielk197796d48e92009-06-29 06:00:37 +0000147 {OP_Column, 0, 0, 1}, /* 7 */
148 {OP_ResultRow, 1, 0, 0}, /* 8 */
dan4e76cc32010-10-20 18:56:04 +0000149 {OP_Goto, 0, 5, 0}, /* 9 */
150 {OP_Close, 0, 0, 0}, /* 10 */
151 {OP_Halt, 0, 0, 0}, /* 11 */
danielk1977b4e9af92007-05-01 17:49:49 +0000152 };
153
danielk19778cbadb02007-05-03 16:31:26 +0000154 int rc = SQLITE_OK;
drh6ee700c2009-06-01 19:53:30 +0000155 char *zErr = 0;
156 Table *pTab;
dan61c7f592010-10-26 18:42:52 +0000157 Parse *pParse = 0;
158 Incrblob *pBlob = 0;
danielk1977b4e9af92007-05-01 17:49:49 +0000159
dan4e76cc32010-10-20 18:56:04 +0000160 flags = !!flags; /* flags = (flags ? 1 : 0); */
drh28414ee2009-05-09 15:17:47 +0000161 *ppBlob = 0;
dan4e76cc32010-10-20 18:56:04 +0000162
drh605264d2007-08-21 15:13:19 +0000163 sqlite3_mutex_enter(db->mutex);
dan4e76cc32010-10-20 18:56:04 +0000164
165 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
dan61c7f592010-10-26 18:42:52 +0000166 if( !pBlob ) goto blob_open_out;
drh6ee700c2009-06-01 19:53:30 +0000167 pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
dan61c7f592010-10-26 18:42:52 +0000168 if( !pParse ) goto blob_open_out;
dan4e76cc32010-10-20 18:56:04 +0000169
danielk1977b4e9af92007-05-01 17:49:49 +0000170 do {
drh6ee700c2009-06-01 19:53:30 +0000171 memset(pParse, 0, sizeof(Parse));
172 pParse->db = db;
dan61c7f592010-10-26 18:42:52 +0000173 sqlite3DbFree(db, zErr);
174 zErr = 0;
danielk1977b4e9af92007-05-01 17:49:49 +0000175
drhff0587c2007-08-29 17:43:19 +0000176 sqlite3BtreeEnterAll(db);
drh6ee700c2009-06-01 19:53:30 +0000177 pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
danielk197736961ed2008-04-24 09:49:55 +0000178 if( pTab && IsVirtual(pTab) ){
179 pTab = 0;
drh6ee700c2009-06-01 19:53:30 +0000180 sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
danielk197736961ed2008-04-24 09:49:55 +0000181 }
182#ifndef SQLITE_OMIT_VIEW
183 if( pTab && pTab->pSelect ){
184 pTab = 0;
drh6ee700c2009-06-01 19:53:30 +0000185 sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
danielk197736961ed2008-04-24 09:49:55 +0000186 }
187#endif
danielk1977b4e9af92007-05-01 17:49:49 +0000188 if( !pTab ){
drh6ee700c2009-06-01 19:53:30 +0000189 if( pParse->zErrMsg ){
190 sqlite3DbFree(db, zErr);
191 zErr = pParse->zErrMsg;
192 pParse->zErrMsg = 0;
danielk19778cbadb02007-05-03 16:31:26 +0000193 }
danielk19778cbadb02007-05-03 16:31:26 +0000194 rc = SQLITE_ERROR;
drhff0587c2007-08-29 17:43:19 +0000195 sqlite3BtreeLeaveAll(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000196 goto blob_open_out;
197 }
198
199 /* Now search pTab for the exact column. */
dan61c7f592010-10-26 18:42:52 +0000200 for(iCol=0; iCol<pTab->nCol; iCol++) {
danielk1977b4e9af92007-05-01 17:49:49 +0000201 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
202 break;
203 }
204 }
205 if( iCol==pTab->nCol ){
drh6ee700c2009-06-01 19:53:30 +0000206 sqlite3DbFree(db, zErr);
207 zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
danielk1977b4e9af92007-05-01 17:49:49 +0000208 rc = SQLITE_ERROR;
drhff0587c2007-08-29 17:43:19 +0000209 sqlite3BtreeLeaveAll(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000210 goto blob_open_out;
211 }
212
danielk1977f1819242007-05-03 18:14:10 +0000213 /* If the value is being opened for writing, check that the
dan1da40a32009-09-19 17:00:31 +0000214 ** column is not indexed, and that it is not part of a foreign key.
215 ** It is against the rules to open a column to which either of these
216 ** descriptions applies for writing. */
danielk1977f1819242007-05-03 18:14:10 +0000217 if( flags ){
dan1da40a32009-09-19 17:00:31 +0000218 const char *zFault = 0;
danielk1977f1819242007-05-03 18:14:10 +0000219 Index *pIdx;
dan1da40a32009-09-19 17:00:31 +0000220#ifndef SQLITE_OMIT_FOREIGN_KEY
221 if( db->flags&SQLITE_ForeignKeys ){
dan13167002009-10-02 06:35:06 +0000222 /* Check that the column is not part of an FK child key definition. It
223 ** is not necessary to check if it is part of a parent key, as parent
224 ** key columns must be indexed. The check below will pick up this
225 ** case. */
dan1da40a32009-09-19 17:00:31 +0000226 FKey *pFKey;
227 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
228 int j;
229 for(j=0; j<pFKey->nCol; j++){
230 if( pFKey->aCol[j].iFrom==iCol ){
231 zFault = "foreign key";
232 }
233 }
234 }
235 }
236#endif
danielk1977f1819242007-05-03 18:14:10 +0000237 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
238 int j;
239 for(j=0; j<pIdx->nColumn; j++){
240 if( pIdx->aiColumn[j]==iCol ){
dan1da40a32009-09-19 17:00:31 +0000241 zFault = "indexed";
danielk1977f1819242007-05-03 18:14:10 +0000242 }
243 }
244 }
dan1da40a32009-09-19 17:00:31 +0000245 if( zFault ){
246 sqlite3DbFree(db, zErr);
247 zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
248 rc = SQLITE_ERROR;
dan1da40a32009-09-19 17:00:31 +0000249 sqlite3BtreeLeaveAll(db);
250 goto blob_open_out;
251 }
danielk1977f1819242007-05-03 18:14:10 +0000252 }
253
dan61c7f592010-10-26 18:42:52 +0000254 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(db);
255 assert( pBlob->pStmt || db->mallocFailed );
256 if( pBlob->pStmt ){
257 Vdbe *v = (Vdbe *)pBlob->pStmt;
danielk1977b4e9af92007-05-01 17:49:49 +0000258 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
dan61c7f592010-10-26 18:42:52 +0000259
danielk1977b4e9af92007-05-01 17:49:49 +0000260 sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
261
dan61c7f592010-10-26 18:42:52 +0000262
danielk1977b4e9af92007-05-01 17:49:49 +0000263 /* Configure the OP_Transaction */
264 sqlite3VdbeChangeP1(v, 0, iDb);
danielk197796d48e92009-06-29 06:00:37 +0000265 sqlite3VdbeChangeP2(v, 0, flags);
danielk1977b4e9af92007-05-01 17:49:49 +0000266
267 /* Configure the OP_VerifyCookie */
268 sqlite3VdbeChangeP1(v, 1, iDb);
269 sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
drhc2a75552011-03-18 21:55:46 +0000270 sqlite3VdbeChangeP3(v, 1, pTab->pSchema->iGeneration);
danielk1977b4e9af92007-05-01 17:49:49 +0000271
drhff0587c2007-08-29 17:43:19 +0000272 /* Make sure a mutex is held on the table to be accessed */
drhfb982642007-08-30 01:19:59 +0000273 sqlite3VdbeUsesBtree(v, iDb);
drhff0587c2007-08-29 17:43:19 +0000274
danielk197796d48e92009-06-29 06:00:37 +0000275 /* Configure the OP_TableLock instruction */
danc548b782010-06-19 19:06:33 +0000276#ifdef SQLITE_OMIT_SHARED_CACHE
drh48f2d3b2011-09-16 01:34:43 +0000277 sqlite3VdbeChangeToNoop(v, 2);
danc548b782010-06-19 19:06:33 +0000278#else
danielk197796d48e92009-06-29 06:00:37 +0000279 sqlite3VdbeChangeP1(v, 2, iDb);
280 sqlite3VdbeChangeP2(v, 2, pTab->tnum);
281 sqlite3VdbeChangeP3(v, 2, flags);
282 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
danc548b782010-06-19 19:06:33 +0000283#endif
danielk197796d48e92009-06-29 06:00:37 +0000284
danielk1977f1819242007-05-03 18:14:10 +0000285 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
danielk197796d48e92009-06-29 06:00:37 +0000286 ** parameter of the other to pTab->tnum. */
drh48f2d3b2011-09-16 01:34:43 +0000287 sqlite3VdbeChangeToNoop(v, 4 - flags);
danielk197796d48e92009-06-29 06:00:37 +0000288 sqlite3VdbeChangeP2(v, 3 + flags, pTab->tnum);
289 sqlite3VdbeChangeP3(v, 3 + flags, iDb);
danielk1977b4e9af92007-05-01 17:49:49 +0000290
danielk1977d336e222009-02-20 10:58:41 +0000291 /* Configure the number of columns. Configure the cursor to
danielk1977b4e9af92007-05-01 17:49:49 +0000292 ** think that the table has one more column than it really
293 ** does. An OP_Column to retrieve this imaginary column will
294 ** always return an SQL NULL. This is useful because it means
295 ** we can invoke OP_Column to fill in the vdbe cursors type
296 ** and offset cache without causing any IO.
297 */
danielk197796d48e92009-06-29 06:00:37 +0000298 sqlite3VdbeChangeP4(v, 3+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
299 sqlite3VdbeChangeP2(v, 7, pTab->nCol);
drh17435752007-08-16 04:30:38 +0000300 if( !db->mallocFailed ){
drh124c0b42011-06-01 18:15:55 +0000301 pParse->nVar = 1;
302 pParse->nMem = 1;
303 pParse->nTab = 1;
304 sqlite3VdbeMakeReady(v, pParse);
danielk197792d4d7a2007-05-04 12:05:56 +0000305 }
danielk1977b4e9af92007-05-01 17:49:49 +0000306 }
drhff0587c2007-08-29 17:43:19 +0000307
danielk1977b4e9af92007-05-01 17:49:49 +0000308 pBlob->flags = flags;
dan4e76cc32010-10-20 18:56:04 +0000309 pBlob->iCol = iCol;
drh605264d2007-08-21 15:13:19 +0000310 pBlob->db = db;
dan4e76cc32010-10-20 18:56:04 +0000311 sqlite3BtreeLeaveAll(db);
dan4e76cc32010-10-20 18:56:04 +0000312 if( db->mallocFailed ){
313 goto blob_open_out;
314 }
dan4e76cc32010-10-20 18:56:04 +0000315 sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
316 rc = blobSeekToRow(pBlob, iRow, &zErr);
317 } while( (++nAttempt)<5 && rc==SQLITE_SCHEMA );
danielk1977b4e9af92007-05-01 17:49:49 +0000318
319blob_open_out:
dan4e76cc32010-10-20 18:56:04 +0000320 if( rc==SQLITE_OK && db->mallocFailed==0 ){
321 *ppBlob = (sqlite3_blob *)pBlob;
322 }else{
dan4e76cc32010-10-20 18:56:04 +0000323 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
324 sqlite3DbFree(db, pBlob);
danielk1977b4e9af92007-05-01 17:49:49 +0000325 }
dan4e76cc32010-10-20 18:56:04 +0000326 sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
drh6ee700c2009-06-01 19:53:30 +0000327 sqlite3DbFree(db, zErr);
328 sqlite3StackFree(db, pParse);
drh605264d2007-08-21 15:13:19 +0000329 rc = sqlite3ApiExit(db, rc);
330 sqlite3_mutex_leave(db->mutex);
331 return rc;
danielk1977b4e9af92007-05-01 17:49:49 +0000332}
333
334/*
drh16a9b832007-05-05 18:39:25 +0000335** Close a blob handle that was previously created using
336** sqlite3_blob_open().
danielk1977b4e9af92007-05-01 17:49:49 +0000337*/
338int sqlite3_blob_close(sqlite3_blob *pBlob){
339 Incrblob *p = (Incrblob *)pBlob;
drh605264d2007-08-21 15:13:19 +0000340 int rc;
drhd9da78a2009-03-24 15:08:09 +0000341 sqlite3 *db;
drh605264d2007-08-21 15:13:19 +0000342
drh28414ee2009-05-09 15:17:47 +0000343 if( p ){
344 db = p->db;
345 sqlite3_mutex_enter(db->mutex);
346 rc = sqlite3_finalize(p->pStmt);
347 sqlite3DbFree(db, p);
348 sqlite3_mutex_leave(db->mutex);
349 }else{
350 rc = SQLITE_OK;
351 }
drh605264d2007-08-21 15:13:19 +0000352 return rc;
danielk1977b4e9af92007-05-01 17:49:49 +0000353}
354
drh605264d2007-08-21 15:13:19 +0000355/*
356** Perform a read or write operation on a blob
357*/
drhee858132007-05-08 20:37:38 +0000358static int blobReadWrite(
danielk1977dcbb5d32007-05-04 18:36:44 +0000359 sqlite3_blob *pBlob,
360 void *z,
361 int n,
362 int iOffset,
363 int (*xCall)(BtCursor*, u32, u32, void*)
364){
365 int rc;
366 Incrblob *p = (Incrblob *)pBlob;
drh605264d2007-08-21 15:13:19 +0000367 Vdbe *v;
drh28414ee2009-05-09 15:17:47 +0000368 sqlite3 *db;
danielk1977dcbb5d32007-05-04 18:36:44 +0000369
drh413c3d32010-02-23 20:11:56 +0000370 if( p==0 ) return SQLITE_MISUSE_BKPT;
drh28414ee2009-05-09 15:17:47 +0000371 db = p->db;
drh605264d2007-08-21 15:13:19 +0000372 sqlite3_mutex_enter(db->mutex);
drh605264d2007-08-21 15:13:19 +0000373 v = (Vdbe*)p->pStmt;
danielk1977a8b30182008-10-02 14:49:01 +0000374
375 if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
376 /* Request is out of range. Return a transient error. */
377 rc = SQLITE_ERROR;
378 sqlite3Error(db, SQLITE_ERROR, 0);
dan4e76cc32010-10-20 18:56:04 +0000379 }else if( v==0 ){
danielk1977a8b30182008-10-02 14:49:01 +0000380 /* If there is no statement handle, then the blob-handle has
381 ** already been invalidated. Return SQLITE_ABORT in this case.
382 */
drh605264d2007-08-21 15:13:19 +0000383 rc = SQLITE_ABORT;
danielk1977dcbb5d32007-05-04 18:36:44 +0000384 }else{
drh605264d2007-08-21 15:13:19 +0000385 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
386 ** returned, clean-up the statement handle.
387 */
dan55388072011-09-20 15:53:02 +0000388 Pager *pPager = p->pCsr->pBt->pPager;
389 int nHit = 0;
390 int nMiss = 0;
391
drh605264d2007-08-21 15:13:19 +0000392 assert( db == v->db );
drhff0587c2007-08-29 17:43:19 +0000393 sqlite3BtreeEnterCursor(p->pCsr);
dan55388072011-09-20 15:53:02 +0000394 sqlite3PagerCacheStats(pPager, &nHit, &nMiss);
drh605264d2007-08-21 15:13:19 +0000395 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
dan55388072011-09-20 15:53:02 +0000396 db->aHitMiss[0] -= nHit;
397 db->aHitMiss[1] -= nMiss;
398 sqlite3PagerCacheStats(pPager, &db->aHitMiss[0], &db->aHitMiss[1]);
drhff0587c2007-08-29 17:43:19 +0000399 sqlite3BtreeLeaveCursor(p->pCsr);
drh605264d2007-08-21 15:13:19 +0000400 if( rc==SQLITE_ABORT ){
401 sqlite3VdbeFinalize(v);
402 p->pStmt = 0;
403 }else{
404 db->errCode = rc;
405 v->rc = rc;
406 }
danielk1977dcbb5d32007-05-04 18:36:44 +0000407 }
drh605264d2007-08-21 15:13:19 +0000408 rc = sqlite3ApiExit(db, rc);
409 sqlite3_mutex_leave(db->mutex);
410 return rc;
danielk1977dcbb5d32007-05-04 18:36:44 +0000411}
412
danielk1977b4e9af92007-05-01 17:49:49 +0000413/*
414** Read data from a blob handle.
415*/
416int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
danielk1977dcbb5d32007-05-04 18:36:44 +0000417 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
danielk1977b4e9af92007-05-01 17:49:49 +0000418}
419
420/*
421** Write data to a blob handle.
422*/
423int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
danielk1977dcbb5d32007-05-04 18:36:44 +0000424 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
danielk1977b4e9af92007-05-01 17:49:49 +0000425}
426
427/*
428** Query a blob handle for the size of the data.
drh605264d2007-08-21 15:13:19 +0000429**
430** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
431** so no mutex is required for access.
danielk1977b4e9af92007-05-01 17:49:49 +0000432*/
433int sqlite3_blob_bytes(sqlite3_blob *pBlob){
434 Incrblob *p = (Incrblob *)pBlob;
daneefab752010-12-06 17:11:05 +0000435 return (p && p->pStmt) ? p->nByte : 0;
danielk1977b4e9af92007-05-01 17:49:49 +0000436}
437
dan4e76cc32010-10-20 18:56:04 +0000438/*
439** Move an existing blob handle to point to a different row of the same
440** database table.
441**
442** If an error occurs, or if the specified row does not exist or does not
443** contain a blob or text value, then an error code is returned and the
444** database handle error code and message set. If this happens, then all
445** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
446** immediately return SQLITE_ABORT.
447*/
448int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
449 int rc;
450 Incrblob *p = (Incrblob *)pBlob;
451 sqlite3 *db;
452
453 if( p==0 ) return SQLITE_MISUSE_BKPT;
454 db = p->db;
455 sqlite3_mutex_enter(db->mutex);
456
457 if( p->pStmt==0 ){
458 /* If there is no statement handle, then the blob-handle has
459 ** already been invalidated. Return SQLITE_ABORT in this case.
460 */
461 rc = SQLITE_ABORT;
462 }else{
463 char *zErr;
464 rc = blobSeekToRow(p, iRow, &zErr);
465 if( rc!=SQLITE_OK ){
466 sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
467 sqlite3DbFree(db, zErr);
468 }
469 assert( rc!=SQLITE_SCHEMA );
470 }
471
472 rc = sqlite3ApiExit(db, rc);
daneefab752010-12-06 17:11:05 +0000473 assert( rc==SQLITE_OK || p->pStmt==0 );
dan4e76cc32010-10-20 18:56:04 +0000474 sqlite3_mutex_leave(db->mutex);
475 return rc;
476}
477
danielk1977b4e9af92007-05-01 17:49:49 +0000478#endif /* #ifndef SQLITE_OMIT_INCRBLOB */