blob: a18ee05b52b413d3db062b21bb968612bd7507f3 [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 {
danielk1977b4e9af92007-05-01 17:49:49 +000026 int nByte; /* Size of open blob, in bytes */
27 int iOffset; /* Byte offset of blob in cursor data */
drh36cae852017-01-21 14:11:28 +000028 u16 iCol; /* Table column this handle is open on */
danielk1977b4e9af92007-05-01 17:49:49 +000029 BtCursor *pCsr; /* Cursor pointing at blob row */
30 sqlite3_stmt *pStmt; /* Statement holding cursor open */
drh605264d2007-08-21 15:13:19 +000031 sqlite3 *db; /* The associated database */
dane437ca52011-07-11 19:45:38 +000032 char *zDb; /* Database name */
33 Table *pTab; /* Table object */
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
drh4df65fc2017-01-20 00:40:26 +000059 /* Set the value of register r[1] in the SQL statement to integer iRow.
60 ** This is done directly as a performance optimization
dane3d82a82010-10-26 11:56:57 +000061 */
drh4df65fc2017-01-20 00:40:26 +000062 v->aMem[1].flags = MEM_Int;
63 v->aMem[1].u.i = iRow;
64
65 /* If the statement has been run before (and is paused at the OP_ResultRow)
dan952523f2017-10-24 17:28:25 +000066 ** then back it up to the point where it does the OP_NotExists. This could
drh4df65fc2017-01-20 00:40:26 +000067 ** have been down with an extra OP_Goto, but simply setting the program
68 ** counter is faster. */
dan952523f2017-10-24 17:28:25 +000069 if( v->pc>4 ){
70 v->pc = 4;
71 assert( v->aOp[v->pc].opcode==OP_NotExists );
drhbaf5dec2017-01-31 19:02:15 +000072 rc = sqlite3VdbeExec(v);
drh3b2936f2017-01-21 16:27:56 +000073 }else{
74 rc = sqlite3_step(p->pStmt);
75 }
dan4e76cc32010-10-20 18:56:04 +000076 if( rc==SQLITE_ROW ){
drh14da87f2013-11-20 21:51:33 +000077 VdbeCursor *pC = v->apCsr[0];
drh3ab4ffc2021-11-11 11:23:08 +000078 u32 type;
79 assert( pC!=0 );
80 assert( pC->eCurType==CURTYPE_BTREE );
81 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
drh210b0d02017-01-25 04:41:34 +000082 testcase( pC->nHdrParsed==p->iCol );
83 testcase( pC->nHdrParsed==p->iCol+1 );
dan4e76cc32010-10-20 18:56:04 +000084 if( type<12 ){
85 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
86 type==0?"null": type==7?"real": "integer"
87 );
88 rc = SQLITE_ERROR;
89 sqlite3_finalize(p->pStmt);
90 p->pStmt = 0;
91 }else{
drh14da87f2013-11-20 21:51:33 +000092 p->iOffset = pC->aType[p->iCol + pC->nField];
dan4e76cc32010-10-20 18:56:04 +000093 p->nByte = sqlite3VdbeSerialTypeLen(type);
drhc960dcb2015-11-20 19:22:01 +000094 p->pCsr = pC->uc.pCursor;
dan5a500af2014-03-11 20:33:04 +000095 sqlite3BtreeIncrblobCursor(p->pCsr);
dan4e76cc32010-10-20 18:56:04 +000096 }
97 }
98
99 if( rc==SQLITE_ROW ){
100 rc = SQLITE_OK;
101 }else if( p->pStmt ){
102 rc = sqlite3_finalize(p->pStmt);
103 p->pStmt = 0;
104 if( rc==SQLITE_OK ){
105 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
106 rc = SQLITE_ERROR;
107 }else{
108 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
109 }
110 }
111
112 assert( rc!=SQLITE_OK || zErr==0 );
113 assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
114
115 *pzErr = zErr;
116 return rc;
117}
118
danielk1977b4e9af92007-05-01 17:49:49 +0000119/*
120** Open a blob handle.
121*/
122int sqlite3_blob_open(
drh16a9b832007-05-05 18:39:25 +0000123 sqlite3* db, /* The database connection */
124 const char *zDb, /* The attached database containing the blob */
125 const char *zTable, /* The table containing the blob */
126 const char *zColumn, /* The column containing the blob */
127 sqlite_int64 iRow, /* The row containing the glob */
drh36cae852017-01-21 14:11:28 +0000128 int wrFlag, /* True -> read/write access, false -> read-only */
drh16a9b832007-05-05 18:39:25 +0000129 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
danielk1977b4e9af92007-05-01 17:49:49 +0000130){
danielk1977b4e9af92007-05-01 17:49:49 +0000131 int nAttempt = 0;
132 int iCol; /* Index of zColumn in row-record */
danielk19778cbadb02007-05-03 16:31:26 +0000133 int rc = SQLITE_OK;
drh6ee700c2009-06-01 19:53:30 +0000134 char *zErr = 0;
135 Table *pTab;
dan61c7f592010-10-26 18:42:52 +0000136 Incrblob *pBlob = 0;
drh6903bf62017-08-01 20:59:41 +0000137 Parse sParse;
danielk1977b4e9af92007-05-01 17:49:49 +0000138
drh9ca95732014-10-24 00:35:58 +0000139#ifdef SQLITE_ENABLE_API_ARMOR
drhd10d18d2015-02-07 15:16:35 +0000140 if( ppBlob==0 ){
141 return SQLITE_MISUSE_BKPT;
142 }
143#endif
144 *ppBlob = 0;
145#ifdef SQLITE_ENABLE_API_ARMOR
146 if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
drh9ca95732014-10-24 00:35:58 +0000147 return SQLITE_MISUSE_BKPT;
148 }
149#endif
drh36cae852017-01-21 14:11:28 +0000150 wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */
dan4e76cc32010-10-20 18:56:04 +0000151
drh605264d2007-08-21 15:13:19 +0000152 sqlite3_mutex_enter(db->mutex);
dan4e76cc32010-10-20 18:56:04 +0000153
154 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
drhc692df22022-01-24 15:34:55 +0000155 while(1){
156 sqlite3ParseObjectInit(&sParse,db);
drh6903bf62017-08-01 20:59:41 +0000157 if( !pBlob ) goto blob_open_out;
dan61c7f592010-10-26 18:42:52 +0000158 sqlite3DbFree(db, zErr);
159 zErr = 0;
danielk1977b4e9af92007-05-01 17:49:49 +0000160
drhff0587c2007-08-29 17:43:19 +0000161 sqlite3BtreeEnterAll(db);
drh6903bf62017-08-01 20:59:41 +0000162 pTab = sqlite3LocateTable(&sParse, 0, zTable, zDb);
danielk197736961ed2008-04-24 09:49:55 +0000163 if( pTab && IsVirtual(pTab) ){
164 pTab = 0;
drh6903bf62017-08-01 20:59:41 +0000165 sqlite3ErrorMsg(&sParse, "cannot open virtual table: %s", zTable);
danielk197736961ed2008-04-24 09:49:55 +0000166 }
drh63f0eed2013-11-02 22:09:48 +0000167 if( pTab && !HasRowid(pTab) ){
168 pTab = 0;
drh6903bf62017-08-01 20:59:41 +0000169 sqlite3ErrorMsg(&sParse, "cannot open table without rowid: %s", zTable);
drh63f0eed2013-11-02 22:09:48 +0000170 }
danielk197736961ed2008-04-24 09:49:55 +0000171#ifndef SQLITE_OMIT_VIEW
drhf38524d2021-08-02 16:41:57 +0000172 if( pTab && IsView(pTab) ){
danielk197736961ed2008-04-24 09:49:55 +0000173 pTab = 0;
drh6903bf62017-08-01 20:59:41 +0000174 sqlite3ErrorMsg(&sParse, "cannot open view: %s", zTable);
danielk197736961ed2008-04-24 09:49:55 +0000175 }
176#endif
danielk1977b4e9af92007-05-01 17:49:49 +0000177 if( !pTab ){
drh6903bf62017-08-01 20:59:41 +0000178 if( sParse.zErrMsg ){
drh6ee700c2009-06-01 19:53:30 +0000179 sqlite3DbFree(db, zErr);
drh6903bf62017-08-01 20:59:41 +0000180 zErr = sParse.zErrMsg;
181 sParse.zErrMsg = 0;
danielk19778cbadb02007-05-03 16:31:26 +0000182 }
danielk19778cbadb02007-05-03 16:31:26 +0000183 rc = SQLITE_ERROR;
drhff0587c2007-08-29 17:43:19 +0000184 sqlite3BtreeLeaveAll(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000185 goto blob_open_out;
186 }
dane437ca52011-07-11 19:45:38 +0000187 pBlob->pTab = pTab;
drh69c33822016-08-18 14:33:11 +0000188 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
danielk1977b4e9af92007-05-01 17:49:49 +0000189
190 /* Now search pTab for the exact column. */
dan61c7f592010-10-26 18:42:52 +0000191 for(iCol=0; iCol<pTab->nCol; iCol++) {
drhcf9d36d2021-08-02 18:03:43 +0000192 if( sqlite3StrICmp(pTab->aCol[iCol].zCnName, zColumn)==0 ){
danielk1977b4e9af92007-05-01 17:49:49 +0000193 break;
194 }
195 }
196 if( iCol==pTab->nCol ){
drh6ee700c2009-06-01 19:53:30 +0000197 sqlite3DbFree(db, zErr);
198 zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
danielk1977b4e9af92007-05-01 17:49:49 +0000199 rc = SQLITE_ERROR;
drhff0587c2007-08-29 17:43:19 +0000200 sqlite3BtreeLeaveAll(db);
danielk1977b4e9af92007-05-01 17:49:49 +0000201 goto blob_open_out;
202 }
203
danielk1977f1819242007-05-03 18:14:10 +0000204 /* If the value is being opened for writing, check that the
dan1da40a32009-09-19 17:00:31 +0000205 ** column is not indexed, and that it is not part of a foreign key.
drh36cae852017-01-21 14:11:28 +0000206 */
207 if( wrFlag ){
dan1da40a32009-09-19 17:00:31 +0000208 const char *zFault = 0;
danielk1977f1819242007-05-03 18:14:10 +0000209 Index *pIdx;
dan1da40a32009-09-19 17:00:31 +0000210#ifndef SQLITE_OMIT_FOREIGN_KEY
211 if( db->flags&SQLITE_ForeignKeys ){
dan13167002009-10-02 06:35:06 +0000212 /* Check that the column is not part of an FK child key definition. It
213 ** is not necessary to check if it is part of a parent key, as parent
214 ** key columns must be indexed. The check below will pick up this
215 ** case. */
dan1da40a32009-09-19 17:00:31 +0000216 FKey *pFKey;
drh78b2fa82021-10-07 12:11:20 +0000217 assert( IsOrdinaryTable(pTab) );
drhf38524d2021-08-02 16:41:57 +0000218 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
dan1da40a32009-09-19 17:00:31 +0000219 int j;
220 for(j=0; j<pFKey->nCol; j++){
221 if( pFKey->aCol[j].iFrom==iCol ){
222 zFault = "foreign key";
223 }
224 }
225 }
226 }
227#endif
danielk1977f1819242007-05-03 18:14:10 +0000228 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
229 int j;
drhbbbdc832013-10-22 18:01:40 +0000230 for(j=0; j<pIdx->nKeyCol; j++){
drh1f9ca2c2015-08-25 16:57:52 +0000231 /* FIXME: Be smarter about indexes that use expressions */
drh4b92f982015-09-29 17:20:14 +0000232 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
dan1da40a32009-09-19 17:00:31 +0000233 zFault = "indexed";
danielk1977f1819242007-05-03 18:14:10 +0000234 }
235 }
236 }
dan1da40a32009-09-19 17:00:31 +0000237 if( zFault ){
238 sqlite3DbFree(db, zErr);
239 zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
240 rc = SQLITE_ERROR;
dan1da40a32009-09-19 17:00:31 +0000241 sqlite3BtreeLeaveAll(db);
242 goto blob_open_out;
243 }
danielk1977f1819242007-05-03 18:14:10 +0000244 }
245
drh6903bf62017-08-01 20:59:41 +0000246 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
dan61c7f592010-10-26 18:42:52 +0000247 assert( pBlob->pStmt || db->mallocFailed );
248 if( pBlob->pStmt ){
drh2ce18652016-01-16 20:50:21 +0000249
250 /* This VDBE program seeks a btree cursor to the identified
251 ** db/table/row entry. The reason for using a vdbe program instead
252 ** of writing code to use the b-tree layer directly is that the
253 ** vdbe program will take advantage of the various transaction,
254 ** locking and error handling infrastructure built into the vdbe.
255 **
256 ** After seeking the cursor, the vdbe executes an OP_ResultRow.
257 ** Code external to the Vdbe then "borrows" the b-tree cursor and
258 ** uses it to implement the blob_read(), blob_write() and
259 ** blob_bytes() functions.
260 **
261 ** The sqlite3_blob_close() function finalizes the vdbe program,
262 ** which closes the b-tree cursor and (possibly) commits the
263 ** transaction.
264 */
drh1b325542016-02-03 01:55:44 +0000265 static const int iLn = VDBE_OFFSET_LINENO(2);
drh2ce18652016-01-16 20:50:21 +0000266 static const VdbeOpList openBlob[] = {
drh1b325542016-02-03 01:55:44 +0000267 {OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */
268 {OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */
drh4df65fc2017-01-20 00:40:26 +0000269 /* blobSeekToRow() will initialize r[1] to the desired rowid */
270 {OP_NotExists, 0, 5, 1}, /* 2: Seek the cursor to rowid=r[1] */
271 {OP_Column, 0, 0, 1}, /* 3 */
272 {OP_ResultRow, 1, 0, 0}, /* 4 */
273 {OP_Halt, 0, 0, 0}, /* 5 */
drh2ce18652016-01-16 20:50:21 +0000274 };
dan61c7f592010-10-26 18:42:52 +0000275 Vdbe *v = (Vdbe *)pBlob->pStmt;
danielk1977b4e9af92007-05-01 17:49:49 +0000276 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drh2ce18652016-01-16 20:50:21 +0000277 VdbeOp *aOp;
drhb22f7c82014-02-06 23:56:27 +0000278
drh36cae852017-01-21 14:11:28 +0000279 sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag,
drhb22f7c82014-02-06 23:56:27 +0000280 pTab->pSchema->schema_cookie,
281 pTab->pSchema->iGeneration);
drh55965612017-09-16 20:58:41 +0000282 sqlite3VdbeChangeP5(v, 1);
283 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
drh2ce18652016-01-16 20:50:21 +0000284 aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
danielk1977b4e9af92007-05-01 17:49:49 +0000285
drhff0587c2007-08-29 17:43:19 +0000286 /* Make sure a mutex is held on the table to be accessed */
drhfb982642007-08-30 01:19:59 +0000287 sqlite3VdbeUsesBtree(v, iDb);
drhff0587c2007-08-29 17:43:19 +0000288
drh2ce18652016-01-16 20:50:21 +0000289 if( db->mallocFailed==0 ){
290 assert( aOp!=0 );
291 /* Configure the OP_TableLock instruction */
danc548b782010-06-19 19:06:33 +0000292#ifdef SQLITE_OMIT_SHARED_CACHE
drh2ce18652016-01-16 20:50:21 +0000293 aOp[0].opcode = OP_Noop;
danc548b782010-06-19 19:06:33 +0000294#else
drh2ce18652016-01-16 20:50:21 +0000295 aOp[0].p1 = iDb;
296 aOp[0].p2 = pTab->tnum;
drh36cae852017-01-21 14:11:28 +0000297 aOp[0].p3 = wrFlag;
drh55965612017-09-16 20:58:41 +0000298 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
drh2ce18652016-01-16 20:50:21 +0000299 }
300 if( db->mallocFailed==0 ){
danc548b782010-06-19 19:06:33 +0000301#endif
danielk197796d48e92009-06-29 06:00:37 +0000302
drh2ce18652016-01-16 20:50:21 +0000303 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
304 ** parameter of the other to pTab->tnum. */
drh36cae852017-01-21 14:11:28 +0000305 if( wrFlag ) aOp[1].opcode = OP_OpenWrite;
drhe617bc82016-01-18 00:46:11 +0000306 aOp[1].p2 = pTab->tnum;
307 aOp[1].p3 = iDb;
danielk1977b4e9af92007-05-01 17:49:49 +0000308
drh2ce18652016-01-16 20:50:21 +0000309 /* Configure the number of columns. Configure the cursor to
310 ** think that the table has one more column than it really
311 ** does. An OP_Column to retrieve this imaginary column will
312 ** always return an SQL NULL. This is useful because it means
313 ** we can invoke OP_Column to fill in the vdbe cursors type
314 ** and offset cache without causing any IO.
315 */
drhe617bc82016-01-18 00:46:11 +0000316 aOp[1].p4type = P4_INT32;
317 aOp[1].p4.i = pTab->nCol+1;
drh4df65fc2017-01-20 00:40:26 +0000318 aOp[3].p2 = pTab->nCol;
drh2ce18652016-01-16 20:50:21 +0000319
drh6903bf62017-08-01 20:59:41 +0000320 sParse.nVar = 0;
321 sParse.nMem = 1;
322 sParse.nTab = 1;
323 sqlite3VdbeMakeReady(v, &sParse);
danielk197792d4d7a2007-05-04 12:05:56 +0000324 }
danielk1977b4e9af92007-05-01 17:49:49 +0000325 }
drhff0587c2007-08-29 17:43:19 +0000326
dan4e76cc32010-10-20 18:56:04 +0000327 pBlob->iCol = iCol;
drh605264d2007-08-21 15:13:19 +0000328 pBlob->db = db;
dan4e76cc32010-10-20 18:56:04 +0000329 sqlite3BtreeLeaveAll(db);
dan4e76cc32010-10-20 18:56:04 +0000330 if( db->mallocFailed ){
331 goto blob_open_out;
332 }
dan4e76cc32010-10-20 18:56:04 +0000333 rc = blobSeekToRow(pBlob, iRow, &zErr);
drhc692df22022-01-24 15:34:55 +0000334 if( (++nAttempt)>=SQLITE_MAX_SCHEMA_RETRY || rc!=SQLITE_SCHEMA ) break;
335 sqlite3ParseObjectReset(&sParse);
336 }
danielk1977b4e9af92007-05-01 17:49:49 +0000337
338blob_open_out:
dan4e76cc32010-10-20 18:56:04 +0000339 if( rc==SQLITE_OK && db->mallocFailed==0 ){
340 *ppBlob = (sqlite3_blob *)pBlob;
341 }else{
dan4e76cc32010-10-20 18:56:04 +0000342 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
343 sqlite3DbFree(db, pBlob);
danielk1977b4e9af92007-05-01 17:49:49 +0000344 }
drh13f40da2014-08-22 18:00:11 +0000345 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
drh6ee700c2009-06-01 19:53:30 +0000346 sqlite3DbFree(db, zErr);
drhc692df22022-01-24 15:34:55 +0000347 sqlite3ParseObjectReset(&sParse);
drh605264d2007-08-21 15:13:19 +0000348 rc = sqlite3ApiExit(db, rc);
349 sqlite3_mutex_leave(db->mutex);
350 return rc;
danielk1977b4e9af92007-05-01 17:49:49 +0000351}
352
353/*
drh16a9b832007-05-05 18:39:25 +0000354** Close a blob handle that was previously created using
355** sqlite3_blob_open().
danielk1977b4e9af92007-05-01 17:49:49 +0000356*/
357int sqlite3_blob_close(sqlite3_blob *pBlob){
358 Incrblob *p = (Incrblob *)pBlob;
drh605264d2007-08-21 15:13:19 +0000359 int rc;
drhd9da78a2009-03-24 15:08:09 +0000360 sqlite3 *db;
drh605264d2007-08-21 15:13:19 +0000361
drh28414ee2009-05-09 15:17:47 +0000362 if( p ){
dane4648022019-07-15 13:58:28 +0000363 sqlite3_stmt *pStmt = p->pStmt;
drh28414ee2009-05-09 15:17:47 +0000364 db = p->db;
365 sqlite3_mutex_enter(db->mutex);
drh28414ee2009-05-09 15:17:47 +0000366 sqlite3DbFree(db, p);
367 sqlite3_mutex_leave(db->mutex);
dane4648022019-07-15 13:58:28 +0000368 rc = sqlite3_finalize(pStmt);
drh28414ee2009-05-09 15:17:47 +0000369 }else{
370 rc = SQLITE_OK;
371 }
drh605264d2007-08-21 15:13:19 +0000372 return rc;
danielk1977b4e9af92007-05-01 17:49:49 +0000373}
374
drh605264d2007-08-21 15:13:19 +0000375/*
376** Perform a read or write operation on a blob
377*/
drhee858132007-05-08 20:37:38 +0000378static int blobReadWrite(
danielk1977dcbb5d32007-05-04 18:36:44 +0000379 sqlite3_blob *pBlob,
380 void *z,
381 int n,
382 int iOffset,
383 int (*xCall)(BtCursor*, u32, u32, void*)
384){
385 int rc;
386 Incrblob *p = (Incrblob *)pBlob;
drh605264d2007-08-21 15:13:19 +0000387 Vdbe *v;
drh28414ee2009-05-09 15:17:47 +0000388 sqlite3 *db;
danielk1977dcbb5d32007-05-04 18:36:44 +0000389
drh413c3d32010-02-23 20:11:56 +0000390 if( p==0 ) return SQLITE_MISUSE_BKPT;
drh28414ee2009-05-09 15:17:47 +0000391 db = p->db;
drh605264d2007-08-21 15:13:19 +0000392 sqlite3_mutex_enter(db->mutex);
drh605264d2007-08-21 15:13:19 +0000393 v = (Vdbe*)p->pStmt;
danielk1977a8b30182008-10-02 14:49:01 +0000394
drhd10d18d2015-02-07 15:16:35 +0000395 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
danielk1977a8b30182008-10-02 14:49:01 +0000396 /* Request is out of range. Return a transient error. */
397 rc = SQLITE_ERROR;
dan4e76cc32010-10-20 18:56:04 +0000398 }else if( v==0 ){
danielk1977a8b30182008-10-02 14:49:01 +0000399 /* If there is no statement handle, then the blob-handle has
400 ** already been invalidated. Return SQLITE_ABORT in this case.
401 */
drh605264d2007-08-21 15:13:19 +0000402 rc = SQLITE_ABORT;
danielk1977dcbb5d32007-05-04 18:36:44 +0000403 }else{
drh605264d2007-08-21 15:13:19 +0000404 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
405 ** returned, clean-up the statement handle.
406 */
407 assert( db == v->db );
drhff0587c2007-08-29 17:43:19 +0000408 sqlite3BtreeEnterCursor(p->pCsr);
dane437ca52011-07-11 19:45:38 +0000409
410#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
drh7f197bb2011-08-03 21:32:11 +0000411 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
dane437ca52011-07-11 19:45:38 +0000412 /* If a pre-update hook is registered and this is a write cursor,
413 ** invoke it here.
414 **
415 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
416 ** operation should really be an SQLITE_UPDATE. This is probably
417 ** incorrect, but is convenient because at this point the new.* values
418 ** are not easily obtainable. And for the sessions module, an
419 ** SQLITE_UPDATE where the PK columns do not change is handled in the
420 ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually
421 ** slightly more efficient). Since you cannot write to a PK column
422 ** using the incremental-blob API, this works. For the sessions module
423 ** anyhow.
424 */
425 sqlite3_int64 iKey;
drha7c90c42016-06-04 20:37:10 +0000426 iKey = sqlite3BtreeIntegerKey(p->pCsr);
drh3ab4ffc2021-11-11 11:23:08 +0000427 assert( v->apCsr[0]!=0 );
428 assert( v->apCsr[0]->eCurType==CURTYPE_BTREE );
dane437ca52011-07-11 19:45:38 +0000429 sqlite3VdbePreUpdateHook(
dana23a8732021-04-21 20:52:17 +0000430 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1, p->iCol
dane437ca52011-07-11 19:45:38 +0000431 );
432 }
433#endif
434
drh605264d2007-08-21 15:13:19 +0000435 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
drhff0587c2007-08-29 17:43:19 +0000436 sqlite3BtreeLeaveCursor(p->pCsr);
drh605264d2007-08-21 15:13:19 +0000437 if( rc==SQLITE_ABORT ){
438 sqlite3VdbeFinalize(v);
439 p->pStmt = 0;
440 }else{
drh605264d2007-08-21 15:13:19 +0000441 v->rc = rc;
442 }
danielk1977dcbb5d32007-05-04 18:36:44 +0000443 }
dan923c4b32014-11-10 17:53:03 +0000444 sqlite3Error(db, rc);
drh605264d2007-08-21 15:13:19 +0000445 rc = sqlite3ApiExit(db, rc);
446 sqlite3_mutex_leave(db->mutex);
447 return rc;
danielk1977dcbb5d32007-05-04 18:36:44 +0000448}
449
danielk1977b4e9af92007-05-01 17:49:49 +0000450/*
451** Read data from a blob handle.
452*/
453int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
drhcb3cabd2016-11-25 19:18:28 +0000454 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreePayloadChecked);
danielk1977b4e9af92007-05-01 17:49:49 +0000455}
456
457/*
458** Write data to a blob handle.
459*/
460int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
danielk1977dcbb5d32007-05-04 18:36:44 +0000461 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
danielk1977b4e9af92007-05-01 17:49:49 +0000462}
463
464/*
465** Query a blob handle for the size of the data.
drh605264d2007-08-21 15:13:19 +0000466**
467** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
468** so no mutex is required for access.
danielk1977b4e9af92007-05-01 17:49:49 +0000469*/
470int sqlite3_blob_bytes(sqlite3_blob *pBlob){
471 Incrblob *p = (Incrblob *)pBlob;
daneefab752010-12-06 17:11:05 +0000472 return (p && p->pStmt) ? p->nByte : 0;
danielk1977b4e9af92007-05-01 17:49:49 +0000473}
474
dan4e76cc32010-10-20 18:56:04 +0000475/*
476** Move an existing blob handle to point to a different row of the same
477** database table.
478**
479** If an error occurs, or if the specified row does not exist or does not
480** contain a blob or text value, then an error code is returned and the
481** database handle error code and message set. If this happens, then all
482** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
483** immediately return SQLITE_ABORT.
484*/
485int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
486 int rc;
487 Incrblob *p = (Incrblob *)pBlob;
488 sqlite3 *db;
489
490 if( p==0 ) return SQLITE_MISUSE_BKPT;
491 db = p->db;
492 sqlite3_mutex_enter(db->mutex);
493
494 if( p->pStmt==0 ){
495 /* If there is no statement handle, then the blob-handle has
496 ** already been invalidated. Return SQLITE_ABORT in this case.
497 */
498 rc = SQLITE_ABORT;
499 }else{
500 char *zErr;
dan385b9822021-04-14 15:25:10 +0000501 ((Vdbe*)p->pStmt)->rc = SQLITE_OK;
dan4e76cc32010-10-20 18:56:04 +0000502 rc = blobSeekToRow(p, iRow, &zErr);
503 if( rc!=SQLITE_OK ){
drh13f40da2014-08-22 18:00:11 +0000504 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
dan4e76cc32010-10-20 18:56:04 +0000505 sqlite3DbFree(db, zErr);
506 }
507 assert( rc!=SQLITE_SCHEMA );
508 }
509
510 rc = sqlite3ApiExit(db, rc);
daneefab752010-12-06 17:11:05 +0000511 assert( rc==SQLITE_OK || p->pStmt==0 );
dan4e76cc32010-10-20 18:56:04 +0000512 sqlite3_mutex_leave(db->mutex);
513 return rc;
514}
515
danielk1977b4e9af92007-05-01 17:49:49 +0000516#endif /* #ifndef SQLITE_OMIT_INCRBLOB */