📄 test3.c
字号:
Tcl_AppendResult(interp, zResult, 0); sqliteFree(zResult); } return TCL_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; } if( Tcl_GetInt(interp, argv[1], (int*)&pBt) ) return TCL_ERROR; if( Tcl_GetInt(interp, argv[2], &iTable) ) return TCL_ERROR; if( Tcl_GetBoolean(interp, argv[3], &wrFlag) ) return TCL_ERROR; rc = sqliteBtreeCursor(pBt, iTable, wrFlag, &pCur); if( rc ){ Tcl_AppendResult(interp, errorName(rc), 0); return TCL_ERROR; } sprintf(zBuf,"0x%x", (int)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; int rc; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID\"", 0); return TCL_ERROR; } if( Tcl_GetInt(interp, argv[1], (int*)&pCur) ) return TCL_ERROR; rc = sqliteBtreeCloseCursor(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; } if( Tcl_GetInt(interp, argv[1], (int*)&pCur) ) return TCL_ERROR; rc = sqliteBtreeMoveto(pCur, argv[2], strlen(argv[2]), &res); if( rc ){ Tcl_AppendResult(interp, errorName(rc), 0); return TCL_ERROR; } if( res<0 ) res = -1; if( res>0 ) res = 1; sprintf(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; } if( Tcl_GetInt(interp, argv[1], (int*)&pCur) ) return TCL_ERROR; rc = sqliteBtreeDelete(pCur); if( rc ){ Tcl_AppendResult(interp, errorName(rc), 0); return TCL_ERROR; } return SQLITE_OK;}/*** Usage: btree_insert ID KEY DATA**** 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 *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!=4 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID KEY DATA\"", 0); return TCL_ERROR; } if( Tcl_GetInt(interp, argv[1], (int*)&pCur) ) return TCL_ERROR; rc = sqliteBtreeInsert(pCur, argv[2], strlen(argv[2]), argv[3], strlen(argv[3])); 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; } if( Tcl_GetInt(interp, argv[1], (int*)&pCur) ) return TCL_ERROR; rc = sqliteBtreeNext(pCur, &res); if( rc ){ Tcl_AppendResult(interp, errorName(rc), 0); return TCL_ERROR; } sprintf(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; } if( Tcl_GetInt(interp, argv[1], (int*)&pCur) ) return TCL_ERROR; rc = sqliteBtreePrevious(pCur, &res); if( rc ){ Tcl_AppendResult(interp, errorName(rc), 0); return TCL_ERROR; } sprintf(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; } if( Tcl_GetInt(interp, argv[1], (int*)&pCur) ) return TCL_ERROR; rc = sqliteBtreeFirst(pCur, &res); if( rc ){ Tcl_AppendResult(interp, errorName(rc), 0); return TCL_ERROR; } sprintf(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; } if( Tcl_GetInt(interp, argv[1], (int*)&pCur) ) return TCL_ERROR; rc = sqliteBtreeLast(pCur, &res); if( rc ){ Tcl_AppendResult(interp, errorName(rc), 0); return TCL_ERROR; } sprintf(zBuf,"%d",res); 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; int n; char *zBuf; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID\"", 0); return TCL_ERROR; } if( Tcl_GetInt(interp, argv[1], (int*)&pCur) ) return TCL_ERROR; sqliteBtreeKeySize(pCur, &n); zBuf = malloc( n+1 ); rc = sqliteBtreeKey(pCur, 0, n, zBuf); if( rc!=n ){ char zMsg[100]; free(zBuf); sprintf(zMsg, "truncated key: got %d of %d bytes", rc, n); Tcl_AppendResult(interp, zMsg, 0); return TCL_ERROR; } zBuf[n] = 0; Tcl_AppendResult(interp, zBuf, 0); free(zBuf); return SQLITE_OK;}/*** Usage: btree_data ID**** 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; int n; char *zBuf; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID\"", 0); return TCL_ERROR; } if( Tcl_GetInt(interp, argv[1], (int*)&pCur) ) return TCL_ERROR; sqliteBtreeDataSize(pCur, &n); zBuf = malloc( n+1 ); rc = sqliteBtreeData(pCur, 0, n, zBuf); if( rc!=n ){ char zMsg[100]; free(zBuf); sprintf(zMsg, "truncated data: got %d of %d bytes", rc, n); Tcl_AppendResult(interp, zMsg, 0); return TCL_ERROR; } zBuf[n] = 0; Tcl_AppendResult(interp, zBuf, 0); free(zBuf); return SQLITE_OK;}/*** Usage: btree_payload_size ID**** Return the number of bytes of payload*/static int btree_payload_size( 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 n1, n2; char zBuf[50]; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID\"", 0); return TCL_ERROR; } if( Tcl_GetInt(interp, argv[1], (int*)&pCur) ) return TCL_ERROR; sqliteBtreeKeySize(pCur, &n1); sqliteBtreeDataSize(pCur, &n2); sprintf(zBuf, "%d", n1+n2); Tcl_AppendResult(interp, zBuf, 0); return SQLITE_OK;}/*** Usage: btree_cursor_dump ID**** Return eight integers containing information about the entry the** cursor is pointing to:**** aResult[0] = The page number** aResult[1] = The entry number** aResult[2] = Total number of entries on this page** aResult[3] = Size of this entry** aResult[4] = Number of free bytes on this page** aResult[5] = Number of free blocks on the page** aResult[6] = Page number of the left child of this entry** aResult[7] = Page number of the right child for the whole page*/static int btree_cursor_dump( 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 i, j; int aResult[8]; char zBuf[400]; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID\"", 0); return TCL_ERROR; } if( Tcl_GetInt(interp, argv[1], (int*)&pCur) ) return TCL_ERROR; rc = sqliteBtreeCursorDump(pCur, aResult); if( rc ){ Tcl_AppendResult(interp, errorName(rc), 0); return TCL_ERROR; } j = 0; for(i=0; i<sizeof(aResult)/sizeof(aResult[0]); i++){ sprintf(&zBuf[j]," %d", aResult[i]); j += strlen(&zBuf[j]); } Tcl_AppendResult(interp, &zBuf[1], 0); return SQLITE_OK;}/*** Register commands with the TCL interpreter.*/int Sqlitetest3_Init(Tcl_Interp *interp){ static struct { char *zName; Tcl_CmdProc *xProc; } aCmd[] = { { "btree_open", (Tcl_CmdProc*)btree_open }, { "btree_close", (Tcl_CmdProc*)btree_close }, { "btree_begin_transaction", (Tcl_CmdProc*)btree_begin_transaction }, { "btree_commit", (Tcl_CmdProc*)btree_commit }, { "btree_rollback", (Tcl_CmdProc*)btree_rollback }, { "btree_create_table", (Tcl_CmdProc*)btree_create_table }, { "btree_drop_table", (Tcl_CmdProc*)btree_drop_table }, { "btree_clear_table", (Tcl_CmdProc*)btree_clear_table }, { "btree_get_meta", (Tcl_CmdProc*)btree_get_meta }, { "btree_update_meta", (Tcl_CmdProc*)btree_update_meta }, { "btree_page_dump", (Tcl_CmdProc*)btree_page_dump }, { "btree_tree_dump", (Tcl_CmdProc*)btree_tree_dump }, { "btree_pager_stats", (Tcl_CmdProc*)btree_pager_stats }, { "btree_pager_ref_dump", (Tcl_CmdProc*)btree_pager_ref_dump }, { "btree_cursor", (Tcl_CmdProc*)btree_cursor }, { "btree_close_cursor", (Tcl_CmdProc*)btree_close_cursor }, { "btree_move_to", (Tcl_CmdProc*)btree_move_to }, { "btree_delete", (Tcl_CmdProc*)btree_delete }, { "btree_insert", (Tcl_CmdProc*)btree_insert }, { "btree_next", (Tcl_CmdProc*)btree_next }, { "btree_prev", (Tcl_CmdProc*)btree_prev }, { "btree_key", (Tcl_CmdProc*)btree_key }, { "btree_data", (Tcl_CmdProc*)btree_data }, { "btree_payload_size", (Tcl_CmdProc*)btree_payload_size }, { "btree_first", (Tcl_CmdProc*)btree_first }, { "btree_last", (Tcl_CmdProc*)btree_last }, { "btree_cursor_dump", (Tcl_CmdProc*)btree_cursor_dump }, { "btree_integrity_check", (Tcl_CmdProc*)btree_integrity_check }, }; int i; for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); } Tcl_LinkVar(interp, "pager_refinfo_enable", (char*)&pager_refinfo_enable, TCL_LINK_INT); Tcl_LinkVar(interp, "btree_native_byte_order",(char*)&btree_native_byte_order, TCL_LINK_INT); return TCL_OK;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -