コード例 #1
0
        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);
        }
コード例 #2
0
        public void ShouldResolveFromStatementWithFunctionAsDataObject()
        {
            //Arrange
            string                   joinStatement = "FROM fn_helpcollations()";
            CompilerContext          context       = new CompilerContext("xUnit", "irrelevant", "irrelevant", true);
            int                      fileIndex     = 0;
            ReadOnlySpan <TSQLToken> tokens        = TSQLTokenizer.ParseTokens(joinStatement).ToArray();

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

            //Assert
            Assert.True(tokens.Length == fileIndex);
        }
コード例 #3
0
        public void ShouldResolveFromStatement_01()
        {
            //Arrange
            string fromStatement = "FROM ( SELECT HIST.someCol_01, HIST.someCol_02 FROM server_01.db_01.dbo.historyTable HIST) AS R " +
                                   "INNER JOIN DWH.dbo.someDimension SOME_ALIAS_01 ON SOME_ALIAS_01.someCol_03 = some_FK COLLATE Latin1_General_CI_AS " +
                                   "AND SOME_DATE BETWEEN SOME_ALIAS_01.ValidFrom AND SOME_ALIAS_01.ValidUntil";
            CompilerContext          context   = new CompilerContext("xUnit", "irrelevant", "irrelevant", true);
            int                      fileIndex = 0;
            ReadOnlySpan <TSQLToken> tokens    = TSQLTokenizer.ParseTokens(fromStatement).ToArray();

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

            //Assert
            Assert.True(tokens.Length == fileIndex);
        }
コード例 #4
0
        public void ShouldResolveFromStatementWithPivotClause()
        {
            //Arrange
            string joinStatement = "FROM (SELECT DaysToManufacture, StandardCost " +
                                   "      FROM Production.dbo.Product) AS SourceTable " +
                                   "PIVOT( AVG(StandardCost)" +
                                   "FOR DaysToManufacture IN([0], [1], [2], [3], [4])" +
                                   "     ) AS PivotTable";
            CompilerContext          context   = new CompilerContext("xUnit", "irrelevant", "irrelevant", true);
            int                      fileIndex = 0;
            ReadOnlySpan <TSQLToken> tokens    = TSQLTokenizer.ParseTokens(joinStatement).ToArray();

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

            //Assert
            Assert.True(tokens.Length == fileIndex);
        }
コード例 #5
0
        public void ShouldResolveFromStatementwithApplyKeyword_02()
        {
            //Arrange
            string fromStatement = "FROM t1                     " +
                                   "CROSS APPLY                 " +
                                   "(                           " +
                                   "SELECT TOP 3 *              " +
                                   "FROM    t2                  " +
                                   "WHERE   t2.t1_id = t1.id    " +
                                   "ORDER BY                    " +
                                   "       t2.rank DESC         " +
                                   ") t2o                       ";

            CompilerContext          context   = new CompilerContext("xUnit", "irrelevant", "irrelevant", true);
            int                      fileIndex = 0;
            ReadOnlySpan <TSQLToken> tokens    = TSQLTokenizer.ParseTokens(fromStatement).ToArray();

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

            //Assert
            Assert.True(tokens.Length == fileIndex);
        }
コード例 #6
0
        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);
        }