blob: 520d16c985f511bfea377542ee778fa65cbf39d0 [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 ){
drh14da87f2013-11-20 21:51:33 +000069 VdbeCursor *pC = v->apCsr[0];
70 u32 type = pC->aType[p->iCol];
dan4e76cc32010-10-20 18:56:04 +000071 if( type<12 ){
72 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
73 type==0?"null": type==7?"real": "integer"
74 );
75 rc = SQLITE_ERROR;
76 sqlite3_finalize(p->pStmt);
77 p->pStmt = 0;
78 }else{
drh14da87f2013-11-20 21:51:33 +000079 p->iOffset = pC->aType[p->iCol + pC->nField];
dan4e76cc32010-10-20 18:56:04 +000080 p->nByte = sqlite3VdbeSerialTypeLen(type);
drhc960dcb2015-11-20 19:22:01 +000081 p->pCsr = pC->uc.pCursor;
dan5a500af2014-03-11 20:33:04 +000082 sqlite3BtreeIncrblobCursor(p->pCsr);
dan4e76cc32010-10-20 18:56:04 +000083 }
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 */
danielk19778cbadb02007-05-03 16:31:26 +0000120 int rc = SQLITE_OK;
drh6ee700c2009-06-01 19:53:30 +0000121 char *zErr = 0;
122 Table *pTab;
dan61c7f592010-10-26 18:42:52 +0000123 Parse *pParse = 0;
124 Incrblob *pBlob = 0;
danielk1977b4e9af92007-05-01 17:49:49 +0000125
drh9ca95732014-10-24 00:35:58 +0000126#ifdef SQLITE_ENABLE_API_ARMOR
drhd10d18d2015-02-07 15:16:35 +0000127 if( ppBlob==0 ){
128 return SQLITE_MISUSE_BKPT;
129 }
130#endif
131 *ppBlob = 0;
132#ifdef SQLITE_ENABLE_API_ARMOR
133 if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
drh9ca95732014-10-24 00:35:58 +0000134 return SQLITE_MISUSE_BKPT;
135 }
136#endif
dan4e76cc32010-10-20 18:56:04 +0000137 flags = !!flags; /* flags = (flags ? 1 : 0); */
dan4e76cc32010-10-20 18:56:04 +0000138
drh605264d2007-08-21 15:13:19 +0000139 sqlite3_mutex_enter(db->mutex);
dan4e76cc32010-10-20 18:56:04 +0000140
141 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
dan61c7f592010-10-26 18:42:52 +0000142 if( !pBlob ) goto blob_open_out;
drh6ee700c2009-06-01 19:53:30 +0000143 pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
dan61c7f592010-10-26 18:42:52 +0000144 if( !pParse ) goto blob_open_out;
dan4e76cc32010-10-20 18:56:04 +0000145
danielk1977b4e9af92007-05-01 17:49:49 +0000146 do {
drh6ee700c2009-06-01 19:53:30 +0000147 memset(pParse, 0, sizeof(Parse));
148 pParse->db = db;
dan61c7f592010-10-26 18:42:52 +0000149 sqlite3DbFree(db, zErr);
150 zErr = 0;
danielk1977b4e9af92007-05-01 17:49:49 +0000151
drhff0587c2007-08-29 17:43:19 +0000152 sqlite3BtreeEnterAll(db);
drh6ee700c2009-06-01 19:53:30 +0000153 pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
danielk197736961ed2008-04-24 09:49:55 +0000154 if( pTab && IsVirtual(pTab) ){
155 pTab = 0;
drh6ee700c2009-06-01 19:53:30 +0000156 sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
danielk197736961ed2008-04-24 09:49:55 +0000157 }
drh63f0eed2013-11-02 22:09:48 +0000158 if( pTab && !HasRowid(pTab) ){
159 pTab = 0;
160 sqlite3ErrorMsg(pParse, "cannot open table without rowid: %s", zTable);
161 }
danielk197736961ed2008-04-24 09:49:55 +0000162#ifndef SQLITE_OMIT_VIEW
163 if( pTab && pTab->pSelect ){
164 pTab = 0;
drh6ee700c2009-06-01 19:53:30 +0000165 sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
danielk197736961ed2008-04-24 09:49:55 +0000166 }
167#endif
danielk1977b4e9af92007-05-01 17:49:49 +0000168 if( !pTab ){
drh6ee700c2009-06-01 19:53:30 +0000169 if( pParse->zErrMsg ){
170 sqlite3DbFree(db, zErr);
171 zErr = pParse->zErrMsg;
172 pParse->zErrMsg = 0;
danielk19778cbadb02007-05-03 16:31:26 +0000173 }
danielk19778cbadb02007-05-03 16:31:26 +0000174 rc = SQLITE_ERROR;
drhff0587c2007-08-29 17:43:19 +0000175 sqlite3BtreeLeaveAll(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000176 goto blob_open_out;
177 }
dane437ca52011-07-11 19:45:38 +0000178 pBlob->pTab = pTab;
drh69c33822016-08-18 14:33:11 +0000179 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
danielk1977b4e9af92007-05-01 17:49:49 +0000180
181 /* Now search pTab for the exact column. */
dan61c7f592010-10-26 18:42:52 +0000182 for(iCol=0; iCol<pTab->nCol; iCol++) {
danielk1977b4e9af92007-05-01 17:49:49 +0000183 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
184 break;
185 }
186 }
187 if( iCol==pTab->nCol ){
drh6ee700c2009-06-01 19:53:30 +0000188 sqlite3DbFree(db, zErr);
189 zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
danielk1977b4e9af92007-05-01 17:49:49 +0000190 rc = SQLITE_ERROR;
drhff0587c2007-08-29 17:43:19 +0000191 sqlite3BtreeLeaveAll(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000192 goto blob_open_out;
193 }
194
danielk1977f1819242007-05-03 18:14:10 +0000195 /* If the value is being opened for writing, check that the
dan1da40a32009-09-19 17:00:31 +0000196 ** column is not indexed, and that it is not part of a foreign key.
197 ** It is against the rules to open a column to which either of these
198 ** descriptions applies for writing. */
danielk1977f1819242007-05-03 18:14:10 +0000199 if( flags ){
dan1da40a32009-09-19 17:00:31 +0000200 const char *zFault = 0;
danielk1977f1819242007-05-03 18:14:10 +0000201 Index *pIdx;
dan1da40a32009-09-19 17:00:31 +0000202#ifndef SQLITE_OMIT_FOREIGN_KEY
203 if( db->flags&SQLITE_ForeignKeys ){
dan13167002009-10-02 06:35:06 +0000204 /* Check that the column is not part of an FK child key definition. It
205 ** is not necessary to check if it is part of a parent key, as parent
206 ** key columns must be indexed. The check below will pick up this
207 ** case. */
dan1da40a32009-09-19 17:00:31 +0000208 FKey *pFKey;
209 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
210 int j;
211 for(j=0; j<pFKey->nCol; j++){
212 if( pFKey->aCol[j].iFrom==iCol ){
213 zFault = "foreign key";
214 }
215 }
216 }
217 }
218#endif
danielk1977f1819242007-05-03 18:14:10 +0000219 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
220 int j;
drhbbbdc832013-10-22 18:01:40 +0000221 for(j=0; j<pIdx->nKeyCol; j++){
drh1f9ca2c2015-08-25 16:57:52 +0000222 /* FIXME: Be smarter about indexes that use expressions */
drh4b92f982015-09-29 17:20:14 +0000223 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
dan1da40a32009-09-19 17:00:31 +0000224 zFault = "indexed";
danielk1977f1819242007-05-03 18:14:10 +0000225 }
226 }
227 }
dan1da40a32009-09-19 17:00:31 +0000228 if( zFault ){
229 sqlite3DbFree(db, zErr);
230 zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
231 rc = SQLITE_ERROR;
dan1da40a32009-09-19 17:00:31 +0000232 sqlite3BtreeLeaveAll(db);
233 goto blob_open_out;
234 }
danielk1977f1819242007-05-03 18:14:10 +0000235 }
236
drh9ac79622013-12-18 15:11:47 +0000237 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse);
dan61c7f592010-10-26 18:42:52 +0000238 assert( pBlob->pStmt || db->mallocFailed );
239 if( pBlob->pStmt ){
drh2ce18652016-01-16 20:50:21 +0000240
241 /* This VDBE program seeks a btree cursor to the identified
242 ** db/table/row entry. The reason for using a vdbe program instead
243 ** of writing code to use the b-tree layer directly is that the
244 ** vdbe program will take advantage of the various transaction,
245 ** locking and error handling infrastructure built into the vdbe.
246 **
247 ** After seeking the cursor, the vdbe executes an OP_ResultRow.
248 ** Code external to the Vdbe then "borrows" the b-tree cursor and
249 ** uses it to implement the blob_read(), blob_write() and
250 ** blob_bytes() functions.
251 **
252 ** The sqlite3_blob_close() function finalizes the vdbe program,
253 ** which closes the b-tree cursor and (possibly) commits the
254 ** transaction.
255 */
drh1b325542016-02-03 01:55:44 +0000256 static const int iLn = VDBE_OFFSET_LINENO(2);
drh2ce18652016-01-16 20:50:21 +0000257 static const VdbeOpList openBlob[] = {
drh1b325542016-02-03 01:55:44 +0000258 {OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */
259 {OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */
260 {OP_Variable, 1, 1, 0}, /* 2: Move ?1 into reg[1] */
261 {OP_NotExists, 0, 7, 1}, /* 3: Seek the cursor */
262 {OP_Column, 0, 0, 1}, /* 4 */
263 {OP_ResultRow, 1, 0, 0}, /* 5 */
264 {OP_Goto, 0, 2, 0}, /* 6 */
drh65f38d92016-11-22 01:26:42 +0000265 {OP_Halt, 0, 0, 0}, /* 7 */
drh2ce18652016-01-16 20:50:21 +0000266 };
dan61c7f592010-10-26 18:42:52 +0000267 Vdbe *v = (Vdbe *)pBlob->pStmt;
danielk1977b4e9af92007-05-01 17:49:49 +0000268 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drh2ce18652016-01-16 20:50:21 +0000269 VdbeOp *aOp;
drhb22f7c82014-02-06 23:56:27 +0000270
271 sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags,
272 pTab->pSchema->schema_cookie,
273 pTab->pSchema->iGeneration);
274 sqlite3VdbeChangeP5(v, 1);
drh2ce18652016-01-16 20:50:21 +0000275 aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
danielk1977b4e9af92007-05-01 17:49:49 +0000276
drhff0587c2007-08-29 17:43:19 +0000277 /* Make sure a mutex is held on the table to be accessed */
drhfb982642007-08-30 01:19:59 +0000278 sqlite3VdbeUsesBtree(v, iDb);
drhff0587c2007-08-29 17:43:19 +0000279
drh2ce18652016-01-16 20:50:21 +0000280 if( db->mallocFailed==0 ){
281 assert( aOp!=0 );
282 /* Configure the OP_TableLock instruction */
danc548b782010-06-19 19:06:33 +0000283#ifdef SQLITE_OMIT_SHARED_CACHE
drh2ce18652016-01-16 20:50:21 +0000284 aOp[0].opcode = OP_Noop;
danc548b782010-06-19 19:06:33 +0000285#else
drh2ce18652016-01-16 20:50:21 +0000286 aOp[0].p1 = iDb;
287 aOp[0].p2 = pTab->tnum;
288 aOp[0].p3 = flags;
289 sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
290 }
291 if( db->mallocFailed==0 ){
danc548b782010-06-19 19:06:33 +0000292#endif
danielk197796d48e92009-06-29 06:00:37 +0000293
drh2ce18652016-01-16 20:50:21 +0000294 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
295 ** parameter of the other to pTab->tnum. */
drhe617bc82016-01-18 00:46:11 +0000296 if( flags ) aOp[1].opcode = OP_OpenWrite;
297 aOp[1].p2 = pTab->tnum;
298 aOp[1].p3 = iDb;
danielk1977b4e9af92007-05-01 17:49:49 +0000299
drh2ce18652016-01-16 20:50:21 +0000300 /* Configure the number of columns. Configure the cursor to
301 ** think that the table has one more column than it really
302 ** does. An OP_Column to retrieve this imaginary column will
303 ** always return an SQL NULL. This is useful because it means
304 ** we can invoke OP_Column to fill in the vdbe cursors type
305 ** and offset cache without causing any IO.
306 */
drhe617bc82016-01-18 00:46:11 +0000307 aOp[1].p4type = P4_INT32;
308 aOp[1].p4.i = pTab->nCol+1;
309 aOp[4].p2 = pTab->nCol;
drh2ce18652016-01-16 20:50:21 +0000310
drh124c0b42011-06-01 18:15:55 +0000311 pParse->nVar = 1;
312 pParse->nMem = 1;
313 pParse->nTab = 1;
314 sqlite3VdbeMakeReady(v, pParse);
danielk197792d4d7a2007-05-04 12:05:56 +0000315 }
danielk1977b4e9af92007-05-01 17:49:49 +0000316 }
drhff0587c2007-08-29 17:43:19 +0000317
danielk1977b4e9af92007-05-01 17:49:49 +0000318 pBlob->flags = flags;
dan4e76cc32010-10-20 18:56:04 +0000319 pBlob->iCol = iCol;
drh605264d2007-08-21 15:13:19 +0000320 pBlob->db = db;
dan4e76cc32010-10-20 18:56:04 +0000321 sqlite3BtreeLeaveAll(db);
dan4e76cc32010-10-20 18:56:04 +0000322 if( db->mallocFailed ){
323 goto blob_open_out;
324 }
dan4e76cc32010-10-20 18:56:04 +0000325 sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
326 rc = blobSeekToRow(pBlob, iRow, &zErr);
drh60625312013-04-06 18:06:51 +0000327 } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
danielk1977b4e9af92007-05-01 17:49:49 +0000328
329blob_open_out:
dan4e76cc32010-10-20 18:56:04 +0000330 if( rc==SQLITE_OK && db->mallocFailed==0 ){
331 *ppBlob = (sqlite3_blob *)pBlob;
332 }else{
dan4e76cc32010-10-20 18:56:04 +0000333 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
334 sqlite3DbFree(db, pBlob);
danielk1977b4e9af92007-05-01 17:49:49 +0000335 }
drh13f40da2014-08-22 18:00:11 +0000336 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
drh6ee700c2009-06-01 19:53:30 +0000337 sqlite3DbFree(db, zErr);
drhf30a9692013-11-15 01:10:18 +0000338 sqlite3ParserReset(pParse);
drh6ee700c2009-06-01 19:53:30 +0000339 sqlite3StackFree(db, pParse);
drh605264d2007-08-21 15:13:19 +0000340 rc = sqlite3ApiExit(db, rc);
341 sqlite3_mutex_leave(db->mutex);
342 return rc;
danielk1977b4e9af92007-05-01 17:49:49 +0000343}
344
345/*
drh16a9b832007-05-05 18:39:25 +0000346** Close a blob handle that was previously created using
347** sqlite3_blob_open().
danielk1977b4e9af92007-05-01 17:49:49 +0000348*/
349int sqlite3_blob_close(sqlite3_blob *pBlob){
350 Incrblob *p = (Incrblob *)pBlob;
drh605264d2007-08-21 15:13:19 +0000351 int rc;
drhd9da78a2009-03-24 15:08:09 +0000352 sqlite3 *db;
drh605264d2007-08-21 15:13:19 +0000353
drh28414ee2009-05-09 15:17:47 +0000354 if( p ){
355 db = p->db;
356 sqlite3_mutex_enter(db->mutex);
357 rc = sqlite3_finalize(p->pStmt);
358 sqlite3DbFree(db, p);
359 sqlite3_mutex_leave(db->mutex);
360 }else{
361 rc = SQLITE_OK;
362 }
drh605264d2007-08-21 15:13:19 +0000363 return rc;
danielk1977b4e9af92007-05-01 17:49:49 +0000364}
365
drh605264d2007-08-21 15:13:19 +0000366/*
367** Perform a read or write operation on a blob
368*/
drhee858132007-05-08 20:37:38 +0000369static int blobReadWrite(
danielk1977dcbb5d32007-05-04 18:36:44 +0000370 sqlite3_blob *pBlob,
371 void *z,
372 int n,
373 int iOffset,
374 int (*xCall)(BtCursor*, u32, u32, void*)
375){
376 int rc;
377 Incrblob *p = (Incrblob *)pBlob;
drh605264d2007-08-21 15:13:19 +0000378 Vdbe *v;
drh28414ee2009-05-09 15:17:47 +0000379 sqlite3 *db;
danielk1977dcbb5d32007-05-04 18:36:44 +0000380
drh413c3d32010-02-23 20:11:56 +0000381 if( p==0 ) return SQLITE_MISUSE_BKPT;
drh28414ee2009-05-09 15:17:47 +0000382 db = p->db;
drh605264d2007-08-21 15:13:19 +0000383 sqlite3_mutex_enter(db->mutex);
drh605264d2007-08-21 15:13:19 +0000384 v = (Vdbe*)p->pStmt;
danielk1977a8b30182008-10-02 14:49:01 +0000385
drhd10d18d2015-02-07 15:16:35 +0000386 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
danielk1977a8b30182008-10-02 14:49:01 +0000387 /* Request is out of range. Return a transient error. */
388 rc = SQLITE_ERROR;
dan4e76cc32010-10-20 18:56:04 +0000389 }else if( v==0 ){
danielk1977a8b30182008-10-02 14:49:01 +0000390 /* If there is no statement handle, then the blob-handle has
391 ** already been invalidated. Return SQLITE_ABORT in this case.
392 */
drh605264d2007-08-21 15:13:19 +0000393 rc = SQLITE_ABORT;
danielk1977dcbb5d32007-05-04 18:36:44 +0000394 }else{
drh605264d2007-08-21 15:13:19 +0000395 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
396 ** returned, clean-up the statement handle.
397 */
398 assert( db == v->db );
drhff0587c2007-08-29 17:43:19 +0000399 sqlite3BtreeEnterCursor(p->pCsr);
dane437ca52011-07-11 19:45:38 +0000400
401#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
drh7f197bb2011-08-03 21:32:11 +0000402 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
dane437ca52011-07-11 19:45:38 +0000403 /* If a pre-update hook is registered and this is a write cursor,
404 ** invoke it here.
405 **
406 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
407 ** operation should really be an SQLITE_UPDATE. This is probably
408 ** incorrect, but is convenient because at this point the new.* values
409 ** are not easily obtainable. And for the sessions module, an
410 ** SQLITE_UPDATE where the PK columns do not change is handled in the
411 ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually
412 ** slightly more efficient). Since you cannot write to a PK column
413 ** using the incremental-blob API, this works. For the sessions module
414 ** anyhow.
415 */
416 sqlite3_int64 iKey;
drha7c90c42016-06-04 20:37:10 +0000417 iKey = sqlite3BtreeIntegerKey(p->pCsr);
dane437ca52011-07-11 19:45:38 +0000418 sqlite3VdbePreUpdateHook(
419 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1
420 );
421 }
422#endif
423
drh605264d2007-08-21 15:13:19 +0000424 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
drhff0587c2007-08-29 17:43:19 +0000425 sqlite3BtreeLeaveCursor(p->pCsr);
drh605264d2007-08-21 15:13:19 +0000426 if( rc==SQLITE_ABORT ){
427 sqlite3VdbeFinalize(v);
428 p->pStmt = 0;
429 }else{
drh605264d2007-08-21 15:13:19 +0000430 v->rc = rc;
431 }
danielk1977dcbb5d32007-05-04 18:36:44 +0000432 }
dan923c4b32014-11-10 17:53:03 +0000433 sqlite3Error(db, rc);
drh605264d2007-08-21 15:13:19 +0000434 rc = sqlite3ApiExit(db, rc);
435 sqlite3_mutex_leave(db->mutex);
436 return rc;
danielk1977dcbb5d32007-05-04 18:36:44 +0000437}
438
danielk1977b4e9af92007-05-01 17:49:49 +0000439/*
440** Read data from a blob handle.
441*/
442int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
drhcb3cabd2016-11-25 19:18:28 +0000443 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreePayloadChecked);
danielk1977b4e9af92007-05-01 17:49:49 +0000444}
445
446/*
447** Write data to a blob handle.
448*/
449int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
danielk1977dcbb5d32007-05-04 18:36:44 +0000450 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
danielk1977b4e9af92007-05-01 17:49:49 +0000451}
452
453/*
454** Query a blob handle for the size of the data.
drh605264d2007-08-21 15:13:19 +0000455**
456** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
457** so no mutex is required for access.
danielk1977b4e9af92007-05-01 17:49:49 +0000458*/
459int sqlite3_blob_bytes(sqlite3_blob *pBlob){
460 Incrblob *p = (Incrblob *)pBlob;
daneefab752010-12-06 17:11:05 +0000461 return (p && p->pStmt) ? p->nByte : 0;
danielk1977b4e9af92007-05-01 17:49:49 +0000462}
463
dan4e76cc32010-10-20 18:56:04 +0000464/*
465** Move an existing blob handle to point to a different row of the same
466** database table.
467**
468** If an error occurs, or if the specified row does not exist or does not
469** contain a blob or text value, then an error code is returned and the
470** database handle error code and message set. If this happens, then all
471** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
472** immediately return SQLITE_ABORT.
473*/
474int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
475 int rc;
476 Incrblob *p = (Incrblob *)pBlob;
477 sqlite3 *db;
478
479 if( p==0 ) return SQLITE_MISUSE_BKPT;
480 db = p->db;
481 sqlite3_mutex_enter(db->mutex);
482
483 if( p->pStmt==0 ){
484 /* If there is no statement handle, then the blob-handle has
485 ** already been invalidated. Return SQLITE_ABORT in this case.
486 */
487 rc = SQLITE_ABORT;
488 }else{
489 char *zErr;
490 rc = blobSeekToRow(p, iRow, &zErr);
491 if( rc!=SQLITE_OK ){
drh13f40da2014-08-22 18:00:11 +0000492 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
dan4e76cc32010-10-20 18:56:04 +0000493 sqlite3DbFree(db, zErr);
494 }
495 assert( rc!=SQLITE_SCHEMA );
496 }
497
498 rc = sqlite3ApiExit(db, rc);
daneefab752010-12-06 17:11:05 +0000499 assert( rc==SQLITE_OK || p->pStmt==0 );
dan4e76cc32010-10-20 18:56:04 +0000500 sqlite3_mutex_leave(db->mutex);
501 return rc;
502}
503
danielk1977b4e9af92007-05-01 17:49:49 +0000504#endif /* #ifndef SQLITE_OMIT_INCRBLOB */