OLD | NEW |
1 /* | 1 /* |
2 ** 2007 May 1 | 2 ** 2007 May 1 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
11 ************************************************************************* | 11 ************************************************************************* |
12 ** | 12 ** |
13 ** This file contains code used to implement incremental BLOB I/O. | 13 ** This file contains code used to implement incremental BLOB I/O. |
14 */ | 14 */ |
15 | 15 |
16 #include "sqliteInt.h" | 16 #include "sqliteInt.h" |
17 #include "vdbeInt.h" | 17 #include "vdbeInt.h" |
18 | 18 |
19 #ifndef SQLITE_OMIT_INCRBLOB | 19 #ifndef SQLITE_OMIT_INCRBLOB |
20 | 20 |
21 /* | 21 /* |
22 ** Valid sqlite3_blob* handles point to Incrblob structures. | 22 ** Valid sqlite3_blob* handles point to Incrblob structures. |
23 */ | 23 */ |
24 typedef struct Incrblob Incrblob; | 24 typedef struct Incrblob Incrblob; |
25 struct Incrblob { | 25 struct Incrblob { |
26 int flags; /* Copy of "flags" passed to sqlite3_blob_open() */ | |
27 int nByte; /* Size of open blob, in bytes */ | 26 int nByte; /* Size of open blob, in bytes */ |
28 int iOffset; /* Byte offset of blob in cursor data */ | 27 int iOffset; /* Byte offset of blob in cursor data */ |
29 int iCol; /* Table column this handle is open on */ | 28 u16 iCol; /* Table column this handle is open on */ |
30 BtCursor *pCsr; /* Cursor pointing at blob row */ | 29 BtCursor *pCsr; /* Cursor pointing at blob row */ |
31 sqlite3_stmt *pStmt; /* Statement holding cursor open */ | 30 sqlite3_stmt *pStmt; /* Statement holding cursor open */ |
32 sqlite3 *db; /* The associated database */ | 31 sqlite3 *db; /* The associated database */ |
| 32 char *zDb; /* Database name */ |
| 33 Table *pTab; /* Table object */ |
33 }; | 34 }; |
34 | 35 |
35 | 36 |
36 /* | 37 /* |
37 ** This function is used by both blob_open() and blob_reopen(). It seeks | 38 ** 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 ** 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 ** If successful, SQLITE_OK is returned and subsequent calls to |
40 ** sqlite3_blob_read() or sqlite3_blob_write() access the specified row. | 41 ** sqlite3_blob_read() or sqlite3_blob_write() access the specified row. |
41 ** | 42 ** |
42 ** If an error occurs, or if the specified row does not exist or does not | 43 ** 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 ** 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 ** 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 ** 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 ** responsibility of the caller to free the error message buffer using |
47 ** sqlite3DbFree(). | 48 ** sqlite3DbFree(). |
48 ** | 49 ** |
49 ** If an error does occur, then the b-tree cursor is closed. All subsequent | 50 ** 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 ** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will |
51 ** immediately return SQLITE_ABORT. | 52 ** immediately return SQLITE_ABORT. |
52 */ | 53 */ |
53 static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){ | 54 static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){ |
54 int rc; /* Error code */ | 55 int rc; /* Error code */ |
55 char *zErr = 0; /* Error message */ | 56 char *zErr = 0; /* Error message */ |
56 Vdbe *v = (Vdbe *)p->pStmt; | 57 Vdbe *v = (Vdbe *)p->pStmt; |
57 | 58 |
58 /* Set the value of the SQL statements only variable to integer iRow. | 59 /* Set the value of register r[1] in the SQL statement to integer iRow. |
59 ** This is done directly instead of using sqlite3_bind_int64() to avoid | 60 ** This is done directly as a performance optimization |
60 ** triggering asserts related to mutexes. | |
61 */ | 61 */ |
62 assert( v->aVar[0].flags&MEM_Int ); | 62 v->aMem[1].flags = MEM_Int; |
63 v->aVar[0].u.i = iRow; | 63 v->aMem[1].u.i = iRow; |
64 | 64 |
65 rc = sqlite3_step(p->pStmt); | 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. */ |
| 69 if( v->pc>3 ){ |
| 70 v->pc = 3; |
| 71 rc = sqlite3VdbeExec(v); |
| 72 }else{ |
| 73 rc = sqlite3_step(p->pStmt); |
| 74 } |
66 if( rc==SQLITE_ROW ){ | 75 if( rc==SQLITE_ROW ){ |
67 VdbeCursor *pC = v->apCsr[0]; | 76 VdbeCursor *pC = v->apCsr[0]; |
68 u32 type = pC->aType[p->iCol]; | 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 ); |
69 if( type<12 ){ | 80 if( type<12 ){ |
70 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s", | 81 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s", |
71 type==0?"null": type==7?"real": "integer" | 82 type==0?"null": type==7?"real": "integer" |
72 ); | 83 ); |
73 rc = SQLITE_ERROR; | 84 rc = SQLITE_ERROR; |
74 sqlite3_finalize(p->pStmt); | 85 sqlite3_finalize(p->pStmt); |
75 p->pStmt = 0; | 86 p->pStmt = 0; |
76 }else{ | 87 }else{ |
77 p->iOffset = pC->aType[p->iCol + pC->nField]; | 88 p->iOffset = pC->aType[p->iCol + pC->nField]; |
78 p->nByte = sqlite3VdbeSerialTypeLen(type); | 89 p->nByte = sqlite3VdbeSerialTypeLen(type); |
(...skipping 24 matching lines...) Expand all Loading... |
103 | 114 |
104 /* | 115 /* |
105 ** Open a blob handle. | 116 ** Open a blob handle. |
106 */ | 117 */ |
107 int sqlite3_blob_open( | 118 int sqlite3_blob_open( |
108 sqlite3* db, /* The database connection */ | 119 sqlite3* db, /* The database connection */ |
109 const char *zDb, /* The attached database containing the blob */ | 120 const char *zDb, /* The attached database containing the blob */ |
110 const char *zTable, /* The table containing the blob */ | 121 const char *zTable, /* The table containing the blob */ |
111 const char *zColumn, /* The column containing the blob */ | 122 const char *zColumn, /* The column containing the blob */ |
112 sqlite_int64 iRow, /* The row containing the glob */ | 123 sqlite_int64 iRow, /* The row containing the glob */ |
113 int flags, /* True -> read/write access, false -> read-only */ | 124 int wrFlag, /* True -> read/write access, false -> read-only */ |
114 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */ | 125 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */ |
115 ){ | 126 ){ |
116 int nAttempt = 0; | 127 int nAttempt = 0; |
117 int iCol; /* Index of zColumn in row-record */ | 128 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 ** | |
125 ** After seeking the cursor, the vdbe executes an OP_ResultRow. | |
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 */ | |
134 static const int iLn = VDBE_OFFSET_LINENO(4); | |
135 static const VdbeOpList openBlob[] = { | |
136 /* {OP_Transaction, 0, 0, 0}, // 0: Inserted separately */ | |
137 {OP_TableLock, 0, 0, 0}, /* 1: Acquire a read or write lock */ | |
138 /* One of the following two instructions is replaced by an OP_Noop. */ | |
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 */ | |
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 */ | |
148 }; | |
149 | |
150 int rc = SQLITE_OK; | 129 int rc = SQLITE_OK; |
151 char *zErr = 0; | 130 char *zErr = 0; |
152 Table *pTab; | 131 Table *pTab; |
153 Parse *pParse = 0; | 132 Parse *pParse = 0; |
154 Incrblob *pBlob = 0; | 133 Incrblob *pBlob = 0; |
155 | 134 |
156 #ifdef SQLITE_ENABLE_API_ARMOR | 135 #ifdef SQLITE_ENABLE_API_ARMOR |
157 if( ppBlob==0 ){ | 136 if( ppBlob==0 ){ |
158 return SQLITE_MISUSE_BKPT; | 137 return SQLITE_MISUSE_BKPT; |
159 } | 138 } |
160 #endif | 139 #endif |
161 *ppBlob = 0; | 140 *ppBlob = 0; |
162 #ifdef SQLITE_ENABLE_API_ARMOR | 141 #ifdef SQLITE_ENABLE_API_ARMOR |
163 if( !sqlite3SafetyCheckOk(db) || zTable==0 ){ | 142 if( !sqlite3SafetyCheckOk(db) || zTable==0 ){ |
164 return SQLITE_MISUSE_BKPT; | 143 return SQLITE_MISUSE_BKPT; |
165 } | 144 } |
166 #endif | 145 #endif |
167 flags = !!flags; /* flags = (flags ? 1 : 0); */ | 146 wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */ |
168 | 147 |
169 sqlite3_mutex_enter(db->mutex); | 148 sqlite3_mutex_enter(db->mutex); |
170 | 149 |
171 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob)); | 150 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob)); |
172 if( !pBlob ) goto blob_open_out; | 151 if( !pBlob ) goto blob_open_out; |
173 pParse = sqlite3StackAllocRaw(db, sizeof(*pParse)); | 152 pParse = sqlite3StackAllocRaw(db, sizeof(*pParse)); |
174 if( !pParse ) goto blob_open_out; | 153 if( !pParse ) goto blob_open_out; |
175 | 154 |
176 do { | 155 do { |
177 memset(pParse, 0, sizeof(Parse)); | 156 memset(pParse, 0, sizeof(Parse)); |
(...skipping 20 matching lines...) Expand all Loading... |
198 if( !pTab ){ | 177 if( !pTab ){ |
199 if( pParse->zErrMsg ){ | 178 if( pParse->zErrMsg ){ |
200 sqlite3DbFree(db, zErr); | 179 sqlite3DbFree(db, zErr); |
201 zErr = pParse->zErrMsg; | 180 zErr = pParse->zErrMsg; |
202 pParse->zErrMsg = 0; | 181 pParse->zErrMsg = 0; |
203 } | 182 } |
204 rc = SQLITE_ERROR; | 183 rc = SQLITE_ERROR; |
205 sqlite3BtreeLeaveAll(db); | 184 sqlite3BtreeLeaveAll(db); |
206 goto blob_open_out; | 185 goto blob_open_out; |
207 } | 186 } |
| 187 pBlob->pTab = pTab; |
| 188 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName; |
208 | 189 |
209 /* Now search pTab for the exact column. */ | 190 /* Now search pTab for the exact column. */ |
210 for(iCol=0; iCol<pTab->nCol; iCol++) { | 191 for(iCol=0; iCol<pTab->nCol; iCol++) { |
211 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){ | 192 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){ |
212 break; | 193 break; |
213 } | 194 } |
214 } | 195 } |
215 if( iCol==pTab->nCol ){ | 196 if( iCol==pTab->nCol ){ |
216 sqlite3DbFree(db, zErr); | 197 sqlite3DbFree(db, zErr); |
217 zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn); | 198 zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn); |
218 rc = SQLITE_ERROR; | 199 rc = SQLITE_ERROR; |
219 sqlite3BtreeLeaveAll(db); | 200 sqlite3BtreeLeaveAll(db); |
220 goto blob_open_out; | 201 goto blob_open_out; |
221 } | 202 } |
222 | 203 |
223 /* If the value is being opened for writing, check that the | 204 /* If the value is being opened for writing, check that the |
224 ** column is not indexed, and that it is not part of a foreign key. | 205 ** column is not indexed, and that it is not part of a foreign key. |
225 ** It is against the rules to open a column to which either of these | 206 */ |
226 ** descriptions applies for writing. */ | 207 if( wrFlag ){ |
227 if( flags ){ | |
228 const char *zFault = 0; | 208 const char *zFault = 0; |
229 Index *pIdx; | 209 Index *pIdx; |
230 #ifndef SQLITE_OMIT_FOREIGN_KEY | 210 #ifndef SQLITE_OMIT_FOREIGN_KEY |
231 if( db->flags&SQLITE_ForeignKeys ){ | 211 if( db->flags&SQLITE_ForeignKeys ){ |
232 /* Check that the column is not part of an FK child key definition. It | 212 /* Check that the column is not part of an FK child key definition. It |
233 ** is not necessary to check if it is part of a parent key, as parent | 213 ** is not necessary to check if it is part of a parent key, as parent |
234 ** key columns must be indexed. The check below will pick up this | 214 ** key columns must be indexed. The check below will pick up this |
235 ** case. */ | 215 ** case. */ |
236 FKey *pFKey; | 216 FKey *pFKey; |
237 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){ | 217 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){ |
(...skipping 20 matching lines...) Expand all Loading... |
258 zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault); | 238 zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault); |
259 rc = SQLITE_ERROR; | 239 rc = SQLITE_ERROR; |
260 sqlite3BtreeLeaveAll(db); | 240 sqlite3BtreeLeaveAll(db); |
261 goto blob_open_out; | 241 goto blob_open_out; |
262 } | 242 } |
263 } | 243 } |
264 | 244 |
265 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse); | 245 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse); |
266 assert( pBlob->pStmt || db->mallocFailed ); | 246 assert( pBlob->pStmt || db->mallocFailed ); |
267 if( pBlob->pStmt ){ | 247 if( pBlob->pStmt ){ |
| 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 */ |
| 264 static const int iLn = VDBE_OFFSET_LINENO(2); |
| 265 static const VdbeOpList openBlob[] = { |
| 266 {OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */ |
| 267 {OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */ |
| 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 */ |
| 273 }; |
268 Vdbe *v = (Vdbe *)pBlob->pStmt; | 274 Vdbe *v = (Vdbe *)pBlob->pStmt; |
269 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); | 275 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 276 VdbeOp *aOp; |
270 | 277 |
271 | 278 sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag, |
272 sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags, | |
273 pTab->pSchema->schema_cookie, | 279 pTab->pSchema->schema_cookie, |
274 pTab->pSchema->iGeneration); | 280 pTab->pSchema->iGeneration); |
275 sqlite3VdbeChangeP5(v, 1); | 281 sqlite3VdbeChangeP5(v, 1); |
276 sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn); | 282 aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn); |
277 | 283 |
278 /* Make sure a mutex is held on the table to be accessed */ | 284 /* Make sure a mutex is held on the table to be accessed */ |
279 sqlite3VdbeUsesBtree(v, iDb); | 285 sqlite3VdbeUsesBtree(v, iDb); |
280 | 286 |
281 /* Configure the OP_TableLock instruction */ | 287 if( db->mallocFailed==0 ){ |
| 288 assert( aOp!=0 ); |
| 289 /* Configure the OP_TableLock instruction */ |
282 #ifdef SQLITE_OMIT_SHARED_CACHE | 290 #ifdef SQLITE_OMIT_SHARED_CACHE |
283 sqlite3VdbeChangeToNoop(v, 1); | 291 aOp[0].opcode = OP_Noop; |
284 #else | 292 #else |
285 sqlite3VdbeChangeP1(v, 1, iDb); | 293 aOp[0].p1 = iDb; |
286 sqlite3VdbeChangeP2(v, 1, pTab->tnum); | 294 aOp[0].p2 = pTab->tnum; |
287 sqlite3VdbeChangeP3(v, 1, flags); | 295 aOp[0].p3 = wrFlag; |
288 sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT); | 296 sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT); |
| 297 } |
| 298 if( db->mallocFailed==0 ){ |
289 #endif | 299 #endif |
290 | 300 |
291 /* Remove either the OP_OpenWrite or OpenRead. Set the P2 | 301 /* Remove either the OP_OpenWrite or OpenRead. Set the P2 |
292 ** parameter of the other to pTab->tnum. */ | 302 ** parameter of the other to pTab->tnum. */ |
293 sqlite3VdbeChangeToNoop(v, 3 - flags); | 303 if( wrFlag ) aOp[1].opcode = OP_OpenWrite; |
294 sqlite3VdbeChangeP2(v, 2 + flags, pTab->tnum); | 304 aOp[1].p2 = pTab->tnum; |
295 sqlite3VdbeChangeP3(v, 2 + flags, iDb); | 305 aOp[1].p3 = iDb; |
296 | 306 |
297 /* Configure the number of columns. Configure the cursor to | 307 /* Configure the number of columns. Configure the cursor to |
298 ** think that the table has one more column than it really | 308 ** think that the table has one more column than it really |
299 ** does. An OP_Column to retrieve this imaginary column will | 309 ** does. An OP_Column to retrieve this imaginary column will |
300 ** always return an SQL NULL. This is useful because it means | 310 ** always return an SQL NULL. This is useful because it means |
301 ** we can invoke OP_Column to fill in the vdbe cursors type | 311 ** we can invoke OP_Column to fill in the vdbe cursors type |
302 ** and offset cache without causing any IO. | 312 ** and offset cache without causing any IO. |
303 */ | 313 */ |
304 sqlite3VdbeChangeP4(v, 2+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32); | 314 aOp[1].p4type = P4_INT32; |
305 sqlite3VdbeChangeP2(v, 6, pTab->nCol); | 315 aOp[1].p4.i = pTab->nCol+1; |
306 if( !db->mallocFailed ){ | 316 aOp[3].p2 = pTab->nCol; |
307 pParse->nVar = 1; | 317 |
| 318 pParse->nVar = 0; |
308 pParse->nMem = 1; | 319 pParse->nMem = 1; |
309 pParse->nTab = 1; | 320 pParse->nTab = 1; |
310 sqlite3VdbeMakeReady(v, pParse); | 321 sqlite3VdbeMakeReady(v, pParse); |
311 } | 322 } |
312 } | 323 } |
313 | 324 |
314 pBlob->flags = flags; | |
315 pBlob->iCol = iCol; | 325 pBlob->iCol = iCol; |
316 pBlob->db = db; | 326 pBlob->db = db; |
317 sqlite3BtreeLeaveAll(db); | 327 sqlite3BtreeLeaveAll(db); |
318 if( db->mallocFailed ){ | 328 if( db->mallocFailed ){ |
319 goto blob_open_out; | 329 goto blob_open_out; |
320 } | 330 } |
321 sqlite3_bind_int64(pBlob->pStmt, 1, iRow); | |
322 rc = blobSeekToRow(pBlob, iRow, &zErr); | 331 rc = blobSeekToRow(pBlob, iRow, &zErr); |
323 } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA ); | 332 } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA ); |
324 | 333 |
325 blob_open_out: | 334 blob_open_out: |
326 if( rc==SQLITE_OK && db->mallocFailed==0 ){ | 335 if( rc==SQLITE_OK && db->mallocFailed==0 ){ |
327 *ppBlob = (sqlite3_blob *)pBlob; | 336 *ppBlob = (sqlite3_blob *)pBlob; |
328 }else{ | 337 }else{ |
329 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt); | 338 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt); |
330 sqlite3DbFree(db, pBlob); | 339 sqlite3DbFree(db, pBlob); |
331 } | 340 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 /* If there is no statement handle, then the blob-handle has | 395 /* If there is no statement handle, then the blob-handle has |
387 ** already been invalidated. Return SQLITE_ABORT in this case. | 396 ** already been invalidated. Return SQLITE_ABORT in this case. |
388 */ | 397 */ |
389 rc = SQLITE_ABORT; | 398 rc = SQLITE_ABORT; |
390 }else{ | 399 }else{ |
391 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is | 400 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is |
392 ** returned, clean-up the statement handle. | 401 ** returned, clean-up the statement handle. |
393 */ | 402 */ |
394 assert( db == v->db ); | 403 assert( db == v->db ); |
395 sqlite3BtreeEnterCursor(p->pCsr); | 404 sqlite3BtreeEnterCursor(p->pCsr); |
| 405 |
| 406 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| 407 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){ |
| 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; |
| 422 iKey = sqlite3BtreeIntegerKey(p->pCsr); |
| 423 sqlite3VdbePreUpdateHook( |
| 424 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1 |
| 425 ); |
| 426 } |
| 427 #endif |
| 428 |
396 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z); | 429 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z); |
397 sqlite3BtreeLeaveCursor(p->pCsr); | 430 sqlite3BtreeLeaveCursor(p->pCsr); |
398 if( rc==SQLITE_ABORT ){ | 431 if( rc==SQLITE_ABORT ){ |
399 sqlite3VdbeFinalize(v); | 432 sqlite3VdbeFinalize(v); |
400 p->pStmt = 0; | 433 p->pStmt = 0; |
401 }else{ | 434 }else{ |
402 v->rc = rc; | 435 v->rc = rc; |
403 } | 436 } |
404 } | 437 } |
405 sqlite3Error(db, rc); | 438 sqlite3Error(db, rc); |
406 rc = sqlite3ApiExit(db, rc); | 439 rc = sqlite3ApiExit(db, rc); |
407 sqlite3_mutex_leave(db->mutex); | 440 sqlite3_mutex_leave(db->mutex); |
408 return rc; | 441 return rc; |
409 } | 442 } |
410 | 443 |
411 /* | 444 /* |
412 ** Read data from a blob handle. | 445 ** Read data from a blob handle. |
413 */ | 446 */ |
414 int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){ | 447 int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){ |
415 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData); | 448 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreePayloadChecked); |
416 } | 449 } |
417 | 450 |
418 /* | 451 /* |
419 ** Write data to a blob handle. | 452 ** Write data to a blob handle. |
420 */ | 453 */ |
421 int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){ | 454 int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){ |
422 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData); | 455 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData); |
423 } | 456 } |
424 | 457 |
425 /* | 458 /* |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 assert( rc!=SQLITE_SCHEMA ); | 500 assert( rc!=SQLITE_SCHEMA ); |
468 } | 501 } |
469 | 502 |
470 rc = sqlite3ApiExit(db, rc); | 503 rc = sqlite3ApiExit(db, rc); |
471 assert( rc==SQLITE_OK || p->pStmt==0 ); | 504 assert( rc==SQLITE_OK || p->pStmt==0 ); |
472 sqlite3_mutex_leave(db->mutex); | 505 sqlite3_mutex_leave(db->mutex); |
473 return rc; | 506 return rc; |
474 } | 507 } |
475 | 508 |
476 #endif /* #ifndef SQLITE_OMIT_INCRBLOB */ | 509 #endif /* #ifndef SQLITE_OMIT_INCRBLOB */ |
OLD | NEW |