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 { |
| 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 */ |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 29 | int iCol; /* Table column this handle is open on */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 30 | BtCursor *pCsr; /* Cursor pointing at blob row */ |
| 31 | sqlite3_stmt *pStmt; /* Statement holding cursor open */ |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 32 | sqlite3 *db; /* The associated database */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 35 | |
dan | e3d82a8 | 2010-10-26 11:56:57 +0000 | [diff] [blame] | 36 | /* |
| 37 | ** This function is used by both blob_open() and blob_reopen(). It seeks |
| 38 | ** the b-tree cursor associated with blob handle p to point to row iRow. |
| 39 | ** If successful, SQLITE_OK is returned and subsequent calls to |
| 40 | ** sqlite3_blob_read() or sqlite3_blob_write() access the specified row. |
| 41 | ** |
| 42 | ** If an error occurs, or if the specified row does not exist or does not |
| 43 | ** contain a value of type TEXT or BLOB in the column nominated when the |
| 44 | ** blob handle was opened, then an error code is returned and *pzErr may |
| 45 | ** be set to point to a buffer containing an error message. It is the |
| 46 | ** responsibility of the caller to free the error message buffer using |
| 47 | ** sqlite3DbFree(). |
| 48 | ** |
| 49 | ** If an error does occur, then the b-tree cursor is closed. All subsequent |
| 50 | ** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will |
| 51 | ** immediately return SQLITE_ABORT. |
| 52 | */ |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 53 | static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){ |
| 54 | int rc; /* Error code */ |
| 55 | char *zErr = 0; /* Error message */ |
| 56 | Vdbe *v = (Vdbe *)p->pStmt; |
| 57 | |
dan | e3d82a8 | 2010-10-26 11:56:57 +0000 | [diff] [blame] | 58 | /* Set the value of the SQL statements only variable to integer iRow. |
| 59 | ** This is done directly instead of using sqlite3_bind_int64() to avoid |
| 60 | ** triggering asserts related to mutexes. |
| 61 | */ |
| 62 | assert( v->aVar[0].flags&MEM_Int ); |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 63 | v->aVar[0].u.i = iRow; |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 64 | |
dan | e3d82a8 | 2010-10-26 11:56:57 +0000 | [diff] [blame] | 65 | rc = sqlite3_step(p->pStmt); |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 66 | if( rc==SQLITE_ROW ){ |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 67 | VdbeCursor *pC = v->apCsr[0]; |
| 68 | u32 type = pC->aType[p->iCol]; |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 69 | if( type<12 ){ |
| 70 | zErr = sqlite3MPrintf(p->db, "cannot open value of type %s", |
| 71 | type==0?"null": type==7?"real": "integer" |
| 72 | ); |
| 73 | rc = SQLITE_ERROR; |
| 74 | sqlite3_finalize(p->pStmt); |
| 75 | p->pStmt = 0; |
| 76 | }else{ |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 77 | p->iOffset = pC->aType[p->iCol + pC->nField]; |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 78 | p->nByte = sqlite3VdbeSerialTypeLen(type); |
drh | 14da87f | 2013-11-20 21:51:33 +0000 | [diff] [blame] | 79 | p->pCsr = pC->pCursor; |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame^] | 80 | sqlite3BtreeIncrblobCursor(p->pCsr); |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | if( rc==SQLITE_ROW ){ |
| 85 | rc = SQLITE_OK; |
| 86 | }else if( p->pStmt ){ |
| 87 | rc = sqlite3_finalize(p->pStmt); |
| 88 | p->pStmt = 0; |
| 89 | if( rc==SQLITE_OK ){ |
| 90 | zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow); |
| 91 | rc = SQLITE_ERROR; |
| 92 | }else{ |
| 93 | zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db)); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | assert( rc!=SQLITE_OK || zErr==0 ); |
| 98 | assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE ); |
| 99 | |
| 100 | *pzErr = zErr; |
| 101 | return rc; |
| 102 | } |
| 103 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 104 | /* |
| 105 | ** Open a blob handle. |
| 106 | */ |
| 107 | int sqlite3_blob_open( |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 108 | sqlite3* db, /* The database connection */ |
| 109 | const char *zDb, /* The attached database containing the blob */ |
| 110 | const char *zTable, /* The table containing the blob */ |
| 111 | const char *zColumn, /* The column containing the blob */ |
| 112 | sqlite_int64 iRow, /* The row containing the glob */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 113 | int flags, /* True -> read/write access, false -> read-only */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 114 | sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 115 | ){ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 116 | int nAttempt = 0; |
| 117 | int iCol; /* Index of zColumn in row-record */ |
| 118 | |
| 119 | /* This VDBE program seeks a btree cursor to the identified |
| 120 | ** db/table/row entry. The reason for using a vdbe program instead |
| 121 | ** of writing code to use the b-tree layer directly is that the |
| 122 | ** vdbe program will take advantage of the various transaction, |
| 123 | ** locking and error handling infrastructure built into the vdbe. |
| 124 | ** |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 125 | ** After seeking the cursor, the vdbe executes an OP_ResultRow. |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 126 | ** Code external to the Vdbe then "borrows" the b-tree cursor and |
| 127 | ** uses it to implement the blob_read(), blob_write() and |
| 128 | ** blob_bytes() functions. |
| 129 | ** |
| 130 | ** The sqlite3_blob_close() function finalizes the vdbe program, |
| 131 | ** which closes the b-tree cursor and (possibly) commits the |
| 132 | ** transaction. |
| 133 | */ |
drh | b06a4ec | 2014-03-10 18:03:09 +0000 | [diff] [blame] | 134 | static const int iLn = VDBE_OFFSET_LINENO(4); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 135 | static const VdbeOpList openBlob[] = { |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 136 | /* {OP_Transaction, 0, 0, 0}, // 0: Inserted separately */ |
| 137 | {OP_TableLock, 0, 0, 0}, /* 1: Acquire a read or write lock */ |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 138 | /* One of the following two instructions is replaced by an OP_Noop. */ |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 139 | {OP_OpenRead, 0, 0, 0}, /* 2: Open cursor 0 for reading */ |
| 140 | {OP_OpenWrite, 0, 0, 0}, /* 3: Open cursor 0 for read/write */ |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 141 | {OP_Variable, 1, 1, 1}, /* 4: Push the rowid to the stack */ |
| 142 | {OP_NotExists, 0, 10, 1}, /* 5: Seek the cursor */ |
| 143 | {OP_Column, 0, 0, 1}, /* 6 */ |
| 144 | {OP_ResultRow, 1, 0, 0}, /* 7 */ |
| 145 | {OP_Goto, 0, 4, 0}, /* 8 */ |
| 146 | {OP_Close, 0, 0, 0}, /* 9 */ |
| 147 | {OP_Halt, 0, 0, 0}, /* 10 */ |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 150 | int rc = SQLITE_OK; |
drh | 6ee700c | 2009-06-01 19:53:30 +0000 | [diff] [blame] | 151 | char *zErr = 0; |
| 152 | Table *pTab; |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 153 | Parse *pParse = 0; |
| 154 | Incrblob *pBlob = 0; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 155 | |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 156 | flags = !!flags; /* flags = (flags ? 1 : 0); */ |
drh | 28414ee | 2009-05-09 15:17:47 +0000 | [diff] [blame] | 157 | *ppBlob = 0; |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 158 | |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 159 | sqlite3_mutex_enter(db->mutex); |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 160 | |
| 161 | pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob)); |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 162 | if( !pBlob ) goto blob_open_out; |
drh | 6ee700c | 2009-06-01 19:53:30 +0000 | [diff] [blame] | 163 | pParse = sqlite3StackAllocRaw(db, sizeof(*pParse)); |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 164 | if( !pParse ) goto blob_open_out; |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 165 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 166 | do { |
drh | 6ee700c | 2009-06-01 19:53:30 +0000 | [diff] [blame] | 167 | memset(pParse, 0, sizeof(Parse)); |
| 168 | pParse->db = db; |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 169 | sqlite3DbFree(db, zErr); |
| 170 | zErr = 0; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 171 | |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 172 | sqlite3BtreeEnterAll(db); |
drh | 6ee700c | 2009-06-01 19:53:30 +0000 | [diff] [blame] | 173 | pTab = sqlite3LocateTable(pParse, 0, zTable, zDb); |
danielk1977 | 36961ed | 2008-04-24 09:49:55 +0000 | [diff] [blame] | 174 | if( pTab && IsVirtual(pTab) ){ |
| 175 | pTab = 0; |
drh | 6ee700c | 2009-06-01 19:53:30 +0000 | [diff] [blame] | 176 | sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable); |
danielk1977 | 36961ed | 2008-04-24 09:49:55 +0000 | [diff] [blame] | 177 | } |
drh | 63f0eed | 2013-11-02 22:09:48 +0000 | [diff] [blame] | 178 | if( pTab && !HasRowid(pTab) ){ |
| 179 | pTab = 0; |
| 180 | sqlite3ErrorMsg(pParse, "cannot open table without rowid: %s", zTable); |
| 181 | } |
danielk1977 | 36961ed | 2008-04-24 09:49:55 +0000 | [diff] [blame] | 182 | #ifndef SQLITE_OMIT_VIEW |
| 183 | if( pTab && pTab->pSelect ){ |
| 184 | pTab = 0; |
drh | 6ee700c | 2009-06-01 19:53:30 +0000 | [diff] [blame] | 185 | sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable); |
danielk1977 | 36961ed | 2008-04-24 09:49:55 +0000 | [diff] [blame] | 186 | } |
| 187 | #endif |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 188 | if( !pTab ){ |
drh | 6ee700c | 2009-06-01 19:53:30 +0000 | [diff] [blame] | 189 | if( pParse->zErrMsg ){ |
| 190 | sqlite3DbFree(db, zErr); |
| 191 | zErr = pParse->zErrMsg; |
| 192 | pParse->zErrMsg = 0; |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 193 | } |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame] | 194 | rc = SQLITE_ERROR; |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 195 | sqlite3BtreeLeaveAll(db); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 196 | goto blob_open_out; |
| 197 | } |
| 198 | |
| 199 | /* Now search pTab for the exact column. */ |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 200 | for(iCol=0; iCol<pTab->nCol; iCol++) { |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 201 | if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){ |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | if( iCol==pTab->nCol ){ |
drh | 6ee700c | 2009-06-01 19:53:30 +0000 | [diff] [blame] | 206 | sqlite3DbFree(db, zErr); |
| 207 | zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 208 | rc = SQLITE_ERROR; |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 209 | sqlite3BtreeLeaveAll(db); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 210 | goto blob_open_out; |
| 211 | } |
| 212 | |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 213 | /* If the value is being opened for writing, check that the |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 214 | ** column is not indexed, and that it is not part of a foreign key. |
| 215 | ** It is against the rules to open a column to which either of these |
| 216 | ** descriptions applies for writing. */ |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 217 | if( flags ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 218 | const char *zFault = 0; |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 219 | Index *pIdx; |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 220 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 221 | if( db->flags&SQLITE_ForeignKeys ){ |
dan | 1316700 | 2009-10-02 06:35:06 +0000 | [diff] [blame] | 222 | /* Check that the column is not part of an FK child key definition. It |
| 223 | ** is not necessary to check if it is part of a parent key, as parent |
| 224 | ** key columns must be indexed. The check below will pick up this |
| 225 | ** case. */ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 226 | FKey *pFKey; |
| 227 | for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){ |
| 228 | int j; |
| 229 | for(j=0; j<pFKey->nCol; j++){ |
| 230 | if( pFKey->aCol[j].iFrom==iCol ){ |
| 231 | zFault = "foreign key"; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | #endif |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 237 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 238 | int j; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 239 | for(j=0; j<pIdx->nKeyCol; j++){ |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 240 | if( pIdx->aiColumn[j]==iCol ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 241 | zFault = "indexed"; |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 245 | if( zFault ){ |
| 246 | sqlite3DbFree(db, zErr); |
| 247 | zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault); |
| 248 | rc = SQLITE_ERROR; |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 249 | sqlite3BtreeLeaveAll(db); |
| 250 | goto blob_open_out; |
| 251 | } |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 252 | } |
| 253 | |
drh | 9ac7962 | 2013-12-18 15:11:47 +0000 | [diff] [blame] | 254 | pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse); |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 255 | assert( pBlob->pStmt || db->mallocFailed ); |
| 256 | if( pBlob->pStmt ){ |
| 257 | Vdbe *v = (Vdbe *)pBlob->pStmt; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 258 | int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 259 | |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 260 | |
| 261 | sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags, |
| 262 | pTab->pSchema->schema_cookie, |
| 263 | pTab->pSchema->iGeneration); |
| 264 | sqlite3VdbeChangeP5(v, 1); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 265 | sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 266 | |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 267 | /* Make sure a mutex is held on the table to be accessed */ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 268 | sqlite3VdbeUsesBtree(v, iDb); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 269 | |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 270 | /* Configure the OP_TableLock instruction */ |
dan | c548b78 | 2010-06-19 19:06:33 +0000 | [diff] [blame] | 271 | #ifdef SQLITE_OMIT_SHARED_CACHE |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 272 | sqlite3VdbeChangeToNoop(v, 1); |
dan | c548b78 | 2010-06-19 19:06:33 +0000 | [diff] [blame] | 273 | #else |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 274 | sqlite3VdbeChangeP1(v, 1, iDb); |
| 275 | sqlite3VdbeChangeP2(v, 1, pTab->tnum); |
| 276 | sqlite3VdbeChangeP3(v, 1, flags); |
| 277 | sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT); |
dan | c548b78 | 2010-06-19 19:06:33 +0000 | [diff] [blame] | 278 | #endif |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 279 | |
danielk1977 | f181924 | 2007-05-03 18:14:10 +0000 | [diff] [blame] | 280 | /* Remove either the OP_OpenWrite or OpenRead. Set the P2 |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 281 | ** parameter of the other to pTab->tnum. */ |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 282 | sqlite3VdbeChangeToNoop(v, 3 - flags); |
| 283 | sqlite3VdbeChangeP2(v, 2 + flags, pTab->tnum); |
| 284 | sqlite3VdbeChangeP3(v, 2 + flags, iDb); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 285 | |
danielk1977 | d336e22 | 2009-02-20 10:58:41 +0000 | [diff] [blame] | 286 | /* Configure the number of columns. Configure the cursor to |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 287 | ** think that the table has one more column than it really |
| 288 | ** does. An OP_Column to retrieve this imaginary column will |
| 289 | ** always return an SQL NULL. This is useful because it means |
| 290 | ** we can invoke OP_Column to fill in the vdbe cursors type |
| 291 | ** and offset cache without causing any IO. |
| 292 | */ |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 293 | sqlite3VdbeChangeP4(v, 2+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32); |
| 294 | sqlite3VdbeChangeP2(v, 6, pTab->nCol); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 295 | if( !db->mallocFailed ){ |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 296 | pParse->nVar = 1; |
| 297 | pParse->nMem = 1; |
| 298 | pParse->nTab = 1; |
| 299 | sqlite3VdbeMakeReady(v, pParse); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 300 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 301 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 302 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 303 | pBlob->flags = flags; |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 304 | pBlob->iCol = iCol; |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 305 | pBlob->db = db; |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 306 | sqlite3BtreeLeaveAll(db); |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 307 | if( db->mallocFailed ){ |
| 308 | goto blob_open_out; |
| 309 | } |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 310 | sqlite3_bind_int64(pBlob->pStmt, 1, iRow); |
| 311 | rc = blobSeekToRow(pBlob, iRow, &zErr); |
drh | 6062531 | 2013-04-06 18:06:51 +0000 | [diff] [blame] | 312 | } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA ); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 313 | |
| 314 | blob_open_out: |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 315 | if( rc==SQLITE_OK && db->mallocFailed==0 ){ |
| 316 | *ppBlob = (sqlite3_blob *)pBlob; |
| 317 | }else{ |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 318 | if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt); |
| 319 | sqlite3DbFree(db, pBlob); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 320 | } |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 321 | sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr); |
drh | 6ee700c | 2009-06-01 19:53:30 +0000 | [diff] [blame] | 322 | sqlite3DbFree(db, zErr); |
drh | f30a969 | 2013-11-15 01:10:18 +0000 | [diff] [blame] | 323 | sqlite3ParserReset(pParse); |
drh | 6ee700c | 2009-06-01 19:53:30 +0000 | [diff] [blame] | 324 | sqlite3StackFree(db, pParse); |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 325 | rc = sqlite3ApiExit(db, rc); |
| 326 | sqlite3_mutex_leave(db->mutex); |
| 327 | return rc; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /* |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 331 | ** Close a blob handle that was previously created using |
| 332 | ** sqlite3_blob_open(). |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 333 | */ |
| 334 | int sqlite3_blob_close(sqlite3_blob *pBlob){ |
| 335 | Incrblob *p = (Incrblob *)pBlob; |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 336 | int rc; |
drh | d9da78a | 2009-03-24 15:08:09 +0000 | [diff] [blame] | 337 | sqlite3 *db; |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 338 | |
drh | 28414ee | 2009-05-09 15:17:47 +0000 | [diff] [blame] | 339 | if( p ){ |
| 340 | db = p->db; |
| 341 | sqlite3_mutex_enter(db->mutex); |
| 342 | rc = sqlite3_finalize(p->pStmt); |
| 343 | sqlite3DbFree(db, p); |
| 344 | sqlite3_mutex_leave(db->mutex); |
| 345 | }else{ |
| 346 | rc = SQLITE_OK; |
| 347 | } |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 348 | return rc; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 349 | } |
| 350 | |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 351 | /* |
| 352 | ** Perform a read or write operation on a blob |
| 353 | */ |
drh | ee85813 | 2007-05-08 20:37:38 +0000 | [diff] [blame] | 354 | static int blobReadWrite( |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 355 | sqlite3_blob *pBlob, |
| 356 | void *z, |
| 357 | int n, |
| 358 | int iOffset, |
| 359 | int (*xCall)(BtCursor*, u32, u32, void*) |
| 360 | ){ |
| 361 | int rc; |
| 362 | Incrblob *p = (Incrblob *)pBlob; |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 363 | Vdbe *v; |
drh | 28414ee | 2009-05-09 15:17:47 +0000 | [diff] [blame] | 364 | sqlite3 *db; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 365 | |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 366 | if( p==0 ) return SQLITE_MISUSE_BKPT; |
drh | 28414ee | 2009-05-09 15:17:47 +0000 | [diff] [blame] | 367 | db = p->db; |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 368 | sqlite3_mutex_enter(db->mutex); |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 369 | v = (Vdbe*)p->pStmt; |
danielk1977 | a8b3018 | 2008-10-02 14:49:01 +0000 | [diff] [blame] | 370 | |
| 371 | if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){ |
| 372 | /* Request is out of range. Return a transient error. */ |
| 373 | rc = SQLITE_ERROR; |
| 374 | sqlite3Error(db, SQLITE_ERROR, 0); |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 375 | }else if( v==0 ){ |
danielk1977 | a8b3018 | 2008-10-02 14:49:01 +0000 | [diff] [blame] | 376 | /* If there is no statement handle, then the blob-handle has |
| 377 | ** already been invalidated. Return SQLITE_ABORT in this case. |
| 378 | */ |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 379 | rc = SQLITE_ABORT; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 380 | }else{ |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 381 | /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is |
| 382 | ** returned, clean-up the statement handle. |
| 383 | */ |
| 384 | assert( db == v->db ); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 385 | sqlite3BtreeEnterCursor(p->pCsr); |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 386 | rc = xCall(p->pCsr, iOffset+p->iOffset, n, z); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 387 | sqlite3BtreeLeaveCursor(p->pCsr); |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 388 | if( rc==SQLITE_ABORT ){ |
| 389 | sqlite3VdbeFinalize(v); |
| 390 | p->pStmt = 0; |
| 391 | }else{ |
| 392 | db->errCode = rc; |
| 393 | v->rc = rc; |
| 394 | } |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 395 | } |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 396 | rc = sqlite3ApiExit(db, rc); |
| 397 | sqlite3_mutex_leave(db->mutex); |
| 398 | return rc; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 399 | } |
| 400 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 401 | /* |
| 402 | ** Read data from a blob handle. |
| 403 | */ |
| 404 | int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 405 | return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /* |
| 409 | ** Write data to a blob handle. |
| 410 | */ |
| 411 | 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] | 412 | return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /* |
| 416 | ** Query a blob handle for the size of the data. |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 417 | ** |
| 418 | ** The Incrblob.nByte field is fixed for the lifetime of the Incrblob |
| 419 | ** so no mutex is required for access. |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 420 | */ |
| 421 | int sqlite3_blob_bytes(sqlite3_blob *pBlob){ |
| 422 | Incrblob *p = (Incrblob *)pBlob; |
dan | eefab75 | 2010-12-06 17:11:05 +0000 | [diff] [blame] | 423 | return (p && p->pStmt) ? p->nByte : 0; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 424 | } |
| 425 | |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 426 | /* |
| 427 | ** Move an existing blob handle to point to a different row of the same |
| 428 | ** database table. |
| 429 | ** |
| 430 | ** If an error occurs, or if the specified row does not exist or does not |
| 431 | ** contain a blob or text value, then an error code is returned and the |
| 432 | ** database handle error code and message set. If this happens, then all |
| 433 | ** subsequent calls to sqlite3_blob_xxx() functions (except blob_close()) |
| 434 | ** immediately return SQLITE_ABORT. |
| 435 | */ |
| 436 | int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){ |
| 437 | int rc; |
| 438 | Incrblob *p = (Incrblob *)pBlob; |
| 439 | sqlite3 *db; |
| 440 | |
| 441 | if( p==0 ) return SQLITE_MISUSE_BKPT; |
| 442 | db = p->db; |
| 443 | sqlite3_mutex_enter(db->mutex); |
| 444 | |
| 445 | if( p->pStmt==0 ){ |
| 446 | /* If there is no statement handle, then the blob-handle has |
| 447 | ** already been invalidated. Return SQLITE_ABORT in this case. |
| 448 | */ |
| 449 | rc = SQLITE_ABORT; |
| 450 | }else{ |
| 451 | char *zErr; |
| 452 | rc = blobSeekToRow(p, iRow, &zErr); |
| 453 | if( rc!=SQLITE_OK ){ |
| 454 | sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr); |
| 455 | sqlite3DbFree(db, zErr); |
| 456 | } |
| 457 | assert( rc!=SQLITE_SCHEMA ); |
| 458 | } |
| 459 | |
| 460 | rc = sqlite3ApiExit(db, rc); |
dan | eefab75 | 2010-12-06 17:11:05 +0000 | [diff] [blame] | 461 | assert( rc==SQLITE_OK || p->pStmt==0 ); |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 462 | sqlite3_mutex_leave(db->mutex); |
| 463 | return rc; |
| 464 | } |
| 465 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 466 | #endif /* #ifndef SQLITE_OMIT_INCRBLOB */ |