blob: 2cdc3edb006c9c3ee7613aed273d125b72a079fc [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
dane3d82a82010-10-26 11:56:57 +000036/*
37** This function is used by both blob_open() and blob_reopen(). It seeks
38** the b-tree cursor associated with blob handle p to point to row iRow.
39** If successful, SQLITE_OK is returned and subsequent calls to
40** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
41**
42** If an error occurs, or if the specified row does not exist or does not
43** contain a value of type TEXT or BLOB in the column nominated when the
44** blob handle was opened, then an error code is returned and *pzErr may
45** be set to point to a buffer containing an error message. It is the
46** responsibility of the caller to free the error message buffer using
47** sqlite3DbFree().
48**
49** If an error does occur, then the b-tree cursor is closed. All subsequent
50** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
51** immediately return SQLITE_ABORT.
52*/
dan4e76cc32010-10-20 18:56:04 +000053static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
54 int rc; /* Error code */
55 char *zErr = 0; /* Error message */
56 Vdbe *v = (Vdbe *)p->pStmt;
57
dane3d82a82010-10-26 11:56:57 +000058 /* Set the value of the SQL statements only variable to integer iRow.
59 ** This is done directly instead of using sqlite3_bind_int64() to avoid
60 ** triggering asserts related to mutexes.
61 */
62 assert( v->aVar[0].flags&MEM_Int );
dan4e76cc32010-10-20 18:56:04 +000063 v->aVar[0].u.i = iRow;
dan4e76cc32010-10-20 18:56:04 +000064
dane3d82a82010-10-26 11:56:57 +000065 rc = sqlite3_step(p->pStmt);
dan4e76cc32010-10-20 18:56:04 +000066 if( rc==SQLITE_ROW ){
drh14da87f2013-11-20 21:51:33 +000067 VdbeCursor *pC = v->apCsr[0];
68 u32 type = pC->aType[p->iCol];
dan4e76cc32010-10-20 18:56:04 +000069 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{
drh14da87f2013-11-20 21:51:33 +000077 p->iOffset = pC->aType[p->iCol + pC->nField];
dan4e76cc32010-10-20 18:56:04 +000078 p->nByte = sqlite3VdbeSerialTypeLen(type);
drh14da87f2013-11-20 21:51:33 +000079 p->pCsr = pC->pCursor;
dan5a500af2014-03-11 20:33:04 +000080 sqlite3BtreeIncrblobCursor(p->pCsr);
dan4e76cc32010-10-20 18:56:04 +000081 }
82 }
83
84 if( rc==SQLITE_ROW ){
85 rc = SQLITE_OK;
86 }else if( p->pStmt ){
87 rc = sqlite3_finalize(p->pStmt);
88 p->pStmt = 0;
89 if( rc==SQLITE_OK ){
90 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
91 rc = SQLITE_ERROR;
92 }else{
93 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
94 }
95 }
96
97 assert( rc!=SQLITE_OK || zErr==0 );
98 assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
99
100 *pzErr = zErr;
101 return rc;
102}
103
danielk1977b4e9af92007-05-01 17:49:49 +0000104/*
105** Open a blob handle.
106*/
107int sqlite3_blob_open(
drh16a9b832007-05-05 18:39:25 +0000108 sqlite3* db, /* The database connection */
109 const char *zDb, /* The attached database containing the blob */
110 const char *zTable, /* The table containing the blob */
111 const char *zColumn, /* The column containing the blob */
112 sqlite_int64 iRow, /* The row containing the glob */
danielk1977b4e9af92007-05-01 17:49:49 +0000113 int flags, /* True -> read/write access, false -> read-only */
drh16a9b832007-05-05 18:39:25 +0000114 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
danielk1977b4e9af92007-05-01 17:49:49 +0000115){
danielk1977b4e9af92007-05-01 17:49:49 +0000116 int nAttempt = 0;
117 int iCol; /* Index of zColumn in row-record */
118
119 /* This VDBE program seeks a btree cursor to the identified
120 ** db/table/row entry. The reason for using a vdbe program instead
121 ** of writing code to use the b-tree layer directly is that the
122 ** vdbe program will take advantage of the various transaction,
123 ** locking and error handling infrastructure built into the vdbe.
124 **
drh2d401ab2008-01-10 23:50:11 +0000125 ** After seeking the cursor, the vdbe executes an OP_ResultRow.
danielk1977b4e9af92007-05-01 17:49:49 +0000126 ** Code external to the Vdbe then "borrows" the b-tree cursor and
127 ** uses it to implement the blob_read(), blob_write() and
128 ** blob_bytes() functions.
129 **
130 ** The sqlite3_blob_close() function finalizes the vdbe program,
131 ** which closes the b-tree cursor and (possibly) commits the
132 ** transaction.
133 */
drhb06a4ec2014-03-10 18:03:09 +0000134 static const int iLn = VDBE_OFFSET_LINENO(4);
danielk1977b4e9af92007-05-01 17:49:49 +0000135 static const VdbeOpList openBlob[] = {
drhb22f7c82014-02-06 23:56:27 +0000136 /* {OP_Transaction, 0, 0, 0}, // 0: Inserted separately */
137 {OP_TableLock, 0, 0, 0}, /* 1: Acquire a read or write lock */
danielk197796d48e92009-06-29 06:00:37 +0000138 /* One of the following two instructions is replaced by an OP_Noop. */
drhb22f7c82014-02-06 23:56:27 +0000139 {OP_OpenRead, 0, 0, 0}, /* 2: Open cursor 0 for reading */
140 {OP_OpenWrite, 0, 0, 0}, /* 3: Open cursor 0 for read/write */
drhb22f7c82014-02-06 23:56:27 +0000141 {OP_Variable, 1, 1, 1}, /* 4: Push the rowid to the stack */
142 {OP_NotExists, 0, 10, 1}, /* 5: Seek the cursor */
143 {OP_Column, 0, 0, 1}, /* 6 */
144 {OP_ResultRow, 1, 0, 0}, /* 7 */
145 {OP_Goto, 0, 4, 0}, /* 8 */
146 {OP_Close, 0, 0, 0}, /* 9 */
147 {OP_Halt, 0, 0, 0}, /* 10 */
danielk1977b4e9af92007-05-01 17:49:49 +0000148 };
149
danielk19778cbadb02007-05-03 16:31:26 +0000150 int rc = SQLITE_OK;
drh6ee700c2009-06-01 19:53:30 +0000151 char *zErr = 0;
152 Table *pTab;
dan61c7f592010-10-26 18:42:52 +0000153 Parse *pParse = 0;
154 Incrblob *pBlob = 0;
danielk1977b4e9af92007-05-01 17:49:49 +0000155
drh9ca95732014-10-24 00:35:58 +0000156#ifdef SQLITE_ENABLE_API_ARMOR
drhd10d18d2015-02-07 15:16:35 +0000157 if( ppBlob==0 ){
158 return SQLITE_MISUSE_BKPT;
159 }
160#endif
161 *ppBlob = 0;
162#ifdef SQLITE_ENABLE_API_ARMOR
163 if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
drh9ca95732014-10-24 00:35:58 +0000164 return SQLITE_MISUSE_BKPT;
165 }
166#endif
dan4e76cc32010-10-20 18:56:04 +0000167 flags = !!flags; /* flags = (flags ? 1 : 0); */
dan4e76cc32010-10-20 18:56:04 +0000168
drh605264d2007-08-21 15:13:19 +0000169 sqlite3_mutex_enter(db->mutex);
dan4e76cc32010-10-20 18:56:04 +0000170
171 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
dan61c7f592010-10-26 18:42:52 +0000172 if( !pBlob ) goto blob_open_out;
drh6ee700c2009-06-01 19:53:30 +0000173 pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
dan61c7f592010-10-26 18:42:52 +0000174 if( !pParse ) goto blob_open_out;
dan4e76cc32010-10-20 18:56:04 +0000175
danielk1977b4e9af92007-05-01 17:49:49 +0000176 do {
drh6ee700c2009-06-01 19:53:30 +0000177 memset(pParse, 0, sizeof(Parse));
178 pParse->db = db;
dan61c7f592010-10-26 18:42:52 +0000179 sqlite3DbFree(db, zErr);
180 zErr = 0;
danielk1977b4e9af92007-05-01 17:49:49 +0000181
drhff0587c2007-08-29 17:43:19 +0000182 sqlite3BtreeEnterAll(db);
drh6ee700c2009-06-01 19:53:30 +0000183 pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
danielk197736961ed2008-04-24 09:49:55 +0000184 if( pTab && IsVirtual(pTab) ){
185 pTab = 0;
drh6ee700c2009-06-01 19:53:30 +0000186 sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
danielk197736961ed2008-04-24 09:49:55 +0000187 }
drh63f0eed2013-11-02 22:09:48 +0000188 if( pTab && !HasRowid(pTab) ){
189 pTab = 0;
190 sqlite3ErrorMsg(pParse, "cannot open table without rowid: %s", zTable);
191 }
danielk197736961ed2008-04-24 09:49:55 +0000192#ifndef SQLITE_OMIT_VIEW
193 if( pTab && pTab->pSelect ){
194 pTab = 0;
drh6ee700c2009-06-01 19:53:30 +0000195 sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
danielk197736961ed2008-04-24 09:49:55 +0000196 }
197#endif
danielk1977b4e9af92007-05-01 17:49:49 +0000198 if( !pTab ){
drh6ee700c2009-06-01 19:53:30 +0000199 if( pParse->zErrMsg ){
200 sqlite3DbFree(db, zErr);
201 zErr = pParse->zErrMsg;
202 pParse->zErrMsg = 0;
danielk19778cbadb02007-05-03 16:31:26 +0000203 }
danielk19778cbadb02007-05-03 16:31:26 +0000204 rc = SQLITE_ERROR;
drhff0587c2007-08-29 17:43:19 +0000205 sqlite3BtreeLeaveAll(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000206 goto blob_open_out;
207 }
208
209 /* Now search pTab for the exact column. */
dan61c7f592010-10-26 18:42:52 +0000210 for(iCol=0; iCol<pTab->nCol; iCol++) {
danielk1977b4e9af92007-05-01 17:49:49 +0000211 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
212 break;
213 }
214 }
215 if( iCol==pTab->nCol ){
drh6ee700c2009-06-01 19:53:30 +0000216 sqlite3DbFree(db, zErr);
217 zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
danielk1977b4e9af92007-05-01 17:49:49 +0000218 rc = SQLITE_ERROR;
drhff0587c2007-08-29 17:43:19 +0000219 sqlite3BtreeLeaveAll(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000220 goto blob_open_out;
221 }
222
danielk1977f1819242007-05-03 18:14:10 +0000223 /* If the value is being opened for writing, check that the
dan1da40a32009-09-19 17:00:31 +0000224 ** column is not indexed, and that it is not part of a foreign key.
225 ** It is against the rules to open a column to which either of these
226 ** descriptions applies for writing. */
danielk1977f1819242007-05-03 18:14:10 +0000227 if( flags ){
dan1da40a32009-09-19 17:00:31 +0000228 const char *zFault = 0;
danielk1977f1819242007-05-03 18:14:10 +0000229 Index *pIdx;
dan1da40a32009-09-19 17:00:31 +0000230#ifndef SQLITE_OMIT_FOREIGN_KEY
231 if( db->flags&SQLITE_ForeignKeys ){
dan13167002009-10-02 06:35:06 +0000232 /* Check that the column is not part of an FK child key definition. It
233 ** is not necessary to check if it is part of a parent key, as parent
234 ** key columns must be indexed. The check below will pick up this
235 ** case. */
dan1da40a32009-09-19 17:00:31 +0000236 FKey *pFKey;
237 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
238 int j;
239 for(j=0; j<pFKey->nCol; j++){
240 if( pFKey->aCol[j].iFrom==iCol ){
241 zFault = "foreign key";
242 }
243 }
244 }
245 }
246#endif
danielk1977f1819242007-05-03 18:14:10 +0000247 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
248 int j;
drhbbbdc832013-10-22 18:01:40 +0000249 for(j=0; j<pIdx->nKeyCol; j++){
drh1f9ca2c2015-08-25 16:57:52 +0000250 /* FIXME: Be smarter about indexes that use expressions */
251 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==(-2) ){
dan1da40a32009-09-19 17:00:31 +0000252 zFault = "indexed";
danielk1977f1819242007-05-03 18:14:10 +0000253 }
254 }
255 }
dan1da40a32009-09-19 17:00:31 +0000256 if( zFault ){
257 sqlite3DbFree(db, zErr);
258 zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
259 rc = SQLITE_ERROR;
dan1da40a32009-09-19 17:00:31 +0000260 sqlite3BtreeLeaveAll(db);
261 goto blob_open_out;
262 }
danielk1977f1819242007-05-03 18:14:10 +0000263 }
264
drh9ac79622013-12-18 15:11:47 +0000265 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse);
dan61c7f592010-10-26 18:42:52 +0000266 assert( pBlob->pStmt || db->mallocFailed );
267 if( pBlob->pStmt ){
268 Vdbe *v = (Vdbe *)pBlob->pStmt;
danielk1977b4e9af92007-05-01 17:49:49 +0000269 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
dan61c7f592010-10-26 18:42:52 +0000270
drhb22f7c82014-02-06 23:56:27 +0000271
272 sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags,
273 pTab->pSchema->schema_cookie,
274 pTab->pSchema->iGeneration);
275 sqlite3VdbeChangeP5(v, 1);
drh688852a2014-02-17 22:40:43 +0000276 sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
danielk1977b4e9af92007-05-01 17:49:49 +0000277
drhff0587c2007-08-29 17:43:19 +0000278 /* Make sure a mutex is held on the table to be accessed */
drhfb982642007-08-30 01:19:59 +0000279 sqlite3VdbeUsesBtree(v, iDb);
drhff0587c2007-08-29 17:43:19 +0000280
danielk197796d48e92009-06-29 06:00:37 +0000281 /* Configure the OP_TableLock instruction */
danc548b782010-06-19 19:06:33 +0000282#ifdef SQLITE_OMIT_SHARED_CACHE
drhb22f7c82014-02-06 23:56:27 +0000283 sqlite3VdbeChangeToNoop(v, 1);
danc548b782010-06-19 19:06:33 +0000284#else
drhb22f7c82014-02-06 23:56:27 +0000285 sqlite3VdbeChangeP1(v, 1, iDb);
286 sqlite3VdbeChangeP2(v, 1, pTab->tnum);
287 sqlite3VdbeChangeP3(v, 1, flags);
288 sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
danc548b782010-06-19 19:06:33 +0000289#endif
danielk197796d48e92009-06-29 06:00:37 +0000290
danielk1977f1819242007-05-03 18:14:10 +0000291 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
danielk197796d48e92009-06-29 06:00:37 +0000292 ** parameter of the other to pTab->tnum. */
drhb22f7c82014-02-06 23:56:27 +0000293 sqlite3VdbeChangeToNoop(v, 3 - flags);
294 sqlite3VdbeChangeP2(v, 2 + flags, pTab->tnum);
295 sqlite3VdbeChangeP3(v, 2 + flags, iDb);
danielk1977b4e9af92007-05-01 17:49:49 +0000296
danielk1977d336e222009-02-20 10:58:41 +0000297 /* Configure the number of columns. Configure the cursor to
danielk1977b4e9af92007-05-01 17:49:49 +0000298 ** think that the table has one more column than it really
299 ** does. An OP_Column to retrieve this imaginary column will
300 ** always return an SQL NULL. This is useful because it means
301 ** we can invoke OP_Column to fill in the vdbe cursors type
302 ** and offset cache without causing any IO.
303 */
drhb22f7c82014-02-06 23:56:27 +0000304 sqlite3VdbeChangeP4(v, 2+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
305 sqlite3VdbeChangeP2(v, 6, pTab->nCol);
drh17435752007-08-16 04:30:38 +0000306 if( !db->mallocFailed ){
drh124c0b42011-06-01 18:15:55 +0000307 pParse->nVar = 1;
308 pParse->nMem = 1;
309 pParse->nTab = 1;
310 sqlite3VdbeMakeReady(v, pParse);
danielk197792d4d7a2007-05-04 12:05:56 +0000311 }
danielk1977b4e9af92007-05-01 17:49:49 +0000312 }
drhff0587c2007-08-29 17:43:19 +0000313
danielk1977b4e9af92007-05-01 17:49:49 +0000314 pBlob->flags = flags;
dan4e76cc32010-10-20 18:56:04 +0000315 pBlob->iCol = iCol;
drh605264d2007-08-21 15:13:19 +0000316 pBlob->db = db;
dan4e76cc32010-10-20 18:56:04 +0000317 sqlite3BtreeLeaveAll(db);
dan4e76cc32010-10-20 18:56:04 +0000318 if( db->mallocFailed ){
319 goto blob_open_out;
320 }
dan4e76cc32010-10-20 18:56:04 +0000321 sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
322 rc = blobSeekToRow(pBlob, iRow, &zErr);
drh60625312013-04-06 18:06:51 +0000323 } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
danielk1977b4e9af92007-05-01 17:49:49 +0000324
325blob_open_out:
dan4e76cc32010-10-20 18:56:04 +0000326 if( rc==SQLITE_OK && db->mallocFailed==0 ){
327 *ppBlob = (sqlite3_blob *)pBlob;
328 }else{
dan4e76cc32010-10-20 18:56:04 +0000329 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
330 sqlite3DbFree(db, pBlob);
danielk1977b4e9af92007-05-01 17:49:49 +0000331 }
drh13f40da2014-08-22 18:00:11 +0000332 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
drh6ee700c2009-06-01 19:53:30 +0000333 sqlite3DbFree(db, zErr);
drhf30a9692013-11-15 01:10:18 +0000334 sqlite3ParserReset(pParse);
drh6ee700c2009-06-01 19:53:30 +0000335 sqlite3StackFree(db, pParse);
drh605264d2007-08-21 15:13:19 +0000336 rc = sqlite3ApiExit(db, rc);
337 sqlite3_mutex_leave(db->mutex);
338 return rc;
danielk1977b4e9af92007-05-01 17:49:49 +0000339}
340
341/*
drh16a9b832007-05-05 18:39:25 +0000342** Close a blob handle that was previously created using
343** sqlite3_blob_open().
danielk1977b4e9af92007-05-01 17:49:49 +0000344*/
345int sqlite3_blob_close(sqlite3_blob *pBlob){
346 Incrblob *p = (Incrblob *)pBlob;
drh605264d2007-08-21 15:13:19 +0000347 int rc;
drhd9da78a2009-03-24 15:08:09 +0000348 sqlite3 *db;
drh605264d2007-08-21 15:13:19 +0000349
drh28414ee2009-05-09 15:17:47 +0000350 if( p ){
351 db = p->db;
352 sqlite3_mutex_enter(db->mutex);
353 rc = sqlite3_finalize(p->pStmt);
354 sqlite3DbFree(db, p);
355 sqlite3_mutex_leave(db->mutex);
356 }else{
357 rc = SQLITE_OK;
358 }
drh605264d2007-08-21 15:13:19 +0000359 return rc;
danielk1977b4e9af92007-05-01 17:49:49 +0000360}
361
drh605264d2007-08-21 15:13:19 +0000362/*
363** Perform a read or write operation on a blob
364*/
drhee858132007-05-08 20:37:38 +0000365static int blobReadWrite(
danielk1977dcbb5d32007-05-04 18:36:44 +0000366 sqlite3_blob *pBlob,
367 void *z,
368 int n,
369 int iOffset,
370 int (*xCall)(BtCursor*, u32, u32, void*)
371){
372 int rc;
373 Incrblob *p = (Incrblob *)pBlob;
drh605264d2007-08-21 15:13:19 +0000374 Vdbe *v;
drh28414ee2009-05-09 15:17:47 +0000375 sqlite3 *db;
danielk1977dcbb5d32007-05-04 18:36:44 +0000376
drh413c3d32010-02-23 20:11:56 +0000377 if( p==0 ) return SQLITE_MISUSE_BKPT;
drh28414ee2009-05-09 15:17:47 +0000378 db = p->db;
drh605264d2007-08-21 15:13:19 +0000379 sqlite3_mutex_enter(db->mutex);
drh605264d2007-08-21 15:13:19 +0000380 v = (Vdbe*)p->pStmt;
danielk1977a8b30182008-10-02 14:49:01 +0000381
drhd10d18d2015-02-07 15:16:35 +0000382 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
danielk1977a8b30182008-10-02 14:49:01 +0000383 /* Request is out of range. Return a transient error. */
384 rc = SQLITE_ERROR;
dan4e76cc32010-10-20 18:56:04 +0000385 }else if( v==0 ){
danielk1977a8b30182008-10-02 14:49:01 +0000386 /* If there is no statement handle, then the blob-handle has
387 ** already been invalidated. Return SQLITE_ABORT in this case.
388 */
drh605264d2007-08-21 15:13:19 +0000389 rc = SQLITE_ABORT;
danielk1977dcbb5d32007-05-04 18:36:44 +0000390 }else{
drh605264d2007-08-21 15:13:19 +0000391 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
392 ** returned, clean-up the statement handle.
393 */
394 assert( db == v->db );
drhff0587c2007-08-29 17:43:19 +0000395 sqlite3BtreeEnterCursor(p->pCsr);
drh605264d2007-08-21 15:13:19 +0000396 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
drhff0587c2007-08-29 17:43:19 +0000397 sqlite3BtreeLeaveCursor(p->pCsr);
drh605264d2007-08-21 15:13:19 +0000398 if( rc==SQLITE_ABORT ){
399 sqlite3VdbeFinalize(v);
400 p->pStmt = 0;
401 }else{
drh605264d2007-08-21 15:13:19 +0000402 v->rc = rc;
403 }
danielk1977dcbb5d32007-05-04 18:36:44 +0000404 }
dan923c4b32014-11-10 17:53:03 +0000405 sqlite3Error(db, rc);
drh605264d2007-08-21 15:13:19 +0000406 rc = sqlite3ApiExit(db, rc);
407 sqlite3_mutex_leave(db->mutex);
408 return rc;
danielk1977dcbb5d32007-05-04 18:36:44 +0000409}
410
danielk1977b4e9af92007-05-01 17:49:49 +0000411/*
412** Read data from a blob handle.
413*/
414int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
danielk1977dcbb5d32007-05-04 18:36:44 +0000415 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
danielk1977b4e9af92007-05-01 17:49:49 +0000416}
417
418/*
419** Write data to a blob handle.
420*/
421int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
danielk1977dcbb5d32007-05-04 18:36:44 +0000422 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
danielk1977b4e9af92007-05-01 17:49:49 +0000423}
424
425/*
426** Query a blob handle for the size of the data.
drh605264d2007-08-21 15:13:19 +0000427**
428** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
429** so no mutex is required for access.
danielk1977b4e9af92007-05-01 17:49:49 +0000430*/
431int sqlite3_blob_bytes(sqlite3_blob *pBlob){
432 Incrblob *p = (Incrblob *)pBlob;
daneefab752010-12-06 17:11:05 +0000433 return (p && p->pStmt) ? p->nByte : 0;
danielk1977b4e9af92007-05-01 17:49:49 +0000434}
435
dan4e76cc32010-10-20 18:56:04 +0000436/*
437** Move an existing blob handle to point to a different row of the same
438** database table.
439**
440** If an error occurs, or if the specified row does not exist or does not
441** contain a blob or text value, then an error code is returned and the
442** database handle error code and message set. If this happens, then all
443** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
444** immediately return SQLITE_ABORT.
445*/
446int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
447 int rc;
448 Incrblob *p = (Incrblob *)pBlob;
449 sqlite3 *db;
450
451 if( p==0 ) return SQLITE_MISUSE_BKPT;
452 db = p->db;
453 sqlite3_mutex_enter(db->mutex);
454
455 if( p->pStmt==0 ){
456 /* If there is no statement handle, then the blob-handle has
457 ** already been invalidated. Return SQLITE_ABORT in this case.
458 */
459 rc = SQLITE_ABORT;
460 }else{
461 char *zErr;
462 rc = blobSeekToRow(p, iRow, &zErr);
463 if( rc!=SQLITE_OK ){
drh13f40da2014-08-22 18:00:11 +0000464 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
dan4e76cc32010-10-20 18:56:04 +0000465 sqlite3DbFree(db, zErr);
466 }
467 assert( rc!=SQLITE_SCHEMA );
468 }
469
470 rc = sqlite3ApiExit(db, rc);
daneefab752010-12-06 17:11:05 +0000471 assert( rc==SQLITE_OK || p->pStmt==0 );
dan4e76cc32010-10-20 18:56:04 +0000472 sqlite3_mutex_leave(db->mutex);
473 return rc;
474}
475
danielk1977b4e9af92007-05-01 17:49:49 +0000476#endif /* #ifndef SQLITE_OMIT_INCRBLOB */