📄 test3.c
字号:
const char **argv /* Text of each argument */){ Btree *pBt; int nRoot; int *aRoot; int i; int nErr; char *zResult; if( argc<3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID ROOT ...\"", 0); return TCL_ERROR; } pBt = sqlite3TestTextToPtr(argv[1]); nRoot = argc-2; aRoot = (int*)sqlite3_malloc( sizeof(int)*(argc-2) ); for(i=0; i<argc-2; i++){ if( Tcl_GetInt(interp, argv[i+2], &aRoot[i]) ) return TCL_ERROR; }#ifndef SQLITE_OMIT_INTEGRITY_CHECK sqlite3BtreeEnter(pBt); zResult = sqlite3BtreeIntegrityCheck(pBt, aRoot, nRoot, 10000, &nErr); sqlite3BtreeLeave(pBt);#else zResult = 0;#endif sqlite3_free((void*)aRoot); if( zResult ){ Tcl_AppendResult(interp, zResult, 0); sqlite3_free(zResult); } return TCL_OK;}/*** Usage: btree_cursor_list ID**** Print information about all cursors to standard output for debugging.*/static int btree_cursor_list( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */){ Btree *pBt; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID\"", 0); return TCL_ERROR; } pBt = sqlite3TestTextToPtr(argv[1]); sqlite3BtreeEnter(pBt); sqlite3BtreeCursorList(pBt); sqlite3BtreeLeave(pBt); return SQLITE_OK;}/*** Usage: btree_cursor ID TABLENUM WRITEABLE**** Create a new cursor. Return the ID for the cursor.*/static int btree_cursor( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */){ Btree *pBt; int iTable; BtCursor *pCur; int rc; int wrFlag; char zBuf[30]; if( argc!=4 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID TABLENUM WRITEABLE\"", 0); return TCL_ERROR; } pBt = sqlite3TestTextToPtr(argv[1]); if( Tcl_GetInt(interp, argv[2], &iTable) ) return TCL_ERROR; if( Tcl_GetBoolean(interp, argv[3], &wrFlag) ) return TCL_ERROR; pCur = (BtCursor *)ckalloc(sqlite3BtreeCursorSize()); memset(pCur, 0, sqlite3BtreeCursorSize()); sqlite3BtreeEnter(pBt); rc = sqlite3BtreeCursor(pBt, iTable, wrFlag, 0, pCur); sqlite3BtreeLeave(pBt); if( rc ){ ckfree((char *)pCur); Tcl_AppendResult(interp, errorName(rc), 0); return TCL_ERROR; } sqlite3_snprintf(sizeof(zBuf), zBuf,"%p", pCur); Tcl_AppendResult(interp, zBuf, 0); return SQLITE_OK;}/*** Usage: btree_close_cursor ID**** Close a cursor opened using btree_cursor.*/static int btree_close_cursor( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */){ BtCursor *pCur; Btree *pBt; int rc; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID\"", 0); return TCL_ERROR; } pCur = sqlite3TestTextToPtr(argv[1]); pBt = pCur->pBtree; sqlite3BtreeEnter(pBt); rc = sqlite3BtreeCloseCursor(pCur); sqlite3BtreeLeave(pBt); ckfree((char *)pCur); if( rc ){ Tcl_AppendResult(interp, errorName(rc), 0); return TCL_ERROR; } return SQLITE_OK;}/*** Usage: btree_move_to ID KEY**** Move the cursor to the entry with the given key.*/static int btree_move_to( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */){ BtCursor *pCur; int rc; int res; char zBuf[20]; if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID KEY\"", 0); return TCL_ERROR; } pCur = sqlite3TestTextToPtr(argv[1]); sqlite3BtreeEnter(pCur->pBtree); if( sqlite3BtreeFlags(pCur) & BTREE_INTKEY ){ int iKey; if( Tcl_GetInt(interp, argv[2], &iKey) ){ sqlite3BtreeLeave(pCur->pBtree); return TCL_ERROR; } rc = sqlite3BtreeMovetoUnpacked(pCur, 0, iKey, 0, &res); }else{ rc = sqlite3BtreeMoveto(pCur, argv[2], strlen(argv[2]), 0, &res); } sqlite3BtreeLeave(pCur->pBtree); if( rc ){ Tcl_AppendResult(interp, errorName(rc), 0); return TCL_ERROR; } if( res<0 ) res = -1; if( res>0 ) res = 1; sqlite3_snprintf(sizeof(zBuf), zBuf,"%d",res); Tcl_AppendResult(interp, zBuf, 0); return SQLITE_OK;}/*** Usage: btree_delete ID**** Delete the entry that the cursor is pointing to*/static int btree_delete( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */){ BtCursor *pCur; int rc; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID\"", 0); return TCL_ERROR; } pCur = sqlite3TestTextToPtr(argv[1]); sqlite3BtreeEnter(pCur->pBtree); rc = sqlite3BtreeDelete(pCur); sqlite3BtreeLeave(pCur->pBtree); if( rc ){ Tcl_AppendResult(interp, errorName(rc), 0); return TCL_ERROR; } return SQLITE_OK;}/*** Usage: btree_insert ID KEY DATA ?NZERO?**** Create a new entry with the given key and data. If an entry already** exists with the same key the old entry is overwritten.*/static int btree_insert( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]){ BtCursor *pCur; int rc; int nZero; if( objc!=4 && objc!=5 ){ Tcl_WrongNumArgs(interp, 1, objv, "ID KEY DATA ?NZERO?"); return TCL_ERROR; } pCur = sqlite3TestTextToPtr(Tcl_GetString(objv[1])); if( objc==5 ){ if( Tcl_GetIntFromObj(interp, objv[4], &nZero) ) return TCL_ERROR; }else{ nZero = 0; } sqlite3BtreeEnter(pCur->pBtree); if( sqlite3BtreeFlags(pCur) & BTREE_INTKEY ){ i64 iKey; int len; unsigned char *pBuf; if( Tcl_GetWideIntFromObj(interp, objv[2], &iKey) ){ sqlite3BtreeLeave(pCur->pBtree); return TCL_ERROR; } pBuf = Tcl_GetByteArrayFromObj(objv[3], &len); rc = sqlite3BtreeInsert(pCur, 0, iKey, pBuf, len, nZero, 0); }else{ int keylen; int dlen; unsigned char *pKBuf; unsigned char *pDBuf; pKBuf = Tcl_GetByteArrayFromObj(objv[2], &keylen); pDBuf = Tcl_GetByteArrayFromObj(objv[3], &dlen); rc = sqlite3BtreeInsert(pCur, pKBuf, keylen, pDBuf, dlen, nZero, 0); } sqlite3BtreeLeave(pCur->pBtree); if( rc ){ Tcl_AppendResult(interp, errorName(rc), 0); return TCL_ERROR; } return SQLITE_OK;}/*** Usage: btree_next ID**** Move the cursor to the next entry in the table. Return 0 on success** or 1 if the cursor was already on the last entry in the table or if** the table is empty.*/static int btree_next( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */){ BtCursor *pCur; int rc; int res = 0; char zBuf[100]; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID\"", 0); return TCL_ERROR; } pCur = sqlite3TestTextToPtr(argv[1]); sqlite3BtreeEnter(pCur->pBtree); rc = sqlite3BtreeNext(pCur, &res); sqlite3BtreeLeave(pCur->pBtree); if( rc ){ Tcl_AppendResult(interp, errorName(rc), 0); return TCL_ERROR; } sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",res); Tcl_AppendResult(interp, zBuf, 0); return SQLITE_OK;}/*** Usage: btree_prev ID**** Move the cursor to the previous entry in the table. Return 0 on** success and 1 if the cursor was already on the first entry in** the table or if the table was empty.*/static int btree_prev( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */){ BtCursor *pCur; int rc; int res = 0; char zBuf[100]; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID\"", 0); return TCL_ERROR; } pCur = sqlite3TestTextToPtr(argv[1]); sqlite3BtreeEnter(pCur->pBtree); rc = sqlite3BtreePrevious(pCur, &res); sqlite3BtreeLeave(pCur->pBtree); if( rc ){ Tcl_AppendResult(interp, errorName(rc), 0); return TCL_ERROR; } sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",res); Tcl_AppendResult(interp, zBuf, 0); return SQLITE_OK;}/*** Usage: btree_first ID**** Move the cursor to the first entry in the table. Return 0 if the** cursor was left point to something and 1 if the table is empty.*/static int btree_first( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */){ BtCursor *pCur; int rc; int res = 0; char zBuf[100]; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID\"", 0); return TCL_ERROR; } pCur = sqlite3TestTextToPtr(argv[1]); sqlite3BtreeEnter(pCur->pBtree); rc = sqlite3BtreeFirst(pCur, &res); sqlite3BtreeLeave(pCur->pBtree); if( rc ){ Tcl_AppendResult(interp, errorName(rc), 0); return TCL_ERROR; } sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",res); Tcl_AppendResult(interp, zBuf, 0); return SQLITE_OK;}/*** Usage: btree_last ID**** Move the cursor to the last entry in the table. Return 0 if the** cursor was left point to something and 1 if the table is empty.*/static int btree_last( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */){ BtCursor *pCur; int rc; int res = 0; char zBuf[100]; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID\"", 0); return TCL_ERROR; } pCur = sqlite3TestTextToPtr(argv[1]); sqlite3BtreeEnter(pCur->pBtree); rc = sqlite3BtreeLast(pCur, &res); sqlite3BtreeLeave(pCur->pBtree); if( rc ){ Tcl_AppendResult(interp, errorName(rc), 0); return TCL_ERROR; } sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",res); Tcl_AppendResult(interp, zBuf, 0); return SQLITE_OK;}/*** Usage: btree_eof ID**** Return TRUE if the given cursor is not pointing at a valid entry.** Return FALSE if the cursor does point to a valid entry.*/static int btree_eof( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */){ BtCursor *pCur; int rc; char zBuf[50]; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID\"", 0); return TCL_ERROR; } pCur = sqlite3TestTextToPtr(argv[1]); sqlite3BtreeEnter(pCur->pBtree); rc = sqlite3BtreeEof(pCur); sqlite3BtreeLeave(pCur->pBtree); sqlite3_snprintf(sizeof(zBuf),zBuf, "%d", rc); Tcl_AppendResult(interp, zBuf, 0); return SQLITE_OK;}/*** Usage: btree_keysize ID**** Return the number of bytes of key. For an INTKEY table, this** returns the key itself.*/static int btree_keysize( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */){ BtCursor *pCur; u64 n; char zBuf[50]; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID\"", 0); return TCL_ERROR; } pCur = sqlite3TestTextToPtr(argv[1]); sqlite3BtreeEnter(pCur->pBtree); sqlite3BtreeKeySize(pCur, (i64*)&n); sqlite3BtreeLeave(pCur->pBtree); sqlite3_snprintf(sizeof(zBuf),zBuf, "%llu", n); Tcl_AppendResult(interp, zBuf, 0); return SQLITE_OK;}/*** Usage: btree_key ID**** Return the key for the entry at which the cursor is pointing.*/static int btree_key( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */){ BtCursor *pCur; int rc; u64 n; char *zBuf; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID\"", 0); return TCL_ERROR; } pCur = sqlite3TestTextToPtr(argv[1]); sqlite3BtreeEnter(pCur->pBtree); sqlite3BtreeKeySize(pCur, (i64*)&n); if( sqlite3BtreeFlags(pCur) & BTREE_INTKEY ){ char zBuf2[60]; sqlite3_snprintf(sizeof(zBuf2),zBuf2, "%llu", n); Tcl_AppendResult(interp, zBuf2, 0); }else{ zBuf = sqlite3_malloc( n+1 ); rc = sqlite3BtreeKey(pCur, 0, n, zBuf); if( rc ){ sqlite3BtreeLeave(pCur->pBtree); Tcl_AppendResult(interp, errorName(rc), 0); return TCL_ERROR; } zBuf[n] = 0; Tcl_AppendResult(interp, zBuf, 0); sqlite3_free(zBuf); } sqlite3BtreeLeave(pCur->pBtree); return SQLITE_OK;}/*** Usage: btree_data ID ?N?**** Return the data for the entry at which the cursor is pointing.*/static int btree_data( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */){ BtCursor *pCur; int rc; u32 n; char *zBuf; if( argc!=2 && argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID\"", 0); return TCL_ERROR; } pCur = sqlite3TestTextToPtr(argv[1]); sqlite3BtreeEnter(pCur->pBtree); if( argc==2 ){ sqlite3BtreeDataSize(pCur, &n); }else{ n = atoi(argv[2]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -