vdbeblob.c 14 KB

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