|
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 ** |
|
13 ** This file contains code used to implement incremental BLOB I/O. |
|
14 ** |
|
15 ** $Id: vdbeblob.c,v 1.25 2008/07/28 19:34:54 drh Exp $ |
|
16 */ |
|
17 |
|
18 #include "sqliteInt.h" |
|
19 #include "vdbeInt.h" |
|
20 |
|
21 #ifndef SQLITE_OMIT_INCRBLOB |
|
22 |
|
23 /* |
|
24 ** Valid sqlite3_blob* handles point to Incrblob structures. |
|
25 */ |
|
26 typedef struct Incrblob Incrblob; |
|
27 struct Incrblob { |
|
28 int flags; /* Copy of "flags" passed to sqlite3_blob_open() */ |
|
29 int nByte; /* Size of open blob, in bytes */ |
|
30 int iOffset; /* Byte offset of blob in cursor data */ |
|
31 BtCursor *pCsr; /* Cursor pointing at blob row */ |
|
32 sqlite3_stmt *pStmt; /* Statement holding cursor open */ |
|
33 sqlite3 *db; /* The associated database */ |
|
34 }; |
|
35 |
|
36 /* |
|
37 ** Open a blob handle. |
|
38 */ |
|
39 int sqlite3_blob_open( |
|
40 sqlite3* db, /* The database connection */ |
|
41 const char *zDb, /* The attached database containing the blob */ |
|
42 const char *zTable, /* The table containing the blob */ |
|
43 const char *zColumn, /* The column containing the blob */ |
|
44 sqlite_int64 iRow, /* The row containing the glob */ |
|
45 int flags, /* True -> read/write access, false -> read-only */ |
|
46 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */ |
|
47 ){ |
|
48 int nAttempt = 0; |
|
49 int iCol; /* Index of zColumn in row-record */ |
|
50 |
|
51 /* This VDBE program seeks a btree cursor to the identified |
|
52 ** db/table/row entry. The reason for using a vdbe program instead |
|
53 ** of writing code to use the b-tree layer directly is that the |
|
54 ** vdbe program will take advantage of the various transaction, |
|
55 ** locking and error handling infrastructure built into the vdbe. |
|
56 ** |
|
57 ** After seeking the cursor, the vdbe executes an OP_ResultRow. |
|
58 ** Code external to the Vdbe then "borrows" the b-tree cursor and |
|
59 ** uses it to implement the blob_read(), blob_write() and |
|
60 ** blob_bytes() functions. |
|
61 ** |
|
62 ** The sqlite3_blob_close() function finalizes the vdbe program, |
|
63 ** which closes the b-tree cursor and (possibly) commits the |
|
64 ** transaction. |
|
65 */ |
|
66 static const VdbeOpList openBlob[] = { |
|
67 {OP_Transaction, 0, 0, 0}, /* 0: Start a transaction */ |
|
68 {OP_VerifyCookie, 0, 0, 0}, /* 1: Check the schema cookie */ |
|
69 |
|
70 /* One of the following two instructions is replaced by an |
|
71 ** OP_Noop before exection. |
|
72 */ |
|
73 {OP_SetNumColumns, 0, 0, 0}, /* 2: Num cols for cursor */ |
|
74 {OP_OpenRead, 0, 0, 0}, /* 3: Open cursor 0 for reading */ |
|
75 {OP_SetNumColumns, 0, 0, 0}, /* 4: Num cols for cursor */ |
|
76 {OP_OpenWrite, 0, 0, 0}, /* 5: Open cursor 0 for read/write */ |
|
77 |
|
78 {OP_Variable, 1, 1, 0}, /* 6: Push the rowid to the stack */ |
|
79 {OP_NotExists, 0, 10, 1}, /* 7: Seek the cursor */ |
|
80 {OP_Column, 0, 0, 1}, /* 8 */ |
|
81 {OP_ResultRow, 1, 0, 0}, /* 9 */ |
|
82 {OP_Close, 0, 0, 0}, /* 10 */ |
|
83 {OP_Halt, 0, 0, 0}, /* 11 */ |
|
84 }; |
|
85 |
|
86 Vdbe *v = 0; |
|
87 int rc = SQLITE_OK; |
|
88 char zErr[128]; |
|
89 |
|
90 zErr[0] = 0; |
|
91 sqlite3_mutex_enter(db->mutex); |
|
92 do { |
|
93 Parse sParse; |
|
94 Table *pTab; |
|
95 |
|
96 memset(&sParse, 0, sizeof(Parse)); |
|
97 sParse.db = db; |
|
98 |
|
99 if( sqlite3SafetyOn(db) ){ |
|
100 sqlite3_mutex_leave(db->mutex); |
|
101 return SQLITE_MISUSE; |
|
102 } |
|
103 |
|
104 sqlite3BtreeEnterAll(db); |
|
105 pTab = sqlite3LocateTable(&sParse, 0, zTable, zDb); |
|
106 if( pTab && IsVirtual(pTab) ){ |
|
107 pTab = 0; |
|
108 sqlite3ErrorMsg(&sParse, "cannot open virtual table: %s", zTable); |
|
109 } |
|
110 #ifndef SQLITE_OMIT_VIEW |
|
111 if( pTab && pTab->pSelect ){ |
|
112 pTab = 0; |
|
113 sqlite3ErrorMsg(&sParse, "cannot open view: %s", zTable); |
|
114 } |
|
115 #endif |
|
116 if( !pTab ){ |
|
117 if( sParse.zErrMsg ){ |
|
118 sqlite3_snprintf(sizeof(zErr), zErr, "%s", sParse.zErrMsg); |
|
119 } |
|
120 sqlite3DbFree(db, sParse.zErrMsg); |
|
121 rc = SQLITE_ERROR; |
|
122 (void)sqlite3SafetyOff(db); |
|
123 sqlite3BtreeLeaveAll(db); |
|
124 goto blob_open_out; |
|
125 } |
|
126 |
|
127 /* Now search pTab for the exact column. */ |
|
128 for(iCol=0; iCol < pTab->nCol; iCol++) { |
|
129 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){ |
|
130 break; |
|
131 } |
|
132 } |
|
133 if( iCol==pTab->nCol ){ |
|
134 sqlite3_snprintf(sizeof(zErr), zErr, "no such column: \"%s\"", zColumn); |
|
135 rc = SQLITE_ERROR; |
|
136 (void)sqlite3SafetyOff(db); |
|
137 sqlite3BtreeLeaveAll(db); |
|
138 goto blob_open_out; |
|
139 } |
|
140 |
|
141 /* If the value is being opened for writing, check that the |
|
142 ** column is not indexed. It is against the rules to open an |
|
143 ** indexed column for writing. |
|
144 */ |
|
145 if( flags ){ |
|
146 Index *pIdx; |
|
147 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
|
148 int j; |
|
149 for(j=0; j<pIdx->nColumn; j++){ |
|
150 if( pIdx->aiColumn[j]==iCol ){ |
|
151 sqlite3_snprintf(sizeof(zErr), zErr, |
|
152 "cannot open indexed column for writing"); |
|
153 rc = SQLITE_ERROR; |
|
154 (void)sqlite3SafetyOff(db); |
|
155 sqlite3BtreeLeaveAll(db); |
|
156 goto blob_open_out; |
|
157 } |
|
158 } |
|
159 } |
|
160 } |
|
161 |
|
162 v = sqlite3VdbeCreate(db); |
|
163 if( v ){ |
|
164 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
|
165 sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob); |
|
166 |
|
167 /* Configure the OP_Transaction */ |
|
168 sqlite3VdbeChangeP1(v, 0, iDb); |
|
169 sqlite3VdbeChangeP2(v, 0, (flags ? 1 : 0)); |
|
170 |
|
171 /* Configure the OP_VerifyCookie */ |
|
172 sqlite3VdbeChangeP1(v, 1, iDb); |
|
173 sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie); |
|
174 |
|
175 /* Make sure a mutex is held on the table to be accessed */ |
|
176 sqlite3VdbeUsesBtree(v, iDb); |
|
177 |
|
178 /* Remove either the OP_OpenWrite or OpenRead. Set the P2 |
|
179 ** parameter of the other to pTab->tnum. |
|
180 */ |
|
181 sqlite3VdbeChangeToNoop(v, (flags ? 3 : 5), 1); |
|
182 sqlite3VdbeChangeP2(v, (flags ? 5 : 3), pTab->tnum); |
|
183 sqlite3VdbeChangeP3(v, (flags ? 5 : 3), iDb); |
|
184 |
|
185 /* Configure the OP_SetNumColumns. Configure the cursor to |
|
186 ** think that the table has one more column than it really |
|
187 ** does. An OP_Column to retrieve this imaginary column will |
|
188 ** always return an SQL NULL. This is useful because it means |
|
189 ** we can invoke OP_Column to fill in the vdbe cursors type |
|
190 ** and offset cache without causing any IO. |
|
191 */ |
|
192 sqlite3VdbeChangeP2(v, flags ? 4 : 2, pTab->nCol+1); |
|
193 sqlite3VdbeChangeP2(v, 8, pTab->nCol); |
|
194 if( !db->mallocFailed ){ |
|
195 sqlite3VdbeMakeReady(v, 1, 1, 1, 0); |
|
196 } |
|
197 } |
|
198 |
|
199 sqlite3BtreeLeaveAll(db); |
|
200 rc = sqlite3SafetyOff(db); |
|
201 if( rc!=SQLITE_OK || db->mallocFailed ){ |
|
202 goto blob_open_out; |
|
203 } |
|
204 |
|
205 sqlite3_bind_int64((sqlite3_stmt *)v, 1, iRow); |
|
206 rc = sqlite3_step((sqlite3_stmt *)v); |
|
207 if( rc!=SQLITE_ROW ){ |
|
208 nAttempt++; |
|
209 rc = sqlite3_finalize((sqlite3_stmt *)v); |
|
210 sqlite3_snprintf(sizeof(zErr), zErr, sqlite3_errmsg(db)); |
|
211 v = 0; |
|
212 } |
|
213 } while( nAttempt<5 && rc==SQLITE_SCHEMA ); |
|
214 |
|
215 if( rc==SQLITE_ROW ){ |
|
216 /* The row-record has been opened successfully. Check that the |
|
217 ** column in question contains text or a blob. If it contains |
|
218 ** text, it is up to the caller to get the encoding right. |
|
219 */ |
|
220 Incrblob *pBlob; |
|
221 u32 type = v->apCsr[0]->aType[iCol]; |
|
222 |
|
223 if( type<12 ){ |
|
224 sqlite3_snprintf(sizeof(zErr), zErr, "cannot open value of type %s", |
|
225 type==0?"null": type==7?"real": "integer" |
|
226 ); |
|
227 rc = SQLITE_ERROR; |
|
228 goto blob_open_out; |
|
229 } |
|
230 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob)); |
|
231 if( db->mallocFailed ){ |
|
232 sqlite3DbFree(db, pBlob); |
|
233 goto blob_open_out; |
|
234 } |
|
235 pBlob->flags = flags; |
|
236 pBlob->pCsr = v->apCsr[0]->pCursor; |
|
237 sqlite3BtreeEnterCursor(pBlob->pCsr); |
|
238 sqlite3BtreeCacheOverflow(pBlob->pCsr); |
|
239 sqlite3BtreeLeaveCursor(pBlob->pCsr); |
|
240 pBlob->pStmt = (sqlite3_stmt *)v; |
|
241 pBlob->iOffset = v->apCsr[0]->aOffset[iCol]; |
|
242 pBlob->nByte = sqlite3VdbeSerialTypeLen(type); |
|
243 pBlob->db = db; |
|
244 *ppBlob = (sqlite3_blob *)pBlob; |
|
245 rc = SQLITE_OK; |
|
246 }else if( rc==SQLITE_OK ){ |
|
247 sqlite3_snprintf(sizeof(zErr), zErr, "no such rowid: %lld", iRow); |
|
248 rc = SQLITE_ERROR; |
|
249 } |
|
250 |
|
251 blob_open_out: |
|
252 zErr[sizeof(zErr)-1] = '\0'; |
|
253 if( rc!=SQLITE_OK || db->mallocFailed ){ |
|
254 sqlite3_finalize((sqlite3_stmt *)v); |
|
255 } |
|
256 sqlite3Error(db, rc, (rc==SQLITE_OK?0:zErr)); |
|
257 rc = sqlite3ApiExit(db, rc); |
|
258 sqlite3_mutex_leave(db->mutex); |
|
259 return rc; |
|
260 } |
|
261 |
|
262 /* |
|
263 ** Close a blob handle that was previously created using |
|
264 ** sqlite3_blob_open(). |
|
265 */ |
|
266 int sqlite3_blob_close(sqlite3_blob *pBlob){ |
|
267 Incrblob *p = (Incrblob *)pBlob; |
|
268 int rc; |
|
269 |
|
270 rc = sqlite3_finalize(p->pStmt); |
|
271 sqlite3DbFree(p->db, p); |
|
272 return rc; |
|
273 } |
|
274 |
|
275 /* |
|
276 ** Perform a read or write operation on a blob |
|
277 */ |
|
278 static int blobReadWrite( |
|
279 sqlite3_blob *pBlob, |
|
280 void *z, |
|
281 int n, |
|
282 int iOffset, |
|
283 int (*xCall)(BtCursor*, u32, u32, void*) |
|
284 ){ |
|
285 int rc; |
|
286 Incrblob *p = (Incrblob *)pBlob; |
|
287 Vdbe *v; |
|
288 sqlite3 *db = p->db; |
|
289 |
|
290 /* Request is out of range. Return a transient error. */ |
|
291 if( (iOffset+n)>p->nByte ){ |
|
292 return SQLITE_ERROR; |
|
293 } |
|
294 sqlite3_mutex_enter(db->mutex); |
|
295 |
|
296 /* If there is no statement handle, then the blob-handle has |
|
297 ** already been invalidated. Return SQLITE_ABORT in this case. |
|
298 */ |
|
299 v = (Vdbe*)p->pStmt; |
|
300 if( v==0 ){ |
|
301 rc = SQLITE_ABORT; |
|
302 }else{ |
|
303 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is |
|
304 ** returned, clean-up the statement handle. |
|
305 */ |
|
306 assert( db == v->db ); |
|
307 sqlite3BtreeEnterCursor(p->pCsr); |
|
308 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z); |
|
309 sqlite3BtreeLeaveCursor(p->pCsr); |
|
310 if( rc==SQLITE_ABORT ){ |
|
311 sqlite3VdbeFinalize(v); |
|
312 p->pStmt = 0; |
|
313 }else{ |
|
314 db->errCode = rc; |
|
315 v->rc = rc; |
|
316 } |
|
317 } |
|
318 rc = sqlite3ApiExit(db, rc); |
|
319 sqlite3_mutex_leave(db->mutex); |
|
320 return rc; |
|
321 } |
|
322 |
|
323 /* |
|
324 ** Read data from a blob handle. |
|
325 */ |
|
326 int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){ |
|
327 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData); |
|
328 } |
|
329 |
|
330 /* |
|
331 ** Write data to a blob handle. |
|
332 */ |
|
333 int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){ |
|
334 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData); |
|
335 } |
|
336 |
|
337 /* |
|
338 ** Query a blob handle for the size of the data. |
|
339 ** |
|
340 ** The Incrblob.nByte field is fixed for the lifetime of the Incrblob |
|
341 ** so no mutex is required for access. |
|
342 */ |
|
343 int sqlite3_blob_bytes(sqlite3_blob *pBlob){ |
|
344 Incrblob *p = (Incrblob *)pBlob; |
|
345 return p->nByte; |
|
346 } |
|
347 |
|
348 #endif /* #ifndef SQLITE_OMIT_INCRBLOB */ |