CreateQuery() public method

public CreateQuery ( ) : IQuery
return IQuery
        private void SetupSystem()
        {
            using (var transaction = Database.CreateSafeTransaction(IsolationLevel.Serializable)) {
                try {
                    using (var session = new SystemSession(transaction, SystemSchema.Name)) {
                        using (var query = session.CreateQuery()) {
                            foreach (var feature in Database.System.Features)
                            {
                                try {
                                    feature.OnSystemEvent(SystemEventType.TableCompositeSetup, query);
                                } catch (Exception ex) {
                                    query.Context.OnError(
                                        new Exception(
                                            String.Format("The feature {0} (v{1}) caused an error on Composite-Setup event.", feature.FeatureName,
                                                          feature.Version), ex));
                                    throw;
                                }
                            }
                        }
                    }

                    // Commit and close the transaction.
                    transaction.Commit();
                } catch (TransactionException e) {
                    throw new InvalidOperationException("Transaction Exception initializing the system.", e);
                }
            }
        }
        private void CreateSystem()
        {
            // Create the transaction
            using (var transaction = Database.CreateSafeTransaction(IsolationLevel.Serializable)) {
                try {
                    using (var session = new SystemSession(transaction, SystemSchema.Name)) {
                        using (var query = session.CreateQuery()) {
                            foreach (var feature in Database.System.Features)
                            {
                                feature.OnSystemEvent(SystemEventType.TableCompositeCreate, query);
                            }
                        }
                    }

                    // Commit and close the transaction.
                    transaction.Commit();
                } catch (TransactionException e) {
                    throw new InvalidOperationException("Transaction Exception creating composite.", e);
                }
            }
        }
