Ejemplo n.º 1
0
        /*
        ** Usage:   btree_close ID
        **
        ** Close the given database.
        */
        static int btree_close(
            object NotUsed,
            Tcl_Interp interp, /* The TCL interpreter that invoked this command */
            int argc,          /* Number of arguments */
            TclObject[] argv   /* Text of each argument */
            )
        {
            Btree pBt;
            int   rc;

            if (argc != 2)
            {
                TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0].ToString(),
                                     " ID\"", null);
                return(TCL.TCL_ERROR);
            }
            pBt = (Btree)sqlite3TestTextToPtr(interp, argv[1].ToString());
            rc  = sqlite3BtreeClose(ref pBt);
            if (rc != SQLITE_OK)
            {
                TCL.Tcl_AppendResult(interp, errorName(rc), null);
                return(TCL.TCL_ERROR);
            }
            nRefSqlite3--;
            if (nRefSqlite3 == 0)
            {
                sqlite3_mutex_leave(sDb.mutex);
                sqlite3_mutex_free(sDb.mutex);
                sDb.mutex = null;
                sDb.pVfs  = null;
            }
            return(TCL.TCL_OK);
        }
Ejemplo n.º 2
0
        /*
        ** Usage:   btree_close_cursor ID
        **
        ** Close a cursor opened using btree_cursor.
        */
        static int btree_close_cursor(
            object NotUsed,
            Tcl_Interp interp, /* The TCL interpreter that invoked this command */
            int argc,          /* Number of arguments */
            TclObject[] argv   /* Text of each argument */
            )
        {
            BtCursor pCur;
            Btree    pBt;
            int      rc;

            if (argc != 2)
            {
                TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0].ToString(),
                                     " ID\"");
                return(TCL.TCL_ERROR);
            }
            pCur = (BtCursor)sqlite3TestTextToPtr(interp, argv[1].ToString());
            pBt  = pCur.pBtree;
            sqlite3BtreeEnter(pBt);
            rc = sqlite3BtreeCloseCursor(pCur);
            sqlite3BtreeLeave(pBt);
            pCur = null;//ckfree( (char*)pCur );
            if (rc != 0)
            {
                TCL.Tcl_AppendResult(interp, errorName(rc), null);
                ;
                return(TCL.TCL_ERROR);
            }
            return(SQLITE_OK);
        }
Ejemplo n.º 3
0
        /*
        ** 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(
            object NotUsed,
            Tcl_Interp interp, /* The TCL interpreter that invoked this command */
            int argc,          /* Number of arguments */
            TclObject[] argv   /* Text of each argument */
            )
        {
            BtCursor pCur;
            int      rc;
            int      res  = 0;
            string   zBuf = "";//[100];

            if (argc != 2)
            {
                TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0].ToString(),
                                     " ID\"");
                return(TCL.TCL_ERROR);
            }
            pCur = (BtCursor)sqlite3TestTextToPtr(interp, argv[1].ToString());
#if SQLITE_TEST
            sqlite3BtreeEnter(pCur.pBtree);
#endif
            rc = sqlite3BtreeFirst(pCur, ref res);
#if SQLITE_TEST
            sqlite3BtreeLeave(pCur.pBtree);
#endif
            if (rc != 0)
            {
                TCL.Tcl_AppendResult(interp, errorName(rc), null);;
                return(TCL.TCL_ERROR);
            }
            sqlite3_snprintf(100, ref zBuf, "%d", res);
            TCL.Tcl_AppendResult(interp, zBuf);
            return(SQLITE_OK);
        }
Ejemplo n.º 4
0
        /*
        ** Usage:   btree_ismemdb ID
        **
        ** Return true if the B-Tree is in-memory.
        */
        static int btree_ismemdb(
            object NotUsed,
            Tcl_Interp interp, /* The TCL interpreter that invoked this command */
            int argc,          /* Number of arguments */
            TclObject[] argv   /* Text of each argument */
            )
        {
            Btree pBt;
            int   res;

            if (argc != 2)
            {
                TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
                                     " ID\"");
                return(TCL.TCL_ERROR);
            }
            pBt = (Btree)sqlite3TestTextToPtr(interp, argv[1].ToString());
            sqlite3_mutex_enter(pBt.db.mutex);
            sqlite3BtreeEnter(pBt);
            res = sqlite3PagerIsMemdb(sqlite3BtreePager(pBt)) ? 1 : 0;
            sqlite3BtreeLeave(pBt);
            sqlite3_mutex_leave(pBt.db.mutex);
            TCL.Tcl_SetObjResult(interp, TCL.Tcl_NewBooleanObj(res));
            return(TCL.TCL_OK);
        }
Ejemplo n.º 5
0
     /*
     ** USAGE:  utf8_to_utf8  HEX
     **
     ** The argument is a UTF8 string represented _in hexadecimal.
     ** The UTF8 might not be well-formed.  Run this string through
     ** sqlite3Utf8to8() convert it back to hex and return the result.
     */
     static int utf8_to_utf8(
         object clientdata,
         Tcl_Interp interp,
         int objc,
         Tcl_Obj[] objv
         )
     {
 #if SQLITE_DEBUG
         int    n = 0;
         int    nOut;
         string zOrig;
         byte[] z;
         if (objc != 2)
         {
             TCL.Tcl_WrongNumArgs(interp, 1, objv, "HEX");
             return(TCL.TCL_ERROR);
         }
         zOrig = TCL.Tcl_GetStringFromObj(objv[1], out n);
         z     = new byte[2 * n + 1];//sqlite3Malloc( n + 3 );
         nOut  = sqlite3TestHexToBin(zOrig, n, z);
         //z[n] = 0;
         nOut = sqlite3Utf8To8(z);
         sqlite3TestBinToHex(z, zOrig.Length);
         TCL.Tcl_AppendResult(interp, Encoding.ASCII.GetString(z, 0, n));
         //sqlite3_free( z );
         return(TCL.TCL_OK);
 #else
         Tcl_AppendResult(interp,
                          "[utf8_to_utf8] unavailable - SQLITE_DEBUG not defined", 0
                          );
         return(TCL.TCL_ERROR);
 #endif
     }
Ejemplo n.º 6
0
        /*
        ** A TCL command to take the md5 hash of a file.  The argument is the
        ** name of the file.
        */
        static int md5file_cmd(object cd, Tcl_Interp interp, int argc, Tcl_Obj[] argv)
        {
            StreamReader _in = null;

            byte[]        digest = new byte[16];
            StringBuilder zBuf   = new StringBuilder(10240);

            if (argc != 2)
            {
                TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
                                     " FILENAME\"", 0);
                return(TCL.TCL_ERROR);
            }
            Debugger.Break(); // TODO --   _in = fopen( argv[1], "rb" );
            if (_in == null)
            {
                TCL.Tcl_AppendResult(interp, "unable to open file \"", argv[1],
                                     "\" for reading", 0);
                return(TCL.TCL_ERROR);
            }
            Debugger.Break(); // TODO
            //MD5Init( ctx );
            //for(;;){
            //  int n;
            //  n = fread(zBuf, 1, zBuf.Capacity, _in);
            //  if( n<=0 ) break;
            //  MD5Update(ctx, zBuf.ToString(), (unsigned)n);
            //}
            //fclose(_in);
            //MD5Final(digest, ctx);
            //  DigestToBase16(digest, zBuf);
            //Tcl_AppendResult( interp, zBuf );
            return(TCL.TCL_OK);
        }
Ejemplo n.º 7
0
        /*
        ** Usage:   btree_begin_transaction ID
        **
        ** Start a new transaction
        */
        static int btree_begin_transaction(
            object NotUsed,
            Tcl_Interp interp, /* The TCL interpreter that invoked this command */
            int argc,          /* Number of arguments */
            TclObject[] argv   /* Text of each argument */
            )
        {
            Btree pBt;
            int   rc;

            if (argc != 2)
            {
                TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0].ToString(),
                                     " ID\"", null);
                return(TCL.TCL_ERROR);
            }
            pBt = (Btree)sqlite3TestTextToPtr(interp, argv[1].ToString());
            sqlite3BtreeEnter(pBt);
            rc = sqlite3BtreeBeginTrans(pBt, 1);
            sqlite3BtreeLeave(pBt);
            if (rc != SQLITE_OK)
            {
                TCL.Tcl_AppendResult(interp, errorName(rc), null);
                ;
                return(TCL.TCL_ERROR);
            }
            return(TCL.TCL_OK);
        }
