Represents the foundation class of SQL statements to be executed.

A SQL Statement encapsulates the properties that are required to form a SqlPreparedStatement that can be serialized and executed.

It is prevented to a SqlStatement to be immediately executed for enforcing SqlPreparedStatement to be cached and executed in later moments, optimizing performances and re-usability.

Пример #1
0
        public void Set(string query, SqlStatement[] statements)
        {
            if (String.IsNullOrEmpty(query))
                return;

            SetObject(query, statements);
        }
        public static void RegisterQuery(this IQueryContext context, SqlStatement statement)
        {
            if (statement == null)
                throw new ArgumentNullException("statement");

            context.RegisterQuery(statement.SourceQuery, statement.ToString());
        }
Пример #3
0
        public PlSqlProcedureInfo(ObjectName procedureName, RoutineParameter[] parameters, SqlStatement body)
            : base(procedureName, parameters)
        {
            if (body == null)
                throw new ArgumentNullException("body");

            Body = body;
        }
Пример #4
0
        public StatementEvent(SqlStatement statement, StatementEventType eventType)
        {
            if (statement == null)
                throw new ArgumentNullException("statement");

            Statement = statement;
            EventType = eventType;
        }
Пример #5
0
        public void AddStatement(SqlStatement statement)
        {
            if (statement == null)
                throw new ArgumentNullException("statement");

            AssertIsUserDefined();
            statements.Add(statement);
        }
Пример #6
0
        public CreateProcedureStatement(ObjectName procedureName, RoutineParameter[] parameters, SqlStatement body)
        {
            if (procedureName == null)
                throw new ArgumentNullException("procedureName");
            if (body == null)
                throw new ArgumentNullException("body");

            ProcedureName = procedureName;
            Parameters = parameters;
            Body = body;
        }
Пример #7
0
        public ConditionStatement(SqlExpression condition, SqlStatement[] trueStatements, SqlStatement[] falseStatements)
        {
            if (condition == null)
                throw new ArgumentNullException("condition");
            if (trueStatements == null)
                throw new ArgumentNullException("trueStatements");

            ConditionExpression = condition;
            TrueStatements = trueStatements;
            FalseStatements = falseStatements;
        }
Пример #8
0
        private ExecutionContext(ExecutionContext parent, IRequest request, SqlStatement statement)
        {
            if (request == null)
                throw new ArgumentNullException("request");

            Parent = parent;
            Request = request;
            Statement = statement;

            Assertions = new SecurityAssertionRegistrar(parent != null ? parent.Assertions : null);
            Actions = new SecurityActionRegistrar();
        }
Пример #9
0
        public void SimpleConditional()
        {
            var condition = SqlExpression.Equal(SqlExpression.VariableReference("a"), SqlExpression.Constant(34));
            var ifTrue = new SqlStatement[] {
                new DeclareVariableStatement("b", PrimitiveTypes.String()),
                new AssignVariableStatement(SqlExpression.VariableReference("b"), SqlExpression.Constant(21)),
            };

            var result = AdminQuery.If(condition, ifTrue);

            Assert.IsNotNull(result);
        }
Пример #10
0
        public void AddStatement(SqlStatement statement)
        {
            if (statement == null)
                throw new ArgumentNullException("statement");

            if (TriggerInfo.TriggerType != TriggerType.Procedure)
                throw new ArgumentException(String.Format("The trigger '{0}' is not a PROCEDURE TRIGGER and cannot have any body.",
                    TriggerInfo.TriggerName));

            AssertStatementIsAllowed(statement);

            statements.Add(statement);
        }
Пример #11
0
        public CreateFunctionStatement(ObjectName functionName, SqlType returnType, RoutineParameter[] parameters, SqlStatement body)
        {
            if (functionName == null)
                throw new ArgumentNullException("functionName");
            if (returnType == null)
                throw new ArgumentNullException("returnType");
            if (body == null)
                throw new ArgumentNullException("body");

            FunctionName = functionName;
            ReturnType = returnType;
            Parameters = parameters;
            Body = body;
        }
Пример #12
0
        public PlSqlFunctionInfo(ObjectName functionName, RoutineParameter[] parameters, SqlType returnType, SqlStatement body)
            : base(functionName, parameters,returnType, FunctionType.Static)
        {
            if (body == null)
                throw new ArgumentNullException("body");

            // TODO: in case of RETURNS TABLE verify a select is there
            if (!(returnType is TabularType)) {
                if (!ReturnChecker.HasReturn(body))
                    throw new ArgumentException("The function body has no return");
            }

            Body = body;
        }
