public void ShouldResolveRecursiveInsertStatement()
        {
            //Arrange
            string rawTsql = "INSERT INTO BI_STAGE.dbo.ERP_TABLE ([timestamp], [Name], [CustNo], [VendNo]) " +
                             "SELECT [timestamp], [Name], [CustNo], [VendNo] FROM ERP_SERVER.ERP2099PROD.dbo.[some Table]";

            IDataManipulationResolver resolver = new InsertStatementResolver();
            int                      fileIndex = 0;
            CompilerContext          context   = new CompilerContext("xUnit", "stdserver", "stdDatabase", true);
            ReadOnlySpan <TSQLToken> tokens    = TSQLTokenizer.ParseTokens(rawTsql).ToArray();

            //Act
            DataManipulation statement = resolver.Resolve(tokens, ref fileIndex, context);

            //Assert
            Assert.Equal(tokens.Length, fileIndex);
            Assert.Equal("BI_STAGE.dbo.ERP_TABLE.timestamp", statement.Expressions[0].Name);
            Assert.Equal("ERP2099PROD.dbo.some Table.timestamp", statement.Expressions[0].ChildExpressions[0].Name);
        }
        public void ShouldResolveInsertStatement()
        {
            //Arrange
            string rawTsql = "INSERT INTO BI_STAGE.dbo.EventLogTable (targetcolumn, MessageType, MessageTarget, " +
                             "StepName, Message, DetailedMessage, Duration, InsertDate) " +
                             "VALUES (LoadRunId, 'ERROR', 'some_sql_file.sql', 'ParentChild Dimension AccountScheduleTree [DWH]'," +
                             "'Error while creating Parentchild Accountschema for schema ' + @CurrentSchedule," +
                             "ERROR_MESSAGE(), null, GETDATE())";

            IDataManipulationResolver resolver = new InsertStatementResolver();
            int                      fileIndex = 0;
            CompilerContext          context   = new CompilerContext("xUnit", "stdserver", "stdDatabase", true);
            ReadOnlySpan <TSQLToken> tokens    = TSQLTokenizer.ParseTokens(rawTsql).ToArray();

            //Act
            DataManipulation statement = resolver.Resolve(tokens, ref fileIndex, context);

            //Assert
            Assert.Equal(tokens.Length, fileIndex);
            Assert.Single(statement.Expressions);
            Assert.Equal("BI_STAGE.dbo.EventLogTable.targetcolumn", statement.Expressions[0].Name);
            Assert.Equal("unrelated.unrelated.unrelated.LoadRunId", statement.Expressions[0].ChildExpressions[0].Name);
        }
Пример #3
0
        internal void AnalyzeTSQLContentString(string tsqlContent, CompilerContext context)
        {
            List <TSQLToken>         tokensWithComments = TSQLTokenizer.ParseTokens(tsqlContent);
            ReadOnlySpan <TSQLToken> tokens             = FormatTokenList(tokensWithComments);

            int fileIndex = 0;

            while (fileIndex < tokens.Length)
            {
                IDataManipulationResolver manipulationResolver;
                DataManipulation          manipulation;

                switch (tokens[fileIndex].Text.ToLower())
                {
                case "use":
                    new UseStatementResolver().ResolveUseStatement(tokens, ref fileIndex, context);
                    break;

                //case "declare":
                //    new VariableDeclarationResolver().Resolve(tokens, ref fileIndex, context);
                //    break;
                //case "set":
                //    gic.AssignVariableValue(tokens, ref fileIndex);
                //    break;
                case "with":
                    var withResolver = new WithStatementResolver();
                    withResolver.Resolve(tokens, ref fileIndex, context);
                    break;

                case "merge":
                    manipulationResolver = new MergeStatementResolver();
                    manipulation         = manipulationResolver.Resolve(tokens, ref fileIndex, context);
                    context.DataManipulations.Add(manipulation);
                    context.DropCommonTableExpressions();
                    break;

                case "bulk":
                    var bulkStatementResolver = new BulkInsertStatementResolver();
                    bulkStatementResolver.Resolve(tokens, ref fileIndex, context);
                    break;

                case "insert":
                    manipulationResolver = new InsertStatementResolver();
                    manipulation         = manipulationResolver.Resolve(tokens, ref fileIndex, context);
                    context.DataManipulations.Add(manipulation);
                    context.DropCommonTableExpressions();
                    break;

                case "update":
                    manipulationResolver = new UpdateStatementResolver();
                    manipulation         = manipulationResolver.Resolve(tokens, ref fileIndex, context);
                    context.DataManipulations.Add(manipulation);
                    context.DropCommonTableExpressions();
                    break;

                case "select":
                    SelectStatementResolver selectResolver = new SelectStatementResolver();
                    SelectStatement         statement      = selectResolver.ResolveTopLevel(tokens, ref fileIndex, context);
                    if (statement.TargetObject == null)
                    {
                        if (context.ConsiderQueries)
                        {
                            context.DataQueries.Add(statement.Expression);
                        }
                    }
                    else
                    {
                        context.AddSelectWithIntoClause(statement);
                    }

                    context.DropCommonTableExpressions();
                    break;

                //case "drop":
                //    gic.ResolveDropStatement(tokens, ref fileIndex, causer);
                //    break;
                case "create":
                    new CreateStatementResolver().Resolve(tokens, ref fileIndex, context);
                    break;

                //case "exec":*
                //case "execute": throw new NotImplementedException("Execute statements are no supported yet");
                default:
                    fileIndex++;
                    break;
                }
            }

            result.DataQueries       = context.DataQueries;
            result.DataManipulations = context.DataManipulations;
        }