Exemplo n.º 1
0
        static void UpdateVirtualTable(Parse parse, SrcList src, Table table, ExprList changes, Expr rowid, int[] xrefs, Expr where_, int onError)
        {
            int        i;
            Context    ctx    = parse.Ctx; // Database connection
            VTable     vtable = VTable.GetVTable(ctx, table);
            SelectDest dest   = new SelectDest();

            // Construct the SELECT statement that will find the new values for all updated rows.
            ExprList list = ExprList.Append(parse, 0, Expr.Expr(ctx, TK.ID, "_rowid_")); // The result set of the SELECT statement

            if (rowid != null)
            {
                list = ExprList.Append(parse, list, Expr.Dup(ctx, rowid, 0));
            }
            Debug.Assert(table.PKey < 0);
            for (i = 0; i < table.Cols.length; i++)
            {
                Expr expr = (xrefs[i] >= 0 ? Expr.Dup(ctx, changes.Ids[xrefs[i]].Expr, 0) : Expr.Expr(ctx, TK.ID, table.Cols[i].Name)); // Temporary expression
                list = ExprList.Append(parse, list, expr);
            }
            Select select = Select.New(parse, list, src, where_, null, null, null, 0, null, null); // The SELECT statement

            // Create the ephemeral table into which the update results will be stored.
            Vdbe v = parse.V; // Virtual machine under construction

            Debug.Assert(v != null);
            int ephemTab = parse.Tabs++; // Table holding the result of the SELECT

            v.AddOp2(OP.OpenEphemeral, ephemTab, table.Cols.length + 1 + (rowid != null ? 1 : 0));
            v.ChangeP5(BTREE_UNORDERED);

            // fill the ephemeral table
            Select.DestInit(dest, SRT.Table, ephemTab);
            Select.Select(parse, select, ref dest);

            // Generate code to scan the ephemeral table and call VUpdate.
            int regId = ++parse.Mems;// First register in set passed to OP_VUpdate

            parse.Mems += table.Cols.length + 1;
            int addr = v.AddOp2(OP.Rewind, ephemTab, 0); // Address of top of loop

            v.AddOp3(OP.Column, ephemTab, 0, regId);
            v.AddOp3(OP.Column, ephemTab, (rowid != null ? 1 : 0), regId + 1);
            for (i = 0; i < table.nCol; i++)
            {
                v.AddOp3(OP.Column, ephemTab, i + 1 + (rowid != null ? 1 : 0), regId + 2 + i);
            }
            sqlite3VtabMakeWritable(parse, table);
            v.AddOp4(OP_VUpdate, 0, table.Cols.length + 2, regId, vtable, P4_VTAB);
            v.ChangeP5((byte)(onError == OE_Default ? OE_Abort : onError));
            parse.MayAbort();
            v.AddOp2(OP.Next, ephemTab, addr + 1);
            v.JumpHere(addr);
            v.AddOp2(OP.Close, ephemTab, 0);

            // Cleanup
            Select.Delete(ctx, ref select);
        }
Exemplo n.º 2
0
        public static bool IsReadOnly(Parse parse, Table table, bool viewOk)
        {
            // A table is not writable under the following circumstances:
            //   1) It is a virtual table and no implementation of the xUpdate method has been provided, or
            //   2) It is a system table (i.e. sqlite_master), this call is not part of a nested parse and writable_schema pragma has not 
            //      been specified.
            // In either case leave an error message in pParse and return non-zero.
            if ((E.IsVirtual(table) && VTable.GetVTable(parse.Ctx, table).Module.IModule.Update == null) ||
               ((table.TabFlags & TF.Readonly) != 0 && (parse.Ctx.Flags & Context.FLAG.WriteSchema) == 0 && parse.Nested == 0))
            {
                parse.ErrorMsg("table %s may not be modified", table.Name);
                return true;
            }

#if !OMIT_VIEW
            if (!viewOk && table.Select != null)
            {
                parse.ErrorMsg("cannot modify %s because it is a view", table.Name);
                return true;
            }
#endif
            return false;
        }