Ejemplo n.º 8
0
        /*
        ** Usage:   btree_cursor ID TABLENUM WRITEABLE
        **
        ** Create a new cursor.  Return the ID for the cursor.
        */
        static int btree_cursor(
            object NotUsed,
            Tcl_Interp interp, /* The TCL interpreter that invoked this command */
            int argc,          /* Number of arguments */
            TclObject[] argv   /* Text of each argument */
            )
        {
            Btree         pBt;
            int           iTable = 0;
            BtCursor      pCur;
            int           rc     = SQLITE_OK;
            int           wrFlag = 0;
            StringBuilder zBuf   = new StringBuilder(30);

            if (argc != 4)
            {
                TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0].ToString(),
                                     " ID TABLENUM WRITEABLE\"");
                return(TCL.TCL_ERROR);
            }
            pBt = (Btree)sqlite3TestTextToPtr(interp, argv[1].ToString());
            if (TCL.Tcl_GetInt(interp, argv[2], out iTable))
            {
                return(TCL.TCL_ERROR);
            }
            if (TCL.Tcl_GetBoolean(interp, argv[3], out wrFlag))
            {
                return(TCL.TCL_ERROR);
            }
            //pCur = (BtCursor )ckalloc(sqlite3BtreeCursorSize());
            pCur = new BtCursor();// memset( pCur, 0, sqlite3BtreeCursorSize() );
            sqlite3BtreeEnter(pBt);
#if !SQLITE_OMIT_SHARED_CACHE
            rc = sqlite3BtreeLockTable(pBt, iTable, wrFlag);
#endif
            if (rc == SQLITE_OK)
            {
                rc = sqlite3BtreeCursor(pBt, iTable, wrFlag, null, pCur);
            }
            sqlite3BtreeLeave(pBt);
            if (rc != 0)
            {
                pCur = null;// ckfree( pCur );
                TCL.Tcl_AppendResult(interp, errorName(rc), null);
                ;
                return(TCL.TCL_ERROR);
            }
            sqlite3_snprintf(30, zBuf, "->%p", pCur);
            if (TCL.Tcl_CreateCommandPointer(interp, zBuf, pCur))
            {
                return(TCL.TCL_ERROR);
            }
            else
            {
                TCL.Tcl_AppendResult(interp, zBuf);
            }
            return(SQLITE_OK);
        }
Ejemplo n.º 9
0
        /*
        ** Usage:   hexio_read  FILENAME  OFFSET  AMT
        **
        ** Read AMT bytes from file FILENAME beginning at OFFSET from the
        ** beginning of the file.  Convert that information to hexadecimal
        ** and return the resulting HEX string.
        */
        static int hexio_read(
            object clientdata,
            Tcl_Interp interp,
            int objc,
            Tcl_Obj[] objv
            )
        {
            int    offset = 0;
            int    amt = 0, got;
            string zFile;

            byte[]     zBuf;
            FileStream _in;

            if (objc != 4)
            {
                TCL.Tcl_WrongNumArgs(interp, 1, objv, "FILENAME OFFSET AMT");
                return(TCL.TCL_ERROR);
            }
            if (TCL.Tcl_GetIntFromObj(interp, objv[2], ref offset))
            {
                return(TCL.TCL_ERROR);
            }
            if (TCL.Tcl_GetIntFromObj(interp, objv[3], ref amt))
            {
                return(TCL.TCL_ERROR);
            }
            zFile = TCL.Tcl_GetString(objv[1]);
            zBuf  = new byte[amt * 2 + 1];// sqlite3Malloc( amt * 2 + 1 );
            if (zBuf == null)
            {
                return(TCL.TCL_ERROR);
            }
            _in = new FileStream(zFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            //if( _in==null){
            //  _in = fopen(zFile, "r");
            //}
            if (_in == null)
            {
                TCL.Tcl_AppendResult(interp, "cannot open input file ", zFile);
                return(TCL.TCL_ERROR);
            }
            _in.Seek(offset, SeekOrigin.Begin); //fseek(_in, offset, SEEK_SET);
            got = _in.Read(zBuf, 0, amt);       // got = fread( zBuf, 1, amt, _in );
            _in.Flush();
            _in.Close();                        // fclose( _in );
            if (got < 0)
            {
                got = 0;
            }
            sqlite3TestBinToHex(zBuf, got);
            TCL.Tcl_AppendResult(interp, System.Text.Encoding.UTF8.GetString(zBuf).Substring(0, got * 2));
            zBuf = null;// sqlite3DbFree( db, ref zBuf );
            return(TCL.TCL_OK);
        }
Ejemplo n.º 10
0
        /*
        ** Usage:   hexio_write  FILENAME  OFFSET  DATA
        **
        ** Write DATA into file FILENAME beginning at OFFSET from the
        ** beginning of the file.  DATA is expressed _in hexadecimal.
        */
        static int hexio_write(
            object clientdata,
            Tcl_Interp interp,
            int objc,
            Tcl_Obj[] objv
            )
        {
            int    offset = 0;
            int    nIn = 0, nOut, written;
            string zFile;
            string zIn;

            byte[]     aOut;
            FileStream _out;

            if (objc != 4)
            {
                TCL.Tcl_WrongNumArgs(interp, 1, objv, "FILENAME OFFSET HEXDATA");
                return(TCL.TCL_ERROR);
            }
            if (TCL.Tcl_GetIntFromObj(interp, objv[2], ref offset))
            {
                return(TCL.TCL_ERROR);
            }
            zFile = TCL.Tcl_GetString(objv[1]);
            zIn   = TCL.Tcl_GetStringFromObj(objv[3], ref nIn);
            aOut  = new byte[nIn / 2 + 1];//sqlite3Malloc( nIn/2 );
            if (aOut == null)
            {
                return(TCL.TCL_ERROR);
            }
            nOut = sqlite3TestHexToBin(zIn, nIn, aOut);
            _out = new FileStream(zFile, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);// fopen( zFile, "r+b" );
            //if( _out==0 ){
            //  _out = fopen(zFile, "r+");
            //}
            if (_out == null)
            {
                TCL.Tcl_AppendResult(interp, "cannot open output file ", zFile);
                return(TCL.TCL_ERROR);
            }
            _out.Seek(offset, SeekOrigin.Begin); // fseek( _out, offset, SEEK_SET );
            written = (int)_out.Position;
            _out.Write(aOut, 0, nOut);           // written = fwrite( aOut, 1, nOut, _out );
            written = (int)_out.Position - written;
            aOut    = null;                      // sqlite3DbFree( db, ref aOut );
            _out.Flush();
            _out.Close();                        // fclose( _out );
            TCL.Tcl_SetObjResult(interp, TCL.Tcl_NewIntObj(written));
            return(TCL.TCL_OK);
        }
Ejemplo n.º 11
0
        /*
        ** Usage:   btree_open FILENAME NCACHE FLAGS
        **
        ** Open a new database
        */
        static int btree_open(
            object NotUsed,
            Tcl_Interp interp, /* The TCL interpreter that invoked this command */
            int argc,          /* Number of arguments */
            TclObject[] argv   /* Text of each argument */
            )
        {
            Btree  pBt = null;
            int    rc; int nCache = 0; int flags = 0;
            string zBuf = "";

            if (argc != 4)
            {
                TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0].ToString(),
                                     " FILENAME NCACHE FLAGS\"", "");
                return(TCL.TCL_ERROR);
            }
            if (TCL.Tcl_GetInt(interp, argv[2], ref nCache))
            {
                return(TCL.TCL_ERROR);
            }
            if (TCL.Tcl_GetInt(interp, argv[3], ref flags))
            {
                return(TCL.TCL_ERROR);
            }
            nRefSqlite3++;
            if (nRefSqlite3 == 1)
            {
                sDb.pVfs  = sqlite3_vfs_find(null);
                sDb.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
                sqlite3_mutex_enter(sDb.mutex);
            }
            rc = sqlite3BtreeOpen(argv[1].ToString(), sDb, ref pBt, flags,
                                  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB);
            if (rc != SQLITE_OK)
            {
                TCL.Tcl_AppendResult(interp, errorName(rc), null);
                return(TCL.TCL_ERROR);
            }
            sqlite3BtreeSetCacheSize(pBt, nCache);
            sqlite3_snprintf(100, ref zBuf, "->%p", pBt);
            if (TCL.Tcl_CreateCommandPointer(interp, zBuf, pBt))
            {
                return(TCL.TCL_ERROR);
            }
            else
            {
                TCL.Tcl_AppendResult(interp, zBuf, null);
            }
            return(TCL.TCL_OK);
        }