Пример #13
0
        protected virtual SqlStatement VisitCondition(ConditionStatement statement)
        {
            var trueStatements = new SqlStatement[statement.TrueStatements.Length];
            for (int i = 0; i < trueStatements.Length; i++) {
                trueStatements[i] = VisitStatement(statement.TrueStatements[i]);
            }

            var falseStatements = statement.FalseStatements;
            if (falseStatements != null) {
                for (int i = 0; i < falseStatements.Length; i++) {
                    falseStatements[i] = VisitStatement(falseStatements[i]);
                }
            }

            return new ConditionStatement(statement.ConditionExpression, trueStatements);
        }
Пример #14
0
        public bool TryGet(string query, out SqlStatement[] statements)
        {
            if (String.IsNullOrEmpty(query)) {
                statements = null;
                return false;
            }

            object obj;
            if (!TryGetObject(query, out obj)) {
                statements = null;
                return false;
            }

            statements = (SqlStatement[]) obj;
            return true;
        }
Пример #15
0
 public ConditionalStatement(SqlExpression condition, SqlStatement @else)
     : this(condition, null, @else)
 {
 }
Пример #16
0
 private void AssertAllowedObject(SqlStatement obj)
 {
     AssertPlSqlStatement(obj);
     AssertNotLoopControl(obj);
 }
Пример #17
0
 public void AddStatement(SqlStatement statement)
 {
     // TODO: make further checks, such as if a labeled statement with
     //       the same label already exists
     statements.Add(statement);
 }
Пример #18
0
 private StatementContext NewBlock(SqlStatement statement)
 {
     return(new StatementContext(this, statement.StatementName, statement));
 }
Пример #19
0
 public StatementContext(IContext parent, SqlStatement statement)
     : base(parent, KnownScopes.Statement)
 {
     Statement = statement ?? throw new ArgumentNullException(nameof(statement));
     Result    = new EmptyStatementResult();
 }
Пример #20
0
 public CreateProcedureStatement(ObjectName procedureName, SqlStatement body)
     : this(procedureName, null, body)
 {
 }
