Exemplo n.º 1
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var tableName = context.Access().ResolveTableName(TableName);

            if (!context.Access().TableExists(tableName))
            {
                throw new ObjectNotFoundException(tableName);
            }

            var queryExpression = new SqlQueryExpression(new[] { SelectColumn.Glob("*") });

            queryExpression.FromClause.AddTable(tableName.FullName);
            queryExpression.WhereExpression = WherExpression;

            var queryFrom = QueryExpressionFrom.Create(context, queryExpression);
            var queryPlan = context.Query.Context.QueryPlanner().PlanQuery(new QueryInfo(context, queryExpression));

            var columns = new List <SqlAssignExpression>();

            foreach (var assignment in Assignments)
            {
                var columnName = ObjectName.Parse(assignment.ColumnName);

                var refName    = queryFrom.ResolveReference(columnName);
                var expression = assignment.Expression.Prepare(queryFrom.ExpressionPreparer);

                var assign = SqlExpression.Assign(SqlExpression.Reference(refName), expression);
                columns.Add(assign);
            }

            return(new Prepared(tableName, queryPlan, columns.ToArray(), Limit));
        }
Exemplo n.º 2
0
        public static SqlNumber NextValue(IRequest request, SqlString sequenceName)
        {
            var objName      = ObjectName.Parse(sequenceName.ToString());
            var resolvedName = request.Access().ResolveObjectName(DbObjectType.Sequence, objName);

            return(request.Access().GetNextValue(resolvedName));
        }
Exemplo n.º 3
0
        public static SqlNumber CurrentKey(IRequest request, SqlString tableName)
        {
            var tableNameString = tableName.ToString();
            var resolvedName    = request.Access().ResolveTableName(tableNameString);

            return(request.Access().GetCurrentValue(resolvedName));
        }
Exemplo n.º 4
0
        public static SqlNumber CurrentValue(IRequest query, SqlString sequenceName)
        {
            var objName      = ObjectName.Parse(sequenceName.ToString());
            var resolvedName = query.Access().ResolveObjectName(DbObjectType.Sequence, objName);

            return(query.Access().GetCurrentValue(resolvedName));
        }
Exemplo n.º 5
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var tableName = context.Access().ResolveTableName(TableName);

            if (!context.Access().TableExists(tableName))
            {
                throw new ObjectNotFoundException(tableName);
            }

            var queryExp = new SqlQueryExpression(new SelectColumn[] { SelectColumn.Glob("*") });

            queryExp.FromClause.AddTable(tableName.FullName);
            queryExp.WhereExpression = WhereExpression;

            var queryInfo = new QueryInfo(context, queryExp);

            if (Limit > 0)
            {
                queryInfo.Limit = new QueryLimit(Limit);
            }

            var queryPlan = context.Query.Context.QueryPlanner().PlanQuery(queryInfo);

            return(new Prepared(tableName, queryPlan));
        }
Exemplo n.º 6
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var tableName = context.Access().ResolveTableName(TableName);
            if (!context.Access().TableExists(tableName))
                throw new ObjectNotFoundException(tableName);

            var queryExpression = new SqlQueryExpression(new[]{SelectColumn.Glob("*") });
            queryExpression.FromClause.AddTable(tableName.FullName);
            queryExpression.WhereExpression = WherExpression;

            var queryFrom = QueryExpressionFrom.Create(context, queryExpression);
            var queryPlan = context.Query.Context.QueryPlanner().PlanQuery(new QueryInfo(context, queryExpression));

            var columns = new List<SqlAssignExpression>();
            foreach (var assignment in Assignments) {
                var columnName = ObjectName.Parse(assignment.ColumnName);

                var refName = queryFrom.ResolveReference(columnName);
                var expression = assignment.Expression.Prepare(queryFrom.ExpressionPreparer);

                var assign = SqlExpression.Assign(SqlExpression.Reference(refName), expression);
                columns.Add(assign);
            }

            return new Prepared(tableName, queryPlan, columns.ToArray(), Limit);
        }
Exemplo n.º 7
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var reference = ReferenceExpression;

            if (ReferenceExpression is SqlReferenceExpression)
            {
                var referenceName = ((SqlReferenceExpression)ReferenceExpression).ReferenceName;
                if (referenceName.Parent == null &&
                    context.Context.CursorExists(referenceName.Name))
                {
                    reference = SqlExpression.VariableReference(referenceName.Name);
                }
                else
                {
                    var tableName = context.Access().ResolveTableName(referenceName);
                    if (!context.Access().TableExists(tableName))
                    {
                        throw new ObjectNotFoundException(tableName, "Reference table for the FETCH INTO clause was not found.");
                    }

                    reference = SqlExpression.Reference(tableName);
                }
            }

            return(new FetchIntoStatement(CursorName, Direction, OffsetExpression, reference));
        }
