Ejemplo n.º 1
0
        static void openStatTable(
            Parse pParse, /* Parsing context */
            int iDb,      /* The database we are looking in */
            int iStatCur, /* Open the sqlite_stat1 table on this cursor */
            string zWhere /* Delete entries associated with this table */
            )
        {
            _aTable[] aTable = new _aTable[] {
                new _aTable("sqlite_stat1", "tbl,idx,stat"),
#if SQLITE_ENABLE_STAT2
                new _aTable("sqlite_stat2", "tbl,idx,sampleno,sample"),
#endif
            };

            int[] aRoot      = new int[] { 0, 0 };
            u8[]  aCreateTbl = new u8[] { 0, 0 };

            int     i;
            sqlite3 db = pParse.db;
            Db      pDb;
            Vdbe    v = sqlite3GetVdbe(pParse);

            if (v == null)
            {
                return;
            }
            Debug.Assert(sqlite3BtreeHoldsAllMutexes(db));
            Debug.Assert(sqlite3VdbeDb(v) == db);
            pDb = db.aDb[iDb];

            for (i = 0; i < ArraySize(aTable); i++)
            {
                string zTab = aTable[i].zName;
                Table  pStat;
                if ((pStat = sqlite3FindTable(db, zTab, pDb.zName)) == null)
                {
                    /* The sqlite_stat[12] table does not exist. Create it. Note that a
                    ** side-effect of the CREATE TABLE statement is to leave the rootpage
                    ** of the new table in register pParse.regRoot. This is important
                    ** because the OpenWrite opcode below will be needing it. */
                    sqlite3NestedParse(pParse,
                                       "CREATE TABLE %Q.%s(%s)", pDb.zName, zTab, aTable[i].zCols
                                       );
                    aRoot[i]      = pParse.regRoot;
                    aCreateTbl[i] = 1;
                }
                else
                {
                    /* The table already exists. If zWhere is not NULL, delete all entries
                    ** associated with the table zWhere. If zWhere is NULL, delete the
                    ** entire contents of the table. */
                    aRoot[i] = pStat.tnum;
                    sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
                    if (!String.IsNullOrEmpty(zWhere))
                    {
                        sqlite3NestedParse(pParse,
                                           "DELETE FROM %Q.%s WHERE tbl=%Q", pDb.zName, zTab, zWhere
                                           );
                    }
                    else
                    {
                        /* The sqlite_stat[12] table already exists.  Delete all rows. */
                        sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
                    }
                }
            }

            /* Open the sqlite_stat[12] tables for writing. */
            for (i = 0; i < ArraySize(aTable); i++)
            {
                sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur + i, aRoot[i], iDb);
                sqlite3VdbeChangeP4(v, -1, 3, P4_INT32);
                sqlite3VdbeChangeP5(v, aCreateTbl[i]);
            }
        }
Ejemplo n.º 2
0
    static void openStatTable(
    Parse pParse,       /* Parsing context */
    int iDb,            /* The database we are looking in */
    int iStatCur,       /* Open the sqlite_stat1 table on this cursor */
    string zWhere       /* Delete entries associated with this table */
    )
    {
      _aTable[] aTable = new _aTable[]{
new _aTable( "sqlite_stat1", "tbl,idx,stat" ),
#if SQLITE_ENABLE_STAT2
new _aTable( "sqlite_stat2", "tbl,idx,sampleno,sample" ),
#endif
};

      int[] aRoot = new int[] { 0, 0 };
      u8[] aCreateTbl = new u8[] { 0, 0 };

      int i;
      sqlite3 db = pParse.db;
      Db pDb;
      Vdbe v = sqlite3GetVdbe( pParse );

      if ( v == null ) return;
      Debug.Assert( sqlite3BtreeHoldsAllMutexes( db ) );
      Debug.Assert( sqlite3VdbeDb( v ) == db );
      pDb = db.aDb[iDb];

      for ( i = 0; i < ArraySize( aTable ); i++ )
      {
        string zTab = aTable[i].zName;
        Table pStat;
        if ( ( pStat = sqlite3FindTable( db, zTab, pDb.zName ) ) == null )
        {
          /* The sqlite_stat[12] table does not exist. Create it. Note that a 
          ** side-effect of the CREATE TABLE statement is to leave the rootpage 
          ** of the new table in register pParse.regRoot. This is important 
          ** because the OpenWrite opcode below will be needing it. */
          sqlite3NestedParse( pParse,
          "CREATE TABLE %Q.%s(%s)", pDb.zName, zTab, aTable[i].zCols
          );
          aRoot[i] = pParse.regRoot;
          aCreateTbl[i] = 1;
        }
        else
        {
          /* The table already exists. If zWhere is not NULL, delete all entries 
          ** associated with the table zWhere. If zWhere is NULL, delete the
          ** entire contents of the table. */
          aRoot[i] = pStat.tnum;
          sqlite3TableLock( pParse, iDb, aRoot[i], 1, zTab );
          if ( !String.IsNullOrEmpty( zWhere ) )
          {
            sqlite3NestedParse( pParse,
            "DELETE FROM %Q.%s WHERE tbl=%Q", pDb.zName, zTab, zWhere
            );
          }
          else
          {
            /* The sqlite_stat[12] table already exists.  Delete all rows. */
            sqlite3VdbeAddOp2( v, OP_Clear, aRoot[i], iDb );
          }
        }
      }

      /* Open the sqlite_stat[12] tables for writing. */
      for ( i = 0; i < ArraySize( aTable ); i++ )
      {
        sqlite3VdbeAddOp3( v, OP_OpenWrite, iStatCur + i, aRoot[i], iDb );
        sqlite3VdbeChangeP4( v, -1, 3, P4_INT32 );
        sqlite3VdbeChangeP5( v, aCreateTbl[i] );
      }
    }