Пример #21
0
        protected virtual SqlStatement VisitStatement(SqlStatement statement)
        {
            if (statement == null)
                return null;

            // CREATE
            if (statement is CreateTableStatement)
                return VisitCreateTable((CreateTableStatement) statement);
            if (statement is CreateViewStatement)
                return VisitCreateView((CreateViewStatement) statement);
            if (statement is CreateSchemaStatement)
                return VisitCreateSchema((CreateSchemaStatement) statement);
            if (statement is CreateSequenceStatement)
                return VisitCreateSequence((CreateSequenceStatement) statement);
            if (statement is CreateTriggerStatement)
                return VisitCreateTrigger((CreateTriggerStatement) statement);
            if (statement is CreateCallbackTriggerStatement)
                return VisitCreateCallbackTrigger((CreateCallbackTriggerStatement) statement);
            if (statement is CreateProcedureTriggerStatement)
                return VisitCreateProcedureTrigger((CreateProcedureTriggerStatement) statement);
            if (statement is CreateUserStatement)
                return VisitCreateUser((CreateUserStatement) statement);
            if (statement is CreateRoleStatement)
                return VisitCreateRole((CreateRoleStatement) statement);
            if (statement is CreateProcedureStatement)
                return VisitCreateProcedure((CreateProcedureStatement) statement);
            if (statement is CreateExternalProcedureStatement)
                return VisitCreateExternProcedure((CreateExternalProcedureStatement) statement);
            if (statement is CreateFunctionStatement)
                return VisitCreateFunction((CreateFunctionStatement) statement);
            if (statement is CreateExternalFunctionStatement)
                return VisitCreateExternFunction((CreateExternalFunctionStatement) statement);

            // DROP
            if (statement is DropViewStatement)
                return VisitDropView((DropViewStatement) statement);
            if (statement is DropTableStatement)
                return VisitDropTable((DropTableStatement) statement);
            if (statement is DropCallbackTriggersStatement)
                return VisitDropCallbackTrigger((DropCallbackTriggersStatement) statement);
            if (statement is DropSequenceStatement)
                return VisitDropSequence((DropSequenceStatement) statement);
            if (statement is DropProcedureStatement)
                return VisitDropProcedure((DropProcedureStatement) statement);
            if (statement is DropFunctionStatement)
                return VisitDropFunction((DropFunctionStatement) statement);
            if (statement is DropRoleStatement)
                return VisitDropRole((DropRoleStatement) statement);
            if (statement is DropUserStatement)
                return VisitDropUser((DropUserStatement) statement);
            if (statement is DropTriggerStatement)
                return VisitDropTrigger((DropTriggerStatement) statement);
            if (statement is DropTypeStatement)
                return VisitDropType((DropTypeStatement) statement);

            // ALTER
            if (statement is AlterTableStatement)
                return VisitAlterTable((AlterTableStatement) statement);
            if (statement is AlterUserStatement)
                return VisitAlterUser((AlterUserStatement) statement);

            if (statement is GrantPrivilegesStatement)
                return VisitGrantPrivilege((GrantPrivilegesStatement) statement);
            if (statement is GrantRoleStatement)
                return VisitGrantRole((GrantRoleStatement) statement);
            if (statement is RevokePrivilegesStatement)
                return VisitRevokePrivilege((RevokePrivilegesStatement) statement);
            // TODO: Revoke role statement

            // DECLARE
            if (statement is DeclareCursorStatement)
                return VisitDeclareCursor((DeclareCursorStatement) statement);
            if (statement is DeclareVariableStatement)
                return VisitDeclareVariable((DeclareVariableStatement) statement);
            if (statement is DeclareExceptionStatement)
                return VisitDeclareException((DeclareExceptionStatement) statement);

            if (statement is CallStatement)
                return VisitCall((CallStatement) statement);

            if (statement is InsertStatement)
                return VisitInsert((InsertStatement) statement);
            if (statement is InsertSelectStatement)
                return VisitInsertSelect((InsertSelectStatement) statement);
            if (statement is SelectStatement)
                return VisitSelect((SelectStatement) statement);
            if (statement is SelectIntoStatement)
                return VisitSelectInto((SelectIntoStatement) statement);
            if (statement is DeleteStatement)
                return VisitDelete((DeleteStatement) statement);
            if (statement is UpdateStatement)
                return VisitUpdate((UpdateStatement) statement);
            if (statement is UpdateFromCursorStatement)
                return VisitUpdateFromCursor((UpdateFromCursorStatement) statement);
            if (statement is ShowStatement)
                return VisitShow((ShowStatement) statement);

            // Cursors
            if (statement is OpenStatement)
                return VisitOpen((OpenStatement) statement);
            if (statement is FetchStatement)
                return VisitFetch((FetchStatement) statement);
            if (statement is CloseStatement)
                return VisitClose((CloseStatement) statement);

            // Session Commands
            if (statement is CommitStatement)
                return VisitCommit((CommitStatement) statement);
            if (statement is RollbackStatement)
                return VisitRollback((RollbackStatement) statement);
            if (statement is SetStatement)
                return VisitSet((SetStatement) statement);

            if (statement is AssignVariableStatement)
                return VisitAssignVariable((AssignVariableStatement) statement);

            // Blocks
            if (statement is CodeBlockStatement)
                return VisitCodeBlock((CodeBlockStatement) statement);

            // In-Block
            if (statement is LoopControlStatement)
                return VisitLoopControl((LoopControlStatement) statement);
            if (statement is ReturnStatement)
                return VisitReturn((ReturnStatement) statement);
            if (statement is GoToStatement)
                return VisitGoTo((GoToStatement) statement);
            if (statement is RaiseStatement)
                return VisitRaise((RaiseStatement) statement);

            if (statement is ConditionStatement)
                return VisitCondition((ConditionStatement) statement);

            if (statement is IVisitableStatement)
                return ((IVisitableStatement) statement).Accept(this);

            return statement;
        }
Пример #22
0
 public ExecutionContext(IRequest request, SqlStatement statement)
     : this(null, request, statement)
 {
 }
Пример #23
0
 private void AssertStatementIsAllowed(SqlStatement statement)
 {
     // TODO: validate this statement
 }
Пример #24
0
 public bool Verify(SqlStatement statement)
 {
     VisitStatement(statement);
     return returnFound;
 }
 public BlockStatementContext(IContext parent, string name, SqlStatement statement)
     : base(parent, name, statement)
 {
     Init();
 }
