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