Exemplo n.º 3
0
        public static void RenameTable(Parse parse, SrcList src, Token name)
        {
            Context ctx = parse.Ctx;               // Database connection

            Context.FLAG savedDbFlags = ctx.Flags; // Saved value of db->flags
            //if (C._NEVER(ctx.MallocFailed)) goto exit_rename_table;
            Debug.Assert(src.Srcs == 1);
            Debug.Assert(Btree.HoldsAllMutexes(ctx));

            Table table = parse.LocateTableItem(false, src.Ids[0]); // Table being renamed

            if (table == null)
            {
                goto exit_rename_table;
            }
            int    db     = Prepare.SchemaToIndex(ctx, table.Schema); // Database that contains the table
            string dbName = ctx.DBs[db].Name;                         // Name of database iDb

            ctx.Flags |= Context.FLAG.PreferBuiltin;

            // Get a NULL terminated version of the new table name.
            string nameAsString = Parse.NameFromToken(ctx, name); // NULL-terminated version of pName

            if (nameAsString == null)
            {
                goto exit_rename_table;
            }

            // Check that a table or index named 'zName' does not already exist in database iDb. If so, this is an error.
            if (Parse.FindTable(ctx, nameAsString, dbName) != null || Parse.FindIndex(ctx, nameAsString, dbName) != null)
            {
                parse.ErrorMsg("there is already another table or index with this name: %s", nameAsString);
                goto exit_rename_table;
            }

            // Make sure it is not a system table being altered, or a reserved name that the table is being renamed to.
            if (IsSystemTable(parse, table.Name) || parse->CheckObjectName(nameAsString) != RC.OK)
            {
                goto exit_rename_table;
            }

#if !OMIT_VIEW
            if (table.Select != null)
            {
                parse.ErrorMsg("view %s may not be altered", table.Name);
                goto exit_rename_table;
            }
#endif

#if !OMIT_AUTHORIZATION
            // Invoke the authorization callback.
            if (Auth.Check(parse, AUTH.ALTER_TABLE, dbName, table.Name, null))
            {
                goto exit_rename_table;
            }
#endif

            VTable vtable = null; // Non-zero if this is a v-tab with an xRename()
#if !OMIT_VIRTUALTABLE
            if (parse->ViewGetColumnNames(table) != 0)
            {
                goto exit_rename_table;
            }
            if (E.IsVirtual(table))
            {
                vtable = VTable.GetVTable(ctx, table);
                if (vtable.IVTable.IModule.Rename == null)
                {
                    vtable = null;
                }
            }
#endif

            // Begin a transaction and code the VerifyCookie for database iDb. Then modify the schema cookie (since the ALTER TABLE modifies the
            // schema). Open a statement transaction if the table is a virtual table.
            Vdbe v = parse.GetVdbe();
            if (v == null)
            {
                goto exit_rename_table;
            }
            parse.BeginWriteOperation((vtable != null ? 1 : 0), db);
            parse.ChangeCookie(db);

            // If this is a virtual table, invoke the xRename() function if one is defined. The xRename() callback will modify the names
            // of any resources used by the v-table implementation (including other SQLite tables) that are identified by the name of the virtual table.
#if  !OMIT_VIRTUALTABLE
            if (vtable != null)
            {
                int i = ++parse.Mems;
                v.AddOp4(OP.String8, 0, i, 0, nameAsString, 0);
                v.AddOp4(OP.VRename, i, 0, 0, vtable, Vdbe.P4T.VTAB);
                parse.MayAbort();
            }
#endif

            // figure out how many UTF-8 characters are in zName
            string tableName       = table.Name;                       // Original name of the table
            int    tableNameLength = C._utf8charlength(tableName, -1); // Number of UTF-8 characters in zTabName

#if !OMIT_TRIGGER
            string where_ = string.Empty; // Where clause to locate temp triggers
#endif

#if !OMIT_FOREIGN_KEY && !OMIT_TRIGGER
            if ((ctx.Flags & Context.FLAG.ForeignKeys) != 0)
            {
                // If foreign-key support is enabled, rewrite the CREATE TABLE statements corresponding to all child tables of foreign key constraints
                // for which the renamed table is the parent table.
                if ((where_ = WhereForeignKeys(parse, table)) != null)
                {
                    parse.NestedParse(
                        "UPDATE \"%w\".%s SET " +
                        "sql = sqlite_rename_parent(sql, %Q, %Q) " +
                        "WHERE %s;", dbName, E.SCHEMA_TABLE(db), tableName, nameAsString, where_);
                    C._tagfree(ctx, ref where_);
                }
            }
#endif

            // Modify the sqlite_master table to use the new table name.
            parse.NestedParse(
                "UPDATE %Q.%s SET " +
#if OMIT_TRIGGER
                "sql = sqlite_rename_table(sql, %Q), " +
#else
                "sql = CASE " +
                "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)" +
                "ELSE sqlite_rename_table(sql, %Q) END, " +
#endif
                "tbl_name = %Q, " +
                "name = CASE " +
                "WHEN type='table' THEN %Q " +
                "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN " +
                "'sqlite_autoindex_' || %Q || substr(name,%d+18) " +
                "ELSE name END " +
                "WHERE tbl_name=%Q AND " +
                "(type='table' OR type='index' OR type='trigger');",
                dbName, SCHEMA_TABLE(db), nameAsString, nameAsString, nameAsString,
#if !OMIT_TRIGGER
                nameAsString,
#endif
                nameAsString, tableNameLength, tableName);

#if !OMIT_AUTOINCREMENT
            // If the sqlite_sequence table exists in this database, then update it with the new table name.
            if (Parse.FindTable(ctx, "sqlite_sequence", dbName) != null)
            {
                parse.NestedParse(
                    "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
                    dbName, nameAsString, table.Name);
            }
#endif

#if !OMIT_TRIGGER
            // If there are TEMP triggers on this table, modify the sqlite_temp_master table. Don't do this if the table being ALTERed is itself located in the temp database.
            if ((where_ = WhereTempTriggers(parse, table)) != "")
            {
                parse.NestedParse(
                    "UPDATE sqlite_temp_master SET " +
                    "sql = sqlite_rename_trigger(sql, %Q), " +
                    "tbl_name = %Q " +
                    "WHERE %s;", nameAsString, nameAsString, where_);
                C._tagfree(ctx, ref where_);
            }
#endif

#if !(OMIT_FOREIGN_KEY) && !(OMIT_TRIGGER)
            if ((ctx.Flags & Context.FLAG.ForeignKeys) != 0)
            {
                for (FKey p = Parse.FkReferences(table); p != null; p = p.NextTo)
                {
                    Table from = p.From;
                    if (from != table)
                    {
                        ReloadTableSchema(parse, p.From, from.Name);
                    }
                }
            }
#endif

            // Drop and reload the internal table schema.
            ReloadTableSchema(parse, table, nameAsString);

exit_rename_table:
            Expr.SrcListDelete(ctx, ref src);
            C._tagfree(ctx, ref nameAsString);
            ctx.Flags = savedDbFlags;
        }
