blob: 2f37ad933b3a3badfc22d0a63b4d6cd07cace99a [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 */
dane437ca52011-07-11 19:45:38 +000033 char *zDb; /* Database name */
34 Table *pTab; /* Table object */
danielk1977b4e9af92007-05-01 17:49:49 +000035};
36
dan4e76cc32010-10-20 18:56:04 +000037
dane3d82a82010-10-26 11:56:57 +000038/*
39** This function is used by both blob_open() and blob_reopen(). It seeks
40** the b-tree cursor associated with blob handle p to point to row iRow.
41** If successful, SQLITE_OK is returned and subsequent calls to
42** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
43**
44** If an error occurs, or if the specified row does not exist or does not
45** contain a value of type TEXT or BLOB in the column nominated when the
46** blob handle was opened, then an error code is returned and *pzErr may
47** be set to point to a buffer containing an error message. It is the
48** responsibility of the caller to free the error message buffer using
49** sqlite3DbFree().
50**
51** If an error does occur, then the b-tree cursor is closed. All subsequent
52** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
53** immediately return SQLITE_ABORT.
54*/
dan4e76cc32010-10-20 18:56:04 +000055static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
56 int rc; /* Error code */
57 char *zErr = 0; /* Error message */
58 Vdbe *v = (Vdbe *)p->pStmt;
59
dane3d82a82010-10-26 11:56:57 +000060 /* Set the value of the SQL statements only variable to integer iRow.
61 ** This is done directly instead of using sqlite3_bind_int64() to avoid
62 ** triggering asserts related to mutexes.
63 */
64 assert( v->aVar[0].flags&MEM_Int );
dan4e76cc32010-10-20 18:56:04 +000065 v->aVar[0].u.i = iRow;
dan4e76cc32010-10-20 18:56:04 +000066
dane3d82a82010-10-26 11:56:57 +000067 rc = sqlite3_step(p->pStmt);
dan4e76cc32010-10-20 18:56:04 +000068 if( rc==SQLITE_ROW ){
dan4e76cc32010-10-20 18:56:04 +000069 u32 type = v->apCsr[0]->aType[p->iCol];
70 if( type<12 ){
71 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
72 type==0?"null": type==7?"real": "integer"
73 );
74 rc = SQLITE_ERROR;
75 sqlite3_finalize(p->pStmt);
76 p->pStmt = 0;
77 }else{
78 p->iOffset = v->apCsr[0]->aOffset[p->iCol];
79 p->nByte = sqlite3VdbeSerialTypeLen(type);
80 p->pCsr = v->apCsr[0]->pCursor;
81 sqlite3BtreeEnterCursor(p->pCsr);
82 sqlite3BtreeCacheOverflow(p->pCsr);
83 sqlite3BtreeLeaveCursor(p->pCsr);
84 }
85 }
86
87 if( rc==SQLITE_ROW ){
88 rc = SQLITE_OK;
89 }else if( p->pStmt ){
90 rc = sqlite3_finalize(p->pStmt);
91 p->pStmt = 0;
92 if( rc==SQLITE_OK ){
93 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
94 rc = SQLITE_ERROR;
95 }else{
96 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
97 }
98 }
99
100 assert( rc!=SQLITE_OK || zErr==0 );
101 assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
102
103 *pzErr = zErr;
104 return rc;
105}
106
danielk1977b4e9af92007-05-01 17:49:49 +0000107/*
108** Open a blob handle.
109*/
110int sqlite3_blob_open(
drh16a9b832007-05-05 18:39:25 +0000111 sqlite3* db, /* The database connection */
112 const char *zDb, /* The attached database containing the blob */
113 const char *zTable, /* The table containing the blob */
114 const char *zColumn, /* The column containing the blob */
115 sqlite_int64 iRow, /* The row containing the glob */
danielk1977b4e9af92007-05-01 17:49:49 +0000116 int flags, /* True -> read/write access, false -> read-only */
drh16a9b832007-05-05 18:39:25 +0000117 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
danielk1977b4e9af92007-05-01 17:49:49 +0000118){
danielk1977b4e9af92007-05-01 17:49:49 +0000119 int nAttempt = 0;
120 int iCol; /* Index of zColumn in row-record */
121
122 /* This VDBE program seeks a btree cursor to the identified
123 ** db/table/row entry. The reason for using a vdbe program instead
124 ** of writing code to use the b-tree layer directly is that the
125 ** vdbe program will take advantage of the various transaction,
126 ** locking and error handling infrastructure built into the vdbe.
127 **
drh2d401ab2008-01-10 23:50:11 +0000128 ** After seeking the cursor, the vdbe executes an OP_ResultRow.
danielk1977b4e9af92007-05-01 17:49:49 +0000129 ** Code external to the Vdbe then "borrows" the b-tree cursor and
130 ** uses it to implement the blob_read(), blob_write() and
131 ** blob_bytes() functions.
132 **
133 ** The sqlite3_blob_close() function finalizes the vdbe program,
134 ** which closes the b-tree cursor and (possibly) commits the
135 ** transaction.
136 */
137 static const VdbeOpList openBlob[] = {
138 {OP_Transaction, 0, 0, 0}, /* 0: Start a transaction */
139 {OP_VerifyCookie, 0, 0, 0}, /* 1: Check the schema cookie */
danielk197796d48e92009-06-29 06:00:37 +0000140 {OP_TableLock, 0, 0, 0}, /* 2: Acquire a read or write lock */
danielk1977b4e9af92007-05-01 17:49:49 +0000141
danielk197796d48e92009-06-29 06:00:37 +0000142 /* One of the following two instructions is replaced by an OP_Noop. */
143 {OP_OpenRead, 0, 0, 0}, /* 3: Open cursor 0 for reading */
144 {OP_OpenWrite, 0, 0, 0}, /* 4: Open cursor 0 for read/write */
danielk1977b4e9af92007-05-01 17:49:49 +0000145
danielk197796d48e92009-06-29 06:00:37 +0000146 {OP_Variable, 1, 1, 1}, /* 5: Push the rowid to the stack */
dan4e76cc32010-10-20 18:56:04 +0000147 {OP_NotExists, 0, 10, 1}, /* 6: Seek the cursor */
danielk197796d48e92009-06-29 06:00:37 +0000148 {OP_Column, 0, 0, 1}, /* 7 */
149 {OP_ResultRow, 1, 0, 0}, /* 8 */
dan4e76cc32010-10-20 18:56:04 +0000150 {OP_Goto, 0, 5, 0}, /* 9 */
151 {OP_Close, 0, 0, 0}, /* 10 */
152 {OP_Halt, 0, 0, 0}, /* 11 */
danielk1977b4e9af92007-05-01 17:49:49 +0000153 };
154
danielk19778cbadb02007-05-03 16:31:26 +0000155 int rc = SQLITE_OK;
drh6ee700c2009-06-01 19:53:30 +0000156 char *zErr = 0;
157 Table *pTab;
dan61c7f592010-10-26 18:42:52 +0000158 Parse *pParse = 0;
159 Incrblob *pBlob = 0;
danielk1977b4e9af92007-05-01 17:49:49 +0000160
dan4e76cc32010-10-20 18:56:04 +0000161 flags = !!flags; /* flags = (flags ? 1 : 0); */
drh28414ee2009-05-09 15:17:47 +0000162 *ppBlob = 0;
dan4e76cc32010-10-20 18:56:04 +0000163
drh605264d2007-08-21 15:13:19 +0000164 sqlite3_mutex_enter(db->mutex);
dan4e76cc32010-10-20 18:56:04 +0000165
166 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
dan61c7f592010-10-26 18:42:52 +0000167 if( !pBlob ) goto blob_open_out;
drh6ee700c2009-06-01 19:53:30 +0000168 pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
dan61c7f592010-10-26 18:42:52 +0000169 if( !pParse ) goto blob_open_out;
dan4e76cc32010-10-20 18:56:04 +0000170
danielk1977b4e9af92007-05-01 17:49:49 +0000171 do {
drh6ee700c2009-06-01 19:53:30 +0000172 memset(pParse, 0, sizeof(Parse));
173 pParse->db = db;
dan61c7f592010-10-26 18:42:52 +0000174 sqlite3DbFree(db, zErr);
175 zErr = 0;
danielk1977b4e9af92007-05-01 17:49:49 +0000176
drhff0587c2007-08-29 17:43:19 +0000177 sqlite3BtreeEnterAll(db);
drh6ee700c2009-06-01 19:53:30 +0000178 pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
danielk197736961ed2008-04-24 09:49:55 +0000179 if( pTab && IsVirtual(pTab) ){
180 pTab = 0;
drh6ee700c2009-06-01 19:53:30 +0000181 sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
danielk197736961ed2008-04-24 09:49:55 +0000182 }
183#ifndef SQLITE_OMIT_VIEW
184 if( pTab && pTab->pSelect ){
185 pTab = 0;
drh6ee700c2009-06-01 19:53:30 +0000186 sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
danielk197736961ed2008-04-24 09:49:55 +0000187 }
188#endif
danielk1977b4e9af92007-05-01 17:49:49 +0000189 if( !pTab ){
drh6ee700c2009-06-01 19:53:30 +0000190 if( pParse->zErrMsg ){
191 sqlite3DbFree(db, zErr);
192 zErr = pParse->zErrMsg;
193 pParse->zErrMsg = 0;
danielk19778cbadb02007-05-03 16:31:26 +0000194 }
danielk19778cbadb02007-05-03 16:31:26 +0000195 rc = SQLITE_ERROR;
drhff0587c2007-08-29 17:43:19 +0000196 sqlite3BtreeLeaveAll(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000197 goto blob_open_out;
198 }
dane437ca52011-07-11 19:45:38 +0000199 pBlob->pTab = pTab;
200 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zName;
danielk1977b4e9af92007-05-01 17:49:49 +0000201
202 /* Now search pTab for the exact column. */
dan61c7f592010-10-26 18:42:52 +0000203 for(iCol=0; iCol<pTab->nCol; iCol++) {
danielk1977b4e9af92007-05-01 17:49:49 +0000204 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
205 break;
206 }
207 }
208 if( iCol==pTab->nCol ){
drh6ee700c2009-06-01 19:53:30 +0000209 sqlite3DbFree(db, zErr);
210 zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
danielk1977b4e9af92007-05-01 17:49:49 +0000211 rc = SQLITE_ERROR;
drhff0587c2007-08-29 17:43:19 +0000212 sqlite3BtreeLeaveAll(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000213 goto blob_open_out;
214 }
215
danielk1977f1819242007-05-03 18:14:10 +0000216 /* If the value is being opened for writing, check that the
dan1da40a32009-09-19 17:00:31 +0000217 ** column is not indexed, and that it is not part of a foreign key.
218 ** It is against the rules to open a column to which either of these
219 ** descriptions applies for writing. */
danielk1977f1819242007-05-03 18:14:10 +0000220 if( flags ){
dan1da40a32009-09-19 17:00:31 +0000221 const char *zFault = 0;
danielk1977f1819242007-05-03 18:14:10 +0000222 Index *pIdx;
dan1da40a32009-09-19 17:00:31 +0000223#ifndef SQLITE_OMIT_FOREIGN_KEY
224 if( db->flags&SQLITE_ForeignKeys ){
dan13167002009-10-02 06:35:06 +0000225 /* Check that the column is not part of an FK child key definition. It
226 ** is not necessary to check if it is part of a parent key, as parent
227 ** key columns must be indexed. The check below will pick up this
228 ** case. */
dan1da40a32009-09-19 17:00:31 +0000229 FKey *pFKey;
230 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
231 int j;
232 for(j=0; j<pFKey->nCol; j++){
233 if( pFKey->aCol[j].iFrom==iCol ){
234 zFault = "foreign key";
235 }
236 }
237 }
238 }
239#endif
danielk1977f1819242007-05-03 18:14:10 +0000240 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
241 int j;
242 for(j=0; j<pIdx->nColumn; j++){
243 if( pIdx->aiColumn[j]==iCol ){
dan1da40a32009-09-19 17:00:31 +0000244 zFault = "indexed";
danielk1977f1819242007-05-03 18:14:10 +0000245 }
246 }
247 }
dan1da40a32009-09-19 17:00:31 +0000248 if( zFault ){
249 sqlite3DbFree(db, zErr);
250 zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
251 rc = SQLITE_ERROR;
dan1da40a32009-09-19 17:00:31 +0000252 sqlite3BtreeLeaveAll(db);
253 goto blob_open_out;
254 }
danielk1977f1819242007-05-03 18:14:10 +0000255 }
256
dan61c7f592010-10-26 18:42:52 +0000257 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(db);
258 assert( pBlob->pStmt || db->mallocFailed );
259 if( pBlob->pStmt ){
260 Vdbe *v = (Vdbe *)pBlob->pStmt;
danielk1977b4e9af92007-05-01 17:49:49 +0000261 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
dan61c7f592010-10-26 18:42:52 +0000262
danielk1977b4e9af92007-05-01 17:49:49 +0000263 sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
264
dan61c7f592010-10-26 18:42:52 +0000265
danielk1977b4e9af92007-05-01 17:49:49 +0000266 /* Configure the OP_Transaction */
267 sqlite3VdbeChangeP1(v, 0, iDb);
danielk197796d48e92009-06-29 06:00:37 +0000268 sqlite3VdbeChangeP2(v, 0, flags);
danielk1977b4e9af92007-05-01 17:49:49 +0000269
270 /* Configure the OP_VerifyCookie */
271 sqlite3VdbeChangeP1(v, 1, iDb);
272 sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
drhc2a75552011-03-18 21:55:46 +0000273 sqlite3VdbeChangeP3(v, 1, pTab->pSchema->iGeneration);
danielk1977b4e9af92007-05-01 17:49:49 +0000274
drhff0587c2007-08-29 17:43:19 +0000275 /* Make sure a mutex is held on the table to be accessed */
drhfb982642007-08-30 01:19:59 +0000276 sqlite3VdbeUsesBtree(v, iDb);
drhff0587c2007-08-29 17:43:19 +0000277
danielk197796d48e92009-06-29 06:00:37 +0000278 /* Configure the OP_TableLock instruction */
danc548b782010-06-19 19:06:33 +0000279#ifdef SQLITE_OMIT_SHARED_CACHE
280 sqlite3VdbeChangeToNoop(v, 2, 1);
281#else
danielk197796d48e92009-06-29 06:00:37 +0000282 sqlite3VdbeChangeP1(v, 2, iDb);
283 sqlite3VdbeChangeP2(v, 2, pTab->tnum);
284 sqlite3VdbeChangeP3(v, 2, flags);
285 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
danc548b782010-06-19 19:06:33 +0000286#endif
danielk197796d48e92009-06-29 06:00:37 +0000287
danielk1977f1819242007-05-03 18:14:10 +0000288 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
danielk197796d48e92009-06-29 06:00:37 +0000289 ** parameter of the other to pTab->tnum. */
290 sqlite3VdbeChangeToNoop(v, 4 - flags, 1);
291 sqlite3VdbeChangeP2(v, 3 + flags, pTab->tnum);
292 sqlite3VdbeChangeP3(v, 3 + flags, iDb);
danielk1977b4e9af92007-05-01 17:49:49 +0000293
danielk1977d336e222009-02-20 10:58:41 +0000294 /* Configure the number of columns. Configure the cursor to
danielk1977b4e9af92007-05-01 17:49:49 +0000295 ** think that the table has one more column than it really
296 ** does. An OP_Column to retrieve this imaginary column will
297 ** always return an SQL NULL. This is useful because it means
298 ** we can invoke OP_Column to fill in the vdbe cursors type
299 ** and offset cache without causing any IO.
300 */
danielk197796d48e92009-06-29 06:00:37 +0000301 sqlite3VdbeChangeP4(v, 3+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
302 sqlite3VdbeChangeP2(v, 7, pTab->nCol);
drh17435752007-08-16 04:30:38 +0000303 if( !db->mallocFailed ){
drh124c0b42011-06-01 18:15:55 +0000304 pParse->nVar = 1;
305 pParse->nMem = 1;
306 pParse->nTab = 1;
307 sqlite3VdbeMakeReady(v, pParse);
danielk197792d4d7a2007-05-04 12:05:56 +0000308 }
danielk1977b4e9af92007-05-01 17:49:49 +0000309 }
drhff0587c2007-08-29 17:43:19 +0000310
danielk1977b4e9af92007-05-01 17:49:49 +0000311 pBlob->flags = flags;
dan4e76cc32010-10-20 18:56:04 +0000312 pBlob->iCol = iCol;
drh605264d2007-08-21 15:13:19 +0000313 pBlob->db = db;
dan4e76cc32010-10-20 18:56:04 +0000314 sqlite3BtreeLeaveAll(db);
dan4e76cc32010-10-20 18:56:04 +0000315 if( db->mallocFailed ){
316 goto blob_open_out;
317 }
dan4e76cc32010-10-20 18:56:04 +0000318 sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
319 rc = blobSeekToRow(pBlob, iRow, &zErr);
320 } while( (++nAttempt)<5 && rc==SQLITE_SCHEMA );
danielk1977b4e9af92007-05-01 17:49:49 +0000321
322blob_open_out:
dan4e76cc32010-10-20 18:56:04 +0000323 if( rc==SQLITE_OK && db->mallocFailed==0 ){
324 *ppBlob = (sqlite3_blob *)pBlob;
325 }else{
dan4e76cc32010-10-20 18:56:04 +0000326 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
327 sqlite3DbFree(db, pBlob);
danielk1977b4e9af92007-05-01 17:49:49 +0000328 }
dan4e76cc32010-10-20 18:56:04 +0000329 sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
drh6ee700c2009-06-01 19:53:30 +0000330 sqlite3DbFree(db, zErr);
331 sqlite3StackFree(db, pParse);
drh605264d2007-08-21 15:13:19 +0000332 rc = sqlite3ApiExit(db, rc);
333 sqlite3_mutex_leave(db->mutex);
334 return rc;
danielk1977b4e9af92007-05-01 17:49:49 +0000335}
336
337/*
drh16a9b832007-05-05 18:39:25 +0000338** Close a blob handle that was previously created using
339** sqlite3_blob_open().
danielk1977b4e9af92007-05-01 17:49:49 +0000340*/
341int sqlite3_blob_close(sqlite3_blob *pBlob){
342 Incrblob *p = (Incrblob *)pBlob;
drh605264d2007-08-21 15:13:19 +0000343 int rc;
drhd9da78a2009-03-24 15:08:09 +0000344 sqlite3 *db;
drh605264d2007-08-21 15:13:19 +0000345
drh28414ee2009-05-09 15:17:47 +0000346 if( p ){
347 db = p->db;
348 sqlite3_mutex_enter(db->mutex);
349 rc = sqlite3_finalize(p->pStmt);
350 sqlite3DbFree(db, p);
351 sqlite3_mutex_leave(db->mutex);
352 }else{
353 rc = SQLITE_OK;
354 }
drh605264d2007-08-21 15:13:19 +0000355 return rc;
danielk1977b4e9af92007-05-01 17:49:49 +0000356}
357
drh605264d2007-08-21 15:13:19 +0000358/*
359** Perform a read or write operation on a blob
360*/
drhee858132007-05-08 20:37:38 +0000361static int blobReadWrite(
danielk1977dcbb5d32007-05-04 18:36:44 +0000362 sqlite3_blob *pBlob,
363 void *z,
364 int n,
365 int iOffset,
366 int (*xCall)(BtCursor*, u32, u32, void*)
367){
368 int rc;
369 Incrblob *p = (Incrblob *)pBlob;
drh605264d2007-08-21 15:13:19 +0000370 Vdbe *v;
drh28414ee2009-05-09 15:17:47 +0000371 sqlite3 *db;
danielk1977dcbb5d32007-05-04 18:36:44 +0000372
drh413c3d32010-02-23 20:11:56 +0000373 if( p==0 ) return SQLITE_MISUSE_BKPT;
drh28414ee2009-05-09 15:17:47 +0000374 db = p->db;
drh605264d2007-08-21 15:13:19 +0000375 sqlite3_mutex_enter(db->mutex);
drh605264d2007-08-21 15:13:19 +0000376 v = (Vdbe*)p->pStmt;
danielk1977a8b30182008-10-02 14:49:01 +0000377
378 if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
379 /* Request is out of range. Return a transient error. */
380 rc = SQLITE_ERROR;
381 sqlite3Error(db, SQLITE_ERROR, 0);
dan4e76cc32010-10-20 18:56:04 +0000382 }else if( v==0 ){
danielk1977a8b30182008-10-02 14:49:01 +0000383 /* If there is no statement handle, then the blob-handle has
384 ** already been invalidated. Return SQLITE_ABORT in this case.
385 */
drh605264d2007-08-21 15:13:19 +0000386 rc = SQLITE_ABORT;
danielk1977dcbb5d32007-05-04 18:36:44 +0000387 }else{
drh605264d2007-08-21 15:13:19 +0000388 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
389 ** returned, clean-up the statement handle.
390 */
391 assert( db == v->db );
drhff0587c2007-08-29 17:43:19 +0000392 sqlite3BtreeEnterCursor(p->pCsr);
dane437ca52011-07-11 19:45:38 +0000393
394#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
drh7f197bb2011-08-03 21:32:11 +0000395 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
dane437ca52011-07-11 19:45:38 +0000396 /* If a pre-update hook is registered and this is a write cursor,
397 ** invoke it here.
398 **
399 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
400 ** operation should really be an SQLITE_UPDATE. This is probably
401 ** incorrect, but is convenient because at this point the new.* values
402 ** are not easily obtainable. And for the sessions module, an
403 ** SQLITE_UPDATE where the PK columns do not change is handled in the
404 ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually
405 ** slightly more efficient). Since you cannot write to a PK column
406 ** using the incremental-blob API, this works. For the sessions module
407 ** anyhow.
408 */
409 sqlite3_int64 iKey;
410 sqlite3BtreeKeySize(p->pCsr, &iKey);
411 sqlite3VdbePreUpdateHook(
412 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1
413 );
414 }
415#endif
416
drh605264d2007-08-21 15:13:19 +0000417 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
drhff0587c2007-08-29 17:43:19 +0000418 sqlite3BtreeLeaveCursor(p->pCsr);
drh605264d2007-08-21 15:13:19 +0000419 if( rc==SQLITE_ABORT ){
420 sqlite3VdbeFinalize(v);
421 p->pStmt = 0;
422 }else{
423 db->errCode = rc;
424 v->rc = rc;
425 }
danielk1977dcbb5d32007-05-04 18:36:44 +0000426 }
drh605264d2007-08-21 15:13:19 +0000427 rc = sqlite3ApiExit(db, rc);
428 sqlite3_mutex_leave(db->mutex);
429 return rc;
danielk1977dcbb5d32007-05-04 18:36:44 +0000430}
431
danielk1977b4e9af92007-05-01 17:49:49 +0000432/*
433** Read data from a blob handle.
434*/
435int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
danielk1977dcbb5d32007-05-04 18:36:44 +0000436 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
danielk1977b4e9af92007-05-01 17:49:49 +0000437}
438
439/*
440** Write data to a blob handle.
441*/
442int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
danielk1977dcbb5d32007-05-04 18:36:44 +0000443 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
danielk1977b4e9af92007-05-01 17:49:49 +0000444}
445
446/*
447** Query a blob handle for the size of the data.
drh605264d2007-08-21 15:13:19 +0000448**
449** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
450** so no mutex is required for access.
danielk1977b4e9af92007-05-01 17:49:49 +0000451*/
452int sqlite3_blob_bytes(sqlite3_blob *pBlob){
453 Incrblob *p = (Incrblob *)pBlob;
daneefab752010-12-06 17:11:05 +0000454 return (p && p->pStmt) ? p->nByte : 0;
danielk1977b4e9af92007-05-01 17:49:49 +0000455}
456
dan4e76cc32010-10-20 18:56:04 +0000457/*
458** Move an existing blob handle to point to a different row of the same
459** database table.
460**
461** If an error occurs, or if the specified row does not exist or does not
462** contain a blob or text value, then an error code is returned and the
463** database handle error code and message set. If this happens, then all
464** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
465** immediately return SQLITE_ABORT.
466*/
467int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
468 int rc;
469 Incrblob *p = (Incrblob *)pBlob;
470 sqlite3 *db;
471
472 if( p==0 ) return SQLITE_MISUSE_BKPT;
473 db = p->db;
474 sqlite3_mutex_enter(db->mutex);
475
476 if( p->pStmt==0 ){
477 /* If there is no statement handle, then the blob-handle has
478 ** already been invalidated. Return SQLITE_ABORT in this case.
479 */
480 rc = SQLITE_ABORT;
481 }else{
482 char *zErr;
483 rc = blobSeekToRow(p, iRow, &zErr);
484 if( rc!=SQLITE_OK ){
485 sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
486 sqlite3DbFree(db, zErr);
487 }
488 assert( rc!=SQLITE_SCHEMA );
489 }
490
491 rc = sqlite3ApiExit(db, rc);
daneefab752010-12-06 17:11:05 +0000492 assert( rc==SQLITE_OK || p->pStmt==0 );
dan4e76cc32010-10-20 18:56:04 +0000493 sqlite3_mutex_leave(db->mutex);
494 return rc;
495}
496
danielk1977b4e9af92007-05-01 17:49:49 +0000497#endif /* #ifndef SQLITE_OMIT_INCRBLOB */