danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 1 | /* |
| 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 | ** |
drh | 16a9b83 | 2007-05-05 18:39:25 | [diff] [blame] | 13 | ** This file contains code used to implement incremental BLOB I/O. |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 14 | */ |
| 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 | */ |
| 24 | typedef struct Incrblob Incrblob; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 25 | struct Incrblob { |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 26 | int nByte; /* Size of open blob, in bytes */ |
| 27 | int iOffset; /* Byte offset of blob in cursor data */ |
drh | 36cae85 | 2017-01-21 14:11:28 | [diff] [blame] | 28 | u16 iCol; /* Table column this handle is open on */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 29 | BtCursor *pCsr; /* Cursor pointing at blob row */ |
| 30 | sqlite3_stmt *pStmt; /* Statement holding cursor open */ |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 31 | sqlite3 *db; /* The associated database */ |
dan | e437ca5 | 2011-07-11 19:45:38 | [diff] [blame] | 32 | char *zDb; /* Database name */ |
| 33 | Table *pTab; /* Table object */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 34 | }; |
| 35 | |
dan | 4e76cc3 | 2010-10-20 18:56:04 | [diff] [blame] | 36 | |
dan | e3d82a8 | 2010-10-26 11:56:57 | [diff] [blame] | 37 | /* |
| 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 | */ |
dan | 4e76cc3 | 2010-10-20 18:56:04 | [diff] [blame] | 54 | static 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 | |
drh | 4df65fc | 2017-01-20 00:40:26 | [diff] [blame] | 59 | /* Set the value of register r[1] in the SQL statement to integer iRow. |
| 60 | ** This is done directly as a performance optimization |
dan | e3d82a8 | 2010-10-26 11:56:57 | [diff] [blame] | 61 | */ |
drh | 43e232d | 2023-10-07 08:00:54 | [diff] [blame] | 62 | sqlite3VdbeMemSetInt64(&v->aMem[1], iRow); |
drh | 4df65fc | 2017-01-20 00:40:26 | [diff] [blame] | 63 | |
| 64 | /* If the statement has been run before (and is paused at the OP_ResultRow) |
dan | 952523f | 2017-10-24 17:28:25 | [diff] [blame] | 65 | ** then back it up to the point where it does the OP_NotExists. This could |
drh | 4df65fc | 2017-01-20 00:40:26 | [diff] [blame] | 66 | ** have been down with an extra OP_Goto, but simply setting the program |
| 67 | ** counter is faster. */ |
dan | 952523f | 2017-10-24 17:28:25 | [diff] [blame] | 68 | if( v->pc>4 ){ |
| 69 | v->pc = 4; |
| 70 | assert( v->aOp[v->pc].opcode==OP_NotExists ); |
drh | baf5dec | 2017-01-31 19:02:15 | [diff] [blame] | 71 | rc = sqlite3VdbeExec(v); |
drh | 3b2936f | 2017-01-21 16:27:56 | [diff] [blame] | 72 | }else{ |
| 73 | rc = sqlite3_step(p->pStmt); |
| 74 | } |
dan | 4e76cc3 | 2010-10-20 18:56:04 | [diff] [blame] | 75 | if( rc==SQLITE_ROW ){ |
drh | 14da87f | 2013-11-20 21:51:33 | [diff] [blame] | 76 | VdbeCursor *pC = v->apCsr[0]; |
drh | 3ab4ffc | 2021-11-11 11:23:08 | [diff] [blame] | 77 | u32 type; |
| 78 | assert( pC!=0 ); |
| 79 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 80 | type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0; |
drh | 210b0d0 | 2017-01-25 04:41:34 | [diff] [blame] | 81 | testcase( pC->nHdrParsed==p->iCol ); |
| 82 | testcase( pC->nHdrParsed==p->iCol+1 ); |
dan | 4e76cc3 | 2010-10-20 18:56:04 | [diff] [blame] | 83 | if( type<12 ){ |
| 84 | zErr = sqlite3MPrintf(p->db, "cannot open value of type %s", |
| 85 | type==0?"null": type==7?"real": "integer" |
| 86 | ); |
| 87 | rc = SQLITE_ERROR; |
| 88 | sqlite3_finalize(p->pStmt); |
| 89 | p->pStmt = 0; |
| 90 | }else{ |
drh | 14da87f | 2013-11-20 21:51:33 | [diff] [blame] | 91 | p->iOffset = pC->aType[p->iCol + pC->nField]; |
dan | 4e76cc3 | 2010-10-20 18:56:04 | [diff] [blame] | 92 | p->nByte = sqlite3VdbeSerialTypeLen(type); |
drh | c960dcb | 2015-11-20 19:22:01 | [diff] [blame] | 93 | p->pCsr = pC->uc.pCursor; |
dan | 5a500af | 2014-03-11 20:33:04 | [diff] [blame] | 94 | sqlite3BtreeIncrblobCursor(p->pCsr); |
dan | 4e76cc3 | 2010-10-20 18:56:04 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | if( rc==SQLITE_ROW ){ |
| 99 | rc = SQLITE_OK; |
| 100 | }else if( p->pStmt ){ |
| 101 | rc = sqlite3_finalize(p->pStmt); |
| 102 | p->pStmt = 0; |
| 103 | if( rc==SQLITE_OK ){ |
| 104 | zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow); |
| 105 | rc = SQLITE_ERROR; |
| 106 | }else{ |
| 107 | zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db)); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | assert( rc!=SQLITE_OK || zErr==0 ); |
| 112 | assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE ); |
| 113 | |
| 114 | *pzErr = zErr; |
| 115 | return rc; |
| 116 | } |
| 117 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 118 | /* |
| 119 | ** Open a blob handle. |
| 120 | */ |
| 121 | int sqlite3_blob_open( |
drh | 16a9b83 | 2007-05-05 18:39:25 | [diff] [blame] | 122 | sqlite3* db, /* The database connection */ |
| 123 | const char *zDb, /* The attached database containing the blob */ |
| 124 | const char *zTable, /* The table containing the blob */ |
| 125 | const char *zColumn, /* The column containing the blob */ |
| 126 | sqlite_int64 iRow, /* The row containing the glob */ |
drh | 36cae85 | 2017-01-21 14:11:28 | [diff] [blame] | 127 | int wrFlag, /* True -> read/write access, false -> read-only */ |
drh | 16a9b83 | 2007-05-05 18:39:25 | [diff] [blame] | 128 | sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 129 | ){ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 130 | int nAttempt = 0; |
| 131 | int iCol; /* Index of zColumn in row-record */ |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 | [diff] [blame] | 132 | int rc = SQLITE_OK; |
drh | 6ee700c | 2009-06-01 19:53:30 | [diff] [blame] | 133 | char *zErr = 0; |
| 134 | Table *pTab; |
dan | 61c7f59 | 2010-10-26 18:42:52 | [diff] [blame] | 135 | Incrblob *pBlob = 0; |
drh | 642479d | 2025-03-10 22:31:55 | [diff] [blame] | 136 | int iDb; |
drh | 6903bf6 | 2017-08-01 20:59:41 | [diff] [blame] | 137 | Parse sParse; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 138 | |
drh | 9ca9573 | 2014-10-24 00:35:58 | [diff] [blame] | 139 | #ifdef SQLITE_ENABLE_API_ARMOR |
drh | d10d18d | 2015-02-07 15:16:35 | [diff] [blame] | 140 | if( ppBlob==0 ){ |
| 141 | return SQLITE_MISUSE_BKPT; |
| 142 | } |
| 143 | #endif |
| 144 | *ppBlob = 0; |
| 145 | #ifdef SQLITE_ENABLE_API_ARMOR |
stephan | a17f632 | 2023-10-14 13:24:30 | [diff] [blame] | 146 | if( !sqlite3SafetyCheckOk(db) || zTable==0 || zColumn==0 ){ |
drh | 9ca9573 | 2014-10-24 00:35:58 | [diff] [blame] | 147 | return SQLITE_MISUSE_BKPT; |
| 148 | } |
| 149 | #endif |
drh | 36cae85 | 2017-01-21 14:11:28 | [diff] [blame] | 150 | wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */ |
dan | 4e76cc3 | 2010-10-20 18:56:04 | [diff] [blame] | 151 | |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 152 | sqlite3_mutex_enter(db->mutex); |
dan | 4e76cc3 | 2010-10-20 18:56:04 | [diff] [blame] | 153 | |
| 154 | pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob)); |
drh | c692df2 | 2022-01-24 15:34:55 | [diff] [blame] | 155 | while(1){ |
| 156 | sqlite3ParseObjectInit(&sParse,db); |
drh | 6903bf6 | 2017-08-01 20:59:41 | [diff] [blame] | 157 | if( !pBlob ) goto blob_open_out; |
dan | 61c7f59 | 2010-10-26 18:42:52 | [diff] [blame] | 158 | sqlite3DbFree(db, zErr); |
| 159 | zErr = 0; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 160 | |
drh | ff0587c | 2007-08-29 17:43:19 | [diff] [blame] | 161 | sqlite3BtreeEnterAll(db); |
drh | 6903bf6 | 2017-08-01 20:59:41 | [diff] [blame] | 162 | pTab = sqlite3LocateTable(&sParse, 0, zTable, zDb); |
danielk1977 | 36961ed | 2008-04-24 09:49:55 | [diff] [blame] | 163 | if( pTab && IsVirtual(pTab) ){ |
| 164 | pTab = 0; |
drh | 6903bf6 | 2017-08-01 20:59:41 | [diff] [blame] | 165 | sqlite3ErrorMsg(&sParse, "cannot open virtual table: %s", zTable); |
danielk1977 | 36961ed | 2008-04-24 09:49:55 | [diff] [blame] | 166 | } |
drh | 63f0eed | 2013-11-02 22:09:48 | [diff] [blame] | 167 | if( pTab && !HasRowid(pTab) ){ |
| 168 | pTab = 0; |
drh | 6903bf6 | 2017-08-01 20:59:41 | [diff] [blame] | 169 | sqlite3ErrorMsg(&sParse, "cannot open table without rowid: %s", zTable); |
drh | 63f0eed | 2013-11-02 22:09:48 | [diff] [blame] | 170 | } |
drh | c1547d1 | 2024-08-28 19:13:16 | [diff] [blame] | 171 | if( pTab && (pTab->tabFlags&TF_HasGenerated)!=0 ){ |
| 172 | pTab = 0; |
| 173 | sqlite3ErrorMsg(&sParse, "cannot open table with generated columns: %s", |
| 174 | zTable); |
| 175 | } |
danielk1977 | 36961ed | 2008-04-24 09:49:55 | [diff] [blame] | 176 | #ifndef SQLITE_OMIT_VIEW |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 177 | if( pTab && IsView(pTab) ){ |
danielk1977 | 36961ed | 2008-04-24 09:49:55 | [diff] [blame] | 178 | pTab = 0; |
drh | 6903bf6 | 2017-08-01 20:59:41 | [diff] [blame] | 179 | sqlite3ErrorMsg(&sParse, "cannot open view: %s", zTable); |
danielk1977 | 36961ed | 2008-04-24 09:49:55 | [diff] [blame] | 180 | } |
| 181 | #endif |
drh | 642479d | 2025-03-10 22:31:55 | [diff] [blame] | 182 | if( pTab==0 |
| 183 | || ((iDb = sqlite3SchemaToIndex(db, pTab->pSchema))==1 && |
| 184 | sqlite3OpenTempDatabase(&sParse)) |
| 185 | ){ |
drh | 6903bf6 | 2017-08-01 20:59:41 | [diff] [blame] | 186 | if( sParse.zErrMsg ){ |
drh | 6ee700c | 2009-06-01 19:53:30 | [diff] [blame] | 187 | sqlite3DbFree(db, zErr); |
drh | 6903bf6 | 2017-08-01 20:59:41 | [diff] [blame] | 188 | zErr = sParse.zErrMsg; |
| 189 | sParse.zErrMsg = 0; |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 | [diff] [blame] | 190 | } |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 | [diff] [blame] | 191 | rc = SQLITE_ERROR; |
drh | ff0587c | 2007-08-29 17:43:19 | [diff] [blame] | 192 | sqlite3BtreeLeaveAll(db); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 193 | goto blob_open_out; |
| 194 | } |
dan | e437ca5 | 2011-07-11 19:45:38 | [diff] [blame] | 195 | pBlob->pTab = pTab; |
drh | 642479d | 2025-03-10 22:31:55 | [diff] [blame] | 196 | pBlob->zDb = db->aDb[iDb].zDbSName; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 197 | |
| 198 | /* Now search pTab for the exact column. */ |
drh | 9d90a3a | 2025-02-08 14:15:42 | [diff] [blame] | 199 | iCol = sqlite3ColumnIndex(pTab, zColumn); |
| 200 | if( iCol<0 ){ |
drh | 6ee700c | 2009-06-01 19:53:30 | [diff] [blame] | 201 | sqlite3DbFree(db, zErr); |
| 202 | zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 203 | rc = SQLITE_ERROR; |
drh | ff0587c | 2007-08-29 17:43:19 | [diff] [blame] | 204 | sqlite3BtreeLeaveAll(db); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 205 | goto blob_open_out; |
| 206 | } |
| 207 | |
danielk1977 | f181924 | 2007-05-03 18:14:10 | [diff] [blame] | 208 | /* If the value is being opened for writing, check that the |
dan | 1da40a3 | 2009-09-19 17:00:31 | [diff] [blame] | 209 | ** column is not indexed, and that it is not part of a foreign key. |
drh | 36cae85 | 2017-01-21 14:11:28 | [diff] [blame] | 210 | */ |
| 211 | if( wrFlag ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 | [diff] [blame] | 212 | const char *zFault = 0; |
danielk1977 | f181924 | 2007-05-03 18:14:10 | [diff] [blame] | 213 | Index *pIdx; |
dan | 1da40a3 | 2009-09-19 17:00:31 | [diff] [blame] | 214 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 215 | if( db->flags&SQLITE_ForeignKeys ){ |
dan | 1316700 | 2009-10-02 06:35:06 | [diff] [blame] | 216 | /* Check that the column is not part of an FK child key definition. It |
| 217 | ** is not necessary to check if it is part of a parent key, as parent |
| 218 | ** key columns must be indexed. The check below will pick up this |
| 219 | ** case. */ |
dan | 1da40a3 | 2009-09-19 17:00:31 | [diff] [blame] | 220 | FKey *pFKey; |
drh | 78b2fa8 | 2021-10-07 12:11:20 | [diff] [blame] | 221 | assert( IsOrdinaryTable(pTab) ); |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 222 | for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){ |
dan | 1da40a3 | 2009-09-19 17:00:31 | [diff] [blame] | 223 | int j; |
| 224 | for(j=0; j<pFKey->nCol; j++){ |
| 225 | if( pFKey->aCol[j].iFrom==iCol ){ |
| 226 | zFault = "foreign key"; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | #endif |
danielk1977 | f181924 | 2007-05-03 18:14:10 | [diff] [blame] | 232 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 233 | int j; |
drh | bbbdc83 | 2013-10-22 18:01:40 | [diff] [blame] | 234 | for(j=0; j<pIdx->nKeyCol; j++){ |
drh | 1f9ca2c | 2015-08-25 16:57:52 | [diff] [blame] | 235 | /* FIXME: Be smarter about indexes that use expressions */ |
drh | 4b92f98 | 2015-09-29 17:20:14 | [diff] [blame] | 236 | if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 | [diff] [blame] | 237 | zFault = "indexed"; |
danielk1977 | f181924 | 2007-05-03 18:14:10 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 | [diff] [blame] | 241 | if( zFault ){ |
| 242 | sqlite3DbFree(db, zErr); |
| 243 | zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault); |
| 244 | rc = SQLITE_ERROR; |
dan | 1da40a3 | 2009-09-19 17:00:31 | [diff] [blame] | 245 | sqlite3BtreeLeaveAll(db); |
| 246 | goto blob_open_out; |
| 247 | } |
danielk1977 | f181924 | 2007-05-03 18:14:10 | [diff] [blame] | 248 | } |
| 249 | |
drh | 6903bf6 | 2017-08-01 20:59:41 | [diff] [blame] | 250 | pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse); |
dan | 61c7f59 | 2010-10-26 18:42:52 | [diff] [blame] | 251 | assert( pBlob->pStmt || db->mallocFailed ); |
| 252 | if( pBlob->pStmt ){ |
drh | 2ce1865 | 2016-01-16 20:50:21 | [diff] [blame] | 253 | |
| 254 | /* This VDBE program seeks a btree cursor to the identified |
| 255 | ** db/table/row entry. The reason for using a vdbe program instead |
| 256 | ** of writing code to use the b-tree layer directly is that the |
| 257 | ** vdbe program will take advantage of the various transaction, |
| 258 | ** locking and error handling infrastructure built into the vdbe. |
| 259 | ** |
| 260 | ** After seeking the cursor, the vdbe executes an OP_ResultRow. |
| 261 | ** Code external to the Vdbe then "borrows" the b-tree cursor and |
| 262 | ** uses it to implement the blob_read(), blob_write() and |
| 263 | ** blob_bytes() functions. |
| 264 | ** |
| 265 | ** The sqlite3_blob_close() function finalizes the vdbe program, |
| 266 | ** which closes the b-tree cursor and (possibly) commits the |
| 267 | ** transaction. |
| 268 | */ |
drh | 1b32554 | 2016-02-03 01:55:44 | [diff] [blame] | 269 | static const int iLn = VDBE_OFFSET_LINENO(2); |
drh | 2ce1865 | 2016-01-16 20:50:21 | [diff] [blame] | 270 | static const VdbeOpList openBlob[] = { |
drh | 1b32554 | 2016-02-03 01:55:44 | [diff] [blame] | 271 | {OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */ |
| 272 | {OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */ |
drh | 4df65fc | 2017-01-20 00:40:26 | [diff] [blame] | 273 | /* blobSeekToRow() will initialize r[1] to the desired rowid */ |
| 274 | {OP_NotExists, 0, 5, 1}, /* 2: Seek the cursor to rowid=r[1] */ |
| 275 | {OP_Column, 0, 0, 1}, /* 3 */ |
| 276 | {OP_ResultRow, 1, 0, 0}, /* 4 */ |
| 277 | {OP_Halt, 0, 0, 0}, /* 5 */ |
drh | 2ce1865 | 2016-01-16 20:50:21 | [diff] [blame] | 278 | }; |
dan | 61c7f59 | 2010-10-26 18:42:52 | [diff] [blame] | 279 | Vdbe *v = (Vdbe *)pBlob->pStmt; |
drh | 2ce1865 | 2016-01-16 20:50:21 | [diff] [blame] | 280 | VdbeOp *aOp; |
drh | b22f7c8 | 2014-02-06 23:56:27 | [diff] [blame] | 281 | |
drh | 36cae85 | 2017-01-21 14:11:28 | [diff] [blame] | 282 | sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag, |
drh | b22f7c8 | 2014-02-06 23:56:27 | [diff] [blame] | 283 | pTab->pSchema->schema_cookie, |
| 284 | pTab->pSchema->iGeneration); |
drh | 5596561 | 2017-09-16 20:58:41 | [diff] [blame] | 285 | sqlite3VdbeChangeP5(v, 1); |
| 286 | assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed ); |
drh | 2ce1865 | 2016-01-16 20:50:21 | [diff] [blame] | 287 | aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 288 | |
drh | ff0587c | 2007-08-29 17:43:19 | [diff] [blame] | 289 | /* Make sure a mutex is held on the table to be accessed */ |
drh | fb98264 | 2007-08-30 01:19:59 | [diff] [blame] | 290 | sqlite3VdbeUsesBtree(v, iDb); |
drh | ff0587c | 2007-08-29 17:43:19 | [diff] [blame] | 291 | |
drh | 2ce1865 | 2016-01-16 20:50:21 | [diff] [blame] | 292 | if( db->mallocFailed==0 ){ |
| 293 | assert( aOp!=0 ); |
| 294 | /* Configure the OP_TableLock instruction */ |
dan | c548b78 | 2010-06-19 19:06:33 | [diff] [blame] | 295 | #ifdef SQLITE_OMIT_SHARED_CACHE |
drh | 2ce1865 | 2016-01-16 20:50:21 | [diff] [blame] | 296 | aOp[0].opcode = OP_Noop; |
dan | c548b78 | 2010-06-19 19:06:33 | [diff] [blame] | 297 | #else |
drh | 2ce1865 | 2016-01-16 20:50:21 | [diff] [blame] | 298 | aOp[0].p1 = iDb; |
| 299 | aOp[0].p2 = pTab->tnum; |
drh | 36cae85 | 2017-01-21 14:11:28 | [diff] [blame] | 300 | aOp[0].p3 = wrFlag; |
drh | 5596561 | 2017-09-16 20:58:41 | [diff] [blame] | 301 | sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT); |
drh | 2ce1865 | 2016-01-16 20:50:21 | [diff] [blame] | 302 | } |
| 303 | if( db->mallocFailed==0 ){ |
dan | c548b78 | 2010-06-19 19:06:33 | [diff] [blame] | 304 | #endif |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 | [diff] [blame] | 305 | |
drh | 2ce1865 | 2016-01-16 20:50:21 | [diff] [blame] | 306 | /* Remove either the OP_OpenWrite or OpenRead. Set the P2 |
| 307 | ** parameter of the other to pTab->tnum. */ |
drh | 36cae85 | 2017-01-21 14:11:28 | [diff] [blame] | 308 | if( wrFlag ) aOp[1].opcode = OP_OpenWrite; |
drh | e617bc8 | 2016-01-18 00:46:11 | [diff] [blame] | 309 | aOp[1].p2 = pTab->tnum; |
| 310 | aOp[1].p3 = iDb; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 311 | |
drh | 2ce1865 | 2016-01-16 20:50:21 | [diff] [blame] | 312 | /* Configure the number of columns. Configure the cursor to |
| 313 | ** think that the table has one more column than it really |
| 314 | ** does. An OP_Column to retrieve this imaginary column will |
| 315 | ** always return an SQL NULL. This is useful because it means |
| 316 | ** we can invoke OP_Column to fill in the vdbe cursors type |
| 317 | ** and offset cache without causing any IO. |
| 318 | */ |
drh | e617bc8 | 2016-01-18 00:46:11 | [diff] [blame] | 319 | aOp[1].p4type = P4_INT32; |
| 320 | aOp[1].p4.i = pTab->nCol+1; |
drh | 4df65fc | 2017-01-20 00:40:26 | [diff] [blame] | 321 | aOp[3].p2 = pTab->nCol; |
drh | 2ce1865 | 2016-01-16 20:50:21 | [diff] [blame] | 322 | |
drh | 6903bf6 | 2017-08-01 20:59:41 | [diff] [blame] | 323 | sParse.nVar = 0; |
| 324 | sParse.nMem = 1; |
| 325 | sParse.nTab = 1; |
| 326 | sqlite3VdbeMakeReady(v, &sParse); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 | [diff] [blame] | 327 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 328 | } |
drh | ff0587c | 2007-08-29 17:43:19 | [diff] [blame] | 329 | |
dan | 4e76cc3 | 2010-10-20 18:56:04 | [diff] [blame] | 330 | pBlob->iCol = iCol; |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 331 | pBlob->db = db; |
dan | 4e76cc3 | 2010-10-20 18:56:04 | [diff] [blame] | 332 | sqlite3BtreeLeaveAll(db); |
dan | 4e76cc3 | 2010-10-20 18:56:04 | [diff] [blame] | 333 | if( db->mallocFailed ){ |
| 334 | goto blob_open_out; |
| 335 | } |
dan | 4e76cc3 | 2010-10-20 18:56:04 | [diff] [blame] | 336 | rc = blobSeekToRow(pBlob, iRow, &zErr); |
drh | c692df2 | 2022-01-24 15:34:55 | [diff] [blame] | 337 | if( (++nAttempt)>=SQLITE_MAX_SCHEMA_RETRY || rc!=SQLITE_SCHEMA ) break; |
| 338 | sqlite3ParseObjectReset(&sParse); |
| 339 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 340 | |
| 341 | blob_open_out: |
dan | 4e76cc3 | 2010-10-20 18:56:04 | [diff] [blame] | 342 | if( rc==SQLITE_OK && db->mallocFailed==0 ){ |
| 343 | *ppBlob = (sqlite3_blob *)pBlob; |
| 344 | }else{ |
dan | 4e76cc3 | 2010-10-20 18:56:04 | [diff] [blame] | 345 | if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt); |
| 346 | sqlite3DbFree(db, pBlob); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 347 | } |
drh | 0051a56 | 2023-05-10 13:56:32 | [diff] [blame] | 348 | sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : (char*)0), zErr); |
drh | 6ee700c | 2009-06-01 19:53:30 | [diff] [blame] | 349 | sqlite3DbFree(db, zErr); |
drh | c692df2 | 2022-01-24 15:34:55 | [diff] [blame] | 350 | sqlite3ParseObjectReset(&sParse); |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 351 | rc = sqlite3ApiExit(db, rc); |
| 352 | sqlite3_mutex_leave(db->mutex); |
| 353 | return rc; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* |
drh | 16a9b83 | 2007-05-05 18:39:25 | [diff] [blame] | 357 | ** Close a blob handle that was previously created using |
| 358 | ** sqlite3_blob_open(). |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 359 | */ |
| 360 | int sqlite3_blob_close(sqlite3_blob *pBlob){ |
| 361 | Incrblob *p = (Incrblob *)pBlob; |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 362 | int rc; |
drh | d9da78a | 2009-03-24 15:08:09 | [diff] [blame] | 363 | sqlite3 *db; |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 364 | |
drh | 28414ee | 2009-05-09 15:17:47 | [diff] [blame] | 365 | if( p ){ |
dan | e464802 | 2019-07-15 13:58:28 | [diff] [blame] | 366 | sqlite3_stmt *pStmt = p->pStmt; |
drh | 28414ee | 2009-05-09 15:17:47 | [diff] [blame] | 367 | db = p->db; |
| 368 | sqlite3_mutex_enter(db->mutex); |
drh | 28414ee | 2009-05-09 15:17:47 | [diff] [blame] | 369 | sqlite3DbFree(db, p); |
| 370 | sqlite3_mutex_leave(db->mutex); |
dan | e464802 | 2019-07-15 13:58:28 | [diff] [blame] | 371 | rc = sqlite3_finalize(pStmt); |
drh | 28414ee | 2009-05-09 15:17:47 | [diff] [blame] | 372 | }else{ |
| 373 | rc = SQLITE_OK; |
| 374 | } |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 375 | return rc; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 376 | } |
| 377 | |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 378 | /* |
| 379 | ** Perform a read or write operation on a blob |
| 380 | */ |
drh | ee85813 | 2007-05-08 20:37:38 | [diff] [blame] | 381 | static int blobReadWrite( |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 | [diff] [blame] | 382 | sqlite3_blob *pBlob, |
| 383 | void *z, |
| 384 | int n, |
| 385 | int iOffset, |
| 386 | int (*xCall)(BtCursor*, u32, u32, void*) |
| 387 | ){ |
dan | 8504d37 | 2025-07-07 11:37:55 | [diff] [blame] | 388 | int rc = SQLITE_OK; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 | [diff] [blame] | 389 | Incrblob *p = (Incrblob *)pBlob; |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 390 | Vdbe *v; |
drh | 28414ee | 2009-05-09 15:17:47 | [diff] [blame] | 391 | sqlite3 *db; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 | [diff] [blame] | 392 | |
drh | 413c3d3 | 2010-02-23 20:11:56 | [diff] [blame] | 393 | if( p==0 ) return SQLITE_MISUSE_BKPT; |
drh | 28414ee | 2009-05-09 15:17:47 | [diff] [blame] | 394 | db = p->db; |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 395 | sqlite3_mutex_enter(db->mutex); |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 396 | v = (Vdbe*)p->pStmt; |
danielk1977 | a8b3018 | 2008-10-02 14:49:01 | [diff] [blame] | 397 | |
drh | d10d18d | 2015-02-07 15:16:35 | [diff] [blame] | 398 | if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){ |
danielk1977 | a8b3018 | 2008-10-02 14:49:01 | [diff] [blame] | 399 | /* Request is out of range. Return a transient error. */ |
| 400 | rc = SQLITE_ERROR; |
dan | 4e76cc3 | 2010-10-20 18:56:04 | [diff] [blame] | 401 | }else if( v==0 ){ |
danielk1977 | a8b3018 | 2008-10-02 14:49:01 | [diff] [blame] | 402 | /* If there is no statement handle, then the blob-handle has |
| 403 | ** already been invalidated. Return SQLITE_ABORT in this case. |
| 404 | */ |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 405 | rc = SQLITE_ABORT; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 | [diff] [blame] | 406 | }else{ |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 407 | /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is |
| 408 | ** returned, clean-up the statement handle. |
| 409 | */ |
| 410 | assert( db == v->db ); |
drh | ff0587c | 2007-08-29 17:43:19 | [diff] [blame] | 411 | sqlite3BtreeEnterCursor(p->pCsr); |
dan | e437ca5 | 2011-07-11 19:45:38 | [diff] [blame] | 412 | |
| 413 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
drh | 7f197bb | 2011-08-03 21:32:11 | [diff] [blame] | 414 | if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){ |
dan | e437ca5 | 2011-07-11 19:45:38 | [diff] [blame] | 415 | /* If a pre-update hook is registered and this is a write cursor, |
| 416 | ** invoke it here. |
| 417 | ** |
| 418 | ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this |
| 419 | ** operation should really be an SQLITE_UPDATE. This is probably |
| 420 | ** incorrect, but is convenient because at this point the new.* values |
| 421 | ** are not easily obtainable. And for the sessions module, an |
| 422 | ** SQLITE_UPDATE where the PK columns do not change is handled in the |
| 423 | ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually |
| 424 | ** slightly more efficient). Since you cannot write to a PK column |
| 425 | ** using the incremental-blob API, this works. For the sessions module |
| 426 | ** anyhow. |
| 427 | */ |
dan | 8504d37 | 2025-07-07 11:37:55 | [diff] [blame] | 428 | if( sqlite3BtreeCursorIsValidNN(p->pCsr)==0 ){ |
| 429 | /* If the cursor is not currently valid, try to reseek it. This |
| 430 | ** always either fails or finds the correct row - the cursor will |
| 431 | ** have been marked permanently CURSOR_INVALID if the open row has |
| 432 | ** been deleted. */ |
| 433 | int bDiff = 0; |
| 434 | rc = sqlite3BtreeCursorRestore(p->pCsr, &bDiff); |
| 435 | assert( bDiff==0 || sqlite3BtreeCursorIsValidNN(p->pCsr)==0 ); |
| 436 | } |
| 437 | if( sqlite3BtreeCursorIsValidNN(p->pCsr) ){ |
| 438 | sqlite3_int64 iKey; |
| 439 | iKey = sqlite3BtreeIntegerKey(p->pCsr); |
| 440 | assert( v->apCsr[0]!=0 ); |
| 441 | assert( v->apCsr[0]->eCurType==CURTYPE_BTREE ); |
| 442 | sqlite3VdbePreUpdateHook( |
| 443 | v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1, p->iCol |
| 444 | ); |
| 445 | } |
dan | e437ca5 | 2011-07-11 19:45:38 | [diff] [blame] | 446 | } |
dan | 8504d37 | 2025-07-07 11:37:55 | [diff] [blame] | 447 | if( rc==SQLITE_OK ){ |
| 448 | rc = xCall(p->pCsr, iOffset+p->iOffset, n, z); |
| 449 | } |
| 450 | #else |
| 451 | rc = xCall(p->pCsr, iOffset+p->iOffset, n, z); |
dan | e437ca5 | 2011-07-11 19:45:38 | [diff] [blame] | 452 | #endif |
| 453 | |
drh | ff0587c | 2007-08-29 17:43:19 | [diff] [blame] | 454 | sqlite3BtreeLeaveCursor(p->pCsr); |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 455 | if( rc==SQLITE_ABORT ){ |
| 456 | sqlite3VdbeFinalize(v); |
| 457 | p->pStmt = 0; |
| 458 | }else{ |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 459 | v->rc = rc; |
| 460 | } |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 | [diff] [blame] | 461 | } |
dan | 923c4b3 | 2014-11-10 17:53:03 | [diff] [blame] | 462 | sqlite3Error(db, rc); |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 463 | rc = sqlite3ApiExit(db, rc); |
| 464 | sqlite3_mutex_leave(db->mutex); |
| 465 | return rc; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 | [diff] [blame] | 466 | } |
| 467 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 468 | /* |
| 469 | ** Read data from a blob handle. |
| 470 | */ |
| 471 | int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){ |
drh | cb3cabd | 2016-11-25 19:18:28 | [diff] [blame] | 472 | return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreePayloadChecked); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /* |
| 476 | ** Write data to a blob handle. |
| 477 | */ |
| 478 | int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 | [diff] [blame] | 479 | return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /* |
| 483 | ** Query a blob handle for the size of the data. |
drh | 605264d | 2007-08-21 15:13:19 | [diff] [blame] | 484 | ** |
| 485 | ** The Incrblob.nByte field is fixed for the lifetime of the Incrblob |
| 486 | ** so no mutex is required for access. |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 487 | */ |
| 488 | int sqlite3_blob_bytes(sqlite3_blob *pBlob){ |
| 489 | Incrblob *p = (Incrblob *)pBlob; |
dan | eefab75 | 2010-12-06 17:11:05 | [diff] [blame] | 490 | return (p && p->pStmt) ? p->nByte : 0; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 491 | } |
| 492 | |
dan | 4e76cc3 | 2010-10-20 18:56:04 | [diff] [blame] | 493 | /* |
| 494 | ** Move an existing blob handle to point to a different row of the same |
| 495 | ** database table. |
| 496 | ** |
| 497 | ** If an error occurs, or if the specified row does not exist or does not |
| 498 | ** contain a blob or text value, then an error code is returned and the |
| 499 | ** database handle error code and message set. If this happens, then all |
| 500 | ** subsequent calls to sqlite3_blob_xxx() functions (except blob_close()) |
| 501 | ** immediately return SQLITE_ABORT. |
| 502 | */ |
| 503 | int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){ |
| 504 | int rc; |
| 505 | Incrblob *p = (Incrblob *)pBlob; |
| 506 | sqlite3 *db; |
| 507 | |
| 508 | if( p==0 ) return SQLITE_MISUSE_BKPT; |
| 509 | db = p->db; |
| 510 | sqlite3_mutex_enter(db->mutex); |
| 511 | |
| 512 | if( p->pStmt==0 ){ |
| 513 | /* If there is no statement handle, then the blob-handle has |
| 514 | ** already been invalidated. Return SQLITE_ABORT in this case. |
| 515 | */ |
| 516 | rc = SQLITE_ABORT; |
| 517 | }else{ |
| 518 | char *zErr; |
dan | 385b982 | 2021-04-14 15:25:10 | [diff] [blame] | 519 | ((Vdbe*)p->pStmt)->rc = SQLITE_OK; |
dan | 4e76cc3 | 2010-10-20 18:56:04 | [diff] [blame] | 520 | rc = blobSeekToRow(p, iRow, &zErr); |
| 521 | if( rc!=SQLITE_OK ){ |
drh | 0051a56 | 2023-05-10 13:56:32 | [diff] [blame] | 522 | sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : (char*)0), zErr); |
dan | 4e76cc3 | 2010-10-20 18:56:04 | [diff] [blame] | 523 | sqlite3DbFree(db, zErr); |
| 524 | } |
| 525 | assert( rc!=SQLITE_SCHEMA ); |
| 526 | } |
| 527 | |
| 528 | rc = sqlite3ApiExit(db, rc); |
dan | eefab75 | 2010-12-06 17:11:05 | [diff] [blame] | 529 | assert( rc==SQLITE_OK || p->pStmt==0 ); |
dan | 4e76cc3 | 2010-10-20 18:56:04 | [diff] [blame] | 530 | sqlite3_mutex_leave(db->mutex); |
| 531 | return rc; |
| 532 | } |
| 533 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 | [diff] [blame] | 534 | #endif /* #ifndef SQLITE_OMIT_INCRBLOB */ |