Exemplo n.º 8
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var schemaName  = context.Access().ResolveSchemaName(TriggerName.ParentName);
            var triggerName = new ObjectName(schemaName, TriggerName.Name);

            var tableName = context.Access().ResolveTableName(TableName);

            return(new CreateTriggerStatement(triggerName, tableName, Body, EventTime, EventType));
        }
Exemplo n.º 9
0
        public override ITable Evaluate(IRequest context)
        {
            // Is the result available in the context?
            var childTable = context.Access().GetCachedTable(Id.ToString());
            if (childTable == null) {
                // No so evaluate the child and cache it
                childTable = Child.Evaluate(context);
                context.Access().CacheTable(Id.ToString(), childTable);
            }

            return childTable;
        }
Exemplo n.º 10
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var viewName = context.Access().ResolveObjectName(DbObjectType.View, ViewName);

            if (!IfExists &&
                !context.Access().ViewExists(viewName))
            {
                throw new ObjectNotFoundException(ViewName);
            }

            return(new DropViewStatement(viewName, IfExists));
        }
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var tableName = context.Access().ResolveTableName(TableName);

            if (tableName == null)
            {
                throw new ObjectNotFoundException(TableName);
            }

            var columns = new string[0];

            if (ColumnNames != null)
            {
                columns = ColumnNames.ToArray();
            }

            ITableQueryInfo tableQueryInfo = context.Access().GetTableQueryInfo(tableName, null);
            var             fromTable      = new FromTableDirectSource(context.Query.IgnoreIdentifiersCase(), tableQueryInfo, "INSERT_TABLE", tableName, tableName);

            // Get the table we are inserting to
            var insertTable = context.Access().GetTable(tableName);

            if (columns.Length == 0)
            {
                columns = new string[insertTable.TableInfo.ColumnCount];
                for (int i = 0; i < columns.Length; i++)
                {
                    columns[i] = insertTable.TableInfo[i].ColumnName;
                }
            }

            var colIndices  = new int[columns.Length];
            var colResolved = new ObjectName[columns.Length];

            for (int i = 0; i < columns.Length; ++i)
            {
                var inVar = new ObjectName(columns[i]);
                var col   = ResolveColumn(fromTable, inVar);
                int index = insertTable.FindColumn(col);
                if (index == -1)
                {
                    throw new InvalidOperationException(String.Format("Cannot find column '{0}' in table '{1}'.", col, tableName));
                }

                colIndices[i]  = index;
                colResolved[i] = col;
            }


            var queryPlan = context.Context.QueryPlanner().PlanQuery(new QueryInfo(context, QueryExpression));

            return(new Prepared(tableName, colResolved, colIndices, queryPlan));
        }
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var tableName = context.Access().ResolveTableName(TableName);

            if (!context.Access().TableExists(tableName) &&
                !IfExists)
            {
                throw new ObjectNotFoundException(TableName);
            }

            return(new DropTableStatement(tableName, IfExists));
        }
Exemplo n.º 13
0
        /// <summary>
        /// Checks if the target of the invocation is an aggregate function.
        /// </summary>
        /// <param name="query">The query context used to resolve the routine.</param>
        /// <returns>
        /// Returns <c>true</c> if the target routine of the invocation is a <see cref="IFunction"/>
        /// and the <see cref="IFunction.FunctionType"/> is <see cref="FunctionType.Aggregate"/>,
        /// otherwise it returns <c>false</c>.
        /// </returns>
        public bool IsAggregate(IRequest query)
        {
            var resolvedName = query.Access().ResolveObjectName(DbObjectType.Routine, RoutineName);
            var invoke       = new Invoke(resolvedName, Arguments);

            if (query.Access().IsAggregateFunction(invoke, query))
            {
                return(true);
            }

            // Look at parameterss
            return(Arguments.Any(x => x.Value.HasAggregate(query)));
        }
Exemplo n.º 14
0
        public override ITable Evaluate(IRequest context)
        {
            // Is the result available in the context?
            var childTable = context.Access().GetCachedTable(Id.ToString());

            if (childTable == null)
            {
                // No so evaluate the child and cache it
                childTable = Child.Evaluate(context);
                context.Access().CacheTable(Id.ToString(), childTable);
            }

            return(childTable);
        }
