private SelectStatement ResolveStatement(ReadOnlySpan <TSQLToken> tokens, ref int fileIndex, CompilerContext context)
        {
            statement = new SelectStatement();

            SkipSelectPrequelStatements(tokens, ref fileIndex, context);

            statement.Expression = DetermineSourceColumns(tokens, ref fileIndex, context);

            ResolveIntoClause(tokens, ref fileIndex, context);

            var objects = StatementResolveHelper.ResolveFromStatement(tokens, ref fileIndex, context);

            AddObjectsToContext(objects, context);

            Beautifier.BeautifyColumns(statement.Expression, context);

            AddSynonymousObjects(objects);

            StatementResolveHelper.ResolveWhereStatement(tokens, ref fileIndex, context);

            ResolveGroupByClause(tokens, ref fileIndex, context);

            ResolveOrderByClause(tokens, ref fileIndex, context);

            //Resolve FOR-CLAUSE

            ResolveUnionclause(tokens, ref fileIndex, context);

            PopObjectsFromContextStack(context);

            statement.Expression.Name = "SELECT";

            return(statement);
        }
Beispiel #2
0
        public void ShouldResolveWhereStatement_03()
        {
            //Arrange
            string                   whereStatement = "WHERE someTable.someColumn in ('string_01', 'string_02', 'string_03')";
            CompilerContext          context        = new CompilerContext("xUnit", "irrelevant", "irrelevant", true);
            int                      fileIndex      = 0;
            ReadOnlySpan <TSQLToken> tokens         = TSQLTokenizer.ParseTokens(whereStatement).ToArray();

            //Act
            StatementResolveHelper.ResolveWhereStatement(tokens, ref fileIndex, context);

            //Assert
            Assert.True(tokens.Length == fileIndex);
        }
Beispiel #3
0
        public void ShouldResolveWhereStatement_02()
        {
            //Arrange
            string whereStatement = "WHERE stamm6.deleted = 0 AND object14.deleted = 0 and feld10 is not null AND (ISNUMERIC(LEFT([feld34], 4)) = 1 " +
                                    "OR([feld34] LIKE '[0-9][0-9][0-9][0-9][A-Z][ ]'))";
            CompilerContext          context   = new CompilerContext("xUnit", "irrelevant", "irrelevant", true);
            int                      fileIndex = 0;
            ReadOnlySpan <TSQLToken> tokens    = TSQLTokenizer.ParseTokens(whereStatement).ToArray();

            //Act
            StatementResolveHelper.ResolveWhereStatement(tokens, ref fileIndex, context);

            //Assert
            Assert.True(tokens.Length == fileIndex);
        }
Beispiel #4
0
        public void ShouldResolveComplexWhereStatement_01()
        {
            //Arrange
            string whereStatement = "where (([Budget Name] LIKE 'B%' AND YEAR([Date])>=2010) " +
                                    "OR     ([Budget Name] LIKE 'P%' AND [Budget Name] >= 'P2011' AND YEAR([Date])>=2011)) " +
                                    "AND     YEAR([Date])<=2010";
            CompilerContext          context   = new CompilerContext("xUnit", "irrelevant", "irrelevant", true);
            int                      fileIndex = 0;
            ReadOnlySpan <TSQLToken> tokens    = TSQLTokenizer.ParseTokens(whereStatement).ToArray();

            //Act
            StatementResolveHelper.ResolveWhereStatement(tokens, ref fileIndex, context);

            //Assert
            Assert.True(tokens.Length == fileIndex);
        }
        public DataManipulation Resolve(ReadOnlySpan <TSQLToken> tokens, ref int fileIndex, CompilerContext context)
        {
            manipulation = new DataManipulation();

            fileIndex++; //skip 'update'

            var targetObject = StatementResolveHelper.ResolveDatabaseObject(tokens, ref fileIndex, context, true);

            fileIndex++; //skip 'set'

            do
            {
                //Resolves the target object. Note that this target can be an alias that is resolved later in the FROM statement
                var target = StatementResolveHelper.ResolveExpression(tokens, ref fileIndex, context);

                AddTargetObject(target, targetObject);

                fileIndex++;                                                                           //skip '='

                var source = StatementResolveHelper.ResolveExpression(tokens, ref fileIndex, context); //resolve source column
                target.ChildExpressions.Add(source);

                manipulation.Expressions.Add(target);

                if (fileIndex < tokens.Length && tokens[fileIndex].Text.Equals(","))
                {
                    fileIndex++; //skip ','
                    continue;
                }
                else
                {
                    break;
                }
            } while (true);

            var objects = StatementResolveHelper.ResolveFromStatement(tokens, ref fileIndex, context);

            AddObjectsToContext(objects, context);

            if (objects.Count > 1)
            {
                targetObject = AssignRealTarget(objects, targetObject);
                var targetSynonymous = new Expression(ExpressionType.COLUMN)
                {
                    Name = Beautifier.EnhanceNotation(targetObject, InternalConstants.WHOLE_OBJECT_SYNONYMOUS),
                    WholeObjectSynonymous = true
                };

                foreach (var dbo in objects)
                {
                    if (!dbo.Type.Equals(DatabaseObjectType.REAL) || dbo.Equals(targetObject))
                    {
                        continue;
                    }
                    var sourceSynonymous = new Expression(ExpressionType.COLUMN)
                    {
                        Name = Beautifier.EnhanceNotation(dbo, InternalConstants.WHOLE_OBJECT_SYNONYMOUS),
                        WholeObjectSynonymous = true
                    };
                    targetSynonymous.ChildExpressions.Add(sourceSynonymous);
                    manipulation.Expressions.Add(targetSynonymous);
                }
            }

            var beautified = new List <Expression>();

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

            manipulation.Expressions = beautified;

            StatementResolveHelper.ResolveWhereStatement(tokens, ref fileIndex, context);

            PopObjectsFromContextStack(context);

            return(manipulation);
        }