Ejemplo n.º 12
0
        /*
        ** Create and free a mutex.  Return the mutex pointer.  The pointer
        ** will be invalid since the mutex has already been freed.  The
        ** return pointer just checks to see if the mutex really was allocated.
        */
        static int test_alloc_mutex(
            object clientdata,
            Tcl_Interp interp,
            int objc,
            Tcl_Obj[] objv)
        {
#if SQLITE_THREADSAFE
            sqlite3_mutex p    = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
            StringBuilder zBuf = new StringBuilder(100);
            sqlite3_mutex_free(p);
            sqlite3_snprintf(100, zBuf, "->%p", p);
            TCL.Tcl_AppendResult(interp, zBuf);
#endif
            return(TCL.TCL_OK);
        }
Ejemplo n.º 13
0
        /*
        ** Create and free a mutex.  Return the mutex pointer.  The pointer
        ** will be invalid since the mutex has already been freed.  The
        ** return pointer just checks to see if the mutex really was allocated.
        */
        static int test_alloc_mutex(
            object clientdata,
            Tcl_Interp interp,
            int objc,
            Tcl_Obj[] objv)
        {
#if SQLITE_THREADSAFE
            sqlite3_mutex p    = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
            string        zBuf = "";//[100];
            sqlite3_mutex_free(ref p);
            sqlite3_snprintf(100, ref zBuf, "%p", p);
            TCL.Tcl_AppendResult(interp, zBuf);
#endif
            return(TCL.TCL_OK);
        }
Ejemplo n.º 14
0
        /*
        ** Usage:   btree_pager_stats ID
        **
        ** Returns pager statistics
        */
        static int btree_pager_stats(
            object NotUsed,
            Tcl_Interp interp, /* The TCL interpreter that invoked this command */
            int argc,          /* Number of arguments */
            TclObject[] argv   /* Text of each argument */
            )
        {
            Btree pBt;
            int   i;

            int[] a;

            if (argc != 2)
            {
                TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0].ToString(),
                                     " ID\"");
                return(TCL.TCL_ERROR);
            }
            pBt = (Btree)sqlite3TestTextToPtr(interp, argv[1].ToString());

            /* Normally in this file, with a b-tree handle opened using the
            ** [btree_open] command it is safe to call sqlite3BtreeEnter() directly.
            ** But this function is sometimes called with a btree handle obtained
            ** from an open SQLite connection (using [btree_from_db]). In this case
            ** we need to obtain the mutex for the controlling SQLite handle before
            ** it is safe to call sqlite3BtreeEnter().
            */
            sqlite3_mutex_enter(pBt.db.mutex);

            sqlite3BtreeEnter(pBt);
            a = sqlite3PagerStats(sqlite3BtreePager(pBt));
            for (i = 0; i < 11; i++)
            {
                string[] zName = new string[] {
                    "ref", "page", "max", "size", "state", "err",
                    "hit", "miss", "ovfl", "read", "write"
                };
                string zBuf = "";//char zBuf[100];
                TCL.Tcl_AppendElement(interp, zName[i]);
                sqlite3_snprintf(100, ref zBuf, "%d", a[i]);
                TCL.Tcl_AppendElement(interp, zBuf);
            }
            sqlite3BtreeLeave(pBt);
            /* Release the mutex on the SQLite handle that controls this b-tree */
            sqlite3_mutex_leave(pBt.db.mutex);
            return(TCL.TCL_OK);
        }
Ejemplo n.º 15
0
        /*
        ** sqlite3BitvecBuiltinTest SIZE PROGRAM
        **
        ** Invoke the SQLITE_TESTCTRL_BITVEC_TEST operator on test_control.
        ** See comments on sqlite3BitvecBuiltinTest() for additional information.
        */
        static int testBitvecBuiltinTest(
            object NotUsed,
            Tcl_Interp interp, /* The TCL interpreter that invoked this command */
            int argc,          /* Number of arguments */
            TclObject[] argv   /* Text of each argument */
            )
        {
            int sz = 0, rc;
            int nProg = 0;

            int[]  aProg = new int[100];
            string z;

            if (argc != 3)
            {
                TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
                                     " SIZE PROGRAM\"");
            }
            if (TCL.Tcl_GetInt(interp, argv[1], out sz))
            {
                return(TCL.TCL_ERROR);
            }
            z = argv[2].ToString() + '\0';
            int iz = 0;

            while (nProg < 99 && z[iz] != 0)
            {
                while (z[iz] != 0 && !sqlite3Isdigit(z[iz]))
                {
                    iz++;
                }
                if (z[iz] == 0)
                {
                    break;
                }
                while (sqlite3Isdigit(z[iz]))
                {
                    aProg[nProg] = aProg[nProg] * 10 + (z[iz] - 48);
                    iz++;
                }
                nProg++;
            }
            aProg[nProg] = 0;
            rc           = sqlite3_test_control(SQLITE_TESTCTRL_BITVEC_TEST, sz, aProg);
            TCL.Tcl_SetObjResult(interp, TCL.Tcl_NewIntObj(rc));
            return(TCL.TCL_OK);
        }
Ejemplo n.º 16
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);
        }
Ejemplo n.º 17
0
        /*
        ** 2007 March 29
        **
        ** The author disclaims copyright to this source code.  In place of
        ** a legal notice, here is a blessing:
        **
        **    May you do good and not evil.
        **    May you find forgiveness for yourself and forgive others.
        **    May you share freely, never taking more than you give.
        **
        *************************************************************************
        **
        ** This file contains obscure tests of the C-interface required
        ** for completeness. Test code is written in C for these cases
        ** as there is not much point in binding to Tcl.
        *************************************************************************
        **  Included in SQLite3 port to C#-SQLite;  2008 Noah B Hart
        **  C#-SQLite is an independent reimplementation of the SQLite software library
        **
        **  SQLITE_SOURCE_ID: 2010-08-23 18:52:01 42537b60566f288167f1b5864a5435986838e3a3
        **
        *************************************************************************
        */
        //#include "sqliteInt.h"
        //#include "tcl.h"
        //#include <stdlib.h>
        //#include <string.h>

        /*
        ** c_collation_test
        */
        static int c_collation_test(
            object clientdata, /* Pointer to sqlite3_enable_XXX function */
            Tcl_Interp interp, /* The TCL interpreter that invoked this command */
            int objc,          /* Number of arguments */
            Tcl_Obj[] objv     /* Command arguments */
            )
        {
            string  zErrFunction = "N/A";
            sqlite3 db           = null;

            int rc;

            if (objc != 1)
            {
                TCL.Tcl_WrongNumArgs(interp, 1, objv, "");
                return(TCL.TCL_ERROR);
            }

            /* Open a database. */
            rc = sqlite3_open(":memory:", out db);
            if (rc != SQLITE_OK)
            {
                zErrFunction = "sqlite3_open";
                goto error_out;
            }

            rc = sqlite3_create_collation(db, "collate", 456, null, null);
            if (rc != SQLITE_MISUSE)
            {
                sqlite3_close(db);
                zErrFunction = "sqlite3_create_collation";
                goto error_out;
            }

            sqlite3_close(db);
            return(TCL.TCL_OK);

error_out:
            TCL.Tcl_ResetResult(interp);
            TCL.Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, null);
            return(TCL.TCL_ERROR);
        }