Exemplo n.º 4
0
        public static void DeleteFrom(Parse parse, SrcList tabList, Expr where_)
        {
            AuthContext sContext = new AuthContext(); // Authorization context
            Context ctx = parse.Ctx; // Main database structure
            if (parse.Errs != 0 || ctx.MallocFailed)
                goto delete_from_cleanup;
            Debug.Assert(tabList.Srcs == 1);

            // Locate the table which we want to delete.  This table has to be put in an SrcList structure because some of the subroutines we
            // will be calling are designed to work with multiple tables and expect an SrcList* parameter instead of just a Table* parameter.
            Table table = SrcList.Lookup(parse, tabList); // The table from which records will be deleted
            if (table == null) goto delete_from_cleanup;

            // Figure out if we have any triggers and if the table being deleted from is a view
#if !OMIT_TRIGGER
            int dummy;
            Trigger trigger = Triggers.Exist(parse, table, TK.DELETE, null, out dummy); // List of table triggers, if required
#if OMIT_VIEW
            const bool isView = false;
#else
            bool isView = (table.Select != null); // True if attempting to delete from a view
#endif
#else
            const Trigger trigger = null;
            bool isView = false;
#endif

            // If pTab is really a view, make sure it has been initialized.
            if (sqlite3ViewGetColumnNames(parse, table) != null || IsReadOnly(parse, table, (trigger != null)))
                goto delete_from_cleanup;
            int db = sqlite3SchemaToIndex(ctx, table.Schema); // Database number
            Debug.Assert(db < ctx.DBs.length);
            string dbName = ctx.DBs[db].Name; // Name of database holding pTab
            ARC rcauth = Auth.Check(parse, AUTH.DELETE, table.Name, 0, dbName); // Value returned by authorization callback
            Debug.Assert(rcauth == ARC.OK || rcauth == ARC.DENY || rcauth == ARC.IGNORE);
            if (rcauth == ARC.DENY)
                goto delete_from_cleanup;
            Debug.Assert(!isView || trigger != null);

            // Assign cursor number to the table and all its indices.
            Debug.Assert(tabList.Srcs == 1);
            int curId = tabList.Ids[0].Cursor = parse.Tabs++; // VDBE VdbeCursor number for pTab
            Index idx; // For looping over indices of the table
            for (idx = table.Index; idx != null; idx = idx.Next)
                parse.Tabs++;

            // Start the view context
            if (isView)
                Auth.ContextPush(parse, sContext, table.Name);

            // Begin generating code.
            Vdbe v = parse.GetVdbe(); // The virtual database engine 
            if (v == null)
                goto delete_from_cleanup;
            if (parse.Nested == 0) v.CountChanges();
            parse.BeginWriteOperation(1, db);

            // If we are trying to delete from a view, realize that view into a ephemeral table.
#if !OMIT_VIEW && !OMIT_TRIGGER
            if (isView)
                MaterializeView(parse, table, where_, curId);
#endif
            // Resolve the column names in the WHERE clause.
            NameContext sNC = new NameContext(); // Name context to resolve expressions in
            sNC.Parse = parse;
            sNC.SrcList = tabList;
            if (sqlite3ResolveExprNames(sNC, ref where_) != 0)
                goto delete_from_cleanup;

            // Initialize the counter of the number of rows deleted, if we are counting rows.
            int memCnt = -1; // Memory cell used for change counting
            if ((ctx.Flags & Context.FLAG.CountRows) != 0)
            {
                memCnt = ++parse.Mems;
                v.AddOp2(OP.Integer, 0, memCnt);
            }

#if !OMIT_TRUNCATE_OPTIMIZATION
            // Special case: A DELETE without a WHERE clause deletes everything. It is easier just to erase the whole table. Prior to version 3.6.5,
            // this optimization caused the row change count (the value returned by API function sqlite3_count_changes) to be set incorrectly.
            if (rcauth == ARC.OK && where_ == null && trigger == null && !IsVirtual(table) && !FKey.FkRequired(parse, table, null, 0))
            {
                Debug.Assert(!isView);
                v.AddOp4(OP.Clear, table.Id, db, memCnt, table.Name, Vdbe.P4T.STATIC);
                for (idx = table.Index; idx != null; idx = idx.Next)
                {
                    Debug.Assert(idx.Schema == table.Schema);
                    v.AddOp2(OP.Clear, idx.Id, db);
                }
            }
            else
#endif
            // The usual case: There is a WHERE clause so we have to scan through the table and pick which records to delete.
            {
                int rowSet = ++parse.Mems; // Register for rowset of rows to delete
                int rowid = ++parse.Mems; // Used for storing rowid values.

                // Collect rowids of every row to be deleted.
                v.AddOp2(OP.Null, 0, rowSet);
                ExprList dummy = null;
                WhereInfo winfo = Where.Begin(parse, tabList, where_, ref dummy, WHERE_DUPLICATES_OK, 0); // Information about the WHERE clause
                if (winfo == null) goto delete_from_cleanup;
                int regRowid = Expr.CodeGetColumn(parse, table, -1, curId, rowid); // Actual register containing rowids
                v.AddOp2(OP.RowSetAdd, rowSet, regRowid);
                if ((ctx.Flags & Context.FLAG.CountRows) != 0)
                    v.AddOp2(OP.AddImm, memCnt, 1);
                Where.End(winfo);

                // Delete every item whose key was written to the list during the database scan.  We have to delete items after the scan is complete
                // because deleting an item can change the scan order.
                int end = v.MakeLabel();

                // Unless this is a view, open cursors for the table we are deleting from and all its indices. If this is a view, then the
                // only effect this statement has is to fire the INSTEAD OF triggers.
                if (!isView)
                    sqlite3OpenTableAndIndices(parse, table, curId, OP.OpenWrite);
                int addr = v.AddOp3(OP.RowSetRead, rowSet, end, rowid);

                // Delete the row
#if !OMIT_VIRTUALTABLE
                if (IsVirtual(table))
                {
                    VTable vtable = VTable.GetVTable(ctx, table);
                    VTable.MakeWritable(parse, table);
                    v.AddOp4(OP.VUpdate, 0, 1, rowid, vtable, Vdbe.P4T.VTAB);
                    v.ChangeP5(OE.Abort);
                    sqlite3MayAbort(parse);
                }
                else
#endif
                {
                    int count = (parse.Nested == 0; // True to count changes
                    GenerateRowDelete(parse, table, curId, rowid, count, trigger, OE.Default);
                }

                // End of the delete loop
                v.AddOp2(OP.Goto, 0, addr);
                v.ResolveLabel(end);

                // Close the cursors open on the table and its indexes.
                if (!isView && !IsVirtual(table))
                {
                    for (int i = 1, idx = table.Index; idx != null; i++, idx = idx.Next)
                        v.AddOp2(OP.Close, curId + i, idx.Id);
                    v.AddOp1(OP.Close, curId);
                }
            }

            // Update the sqlite_sequence table by storing the content of the maximum rowid counter values recorded while inserting into
		    // autoincrement tables.
            if (parse.Nested == 0 && parse.TriggerTab == null)
                sqlite3AutoincrementEnd(parse);

            // Return the number of rows that were deleted. If this routine is generating code because of a call to sqlite3NestedParse(), do not
		    // invoke the callback function.
            if ((ctx.Flags & Context.FLAG.CountRows) != 0 && parse.Nested == 0 && parse.TriggerTab == null)
            {
                v.AddOp2(OP.ResultRow, memCnt, 1);
                v.SetNumCols(1);
                v.SetColName(0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
            }

        delete_from_cleanup:
            Auth.ContextPop(sContext);
            SrcList.Delete(ctx, ref tabList);
            Expr.Delete(ctx, ref where_);
            return;
        }