Пример #26
0
 public ExecutionContext NewBlock(SqlStatement statement)
 {
     return new ExecutionContext(this, Request.CreateBlock(), statement);
 }
Пример #27
0
 public ConditionStatement(SqlExpression condition, SqlStatement[] trueStatements)
     : this(condition, trueStatements, new SqlStatement[0])
 {
 }
Пример #28
0
 public string Format(SqlStatement statement)
 {
     VisitStatement(statement);
     return builder.ToString();
 }
Пример #29
0
        protected virtual SqlStatement VisitStatement(SqlStatement statement)
        {
            if (statement == null)
            {
                return(null);
            }

            // CREATE
            if (statement is CreateTableStatement)
            {
                return(VisitCreateTable((CreateTableStatement)statement));
            }
            if (statement is CreateViewStatement)
            {
                return(VisitCreateView((CreateViewStatement)statement));
            }
            if (statement is CreateSchemaStatement)
            {
                return(VisitCreateSchema((CreateSchemaStatement)statement));
            }
            if (statement is CreateSequenceStatement)
            {
                return(VisitCreateSequence((CreateSequenceStatement)statement));
            }
            if (statement is CreateTriggerStatement)
            {
                return(VisitCreateTrigger((CreateTriggerStatement)statement));
            }
            if (statement is CreateCallbackTriggerStatement)
            {
                return(VisitCreateCallbackTrigger((CreateCallbackTriggerStatement)statement));
            }
            if (statement is CreateProcedureTriggerStatement)
            {
                return(VisitCreateProcedureTrigger((CreateProcedureTriggerStatement)statement));
            }
            if (statement is CreateUserStatement)
            {
                return(VisitCreateUser((CreateUserStatement)statement));
            }
            if (statement is CreateRoleStatement)
            {
                return(VisitCreateRole((CreateRoleStatement)statement));
            }
            if (statement is CreateProcedureStatement)
            {
                return(VisitCreateProcedure((CreateProcedureStatement)statement));
            }
            if (statement is CreateExternalProcedureStatement)
            {
                return(VisitCreateExternProcedure((CreateExternalProcedureStatement)statement));
            }
            if (statement is CreateFunctionStatement)
            {
                return(VisitCreateFunction((CreateFunctionStatement)statement));
            }
            if (statement is CreateExternalFunctionStatement)
            {
                return(VisitCreateExternFunction((CreateExternalFunctionStatement)statement));
            }

            // DROP
            if (statement is DropViewStatement)
            {
                return(VisitDropView((DropViewStatement)statement));
            }
            if (statement is DropTableStatement)
            {
                return(VisitDropTable((DropTableStatement)statement));
            }
            if (statement is DropCallbackTriggersStatement)
            {
                return(VisitDropCallbackTrigger((DropCallbackTriggersStatement)statement));
            }
            if (statement is DropSequenceStatement)
            {
                return(VisitDropSequence((DropSequenceStatement)statement));
            }
            if (statement is DropProcedureStatement)
            {
                return(VisitDropProcedure((DropProcedureStatement)statement));
            }
            if (statement is DropFunctionStatement)
            {
                return(VisitDropFunction((DropFunctionStatement)statement));
            }
            if (statement is DropRoleStatement)
            {
                return(VisitDropRole((DropRoleStatement)statement));
            }
            if (statement is DropUserStatement)
            {
                return(VisitDropUser((DropUserStatement)statement));
            }
            if (statement is DropTriggerStatement)
            {
                return(VisitDropTrigger((DropTriggerStatement)statement));
            }
            if (statement is DropTypeStatement)
            {
                return(VisitDropType((DropTypeStatement)statement));
            }

            // ALTER
            if (statement is AlterTableStatement)
            {
                return(VisitAlterTable((AlterTableStatement)statement));
            }
            if (statement is AlterUserStatement)
            {
                return(VisitAlterUser((AlterUserStatement)statement));
            }

            if (statement is GrantPrivilegesStatement)
            {
                return(VisitGrantPrivilege((GrantPrivilegesStatement)statement));
            }
            if (statement is GrantRoleStatement)
            {
                return(VisitGrantRole((GrantRoleStatement)statement));
            }
            if (statement is RevokePrivilegesStatement)
            {
                return(VisitRevokePrivilege((RevokePrivilegesStatement)statement));
            }
            // TODO: Revoke role statement

            // DECLARE
            if (statement is DeclareCursorStatement)
            {
                return(VisitDeclareCursor((DeclareCursorStatement)statement));
            }
            if (statement is DeclareVariableStatement)
            {
                return(VisitDeclareVariable((DeclareVariableStatement)statement));
            }
            if (statement is DeclareExceptionStatement)
            {
                return(VisitDeclareException((DeclareExceptionStatement)statement));
            }

            if (statement is CallStatement)
            {
                return(VisitCall((CallStatement)statement));
            }

            if (statement is InsertStatement)
            {
                return(VisitInsert((InsertStatement)statement));
            }
            if (statement is InsertSelectStatement)
            {
                return(VisitInsertSelect((InsertSelectStatement)statement));
            }
            if (statement is SelectStatement)
            {
                return(VisitSelect((SelectStatement)statement));
            }
            if (statement is SelectIntoStatement)
            {
                return(VisitSelectInto((SelectIntoStatement)statement));
            }
            if (statement is DeleteStatement)
            {
                return(VisitDelete((DeleteStatement)statement));
            }
            if (statement is UpdateStatement)
            {
                return(VisitUpdate((UpdateStatement)statement));
            }
            if (statement is UpdateFromCursorStatement)
            {
                return(VisitUpdateFromCursor((UpdateFromCursorStatement)statement));
            }
            if (statement is ShowStatement)
            {
                return(VisitShow((ShowStatement)statement));
            }

            // Cursors
            if (statement is OpenStatement)
            {
                return(VisitOpen((OpenStatement)statement));
            }
            if (statement is FetchStatement)
            {
                return(VisitFetch((FetchStatement)statement));
            }
            if (statement is CloseStatement)
            {
                return(VisitClose((CloseStatement)statement));
            }

            // Session Commands
            if (statement is CommitStatement)
            {
                return(VisitCommit((CommitStatement)statement));
            }
            if (statement is RollbackStatement)
            {
                return(VisitRollback((RollbackStatement)statement));
            }
            if (statement is SetStatement)
            {
                return(VisitSet((SetStatement)statement));
            }

            if (statement is AssignVariableStatement)
            {
                return(VisitAssignVariable((AssignVariableStatement)statement));
            }

            // Blocks
            if (statement is CodeBlockStatement)
            {
                return(VisitCodeBlock((CodeBlockStatement)statement));
            }

            // In-Block
            if (statement is LoopControlStatement)
            {
                return(VisitLoopControl((LoopControlStatement)statement));
            }
            if (statement is ReturnStatement)
            {
                return(VisitReturn((ReturnStatement)statement));
            }
            if (statement is GoToStatement)
            {
                return(VisitGoTo((GoToStatement)statement));
            }
            if (statement is RaiseStatement)
            {
                return(VisitRaise((RaiseStatement)statement));
            }

            if (statement is ConditionStatement)
            {
                return(VisitCondition((ConditionStatement)statement));
            }

            if (statement is IVisitableStatement)
            {
                return(((IVisitableStatement)statement).Accept(this));
            }

            return(statement);
        }