Exemplo n.º 3
0
        private ITable FindEntry(Table table, ObjectName routineName)
        {
            var schemav = table.GetResolvedColumnName(0);
            var namev = table.GetResolvedColumnName(1);

            using (var session = new SystemSession(transaction)) {
                using (var context = session.CreateQuery()) {
                    var t = table.SimpleSelect(context, namev, SqlExpressionType.Equal,
                        SqlExpression.Constant(DataObject.String(routineName.Name)));
                    t = t.ExhaustiveSelect(context,
                        SqlExpression.Equal(SqlExpression.Reference(schemav),
                            SqlExpression.Constant(DataObject.String(routineName.ParentName))));

                    // This should be at most 1 row in size
                    if (t.RowCount > 1)
                        throw new Exception("Assert failed: multiple procedure names for " + routineName);

                    // Return the entries found.
                    return t;
                }
            }
        }
        public static void CheckAddConstraintViolations(this ITransaction transaction, ITable table, int[] rowIndices,
			ConstraintDeferrability deferred)
        {
            string curSchema = table.TableInfo.TableName.Parent.Name;
            using (var session = new SystemSession(transaction, curSchema)) {
                using (var queryContext = session.CreateQuery()) {

                    // Quick exit case
                    if (rowIndices == null || rowIndices.Length == 0)
                        return;

                    var tableInfo = table.TableInfo;
                    var tableName = tableInfo.TableName;

                    // ---- Constraint checking ----

                    // Check any primary key constraint.
                    var primaryKey = transaction.QueryTablePrimaryKey(tableName);
                    if (primaryKey != null &&
                        (deferred == ConstraintDeferrability.InitiallyDeferred ||
                         primaryKey.Deferred == ConstraintDeferrability.InitiallyImmediate)) {

                        // For each row added to this column
                        foreach (int rowIndex in rowIndices) {
                            if (!IsUniqueColumns(table, rowIndex, primaryKey.ColumnNames, false)) {
                                throw new PrimaryKeyViolationException(tableName, primaryKey.ConstraintName, primaryKey.ColumnNames, deferred);
                            }
                        } // For each row being added
                    }

                    // Check any unique constraints.
                    var uniqueConstraints = transaction.QueryTableUniqueKeys(tableName);
                    foreach (var unique in uniqueConstraints) {
                        if (deferred == ConstraintDeferrability.InitiallyDeferred ||
                            unique.Deferred == ConstraintDeferrability.InitiallyImmediate) {

                            // For each row added to this column
                            foreach (int rowIndex in rowIndices) {
                                if (!IsUniqueColumns(table, rowIndex, unique.ColumnNames, true)) {
                                    throw new UniqueKeyViolationException(tableName, unique.ConstraintName, unique.ColumnNames, deferred);
                                }
                            } // For each row being added
                        }
                    }

                    // Check any foreign key constraints.
                    // This ensures all foreign references in the table are referenced
                    // to valid records.
                    var foreignConstraints = transaction.QueryTableForeignKeys(tableName);

                    foreach (var reference in foreignConstraints) {
                        if (deferred == ConstraintDeferrability.InitiallyDeferred ||
                            reference.Deferred == ConstraintDeferrability.InitiallyImmediate) {
                            // For each row added to this column
                            foreach (int rowIndex in rowIndices) {
                                // Make sure the referenced record exists

                                // Return the count of records where the given row of
                                //   table_name(columns, ...) IN
                                //                    ref_table_name(ref_columns, ...)
                                int rowCount = RowCountOfReferenceTable(transaction,
                                    rowIndex,
                                    reference.TableName, reference.ColumnNames,
                                    reference.ForeignTable, reference.ForeignColumnNames,
                                    false);
                                if (rowCount == -1) {
                                    // foreign key is NULL
                                }

                                if (rowCount == 0) {
                                    throw new ForeignKeyViolationException(tableName, reference.ConstraintName, reference.ColumnNames,
                                        reference.ForeignTable, reference.ForeignColumnNames, deferred);
                                }
                            } // For each row being added.
                        }
                    }

                    // Any general checks of the inserted data
                    var checkConstraints = transaction.QueryTableCheckExpressions(tableName);

                    // For each check constraint, check that it evaluates to true.
                    for (int i = 0; i < checkConstraints.Length; ++i) {
                        var check = checkConstraints[i];
                        if (deferred == ConstraintDeferrability.InitiallyDeferred ||
                            check.Deferred == ConstraintDeferrability.InitiallyImmediate) {

                            var exp = tableInfo.ResolveColumns(transaction.IgnoreIdentifiersCase(), check.CheckExpression);

                            // For each row being added to this column
                            for (int rn = 0; rn < rowIndices.Length; ++rn) {
                                var resolver = new TableRowVariableResolver(table, rowIndices[rn]);
                                var evalExp = exp.Evaluate(queryContext, resolver, null);
                                var ob = ((SqlConstantExpression) evalExp).Value;

                                var b = ob.AsBoolean();

                                if (!b.IsNull) {
                                    if (!((SqlBoolean)b.Value)) {
                                        // Evaluated to false so don't allow this row to be added.
                                        throw new CheckViolationException(tableName, check.ConstraintName, check.CheckExpression, deferred);
                                    }
                                } else {
                                    // NOTE: This error will pass the row by default
                                    // TODO: emit a warning
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 5
0
        private void SetupSystem()
        {
            using (var transaction = Database.CreateSafeTransaction(IsolationLevel.Serializable)) {
                try {
                    using (var session = new SystemSession(transaction, SystemSchema.Name)) {
                        using (var query = session.CreateQuery()) {
                            foreach (var feature in Database.System.Features) {
                                try {
                                    feature.OnSystemEvent(SystemEventType.TableCompositeSetup, query);
                                } catch (Exception ex) {
                                    query.Context.OnError(
                                        new Exception(
                                            String.Format("The feature {0} (v{1}) caused an error on Composite-Setup event.", feature.FeatureName,
                                                feature.Version), ex));
                                    throw;
                                }
                            }
                        }
                    }

                    // Commit and close the transaction.
                    transaction.Commit();
                } catch (TransactionException e) {
                    throw new InvalidOperationException("Transaction Exception initializing the system.", e);
                }
            }
        }
Exemplo n.º 6
0
        private void CreateSystem()
        {
            // Create the transaction
            using (var transaction = Database.CreateSafeTransaction(IsolationLevel.Serializable)) {
                try {
                    using (var session = new SystemSession(transaction, SystemSchema.Name)) {
                        using (var query = session.CreateQuery()) {
                            foreach (var feature in Database.System.Features) {
                                feature.OnSystemEvent(SystemEventType.TableCompositeCreate, query);
                            }
                        }
                    }

                    // Commit and close the transaction.
                    transaction.Commit();
                } catch (TransactionException e) {
                    throw new InvalidOperationException("Transaction Exception creating composite.", e);
                }
            }
        }