private static void RevokeAllGrants(IQuery queryContext, IMutableTable grantTable, DbObjectType objectType,
                                            ObjectName objectName, string revoker, string grantee, bool withOption = false)
        {
            var objectCol      = grantTable.GetResolvedColumnName(1);
            var paramCol       = grantTable.GetResolvedColumnName(2);
            var granteeCol     = grantTable.GetResolvedColumnName(3);
            var grantOptionCol = grantTable.GetResolvedColumnName(4);
            var granterCol     = grantTable.GetResolvedColumnName(5);

            ITable t1 = grantTable;

            // All that match the given object parameter
            // It's most likely this will reduce the search by the most so we do
            // it first.
            t1 = t1.SimpleSelect(queryContext, paramCol, SqlExpressionType.Equal,
                                 SqlExpression.Constant(Field.String(objectName.FullName)));

            // The next is a single exhaustive select through the remaining records.
            // It finds all grants that match either public or the grantee is the
            // user or role, and that match the object type.

            // Expression: ("grantee_col" = grantee)
            var userCheck = SqlExpression.Equal(SqlExpression.Reference(granteeCol),
                                                SqlExpression.Constant(Field.String(grantee)));

            // Expression: ("object_col" = object AND
            //              "grantee_col" = grantee)
            // All that match the given grantee or public and given object
            var expr =
                SqlExpression.And(
                    SqlExpression.Equal(SqlExpression.Reference(objectCol),
                                        SqlExpression.Constant(Field.BigInt((int)objectType))), userCheck);

            // Are we only searching for grant options?
            var grantOptionCheck = SqlExpression.Equal(SqlExpression.Reference(grantOptionCol),
                                                       SqlExpression.Constant(Field.Boolean(withOption)));

            expr = SqlExpression.And(expr, grantOptionCheck);

            // Make sure the granter matches up also
            var granterCheck = SqlExpression.Equal(SqlExpression.Reference(granterCol),
                                                   SqlExpression.Constant(Field.String(revoker)));

            expr = SqlExpression.And(expr, granterCheck);

            t1 = t1.ExhaustiveSelect(queryContext, expr);

            // Remove these rows from the table
            grantTable.Delete(t1);
        }
        public static int Delete(this IMutableTable table, ITable other, int limit)
        {
            List <int> rowSet = new List <int>(other.RowCount);
            var        e      = other.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 = other.IndexOfColumn(table.GetResolvedColumnName(0));

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

            // Generate a row set that is in this tables domain.
            var rowsToDelete = other.ResolveRows(firstColumn, rowSet, table).ToList();

            // row_set may contain duplicate row indices, therefore we must sort so
            // any duplicates are grouped and therefore easier to find.
            rowSet.Sort();

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

            // Remove each row in row set in turn.  Make sure we don't remove the
            // same row index twice.
            int len         = System.Math.Min(rowsToDelete.Count, limit);
            int lastRemoved = -1;
            int removeCount = 0;

            for (int i = 0; i < len; ++i)
            {
                int toRemove = rowsToDelete[i];
                if (toRemove < lastRemoved)
                {
                    throw new DatabaseSystemException("Internal error: row sorting error or row set not in the range > 0");
                }

                if (toRemove != lastRemoved)
                {
                    table.RemoveRow(toRemove);
                    lastRemoved = toRemove;
                    ++removeCount;
                }
            }

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

            return(removeCount);
        }
        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);
        }
Exemple #4
0
        private static void RevokeAllGrants(IQuery queryContext, IMutableTable grantTable, DbObjectType objectType,
			ObjectName objectName, string revoker, string grantee, bool withOption = false)
        {
            var objectCol = grantTable.GetResolvedColumnName(1);
            var paramCol = grantTable.GetResolvedColumnName(2);
            var granteeCol = grantTable.GetResolvedColumnName(3);
            var grantOptionCol = grantTable.GetResolvedColumnName(4);
            var granterCol = grantTable.GetResolvedColumnName(5);

            ITable t1 = grantTable;

            // All that match the given object parameter
            // It's most likely this will reduce the search by the most so we do
            // it first.
            t1 = t1.SimpleSelect(queryContext, paramCol, SqlExpressionType.Equal,
                SqlExpression.Constant(Field.String(objectName.FullName)));

            // The next is a single exhaustive select through the remaining records.
            // It finds all grants that match either public or the grantee is the
            // user or role, and that match the object type.

            // Expression: ("grantee_col" = grantee)
            var userCheck = SqlExpression.Equal(SqlExpression.Reference(granteeCol),
                SqlExpression.Constant(Field.String(grantee)));

            // Expression: ("object_col" = object AND
            //              "grantee_col" = grantee)
            // All that match the given grantee or public and given object
            var expr =
                SqlExpression.And(
                    SqlExpression.Equal(SqlExpression.Reference(objectCol),
                        SqlExpression.Constant(Field.BigInt((int) objectType))), userCheck);

            // Are we only searching for grant options?
            var grantOptionCheck = SqlExpression.Equal(SqlExpression.Reference(grantOptionCol),
                SqlExpression.Constant(Field.Boolean(withOption)));
            expr = SqlExpression.And(expr, grantOptionCheck);

            // Make sure the granter matches up also
            var granterCheck = SqlExpression.Equal(SqlExpression.Reference(granterCol),
                SqlExpression.Constant(Field.String(revoker)));
            expr = SqlExpression.And(expr, granterCheck);

            t1 = t1.ExhaustiveSelect(queryContext, expr);

            // Remove these rows from the table
            grantTable.Delete(t1);
        }