Пример #30
0
 public CreateFunctionStatement(ObjectName functionName, SqlType returnType, SqlStatement body)
     : this(functionName, returnType, null, body)
 {
 }
Пример #31
0
        private void Dispose(bool disposing)
        {
            if (disposing) {
                if (Assertions != null)
                    Assertions.Dispose();
                if (Actions != null)
                    (Actions as IDisposable).Dispose();
            }

            Statement = null;
            Parent = null;
            Request = null;
            Result = null;
            Cursor = null;
            Assertions = null;
            Actions = null;
        }
Пример #32
0
 public StatementContext(IContext parent, SqlStatement statement)
     : this(parent, statement.StatementName, statement)
 {
 }
Пример #33
0
        private SqlStatement FindInTree(SqlStatement root, string label)
        {
            var statement = root;
            while (statement != null) {
                if (statement is CodeBlockStatement) {
                    var block = (CodeBlockStatement) statement;
                    if (String.Equals(label, block.Label))
                        return statement;

                    foreach (var child in block.Statements) {
                        var found = FindInTree(child, label);
                        if (found != null)
                            return found;
                    }
                }

                statement = statement.Parent;
            }

            return null;
        }
Пример #34
0
 public static bool HasReturn(SqlStatement statement)
 {
     var visitor = new ReturnChecker();
     return visitor.Verify(statement);
 }