private void ResolveUnionclause(ReadOnlySpan <TSQLToken> tokens, ref int fileIndex, CompilerContext context)
        {
            if (fileIndex < tokens.Length && tokens[fileIndex].Text.ToLower().Equals("union"))
            {
                fileIndex++; //skip "union"

                if (tokens[fileIndex].Text.ToLower().Equals("all"))
                {
                    fileIndex++;
                }

                int openBracketCounter = 0;

                while (tokens[fileIndex].Text.Equals("("))
                {
                    openBracketCounter++;
                    fileIndex++;
                }

                if (!tokens[fileIndex].Text.ToLower().Equals("select"))
                {
                    throw new InvalidSqlException("'union keyword was not followed by a select'");
                }

                SelectStatementResolver innerResolver = new SelectStatementResolver();

                Expression nextUnion = innerResolver.Resolve(tokens, ref fileIndex, context);
                statement.Expression.ChildExpressions.Add(nextUnion);

                while (openBracketCounter > 0)
                {
                    openBracketCounter--;
                    fileIndex++;
                }
            }
        }
示例#2
0
        public DataManipulation Resolve(ReadOnlySpan <TSQLToken> tokens, ref int fileIndex, CompilerContext context)
        {
            manipulation = new DataManipulation();

            fileIndex++; //skip 'insert'

            //TODO Resolve TOP Expression

            if (tokens[fileIndex].Text.ToLower().Equals("into"))
            {
                fileIndex++; //skip 'into'
            }
            targetObject = StatementResolveHelper.ResolveDatabaseObject(tokens, ref fileIndex, context, true);

            DetermineTargetColumns(tokens, ref fileIndex, context);

            //TODO Resolve Table Hint

            if (tokens[fileIndex].Text.Equals("values", StringComparison.InvariantCultureIgnoreCase))
            {
                fileIndex += 2; //skip 'values ('
                DetermineSourceObjects(tokens, ref fileIndex, context);
            }
            else if (tokens[fileIndex].Text.Equals("select", StringComparison.InvariantCultureIgnoreCase))
            {
                var selectStatement = new SelectStatementResolver().Resolve(tokens, ref fileIndex, context);
                sources = selectStatement.ChildExpressions;
            }

            if (targets.Count == 0)
            {
                for (int index = 0; index < sources.Count; index++)
                {
                    Expression resolvedSource;

                    if (sources[index].Type.Equals(ExpressionType.COMPLEX) || sources[index].Type.Equals(ExpressionType.CONSTANT))
                    {
                        continue;
                    }
                    else if (sources[index].Type.Equals(ExpressionType.ALIAS))
                    {
                        resolvedSource = sources[index].ChildExpressions[0];
                    }
                    else
                    {
                        resolvedSource = sources[index];
                    }

                    var singleManipulation = new Expression(ExpressionType.COLUMN)
                    {
                        Name = Beautifier.EnhanceNotation(targetObject, InternalConstants.UNRELATED_COLUMN_NAME)
                    };

                    singleManipulation.ChildExpressions.Add(resolvedSource);
                    manipulation.Expressions.Add(singleManipulation);
                }
            }
            else if (StatementResolveHelper.HaveEqualAmountOfRealExpression(sources, targets))
            {
                for (int index = 0; index < targets.Count; index++)
                {
                    if (!sources[index].Type.Equals(ExpressionType.COLUMN) && !sources[index].Type.Equals(ExpressionType.SCALAR_FUNCTION))
                    {
                        continue;
                    }

                    var singleManipulation = new Expression(ExpressionType.COLUMN)
                    {
                        Name = targets[index].Name
                    };

                    singleManipulation.ChildExpressions.Add(sources[index]);
                    manipulation.Expressions.Add(singleManipulation);
                }
            }
            else
            {
                throw new InvalidSqlException("Amount of targets does not match the number of sources");
            }

            if (fileIndex < tokens.Length && tokens[fileIndex].Text.Equals(";"))
            {
                fileIndex++;
            }

            var beautified = new List <Expression>();

            foreach (var exp in manipulation.Expressions)
            {
                beautified.Add(Beautifier.BeautifyColumns(exp, context));
            }

            manipulation.Expressions = beautified;

            return(manipulation);
        }