Exemple #1
0
        /*
        ** usage:   btree_from_db  DB-HANDLE
        **
        ** This command returns the btree handle for the main database associated
        ** with the database-handle passed as the argument. Example usage:
        **
        ** sqlite3 db test.db
        ** set bt [btree_from_db db]
        */
        static int btree_from_db(
            object NotUsed,
            Tcl_Interp interp, /* The TCL interpreter that invoked this command */
            int argc,          /* Number of arguments */
            TclObject[] argv   /* Text of each argument */
            )
        {
            string         zBuf = "";//char zBuf[100];
            WrappedCommand info = null;
            sqlite3        db;
            Btree          pBt;
            int            iDb = 0;

            if (argc != 2 && argc != 3)
            {
                TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0].ToString(),
                                     " DB-HANDLE ?N?\"");
                return(TCL.TCL_ERROR);
            }

            if (TCL.Tcl_GetCommandInfo(interp, argv[1].ToString(), ref info))
            {
                TCL.Tcl_AppendResult(interp, "No such db-handle: \"", argv[1], "\"");
                return(TCL.TCL_ERROR);
            }
            if (argc == 3)
            {
                iDb = atoi(argv[2].ToString());
            }

            db = ((SqliteDb)info.objClientData).db;
            Debug.Assert(db != null);

            pBt = db.aDb[iDb].pBt;
            sqlite3_snprintf(50, ref zBuf, "->%p", pBt);
            if (TCL.Tcl_CreateCommandPointer(interp, zBuf, pBt))
            {
                return(TCL.TCL_ERROR);
            }
            else
            {
                TCL.Tcl_SetResult(interp, zBuf, TCL.TCL_VOLATILE);
            }
            return(TCL.TCL_OK);
        }
        static int backupTestCmd(
            ClientData clientData,
            Tcl_Interp interp,
            int objc,
            Tcl_Obj[] objv
            )
        {
            BackupSubCommand[] aSub = new BackupSubCommand[] {
                new BackupSubCommand("step", BackupSubCommandEnum.BACKUP_STEP, 1, "npage"),
                new BackupSubCommand("finish", BackupSubCommandEnum.BACKUP_FINISH, 0, ""),
                new BackupSubCommand("remaining", BackupSubCommandEnum.BACKUP_REMAINING, 0, ""),
                new BackupSubCommand("pagecount", BackupSubCommandEnum.BACKUP_PAGECOUNT, 0, ""),
                new BackupSubCommand(null, 0, 0, null)
            };

            sqlite3_backup p    = (sqlite3_backup)clientData;
            int            iCmd = 0;
            int            rc;

            rc = Tcl_GetIndexFromObjStruct(
                interp, objv[1], aSub, aSub.Length, "option", 0, ref iCmd
                );
            if (rc != TCL.TCL_OK)
            {
                return(rc);
            }
            if (objc != (2 + aSub[iCmd].nArg))
            {
                TCL.Tcl_WrongNumArgs(interp, 2, objv, aSub[iCmd].zArg);
                return(TCL.TCL_ERROR);
            }

            switch (aSub[iCmd].eCmd)
            {
            case BackupSubCommandEnum.BACKUP_FINISH:
            {
                string         zCmdName;
                WrappedCommand cmdInfo = null;
                zCmdName = TCL.Tcl_GetString(objv[0]);
                TCL.Tcl_GetCommandInfo(interp, zCmdName, ref cmdInfo);
                cmdInfo.deleteProc = null;
                TCL.Tcl_SetCommandInfo(interp, zCmdName, cmdInfo);
                TCL.Tcl_DeleteCommand(interp, zCmdName);

                rc = sqlite3_backup_finish(p);
                TCL.Tcl_SetResult(interp, sqlite3TestErrorName(rc), TCL.TCL_STATIC);
                break;
            }

            case BackupSubCommandEnum.BACKUP_STEP:
            {
                int nPage = 0;
                if (TCL.Tcl_GetIntFromObj(interp, objv[2], ref nPage))
                {
                    return(TCL.TCL_ERROR);
                }
                rc = sqlite3_backup_step(p, nPage);
                TCL.Tcl_SetResult(interp, sqlite3TestErrorName(rc), TCL.TCL_STATIC);
                break;
            }

            case BackupSubCommandEnum.BACKUP_REMAINING:
                TCL.Tcl_SetObjResult(interp, TCL.Tcl_NewIntObj(sqlite3_backup_remaining(p)));
                break;

            case BackupSubCommandEnum.BACKUP_PAGECOUNT:
                TCL.Tcl_SetObjResult(interp, TCL.Tcl_NewIntObj(sqlite3_backup_pagecount(p)));
                break;
            }

            return(TCL.TCL_OK);
        }