Exemplo n.º 15
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var triggerSchemaName = context.Access().ResolveSchemaName(TriggerName.ParentName);
            var triggerName       = new ObjectName(triggerSchemaName, TriggerName.Name);

            var tableName     = context.Access().ResolveTableName(TableName);
            var procedureName = context.Access().ResolveObjectName(DbObjectType.Routine, ProcedureName);

            return(new CreateProcedureTriggerStatement(triggerName, tableName, procedureName, ProcedureArguments, EventTime,
                                                       EventType)
            {
                ReplaceIfExists = ReplaceIfExists,
                Status = Status
            });
        }
Exemplo n.º 16
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            ObjectName tableName = null;

            if (Target == ShowTarget.Table &&
                TableName != null)
            {
                tableName = context.Access().ResolveTableName(TableName);
            }

            if (Target == ShowTarget.Schema)
            {
                return(ShowSchema());
            }
            if (Target == ShowTarget.SchemaTables)
            {
                return(ShowSchemaTables(context.Query.CurrentSchema()));
            }
            if (Target == ShowTarget.Table)
            {
                return(ShowTable(tableName));
            }
            if (Target == ShowTarget.Product)
            {
                return(ShowProduct());
            }

            throw new StatementException(String.Format("The SHOW target {0} is not supported.", Target));
        }
Exemplo n.º 17
0
        /// <summary>
        /// Resolves the routine target of the invocation within the give context.
        /// </summary>
        /// <param name="context">The query context used to resolve the routine.</param>
        /// <remarks>
        /// <para>
        /// If the given <paramref name="context"/> is <c>null</c> this method will
        /// try to resolve the routine towards the
        /// </para>
        /// </remarks>
        /// <returns>
        /// Returns an instance of <see cref="IRoutine"/> that is the target of the invocation.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// If the routine could not be resolved for this call.
        /// </exception>
        public IRoutine ResolveRoutine(IRequest context)
        {
            if (cached != null)
            {
                return(cached);
            }

            if (context == null)
            {
                cached = SystemFunctions.Provider.ResolveFunction(this, null);
            }
            else
            {
                var resolvedName = context.Access().ResolveObjectName(DbObjectType.Routine, RoutineName);
                var invoke       = new Invoke(resolvedName, Arguments);
                cached = context.Query.Access().ResolveRoutine(invoke, context);
            }

            if (cached == null)
            {
                throw new InvalidOperationException(String.Format("Unable to resolve the call {0} to a function", this));
            }

            return(cached);
        }
Exemplo n.º 18
0
        public override ITable Evaluate(IRequest context)
        {
            ITable childTable = Child.Evaluate(context);

            context.Access().CacheTable(MarkName, childTable);
            return(childTable);
        }
        private TableInfo CreateTableInfo(IRequest context)
        {
            var tableName = context.Access().ResolveTableName(TableName);

            var idColumnCount = Columns.Count(x => x.IsIdentity);

            if (idColumnCount > 1)
            {
                throw new InvalidOperationException("More than one IDENTITY column specified.");
            }

            bool ignoreCase    = context.Query.IgnoreIdentifiersCase();
            var  columnChecker = new TableColumnChecker(Columns, ignoreCase);


            var tableInfo = new TableInfo(tableName);

            foreach (var column in Columns)
            {
                var columnInfo = CreateColumnInfo(context, tableName.Name, column, columnChecker);

                if (column.IsIdentity)
                {
                    columnInfo.DefaultExpression = SqlExpression.FunctionCall("UNIQUEKEY", new SqlExpression[] {
                        SqlExpression.Constant(tableName.ToString())
                    });
                }

                tableInfo.AddColumn(columnInfo);
            }

            return(tableInfo);
        }
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var viewName = context.Access().ResolveTableName(ViewName);

            var queryFrom = QueryExpressionFrom.Create(context, QueryExpression);
            var queryPlan = context.Query.Context.QueryPlanner().PlanQuery(new QueryInfo(context, QueryExpression));

            var colList = ColumnNames == null ? new string[0] : ColumnNames.ToArray();

            // Wrap the result around a SubsetNode to alias the columns in the
            // table correctly for this view.
            int sz             = colList.Length;
            var originalNames  = queryFrom.GetResolvedColumns();
            var newColumnNames = new ObjectName[originalNames.Length];

            if (sz > 0)
            {
                if (sz != originalNames.Length)
                {
                    throw new InvalidOperationException("Column list is not the same size as the columns selected.");
                }

                for (int i = 0; i < sz; ++i)
                {
                    var colName = colList[i];
                    newColumnNames[i] = new ObjectName(viewName, colName);
                }
            }
            else
            {
                sz = originalNames.Length;
                for (int i = 0; i < sz; ++i)
                {
                    newColumnNames[i] = new ObjectName(viewName, originalNames[i].Name);
                }
            }

            // Check there are no repeat column names in the table.
            for (int i = 0; i < sz; ++i)
            {
                var columnName = newColumnNames[i];
                for (int n = i + 1; n < sz; ++n)
                {
                    if (newColumnNames[n].Equals(columnName))
                    {
                        throw new InvalidOperationException(String.Format("Duplicate column name '{0}' in view. A view may not contain duplicate column names.", columnName));
                    }
                }
            }

            // Wrap the plan around a SubsetNode plan
            queryPlan = new SubsetNode(queryPlan, originalNames, newColumnNames);

            return(new Prepared(viewName, QueryExpression, queryPlan, ReplaceIfExists));
        }