Ejemplo n.º 18
0
        /*
        ** A TCL command for md5.  The argument is the text to be hashed.  The
        ** Result is the hash in base64.
        */
        static int md5_cmd(object cd, Tcl_Interp interp, int argc, Tcl_Obj[] argv)
        {
            MD5Context ctx = new MD5Context();

            byte[] digest = new byte[16];
            byte[] zBuf   = new byte[32];


            if (argc != 2)
            {
                TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
                                     " TEXT\"");
                return(TCL.TCL_ERROR);
            }
            MD5Init(ctx);
            MD5Update(ctx, Encoding.UTF8.GetBytes(argv[1].ToString()), Encoding.UTF8.GetByteCount(argv[1].ToString()));
            MD5Final(digest, ctx);
            DigestToBase16(digest, zBuf);
            TCL.Tcl_AppendResult(interp, Encoding.UTF8.GetString(zBuf));
            return(TCL.TCL_OK);
        }
Ejemplo n.º 19
0
        /*
        **    sqlite3_intarray_bind  INTARRAY  ?VALUE ...?
        **
        ** Invoke the sqlite3_intarray_bind interface on the given array of integers.
        */
        static int test_intarray_bind(
            ClientData clientData, /* Not used */
            Tcl_Interp interp,     /* The TCL interpreter that invoked this command */
            int objc,              /* Number of arguments */
            Tcl_Obj[] objv         /* Command arguments */
            )
        {
            sqlite3_intarray pArray;
            int rc = SQLITE_OK;
            int i, n;

            sqlite3_int64[] a;

            if (objc < 2)
            {
                TCL.Tcl_WrongNumArgs(interp, 1, objv, "INTARRAY");
                return(TCL.TCL_ERROR);
            }
            pArray = (sqlite3_intarray)sqlite3TestTextToPtr(interp, TCL.Tcl_GetString(objv[1]));
            n      = objc - 2;
#if !SQLITE_OMIT_VIRTUALTABLE
            a = new sqlite3_int64[n];//sqlite3_malloc( sizeof(a[0])*n );
            //if( a==0 ){
            //  Tcl_AppendResult(interp, "SQLITE_NOMEM", (char*)0);
            //  return TCL_ERROR;
            //}
            for (i = 0; i < n; i++)
            {
                //a[i] = 0;
                TCL.Tcl_GetWideIntFromObj(null, objv[i + 2], out a[i]);
            }
            rc = sqlite3_intarray_bind(pArray, n, a, sqlite3_free);
            if (rc != SQLITE_OK)
            {
                TCL.Tcl_AppendResult(interp, sqlite3TestErrorName(rc), null);
                return(TCL.TCL_ERROR);
            }
#endif
            return(TCL.TCL_OK);
        }
Ejemplo n.º 20
0
        /*
        ** c_realloc_test
        */
        static int c_realloc_test(
            object clientdata, /* Pointer to sqlite3_enable_XXX function */
            Tcl_Interp interp, /* The TCL interpreter that invoked this command */
            int objc,          /* Number of arguments */
            Tcl_Obj[] objv     /* Command arguments */
            )
        {
            object p;
            string zErrFunction = "N/A";

            if (objc != 1)
            {
                TCL.Tcl_WrongNumArgs(interp, 1, objv, "");
                return(TCL.TCL_ERROR);
            }

            p = sqlite3Malloc(5);
            if (p == null)
            {
                zErrFunction = "sqlite3Malloc";
                goto error_out;
            }

            /* Test that realloc()ing a block of memory to a negative size is
            ** the same as free()ing that memory.
            */
            //TODO -- ignore realloc
            //p = sqlite3_realloc(p, -1);
            //if( p!=null ){
            //  zErrFunction = "sqlite3_realloc";
            //  goto error_out;
            //}

            return(TCL.TCL_OK);

error_out:
            TCL.Tcl_ResetResult(interp);
            TCL.Tcl_AppendResult(interp, "Error testing function: ", zErrFunction);
            return(TCL.TCL_ERROR);
        }
Ejemplo n.º 21
0
        /*
        ** 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(
        //  object NotUsed,
        // Tcl_Interp interp,    /* The TCL interpreter that invoked this command */
        //  int argc,              /* Number of arguments */
        //  TclObject[] argv      /* Text of each argument */
        //){
        //  BtCursor pCur;
        //  int rc;
        //  char zBuf[50];

        //  if( argc!=2 ){
        //   TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0].ToString(),
        //       " ID\"");
        //    return TCL.TCL_ERROR;
        //  }
        //  pCur = (BtCursor)sqlite3TestTextToPtr(interp,argv[1].ToString());
        //  sqlite3BtreeEnter(pCur.pBtree);
        //  rc = sqlite3BtreeEof(pCur);
        //  sqlite3BtreeLeave(pCur.pBtree);
        //  sqlite3_snprintf(100, ref zBuf, "%d", rc);
        // TCL.Tcl_AppendResult(interp, zBuf);
        //  return SQLITE_OK;
        //}

        /*
        ** Usage:   btree_payload_size ID
        **
        ** Return the number of bytes of payload
        */
        static int btree_payload_size(
            object NotUsed,
            Tcl_Interp interp, /* The TCL interpreter that invoked this command */
            int argc,          /* Number of arguments */
            TclObject[] argv   /* Text of each argument */
            )
        {
            BtCursor pCur;
            i64      n1   = 0;
            u32      n2   = 0;
            string   zBuf = "";//[50];

            if (argc != 2)
            {
                TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0].ToString(),
                                     " ID\"");
                return(TCL.TCL_ERROR);
            }
            pCur = (BtCursor)sqlite3TestTextToPtr(interp, argv[1].ToString());
#if SQLITE_TEST
            sqlite3BtreeEnter(pCur.pBtree);
#endif

            /* The cursor may be in "require-seek" state. If this is the case, the
            ** call to BtreeDataSize() will fix it. */
            sqlite3BtreeDataSize(pCur, ref n2);
            if (pCur.apPage[pCur.iPage].intKey != 0)
            {
                n1 = 0;
            }
            else
            {
                sqlite3BtreeKeySize(pCur, ref n1);
            }
            sqlite3BtreeLeave(pCur.pBtree);
            sqlite3_snprintf(30, ref zBuf, "%d", (int)(n1 + n2));
            TCL.Tcl_AppendResult(interp, zBuf);
            return(SQLITE_OK);
        }
Ejemplo n.º 22
0
        /*
        **     sqlite3_backup CMDNAME DESTHANDLE DESTNAME SRCHANDLE SRCNAME
        **
        */
        static int backupTestInit(
            ClientData clientData,
            Tcl_Interp interp,
            int objc,
            Tcl_Obj[] objv
            )
        {
            sqlite3_backup pBackup;
            sqlite3        pDestDb = null;
            sqlite3        pSrcDb  = null;
            string         zDestName;
            string         zSrcName;
            string         zCmd;

            if (objc != 6)
            {
                TCL.Tcl_WrongNumArgs(
                    interp, 1, objv, "CMDNAME DESTHANDLE DESTNAME SRCHANDLE SRCNAME"
                    );
                return(TCL.TCL_ERROR);
            }

            zCmd = TCL.Tcl_GetString(objv[1]);
            getDbPointer(interp, TCL.Tcl_GetString(objv[2]), ref pDestDb);
            zDestName = TCL.Tcl_GetString(objv[3]);
            getDbPointer(interp, TCL.Tcl_GetString(objv[4]), ref pSrcDb);
            zSrcName = TCL.Tcl_GetString(objv[5]);

            pBackup = sqlite3_backup_init(pDestDb, zDestName, pSrcDb, zSrcName);
            if (null == pBackup)
            {
                TCL.Tcl_AppendResult(interp, "sqlite3_backup_init() failed");
                return(TCL.TCL_ERROR);
            }

            TCL.Tcl_CreateObjCommand(interp, zCmd, (Interp.dxObjCmdProc)backupTestCmd, pBackup, (Interp.dxCmdDeleteProc)backupTestFinish);
            TCL.Tcl_SetObjResult(interp, objv[1]);
            return(TCL.TCL_OK);
        }
