Exemple #1
0
        internal static void SetInsertRowToDefault(SystemTransaction transaction, TableName table_name, IMutableTable table, RowId rowid)
        {
            // Get all column defaults on the table
            IList<object> table_defaults = transaction.QueryTableDefaults(table_name);
            int sz = table_defaults.Count / 2;
            // Exit quickly if there's no default values
            if (sz == 0)
                return;

            // Create a query processor
            QueryProcessor processor = new QueryProcessor(transaction);
            // For each default value,
            TableRow row = table.GetRow(rowid);
            for (int i = 0; i < sz; ++i) {
                string colName = (string)table_defaults[i * 2];
                Expression colDefault = (Expression)table_defaults[(i * 2) + 1];
                // Execute the default value expression
                ITable defaultResult = processor.Execute(colDefault);
                // Turn it into a TObject
                SqlObject val = defaultResult.GetValue(0, new RowId(0));
                // The col num of the column name
                int colIndex = table.Columns.IndexOf(colName);
                if (colIndex < 0)
                    throw new ApplicationException("Column '" + colName + "' not found for DEFAULT value");

                // And insert it
                row.SetValue(colIndex, val);
            }
        }
        public static int Update(this IMutableTable mutableTable, IQuery context, ITable table,
                                 IEnumerable <SqlAssignExpression> assignList, int limit)
        {
            // Get the rows from the input table.
            var rowSet = new List <int>();
            var e      = table.GetEnumerator();

            while (e.MoveNext())
            {
                rowSet.Add(e.Current.RowId.RowNumber);
            }

            // HACKY: Find the first column of this table in the search table.  This
            //   will allow us to generate a row set of only the rows in the search
            //   table.
            int firstColumn = table.FindColumn(mutableTable.GetResolvedColumnName(0));

            if (firstColumn == -1)
            {
                throw new InvalidOperationException("Search table does not contain any " +
                                                    "reference to table being updated from");
            }

            // Convert the row_set to this table's domain.
            rowSet = table.ResolveRows(firstColumn, rowSet, mutableTable).ToList();

            // NOTE: Assume there's no duplicate rows.

            // If limit less than zero then limit is whole set.
            if (limit < 0)
            {
                limit = Int32.MaxValue;
            }

            // Update each row in row set in turn up to the limit.
            int len = System.Math.Min(rowSet.Count, limit);

            int updateCount = 0;

            for (int i = 0; i < len; ++i)
            {
                int toUpdate = rowSet[i];

                // Make a row object from this row (plus keep the original intact
                // in case we need to roll back to it).
                var row = mutableTable.GetRow(toUpdate);
                row.SetFromTable();

                // Run each assignment on the row.
                foreach (var assignment in assignList)
                {
                    row.EvaluateAssignment(assignment, context);
                }

                // Update the row
                mutableTable.UpdateRow(row);

                ++updateCount;
            }

            if (updateCount > 0)
            {
                // Perform a referential integrity check on any changes to the table.
                mutableTable.AssertConstraints();
            }

            return(updateCount);
        }