Exemplo n.º 21
0
        public static ColumnChecker Default(IRequest context, ObjectName tableName)
        {
            var table = context.Access().GetTable(tableName);
            if (table == null)
                throw new InvalidOperationException(String.Format("Table '{0}' not found in the context.", tableName));

            var tableInfo = table.TableInfo;
            var ignoreCase = context.Query.IgnoreIdentifiersCase();

            return new DefaultChecker(tableInfo, ignoreCase);
        }
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var triggerName = context.Access().ResolveObjectName(DbObjectType.Trigger, TriggerName);
            var action      = Action;

            if (action is IStatementPreparable)
            {
                action = (IAlterTriggerAction)(action as IStatementPreparable).Prepare(context);
            }

            return(new AlterTriggerStatement(triggerName, action));
        }
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var tableName = context.Access().ResolveTableName(TableName);
            var action    = Action;

            if (action is IStatementPreparable)
            {
                action = (IAlterTableAction)((IStatementPreparable)Action).Prepare(context);
            }

            return(new AlterTableStatement(tableName, action));
        }
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var objectName = context.Access().ResolveTableName(ObjectName);

            if (objectName == null)
            {
                throw new ObjectNotFoundException(ObjectName);
            }

            var columns = (Columns != null ? Columns.ToArray() : null);

            return(new RevokePrivilegesStatement(Grantee, Privileges, GrantOption, objectName, columns));
        }
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var schemaName = context.Access().ResolveSchemaName(TypeName.ParentName);
            var typeName   = new ObjectName(schemaName, TypeName.Name);

            var statement = new CreateTypeStatement(typeName, Members, ReplaceIfExists)
            {
                IsSealed   = IsSealed,
                IsAbstract = IsAbstract
            };

            var parentName = ParentTypeName;

            if (parentName != null)
            {
                parentName = context.Access().ResolveObjectName(DbObjectType.Type, parentName);
            }

            statement.ParentTypeName = parentName;

            return(statement);
        }
Exemplo n.º 26
0
        public static ColumnChecker Default(IRequest context, ObjectName tableName)
        {
            var table = context.Access().GetTable(tableName);

            if (table == null)
            {
                throw new InvalidOperationException(String.Format("Table '{0}' not found in the context.", tableName));
            }

            var tableInfo  = table.TableInfo;
            var ignoreCase = context.Query.IgnoreIdentifiersCase();

            return(new DefaultChecker(tableInfo, ignoreCase));
        }
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var schemaName = context.Access().ResolveSchemaName(SequenceName.ParentName);
            var seqName    = new ObjectName(schemaName, SequenceName.Name);

            return(new CreateSequenceStatement(seqName)
            {
                StartWith = StartWith,
                IncrementBy = IncrementBy,
                Cache = Cache,
                MinValue = MinValue,
                MaxValue = MaxValue,
                Cycle = Cycle
            });
        }
Exemplo n.º 28
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var queryPlan = context.Query.Context.QueryPlanner().PlanQuery(new QueryInfo(context, QueryExpression));

            if (Reference is SqlReferenceExpression)
            {
                var objName = ((SqlReferenceExpression)Reference).ReferenceName;
                objName = context.Access().ResolveObjectName(objName);

                return(new SelectIntoTable(objName, queryPlan));
            }

            if (Reference is SqlVariableReferenceExpression)
            {
                var refName = ((SqlVariableReferenceExpression)Reference).VariableName;
                return(new SelectIntoVariable(new[] { refName }, queryPlan));
            }

            if (Reference is SqlTupleExpression)
            {
                var exps = ((SqlTupleExpression)Reference).Expressions;
                if (exps == null || exps.Length == 0)
                {
                    throw new StatementException("Empty tuple in SELECT INTO");
                }

                var variables = new List <string>();

                for (int i = 0; i < exps.Length; i++)
                {
                    if (!(exps[i] is SqlVariableReferenceExpression))
                    {
                        throw new StatementException("Found an invalid expression in the tuple.");
                    }

                    var varName = ((SqlVariableReferenceExpression)exps[i]).VariableName;
                    variables.Add(varName);
                }

                return(new SelectIntoVariable(variables.ToArray(), queryPlan));
            }

            // Other (impossible) case...
            throw new NotSupportedException();
        }