Ejemplo n.º 23
0
        /*
        ** Usage:   pager_open FILENAME N-PAGE
        **
        ** Open a new pager
        */
        //static int pager_open(
        //  object NotUsed,
        //  Tcl_Interp interp,    /* The TCL interpreter that invoked this command */
        //  int argc,              /* Number of arguments */
        //  TclObject[] argv      /* Text of each argument */
        //){
        //  u16 pageSize;
        //  Pager *pPager;
        //  Pgno nPage;
        //  int rc;
        //  char zBuf[100];
        //  if( argc!=3 ){
        //    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
        //       " FILENAME N-PAGE\"", 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  if( Tcl_GetInt(interp, argv[2], nPage) ) return TCL.TCL_ERROR;
        //  rc = sqlite3PagerOpen(sqlite3_vfs_find(0), pPager, argv[1], 0, 0,
        //      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB,
        // pager_test_reiniter);
        //  if( rc!=SQLITE_OK ){
        //    Tcl_AppendResult(interp, errorName(rc), 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  sqlite3PagerSetCachesize(pPager, nPage);
        //  pageSize = test_pagesize;
        //  sqlite3PagerSetPagesize(pPager, pageSize,-1);
        //  sqlite3_snprintf(100, ref zBuf,"%p",pPager);
        //  Tcl_AppendResult(interp, zBuf);
        //  return TCL.TCL_OK;
        //}

        /*
        ** Usage:   pager_close ID
        **
        ** Close the given pager.
        */
        //static int pager_close(
        //  object NotUsed,
        //  Tcl_Interp interp,    /* The TCL interpreter that invoked this command */
        //  int argc,              /* Number of arguments */
        //  TclObject[] argv      /* Text of each argument */
        //){
        //  Pager *pPager;
        //  int rc;
        //  if( argc!=2 ){
        //    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
        //       " ID\"", 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  pPager = sqlite3TestTextToPtr(interp,argv[1].ToString());
        //  rc = sqlite3PagerClose(pPager);
        //  if( rc!=SQLITE_OK ){
        //    Tcl_AppendResult(interp, errorName(rc), 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  return TCL.TCL_OK;
        //}

        /*
        ** Usage:   pager_rollback ID
        **
        ** Rollback changes
        */
        //static int pager_rollback(
        //  object NotUsed,
        //  Tcl_Interp interp,    /* The TCL interpreter that invoked this command */
        //  int argc,              /* Number of arguments */
        //  TclObject[] argv      /* Text of each argument */
        //){
        //  Pager *pPager;
        //  int rc;
        //  if( argc!=2 ){
        //    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
        //       " ID\"", 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  pPager = sqlite3TestTextToPtr(interp,argv[1].ToString());
        //  rc = sqlite3PagerRollback(pPager);
        //  if( rc!=SQLITE_OK ){
        //    Tcl_AppendResult(interp, errorName(rc), 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  return TCL.TCL_OK;
        //}

        /*
        ** Usage:   pager_commit ID
        **
        ** Commit all changes
        */
        //static int pager_commit(
        //  object NotUsed,
        //  Tcl_Interp interp,    /* The TCL interpreter that invoked this command */
        //  int argc,              /* Number of arguments */
        //  TclObject[] argv      /* Text of each argument */
        //){
        //  Pager *pPager;
        //  int rc;
        //  if( argc!=2 ){
        //    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
        //       " ID\"", 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  pPager = sqlite3TestTextToPtr(interp,argv[1].ToString());
        //  rc = sqlite3PagerCommitPhaseOne(pPager,  0, 0);
        //  if( rc!=SQLITE_OK ){
        //    Tcl_AppendResult(interp, errorName(rc), 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  rc = sqlite3PagerCommitPhaseTwo(pPager);
        //  if( rc!=SQLITE_OK ){
        //    Tcl_AppendResult(interp, errorName(rc), 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  return TCL.TCL_OK;
        //}

        /*
        ** Usage:   pager_stmt_begin ID
        **
        ** Start a new checkpoint.
        */
        //static int pager_stmt_begin(
        //  object NotUsed,
        //  Tcl_Interp interp,    /* The TCL interpreter that invoked this command */
        //  int argc,              /* Number of arguments */
        //  TclObject[] argv      /* Text of each argument */
        //){
        //  Pager *pPager;
        //  int rc;
        //  if( argc!=2 ){
        //    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
        //       " ID\"", 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  pPager = sqlite3TestTextToPtr(interp,argv[1].ToString());
        //  rc = sqlite3PagerOpenSavepoint(pPager, 1);
        //  if( rc!=SQLITE_OK ){
        //    Tcl_AppendResult(interp, errorName(rc), 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  return TCL.TCL_OK;
        //}

        /*
        ** Usage:   pager_stmt_rollback ID
        **
        ** Rollback changes to a checkpoint
        */
        //static int pager_stmt_rollback(
        //  object NotUsed,
        //  Tcl_Interp interp,    /* The TCL interpreter that invoked this command */
        //  int argc,              /* Number of arguments */
        //  TclObject[] argv      /* Text of each argument */
        //){
        //  Pager *pPager;
        //  int rc;
        //  if( argc!=2 ){
        //    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
        //       " ID\"", 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  pPager = sqlite3TestTextToPtr(interp,argv[1].ToString());
        //rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, 0);
        //sqlite3PagerSavepoint(pPager, SAVEPOINT_RELEASE, 0);
        //  if( rc!=SQLITE_OK ){
        //    Tcl_AppendResult(interp, errorName(rc), 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  return TCL.TCL_OK;
        //}

        /*
        ** Usage:   pager_stmt_commit ID
        **
        ** Commit changes to a checkpoint
        */
        //static int pager_stmt_commit(
        //  object NotUsed,
        //  Tcl_Interp interp,    /* The TCL interpreter that invoked this command */
        //  int argc,              /* Number of arguments */
        //  TclObject[] argv      /* Text of each argument */
        //){
        //  Pager *pPager;
        //  int rc;
        //  if( argc!=2 ){
        //    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
        //       " ID\"", 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  pPager = sqlite3TestTextToPtr(interp,argv[1].ToString());
        //  rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_RELEASE, 0);
        //  if( rc!=SQLITE_OK ){
        //    Tcl_AppendResult(interp, errorName(rc), 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  return TCL.TCL_OK;
        //}

        /*
        ** Usage:   pager_stats ID
        **
        ** Return pager statistics.
        */
        //static int pager_stats(
        //  object NotUsed,
        //  Tcl_Interp interp,    /* The TCL interpreter that invoked this command */
        //  int argc,              /* Number of arguments */
        //  TclObject[] argv      /* Text of each argument */
        //){
        //  Pager *pPager;
        //  int i, *a;
        //  if( argc!=2 ){
        //    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
        //       " ID\"", 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  pPager = sqlite3TestTextToPtr(interp,argv[1].ToString());
        //  a = sqlite3PagerStats(pPager);
        //  for(i=0; i<9; i++){
        //    static char *zName[] = {
        //      "ref", "page", "max", "size", "state", "err",
        //      "hit", "miss", "ovfl",
        //    };
        //    char zBuf[100];
        //    Tcl_AppendElement(interp, zName[i]);
        //    sqlite3_snprintf(100, ref zBuf,"%d",a[i]);
        //    Tcl_AppendElement(interp, zBuf);
        //  }
        //  return TCL.TCL_OK;
        //}

        /*
        ** Usage:   pager_pagecount ID
        **
        ** Return the size of the database file.
        */
        //static int pager_pagecount(
        //  object NotUsed,
        //  Tcl_Interp interp,    /* The TCL interpreter that invoked this command */
        //  int argc,              /* Number of arguments */
        //  TclObject[] argv      /* Text of each argument */
        //){
        //  Pager *pPager;
        //  char zBuf[100];
        //  Pgno nPage;
        //  if( argc!=2 ){
        //    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
        //       " ID\"", 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  pPager = sqlite3TestTextToPtr(interp,argv[1].ToString());
        //  sqlite3PagerPagecount(pPager, nPage);
        //  sqlite3_snprintf(100, ref zBuf, "%d", nPage);
        //  Tcl_AppendResult(interp, zBuf);
        //  return TCL.TCL_OK;
        //}

        /*
        ** Usage:   page_get ID PGNO
        **
        ** Return a pointer to a page from the database.
        */
        //static int page_get(
        //  object NotUsed,
        //  Tcl_Interp interp,    /* The TCL interpreter that invoked this command */
        //  int argc,              /* Number of arguments */
        //  TclObject[] argv      /* Text of each argument */
        //){
        //  Pager *pPager;
        //  char zBuf[100];
        //  DbPage *pPage;
        //  int pgno;
        //  int rc;
        //  if( argc!=3 ){
        //    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
        //       " ID PGNO\"", 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  pPager = sqlite3TestTextToPtr(interp,argv[1].ToString());
        //  if( Tcl_GetInt(interp, argv[2], pgno) ) return TCL.TCL_ERROR;
        //rc = sqlite3PagerSharedLock(pPager);
        //if( rc==SQLITE_OK ){
        //  rc = sqlite3PagerGet(pPager, pgno, &pPage);
        //}
        //  if( rc!=SQLITE_OK ){
        //    Tcl_AppendResult(interp, errorName(rc), 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  sqlite3_snprintf(100, ref zBuf,"%p",pPage);
        //  Tcl_AppendResult(interp, zBuf);
        //  return TCL.TCL_OK;
        //}

        /*
        ** Usage:   page_lookup ID PGNO
        **
        ** Return a pointer to a page if the page is already in cache.
        ** If not in cache, return an empty string.
        */
        //static int page_lookup(
        //  object NotUsed,
        //  Tcl_Interp interp,    /* The TCL interpreter that invoked this command */
        //  int argc,              /* Number of arguments */
        //  TclObject[] argv      /* Text of each argument */
        //){
        //  Pager *pPager;
        //  char zBuf[100];
        //  DbPage *pPage;
        //  int pgno;
        //  if( argc!=3 ){
        //    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
        //       " ID PGNO\"", 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  pPager = sqlite3TestTextToPtr(interp,argv[1].ToString());
        //  if( Tcl_GetInt(interp, argv[2], pgno) ) return TCL.TCL_ERROR;
        //  pPage = sqlite3PagerLookup(pPager, pgno);
        //  if( pPage ){
        //    sqlite3_snprintf(100, ref zBuf,"%p",pPage);
        //    Tcl_AppendResult(interp, zBuf);
        //  }
        //  return TCL.TCL_OK;
        //}

        /*
        ** Usage:   pager_truncate ID PGNO
        */
        //static int pager_truncate(
        //  object NotUsed,
        //  Tcl_Interp interp,    /* The TCL interpreter that invoked this command */
        //  int argc,              /* Number of arguments */
        //  TclObject[] argv      /* Text of each argument */
        //){
        //  Pager *pPager;
        //  int pgno;
        //  if( argc!=3 ){
        //    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
        //       " ID PGNO\"", 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  pPager = sqlite3TestTextToPtr(interp,argv[1].ToString());
        //  if( Tcl_GetInt(interp, argv[2], pgno) ) return TCL.TCL_ERROR;
        //  sqlite3PagerTruncateImage(pPager, pgno);
        //  return TCL.TCL_OK;
        //}


        /*
        ** Usage:   page_unref PAGE
        **
        ** Drop a pointer to a page.
        */
        //static int page_unref(
        //  object NotUsed,
        //  Tcl_Interp interp,    /* The TCL interpreter that invoked this command */
        //  int argc,              /* Number of arguments */
        //  TclObject[] argv      /* Text of each argument */
        //){
        //  DbPage *pPage;
        //  if( argc!=2 ){
        //    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
        //       " PAGE\"", 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  pPage = (DbPage *)sqlite3TestTextToPtr(interp,argv[1].ToString());
        //  sqlite3PagerUnref(pPage);
        //  return TCL.TCL_OK;
        //}

        /*
        ** Usage:   page_read PAGE
        **
        ** Return the content of a page
        */
        //static int page_read(
        //  object NotUsed,
        //  Tcl_Interp interp,    /* The TCL interpreter that invoked this command */
        //  int argc,              /* Number of arguments */
        //  TclObject[] argv      /* Text of each argument */
        //){
        //  char zBuf[100];
        //  DbPage *pPage;
        //  if( argc!=2 ){
        //    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
        //       " PAGE\"", 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  pPage = sqlite3TestTextToPtr(interp,argv[1].ToString());
        //  memcpy(zBuf, sqlite3PagerGetData(pPage), sizeof(zBuf));
        //  Tcl_AppendResult(interp, zBuf);
        //  return TCL.TCL_OK;
        //}

        /*
        ** Usage:   page_number PAGE
        **
        ** Return the page number for a page.
        */
        //static int page_number(
        //  object NotUsed,
        //  Tcl_Interp interp,    /* The TCL interpreter that invoked this command */
        //  int argc,              /* Number of arguments */
        //  TclObject[] argv      /* Text of each argument */
        //){
        //  char zBuf[100];
        //  DbPage *pPage;
        //  if( argc!=2 ){
        //    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
        //       " PAGE\"", 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  pPage = (DbPage *)sqlite3TestTextToPtr(interp,argv[1].ToString());
        //  sqlite3_snprintf(100, ref zBuf, "%d", sqlite3PagerPagenumber(pPage));
        //  Tcl_AppendResult(interp, zBuf);
        //  return TCL.TCL_OK;
        //}

        /*
        ** Usage:   page_write PAGE DATA
        **
        ** Write something into a page.
        */
        //static int page_write(
        //  object NotUsed,
        //  Tcl_Interp interp,    /* The TCL interpreter that invoked this command */
        //  int argc,              /* Number of arguments */
        //  TclObject[] argv      /* Text of each argument */
        //){
        //  DbPage *pPage;
        //  char *pData;
        //  int rc;
        //  if( argc!=3 ){
        //    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
        //       " PAGE DATA\"", 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  pPage = (DbPage *)sqlite3TestTextToPtr(interp,argv[1].ToString());
        //  rc = sqlite3PagerWrite(pPage);
        //  if( rc!=SQLITE_OK ){
        //    Tcl_AppendResult(interp, errorName(rc), 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  pData = sqlite3PagerGetData(pPage);
        //  strncpy(pData, argv[2], test_pagesize-1);
        //  pData[test_pagesize-1] = 0;
        //  return TCL.TCL_OK;
        //}

