public static SqlQueryExpression Form(IContext context, PlSqlParser.SubqueryContext subquery, out SqlParseIntoClause into) { var query = Form(context, subquery.subqueryBasicElements(), out @into); var opPart = subquery.subquery_operation_part(); if (opPart.Length > 0) { if (@into != null) { throw new InvalidOperationException("Cannot SELECT INTO in a composite query."); } foreach (var part in opPart) { CompositeFunction function; if (part.MINUS() != null || part.EXCEPT() != null) { function = CompositeFunction.Except; } else if (part.UNION() != null) { function = CompositeFunction.Union; } else if (part.INTERSECT() != null) { function = CompositeFunction.Intersect; } else { throw new ParseCanceledException("Invalid composite function."); } bool isAll = part.ALL() != null; var next = Form(context, part.subqueryBasicElements()); var prev = query.NextComposite; if (prev == null) { prev = new SqlQueryExpressionComposite(function, isAll, next); } else { prev.Expression.NextComposite = new SqlQueryExpressionComposite(function, isAll, next); } query.NextComposite = prev; } } return(query); }
private static SqlQueryExpression Form(IContext context, PlSqlParser.SubqueryBasicElementsContext basicElements, out SqlParseIntoClause into) { var sub = basicElements.subquery(); if (sub != null && !sub.IsEmpty) { return(Form(context, sub, out @into)); } return(Form(context, basicElements.queryBlock(), out @into)); }
private static SqlQueryExpression Form(IContext context, PlSqlParser.QueryBlockContext queryBlock, out SqlParseIntoClause into) { var fromClause = FromClauseBuilder.Build(context, queryBlock.fromClause()); SqlQueryExpressionItem[] columns; if (queryBlock.all != null) { columns = new[] { new SqlQueryExpressionItem(SqlExpression.Reference(new ObjectName("*"))) }; } else { columns = queryBlock.selectedElement().Select(x => SelectElement.BuildColumn(context, x)).ToArray(); } var query = new SqlQueryExpression(); foreach (var column in columns) { query.Items.Add(column); } @into = null; if (queryBlock.DISTINCT() != null || queryBlock.UNIQUE() != null) { query.Distinct = true; } var intoClause = queryBlock.into_clause(); if (intoClause != null) { @into = new SqlParseIntoClause(); if (intoClause.objectName() != null) { @into.TableName = SqlParseUtil.Name.Object(intoClause.objectName()); } else if (intoClause.variable_name() != null) { @into.Variables = intoClause.variable_name().Select(Name.Variable).ToArray(); } } if (fromClause != null) { query.From = fromClause; } var groupBy = queryBlock.groupByClause(); if (groupBy != null && !groupBy.IsEmpty) { query.GroupBy = groupBy.groupByElements().expression() .Select(x => new PlSqlExpressionVisitor(context).Visit(x)).ToList(); var having = groupBy.havingClause(); if (having != null) { query.Having = new PlSqlExpressionVisitor(context).Visit(having.condition()); } } var groupMax = queryBlock.groupMaxClause(); if (groupMax != null && !groupMax.IsEmpty) { var maxColumn = Name.Object(groupMax.objectName()); query.GroupMax = maxColumn; } var whereClause = queryBlock.whereClause(); if (whereClause != null && !whereClause.IsEmpty) { var currentOf = whereClause.current_of_clause(); if (currentOf != null && !currentOf.IsEmpty) { var cursorName = Name.Simple(currentOf.cursor_name()); throw new NotImplementedException(); } else { query.Where = new PlSqlExpressionVisitor(context).Visit(whereClause.conditionWrapper()); } } //TODO: in case of a SELECT INTO cause create a statement return(query); }