Exemplo n.º 29
0
        public override ITable Evaluate(IRequest context)
        {
            // Evaluate the child branch,
            var result = Child.Evaluate(context);
            // Get the table of the complete mark name,
            var completeLeft = context.Access().GetCachedTable(MarkerName);

            // The rows in 'complete_left' that are outside (not in) the rows in the
            // left result.
            var outside = completeLeft.OuterJoin(result);

            // Create an OuterTable
            var outerTable = OuterTable.Create(result);

            outerTable.MergeIn(outside);

            // Return the outer table
            return(outerTable);
        }
Exemplo n.º 30
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            ObjectName tableName = null;

            if (Target == ShowTarget.Table &&
                TableName != null) {
                tableName = context.Access().ResolveTableName(TableName);
            }

            if (Target == ShowTarget.Schema)
                return ShowSchema();
            if (Target == ShowTarget.SchemaTables)
                return ShowSchemaTables(context.Query.CurrentSchema());
            if (Target == ShowTarget.Table)
                return ShowTable(tableName);
            if (Target == ShowTarget.Product)
                return ShowProduct();

            throw new StatementException(String.Format("The SHOW target {0} is not supported.", Target));
        }
Exemplo n.º 31
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var schemaName   = context.Access().ResolveSchemaName(ProcedureName.ParentName);
            var functionName = new ObjectName(schemaName, ProcedureName.Name);

            var parameters = new List <RoutineParameter>();

            if (Parameters != null)
            {
                foreach (var parameter in Parameters)
                {
                    parameters.Add((RoutineParameter)((IStatementPreparable)parameter).Prepare(context));
                }
            }

            return(new CreateExternalProcedureStatement(functionName, parameters.ToArray(), ExternalReference)
            {
                ReplaceIfExists = ReplaceIfExists
            });
        }
Exemplo n.º 32
0
        private ITable Evaluate(IRequest context, SqlExpression[] args, out IList <IDbObject> refs)
        {
            try {
                var prepared  = PrepareQuery(args);
                var queryPlan = context.Query.Context.QueryPlanner().PlanQuery(new QueryInfo(context, prepared));
                var refNames  = queryPlan.DiscoverAccessedResources();

                refs = refNames.Select(x => context.Access().FindObject(x.ResourceName)).ToArray();
                context.Query.Session.Enter(refs, AccessType.Read);

                var tables = refs.Where(x => x.ObjectInfo.ObjectType == DbObjectType.Table).Select(x => x.ObjectInfo.FullName);
                foreach (var table in tables)
                {
                    context.Query.Session.Transaction.GetTableManager().SelectTable(table);
                }

                return(queryPlan.Evaluate(context));
            } catch (CursorException) {
                throw;
            } catch (Exception ex) {
                throw new CursorException(CursorInfo.CursorName, ex);
            }
        }
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var schemaName   = context.Access().ResolveSchemaName(FunctionName.ParentName);
            var functionName = new ObjectName(schemaName, FunctionName.Name);
            var returnType   = ReturnType.Resolve(context);

            var parameters = new List <RoutineParameter>();

            if (Parameters != null)
            {
                foreach (var parameter in Parameters)
                {
                    parameters.Add((RoutineParameter)((IStatementPreparable)parameter).Prepare(context));
                }
            }

            var body = (PlSqlBlockStatement)Body.Prepare(context);

            return(new CreateFunctionStatement(functionName, returnType, parameters.ToArray(), body)
            {
                ReplaceIfExists = ReplaceIfExists
            });
        }