#if !SQLITE_OMIT_DISKIO
        /*
        ** Usage:   fake_big_file  N  FILENAME
        **
        ** Write a few bytes at the N megabyte point of FILENAME.  This will
        ** create a large file.  If the file was a valid SQLite database, then
        ** the next time the database is opened, SQLite will begin allocating
        ** new pages after N.  If N is 2096 or bigger, this will test the
        ** ability of SQLite to write to large files.
        */
        //static int fake_big_file(
        //  object NotUsed,
        //  Tcl_Interp interp,    /* The TCL interpreter that invoked this command */
        //  int argc,              /* Number of arguments */
        //  TclObject[] argv      /* Text of each argument */
        //){
        //  sqlite3_vfs *pVfs;
        //  sqlite3_file *fd = 0;
        //  int rc;
        //  int n;
        //  i64 offset;
        //  if( argc!=3 ){
        //    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
        //       " N-MEGABYTES FILE\"", 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  if( Tcl_GetInt(interp, argv[1], n) ) return TCL.TCL_ERROR;

        //  pVfs = sqlite3_vfs_find(0);
        //  rc = sqlite3OsOpenMalloc(pVfs, argv[2], fd,
        //      (SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB), 0
        //  );
        //  if( rc !=0){
        //    Tcl_AppendResult(interp, "open failed: ", errorName(rc), 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  offset = n;
        //  offset *= 1024*1024;
        //  rc = sqlite3OsWrite(fd, "Hello, World!", 14, offset);
        //  sqlite3OsCloseFree(fd);
        //  if( rc !=0){
        //    Tcl_AppendResult(interp, "write failed: ", errorName(rc), 0);
        //    return TCL.TCL_ERROR;
        //  }
        //  return TCL.TCL_OK;
        //}