Exemplo n.º 34
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var queryPlan = context.Query.Context.QueryPlanner().PlanQuery(new QueryInfo(context, QueryExpression));

            if (Reference is SqlReferenceExpression) {
                var objName = ((SqlReferenceExpression) Reference).ReferenceName;
                objName = context.Access().ResolveObjectName(objName);

                return new SelectIntoTable(objName, queryPlan);
            }

            if (Reference is SqlVariableReferenceExpression) {
                var refName = ((SqlVariableReferenceExpression) Reference).VariableName;
                return new SelectIntoVariable(new[] { refName}, queryPlan);
            }

            if (Reference is SqlTupleExpression) {
                var exps = ((SqlTupleExpression) Reference).Expressions;
                if (exps == null || exps.Length == 0)
                    throw new StatementException("Empty tuple in SELECT INTO");

                var variables = new List<string>();

                for (int i = 0; i < exps.Length; i++) {
                    if (!(exps[i] is SqlVariableReferenceExpression))
                        throw new StatementException("Found an invalid expression in the tuple.");

                    var varName = ((SqlVariableReferenceExpression) exps[i]).VariableName;
                    variables.Add(varName);
                }

                return new SelectIntoVariable(variables.ToArray(), queryPlan);
            }

            // Other (impossible) case...
            throw new NotSupportedException();
        }
Exemplo n.º 35
0
        public static Field NewObject(IRequest context, Field typeName, Field[] args = null)
        {
            if (Field.IsNullField(typeName))
            {
                throw new ArgumentNullException("typeName");
            }

            if (!(typeName.Type is StringType))
            {
                throw new ArgumentException("The type name argument must be of string type.");
            }

            var argExp = new SqlExpression[args == null ? 0 : args.Length];

            if (args != null)
            {
                argExp = args.Select(SqlExpression.Constant).Cast <SqlExpression>().ToArray();
            }

            var type = context.Access().ResolveUserType(typeName.Value.ToString());

            if (type == null)
            {
                throw new InvalidOperationException(String.Format("The type '{0}' was not defined.", typeName));
            }

            if (!(type is UserType))
            {
                throw new InvalidOperationException(String.Format("The type '{0}' is not a user-defined type", typeName));
            }

            var userType = (UserType)type;

            var obj = userType.NewObject(context, argExp);

            return(Field.Object(userType, obj));
        }
Exemplo n.º 36
0
 object IStatementPreparable.Prepare(IRequest request)
 {
     var name = request.Access().ResolveObjectName(DbObjectType.Trigger, Name);
     return new RenameTriggerAction(name);
 }
Exemplo n.º 37
0
 protected override SqlStatement PrepareStatement(IRequest context)
 {
     var procedureName = context.Access().ResolveObjectName(DbObjectType.Routine, ProcedureName);
     return new DropProcedureStatement(procedureName, IfExists);
 }
Exemplo n.º 38
0
 public static SqlNumber UniqueKey(IRequest request, SqlString tableName)
 {
     var tableNameString = tableName.ToString();
     var resolvedName = request.Access().ResolveTableName(tableNameString);
     return request.Access().GetNextValue(resolvedName);
 }
Exemplo n.º 39
0
        /// <summary>
        /// Checks if the target of the invocation is an aggregate function.
        /// </summary>
        /// <param name="query">The query context used to resolve the routine.</param>
        /// <returns>
        /// Returns <c>true</c> if the target routine of the invocation is a <see cref="IFunction"/>
        /// and the <see cref="IFunction.FunctionType"/> is <see cref="FunctionType.Aggregate"/>,
        /// otherwise it returns <c>false</c>.
        /// </returns>
        public bool IsAggregate(IRequest query)
        {
            var resolvedName = query.Access().ResolveObjectName(DbObjectType.Routine, RoutineName);
            var invoke = new Invoke(resolvedName, Arguments);

            if (query.Access().IsAggregateFunction(invoke, query))
                return true;

            // Look at parameterss
            return Arguments.Any(x => x.Value.HasAggregate(query));
        }
Exemplo n.º 40
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var schemaName = context.Access().ResolveSchemaName(FunctionName.ParentName);
            var functionName = new ObjectName(schemaName, FunctionName.Name);
            var returnType = ReturnType.Resolve(context);

            var parameters = new List<RoutineParameter>();
            if (Parameters != null) {
                foreach (var parameter in Parameters) {
                    parameters.Add((RoutineParameter)((IStatementPreparable)parameter).Prepare(context));
                }
            }

            var body = (PlSqlBlockStatement) Body.Prepare(context);
            return new CreateFunctionStatement(functionName, returnType, parameters.ToArray(), body) {
                ReplaceIfExists = ReplaceIfExists
            };
        }
Exemplo n.º 41
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var tableName = context.Access().ResolveTableName(TableName);
            var action = Action;
            if (action is IStatementPreparable)
                action = (IAlterTableAction) ((IStatementPreparable) Action).Prepare(context);

            return new AlterTableStatement(tableName, action);
        }
Exemplo n.º 42
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var objectName = context.Access().ResolveTableName(ObjectName);

            if (objectName == null)
                throw new ObjectNotFoundException(ObjectName);

            var columns = (Columns != null ? Columns.ToArray() : null);
            return new RevokePrivilegesStatement(Grantee, Privileges, GrantOption, objectName, columns);
        }
Exemplo n.º 43
0
 private IQueryPlanNode CreateChildNode(IRequest context)
 {
     return context.Access().GetViewQueryPlan(ViewName);
 }
Exemplo n.º 44
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var schemaName = context.Access().ResolveSchemaName(TypeName.ParentName);
            var typeName = new ObjectName(schemaName, TypeName.Name);

            var statement = new CreateTypeStatement(typeName, Members, ReplaceIfExists) {
                IsSealed = IsSealed,
                IsAbstract = IsAbstract
            };

            var parentName = ParentTypeName;
            if (parentName != null) {
                parentName = context.Access().ResolveObjectName(DbObjectType.Type, parentName);
            }

            statement.ParentTypeName = parentName;

            return statement;
        }
Exemplo n.º 45
0
 public override SqlType Resolve(IRequest context)
 {
     return context.Access().ResolveFieldType(FieldName);
 }
Exemplo n.º 46
0
 public static SqlNumber NextValue(IRequest request, SqlString sequenceName)
 {
     var objName = ObjectName.Parse(sequenceName.ToString());
     var resolvedName = request.Access().ResolveObjectName(DbObjectType.Sequence, objName);
     return request.Access().GetNextValue(resolvedName);
 }
Exemplo n.º 47
0
        private TableInfo CreateTableInfo(IRequest context)
        {
            var tableName = context.Access().ResolveTableName(TableName);

            var idColumnCount = Columns.Count(x => x.IsIdentity);
            if (idColumnCount > 1)
                throw new InvalidOperationException("More than one IDENTITY column specified.");

            bool ignoreCase = context.Query.IgnoreIdentifiersCase();
            var columnChecker = new TableColumnChecker(Columns, ignoreCase);

            var tableInfo = new TableInfo(tableName);

            foreach (var column in Columns) {
                var columnInfo = CreateColumnInfo(context, tableName.Name, column, columnChecker);

                if (column.IsIdentity)
                    columnInfo.DefaultExpression = SqlExpression.FunctionCall("UNIQUEKEY", new SqlExpression[] {
                        SqlExpression.Constant(tableName.ToString())
                    });

                tableInfo.AddColumn(columnInfo);
            }

            return tableInfo;
        }
Exemplo n.º 48
0
 public static SqlNumber CurrentValue(IRequest query, SqlString sequenceName)
 {
     var objName = ObjectName.Parse(sequenceName.ToString());
     var resolvedName = query.Access().ResolveObjectName(DbObjectType.Sequence, objName);
     return query.Access().GetCurrentValue(resolvedName);
 }
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var schemaName = context.Access().ResolveSchemaName(ProcedureName.ParentName);
            var functionName = new ObjectName(schemaName, ProcedureName.Name);

            var parameters = new List<RoutineParameter>();
            if (Parameters != null) {
                foreach (var parameter in Parameters) {
                    parameters.Add((RoutineParameter)((IStatementPreparable)parameter).Prepare(context));
                }
            }

            return new CreateExternalProcedureStatement(functionName, parameters.ToArray(), ExternalReference) {
                ReplaceIfExists = ReplaceIfExists
            };
        }
Exemplo n.º 50
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var tableName = context.Access().ResolveTableName(TableName);

            if (!context.Access().TableExists(tableName))
                throw new ObjectNotFoundException(tableName);

            var queryExp = new SqlQueryExpression(new SelectColumn[] {SelectColumn.Glob("*") });
            queryExp.FromClause.AddTable(tableName.FullName);
            queryExp.WhereExpression = WhereExpression;

            var queryInfo = new QueryInfo(context, queryExp);
            if (Limit > 0)
                queryInfo.Limit = new QueryLimit(Limit);

            var queryPlan = context.Query.Context.QueryPlanner().PlanQuery(queryInfo);

            return new Prepared(tableName, queryPlan);
        }
Exemplo n.º 51
0
 protected override SqlStatement PrepareStatement(IRequest context)
 {
     var tableName = context.Access().ResolveTableName(TableName);
     return new DeleteCurrentStatement(tableName, CursorName);
 }