#endif


        /*
        ** test_control_pending_byte  PENDING_BYTE
        **
        ** Set the PENDING_BYTE using the sqlite3_test_control() interface.
        */
        static int testPendingByte(
            object NotUsed,
            Tcl_Interp interp, /* The TCL interpreter that invoked this command */
            int argc,          /* Number of arguments */
            TclObject[] argv   /* Text of each argument */
            )
        {
            int pbyte = 0;
            int rc;

            if (argc != 2)
            {
                TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
                                     " PENDING-BYTE\"");
            }
            if (TCL.Tcl_GetInt(interp, argv[1], ref pbyte))
            {
                return(TCL.TCL_ERROR);
            }
            rc = sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, pbyte);
            TCL.Tcl_SetObjResult(interp, TCL.Tcl_NewIntObj(rc));
            return(TCL.TCL_OK);
        }
Ejemplo n.º 24
0
        /*****************************************************************************
        ** Everything below is interface for testing this module.
        */
#if SQLITE_TEST
        //#include <tcl.h>

        /*
        ** Routines to encode and decode pointers
        */
        //extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
        //extern void *sqlite3TestTextToPtr(const char*);
        //extern int sqlite3TestMakePointerStr(Tcl_Interp*, char *zPtr, void*);
        //extern const char *sqlite3TestErrorName(int);

        /*
        **    sqlite3_intarray_create  DB  NAME
        **
        ** Invoke the sqlite3_intarray_create interface.  A string that becomes
        ** the first parameter to sqlite3_intarray_bind.
        */
        static int test_intarray_create(
            ClientData clientData, /* Not used */
            Tcl_Interp interp,     /* The TCL interpreter that invoked this command */
            int objc,              /* Number of arguments */
            Tcl_Obj[] objv         /* Command arguments */
            )
        {
            sqlite3          db;
            string           zName;
            sqlite3_intarray pArray;
            int           rc   = SQLITE_OK;
            StringBuilder zPtr = new StringBuilder(100);

            if (objc != 3)
            {
                TCL.Tcl_WrongNumArgs(interp, 1, objv, "DB");
                return(TCL.TCL_ERROR);
            }
            if (getDbPointer(interp, TCL.Tcl_GetString(objv[1]), out db) != 0)
            {
                return(TCL.TCL_ERROR);
            }
            zName = TCL.Tcl_GetString(objv[2]);
#if !SQLITE_OMIT_VIRTUALTABLE
            rc = sqlite3_intarray_create(db, zName, out pArray);
#endif
            if (rc != SQLITE_OK)
            {
                Debug.Assert(pArray == null);
                TCL.Tcl_AppendResult(interp, sqlite3TestErrorName(rc), null);
                return(TCL.TCL_ERROR);
            }
            sqlite3TestMakePointerStr(interp, zPtr, pArray);
            TCL.Tcl_AppendResult(interp, zPtr, null);
            return(TCL.TCL_OK);
        }
Ejemplo n.º 25
0
        /*
        ** tclcmd:  abuse_create_function
        **
        ** Make various calls to sqlite3_create_function that do not have valid
        ** parameters.  Verify that the error condition is detected and reported.
        */
        static int abuse_create_function(
            object clientdata,
            Tcl_Interp interp,
            int objc,
            Tcl_Obj[] objv
            )
        {
            //extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
            sqlite3 db = null;
            int     rc;
            int     mxArg;

            if (getDbPointer(interp, TCL.Tcl_GetString(objv[1]), ref db) != 0)
            {
                return(TCL.TCL_ERROR);
            }

            rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, tStep, tFinal);
            if (rc != SQLITE_MISUSE)
            {
                goto abuse_err;
            }

            rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, tStep, null);
            if (rc != SQLITE_MISUSE)
            {
                goto abuse_err;
            }

            rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, null, tFinal);
            if (rc != SQLITE_MISUSE)
            {
                goto abuse_err;
            }

            rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, null, null, tFinal);
            if (rc != SQLITE_MISUSE)
            {
                goto abuse_err;
            }

            rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, null, tStep, null);
            if (rc != SQLITE_MISUSE)
            {
                goto abuse_err;
            }

            rc = sqlite3_create_function(db, "tx", -2, SQLITE_UTF8, 0, tStep, null, null);
            if (rc != SQLITE_MISUSE)
            {
                goto abuse_err;
            }

            rc = sqlite3_create_function(db, "tx", 128, SQLITE_UTF8, 0, tStep, null, null);
            if (rc != SQLITE_MISUSE)
            {
                goto abuse_err;
            }

            rc = sqlite3_create_function(db, "funcxx" +
                                         "_123456789_123456789_123456789_123456789_123456789" +
                                         "_123456789_123456789_123456789_123456789_123456789" +
                                         "_123456789_123456789_123456789_123456789_123456789" +
                                         "_123456789_123456789_123456789_123456789_123456789" +
                                         "_123456789_123456789_123456789_123456789_123456789",
                                         1, SQLITE_UTF8, 0, tStep, null, null);
            if (rc != SQLITE_MISUSE)
            {
                goto abuse_err;
            }

            /* This last function registration should actually work.  Generate
            ** a no-op function (that always returns NULL) and which has the
            ** maximum-length function name and the maximum number of parameters.
            */
            sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, 10000);
            mxArg = sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, -1);
            rc    = sqlite3_create_function(db, "nullx" +
                                            "_123456789_123456789_123456789_123456789_123456789" +
                                            "_123456789_123456789_123456789_123456789_123456789" +
                                            "_123456789_123456789_123456789_123456789_123456789" +
                                            "_123456789_123456789_123456789_123456789_123456789" +
                                            "_123456789_123456789_123456789_123456789_123456789",
                                            mxArg, SQLITE_UTF8, 0, tStep, null, null);
            if (rc != SQLITE_OK)
            {
                goto abuse_err;
            }

            return(TCL.TCL_OK);

abuse_err:
            TCL.Tcl_AppendResult(interp, "sqlite3_create_function abused test failed"
                                 );
            return(TCL.TCL_ERROR);
        }
Ejemplo n.º 26
0
        /*
        ** install_mutex_counters BOOLEAN
        */
        static int test_install_mutex_counters(
            object clientdata,
            Tcl_Interp interp,
            int objc,
            Tcl_Obj[] objv
            )
        {
            int  rc        = SQLITE_OK;
            bool isInstall = false;

            sqlite3_mutex_methods counter_methods = new sqlite3_mutex_methods(
                (dxMutexInit)counterMutexInit,
                (dxMutexEnd)counterMutexEnd,
                (dxMutexAlloc)counterMutexAlloc,
                (dxMutexFree)counterMutexFree,
                (dxMutexEnter)counterMutexEnter,
                (dxMutexTry)counterMutexTry,
                (dxMutexLeave)counterMutexLeave,
                (dxMutexHeld)counterMutexHeld,
                (dxMutexNotheld)counterMutexNotheld
                );

            if (objc != 2)
            {
                TCL.Tcl_WrongNumArgs(interp, 1, objv, "BOOLEAN");
                return(TCL.TCL_ERROR);
            }
            if (TCL.Tcl_GetBoolean(interp, objv[1], out isInstall))
            {
                return(TCL.TCL_ERROR);
            }

            Debug.Assert(isInstall == false || isInstall == true);
            Debug.Assert(g.isInstalled == false || g.isInstalled == true);
            if (isInstall == g.isInstalled)
            {
                TCL.Tcl_AppendResult(interp, "mutex counters are ");
                TCL.Tcl_AppendResult(interp, isInstall ? "already installed" : "not installed");
                return(TCL.TCL_ERROR);
            }

            if (isInstall)
            {
                Debug.Assert(g.m.xMutexAlloc == null);
                rc = sqlite3_config(SQLITE_CONFIG_GETMUTEX, ref g.m);
                if (rc == SQLITE_OK)
                {
                    sqlite3_config(SQLITE_CONFIG_MUTEX, counter_methods);
                }
                g.disableTry = false;
            }
            else
            {
                Debug.Assert(g.m.xMutexAlloc != null);
                rc  = sqlite3_config(SQLITE_CONFIG_MUTEX, g.m);
                g.m = new sqlite3_mutex_methods();//        memset( &g.m, 0, sizeof( sqlite3_mutex_methods ) );
            }

            if (rc == SQLITE_OK)
            {
                g.isInstalled = isInstall;
            }

            TCL.Tcl_SetResult(interp, sqlite3TestErrorName(rc), TCL.TCL_VOLATILE);
            return(TCL.TCL_OK);
        }
Ejemplo n.º 27
0
        /*
        ** c_misuse_test
        */
        static int c_misuse_test(
            object clientdata, /* Pointer to sqlite3_enable_XXX function */
            Tcl_Interp interp, /* The TCL interpreter that invoked this command */
            int objc,          /* Number of arguments */
            Tcl_Obj[] objv     /* Command arguments */
            )
        {
            string       zErrFunction = "N/A";
            sqlite3      db           = null;
            sqlite3_stmt pStmt;
            int          rc;

            if (objc != 1)
            {
                TCL.Tcl_WrongNumArgs(interp, 1, objv, "");
                return(TCL.TCL_ERROR);
            }

            /* Open a database. Then close it again. We need to do this so that
            ** we have a "closed database handle" to pass to various API functions.
            */
            rc = sqlite3_open(":memory:", out db);
            if (rc != SQLITE_OK)
            {
                zErrFunction = "sqlite3_open";
                goto error_out;
            }
            sqlite3_close(db);


            rc = sqlite3_errcode(db);
            if (rc != SQLITE_MISUSE)
            {
                zErrFunction = "sqlite3_errcode";
                goto error_out;
            }

            pStmt    = new sqlite3_stmt();
            pStmt.pc = 1234;
            rc       = sqlite3_prepare(db, (StringBuilder)null, 0, ref pStmt, 0);
            if (rc != SQLITE_MISUSE)
            {
                zErrFunction = "sqlite3_prepare";
                goto error_out;
            }
            Debug.Assert(pStmt == null); /* Verify that pStmt is zeroed even on a MISUSE error */


            pStmt    = new sqlite3_stmt();
            pStmt.pc = 1234;
            rc       = sqlite3_prepare_v2(db, null, 0, ref pStmt, 0);
            if (rc != SQLITE_MISUSE)
            {
                zErrFunction = "sqlite3_prepare_v2";
                goto error_out;
            }
            Debug.Assert(pStmt == null);

#if !SQLITE_OMIT_UTF16
            pStmt = (sqlite3_stmt)1234;
            rc    = sqlite3_prepare16(db, null, 0, ref pStmt, 0);
            if (rc != SQLITE_MISUSE)
            {
                zErrFunction = "sqlite3_prepare16";
                goto error_out;
            }
            Debug.Assert(pStmt == 0);
            pStmt = (sqlite3_stmt)1234;
            rc    = sqlite3_prepare16_v2(db, null, 0, ref pStmt, 0);
            if (rc != SQLITE_MISUSE)
            {
                zErrFunction = "sqlite3_prepare16_v2";
                goto error_out;
            }
            Debug.Assert(pStmt == 0);
#endif

            return(TCL.TCL_OK);

error_out:
            TCL.Tcl_ResetResult(interp);
            TCL.Tcl_AppendResult(interp, "Error testing function: ", zErrFunction);
            return(TCL.TCL_ERROR);
        }
Ejemplo n.º 28
0
        /*
        ** usage:   varint_test  START  MULTIPLIER  COUNT  INCREMENT
        **
        ** This command tests the putVarint() and getVarint()
        ** routines, both for accuracy and for speed.
        **
        ** An integer is written using putVarint() and read back with
        ** getVarint() and varified to be unchanged.  This repeats COUNT
        ** times.  The first integer is START*MULTIPLIER.  Each iteration
        ** increases the integer by INCREMENT.
        **
        ** This command returns nothing if it works.  It returns an error message
        ** if something goes wrong.
        */
        static int btree_varint_test(
            object NotUsed,
            Tcl_Interp interp, /* The TCL interpreter that _invoked this command */
            int argc,          /* Number of arguments */
            TclObject[] argv   /* Text of each argument */
            )
        {
            int start = 0, mult = 0, count = 0, incr = 0;
            int _in;
            u32 _out = 0;
            int n1, n2, i, j;

            byte[] zBuf = new byte[100];
            if (argc != 5)
            {
                TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0].ToString(),
                                     " START MULTIPLIER COUNT incrEMENT\"", 0);
                return(TCL.TCL_ERROR);
            }
            if (TCL.Tcl_GetInt(interp, argv[1], ref start))
            {
                return(TCL.TCL_ERROR);
            }
            if (TCL.Tcl_GetInt(interp, argv[2], ref mult))
            {
                return(TCL.TCL_ERROR);
            }
            if (TCL.Tcl_GetInt(interp, argv[3], ref count))
            {
                return(TCL.TCL_ERROR);
            }
            if (TCL.Tcl_GetInt(interp, argv[4], ref incr))
            {
                return(TCL.TCL_ERROR);
            }
            _in  = start;
            _in *= mult;
            for (i = 0; i < count; i++)
            {
                string zErr = "";//char zErr[200];
                n1 = putVarint(zBuf, 0, _in);
                if (n1 > 9 || n1 < 1)
                {
                    sqlite3_snprintf(100, ref zErr, "putVarint returned %d - should be between 1 and 9", n1);
                    TCL.Tcl_AppendResult(interp, zErr);
                    return(TCL.TCL_ERROR);
                }
                n2 = getVarint(zBuf, 0, ref _out);
                if (n1 != n2)
                {
                    sqlite3_snprintf(100, ref zErr, "putVarint returned %d and GetVar_int returned %d", n1, n2);
                    TCL.Tcl_AppendResult(interp, zErr);
                    return(TCL.TCL_ERROR);
                }
                if (_in != (int)_out)
                {
                    sqlite3_snprintf(100, ref zErr, "Wrote 0x%016llx and got back 0x%016llx", _in, _out);
                    TCL.Tcl_AppendResult(interp, zErr);
                    return(TCL.TCL_ERROR);
                }
                if ((_in & 0xffffffff) == _in)
                {
                    u32 _out32 = 0;
                    n2   = getVarint32(zBuf, ref _out32);
                    _out = _out32;
                    if (n1 != n2)
                    {
                        sqlite3_snprintf(100, ref zErr, "putVarint returned %d and GetVar_int32 returned %d",
                                         n1, n2);
                        TCL.Tcl_AppendResult(interp, zErr);
                        return(TCL.TCL_ERROR);
                    }
                    if (_in != (int)_out)
                    {
                        sqlite3_snprintf(100, ref zErr, "Wrote 0x%016llx and got back 0x%016llx from GetVar_int32",
                                         _in, _out);
                        TCL.Tcl_AppendResult(interp, zErr);
                        return(TCL.TCL_ERROR);
                    }
                }

                /* _in order to get realistic tim_ings, run getVar_int 19 more times.
                ** This is because getVar_int is called ab_out 20 times more often
                ** than putVarint.
                */
                for (j = 0; j < 19; j++)
                {
                    getVarint(zBuf, 0, ref _out);
                }
                _in += incr;
            }
            return(TCL.TCL_OK);
        }