Exemplo n.º 52
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var triggerName = context.Access().ResolveObjectName(DbObjectType.Trigger, TriggerName);
            var action = Action;
            if (action is IStatementPreparable)
                action = (IAlterTriggerAction) (action as IStatementPreparable).Prepare(context);

            return new AlterTriggerStatement(triggerName, action);
        }
Exemplo n.º 53
0
 protected override SqlStatement PrepareStatement(IRequest context)
 {
     var triggerName = context.Access().ResolveObjectName(DbObjectType.Trigger, TriggerName);
     return new DropTriggerStatement(triggerName);
 }
Exemplo n.º 54
0
        public static Field NewObject(IRequest context, Field typeName, Field[] args = null)
        {
            if (Field.IsNullField(typeName))
                throw new ArgumentNullException("typeName");

            if (!(typeName.Type is StringType))
                throw new ArgumentException("The type name argument must be of string type.");

            var argExp = new SqlExpression[args == null ? 0 : args.Length];

            if (args != null) {
                argExp = args.Select(SqlExpression.Constant).Cast<SqlExpression>().ToArray();
            }

            var type = context.Access().ResolveUserType(typeName.Value.ToString());
            if (type == null)
                throw new InvalidOperationException(String.Format("The type '{0}' was not defined.", typeName));

            if (!(type is UserType))
                throw new InvalidOperationException(String.Format("The type '{0}' is not a user-defined type", typeName));

            var userType = (UserType) type;

            var obj = userType.NewObject(context, argExp);

            return Field.Object(userType, obj);
        }
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var triggerSchemaName = context.Access().ResolveSchemaName(TriggerName.ParentName);
            var triggerName = new ObjectName(triggerSchemaName, TriggerName.Name);

            var tableName = context.Access().ResolveTableName(TableName);
            var procedureName = context.Access().ResolveObjectName(DbObjectType.Routine, ProcedureName);

            return new CreateProcedureTriggerStatement(triggerName, tableName, procedureName, ProcedureArguments, EventTime,
                EventType) {
                    ReplaceIfExists = ReplaceIfExists,
                    Status = Status
                };
        }
Exemplo n.º 56
0
        protected override SqlStatement PrepareStatement(IRequest context)
        {
            var viewName = context.Access().ResolveObjectName(DbObjectType.View, ViewName);

            if (!IfExists &&
                !context.Access().ViewExists(viewName))
                throw new ObjectNotFoundException(ViewName);

            return new DropViewStatement(viewName, IfExists);
        }
Exemplo n.º 57
0
 public override SqlType Resolve(IRequest context)
 {
     return context.Access().ResolveRowType(ObjectName);
 }
Exemplo n.º 58
0
        public override ITable Evaluate(IRequest context)
        {
            // Evaluate the child branch,
            var result = Child.Evaluate(context);
            // Get the table of the complete mark name,
            var completeLeft = context.Access().GetCachedTable(MarkerName);

            // The rows in 'complete_left' that are outside (not in) the rows in the
            // left result.
            var outside = completeLeft.OuterJoin(result);

            // Create an OuterTable
            var outerTable = OuterTable.Create(result);
            outerTable.MergeIn(outside);

            // Return the outer table
            return outerTable;
        }
Exemplo n.º 59
0
        /// <summary>
        /// Resolves the routine target of the invocation within the give context.
        /// </summary>
        /// <param name="context">The query context used to resolve the routine.</param>
        /// <remarks>
        /// <para>
        /// If the given <paramref name="context"/> is <c>null</c> this method will
        /// try to resolve the routine towards the 
        /// </para>
        /// </remarks>
        /// <returns>
        /// Returns an instance of <see cref="IRoutine"/> that is the target of the invocation.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// If the routine could not be resolved for this call.
        /// </exception>
        public IRoutine ResolveRoutine(IRequest context)
        {
            if (cached != null)
                return cached;

            if (context == null) {
                cached = SystemFunctions.Provider.ResolveFunction(this, null);
            } else {
                var resolvedName = context.Access().ResolveObjectName(DbObjectType.Routine, RoutineName);
                var invoke = new Invoke(resolvedName, Arguments);
                cached = context.Query.Access().ResolveRoutine(invoke, context);
            }

            if (cached == null)
                throw new InvalidOperationException(String.Format("Unable to resolve the call {0} to a function", this));

            return cached;
        }
Exemplo n.º 60
0
 public ITable Evaluate(IRequest context)
 {
     var t = context.Access().GetTable(TableName);
     return AliasName != null ? new ReferenceTable(t, AliasName) : t;
 }