protected void VisitWhenNotMatchedThenInsertToken(MergeStatement statement
                                                          , IList <string> tempUpdateAliases
                                                          , string targetAlias
                                                          , string targetColumnOn
                                                          , string targetTable
                                                          , string sourceAlias
                                                          , string sourceTable
                                                          , string sourceColumnOn)
        {
            ExpressionToken tempExpression = new ExpressionToken();

            if (statement.WhenNotMatched.Count != 0)
            {
                int counter = 0;

                foreach (WhenMatchedToken item in statement.WhenNotMatched)
                {
                    IList <CTEDefinition> tmpSourceList = new List <CTEDefinition>();

                    tempExpression = Sql.Name(sourceAlias, sourceColumnOn)
                                     .NotIn(Sql.Select.Output(targetColumnOn).From(targetTable));

                    if (item.AndCondition != null)
                    {
                        if (item.AndCondition is BinaryToken)
                        {
                            ((BinaryToken)item.AndCondition).First = Sql.Name(sourceAlias, ((Name)((BinaryToken)item.AndCondition).First).LastPart);
                        }
                        else if (item.AndCondition is UnaryToken)
                        {
                            ((UnaryToken)item.AndCondition).Token = Sql.Name(sourceAlias, ((Name)((UnaryToken)item.AndCondition).Token).LastPart);
                        }
                        tempExpression = tempExpression.And((ExpressionToken)item.AndCondition);
                    }

                    CTEDefinition source = Sql.With(sourceAlias).As(
                        Sql.Select.From(Sql.NameAs(sourceTable, sourceAlias)).Where(tempExpression));
                    tmpSourceList.Add(source);


                    InsertStatement insertStatement = Sql.Insert.Into(targetTable);

                    if ((((WhenNotMatchedTokenThenInsertToken)item).Values.Count == 0) ||
                        (((WhenNotMatchedTokenThenInsertToken)item).Columns.Count == 0) ||
                        (((WhenNotMatchedTokenThenInsertToken)item).Columns.Count != ((WhenNotMatchedTokenThenInsertToken)item).Values.Count))
                    {
                        insertStatement.Columns.Add(targetColumnOn);
                        insertStatement.From(Sql.Select.From(sourceAlias).Output(sourceColumnOn));
                    }
                    else
                    {
                        foreach (Name columnName in ((WhenNotMatchedTokenThenInsertToken)item).Columns)
                        {
                            insertStatement.Columns.Add(Sql.Name(columnName.LastPart));
                        }
                        SelectStatement fromSelect = Sql.Select.From(sourceAlias);
                        foreach (Token outputColumn in ((WhenNotMatchedTokenThenInsertToken)item).Values)
                        {
                            fromSelect.Output.Add(Sql.Name(((Name)outputColumn).LastPart));
                        }
                        insertStatement.From(fromSelect);
                    }

                    VisitCommonTableExpressions(tmpSourceList);
                    VisitStatement(insertStatement);

                    State.WriteStatementTerminator();

                    InsertStatement whenNotMatchedInsert =
                        Sql.Insert.Into(Sql.Name(targetTable))
                        .From(Sql.Select.From(Sql.Name(sourceAlias))
                              .Where(tempExpression));

                    counter++;
                }
            }
        }
Example #2
0
        protected void VisitWhenNotMatchedThenInsertToken(MergeStatement statement
                                                          , IList <string> tempUpdateAliases
                                                          , string targetAlias
                                                          , string targetColumnOn
                                                          , string targetTable
                                                          , string sourceAlias
                                                          , string sourceTable
                                                          , string sourceColumnOn)
        {
            string          tempSourceAlias = "tmp_" + sourceAlias;
            ExpressionToken tempExpression  = new ExpressionToken();

            if (statement.WhenNotMatched.Count != 0)
            {
                int counter = 0;

                foreach (WhenMatchedToken item in statement.WhenNotMatched)
                {
                    tempExpression = Sql.Name(sourceAlias, sourceColumnOn)
                                     .NotIn(Sql.Select.Output(targetColumnOn).From(targetTable));

                    if (item.AndCondition != null)
                    {
                        tempExpression = tempExpression.And(AddPrefixToExpressionToken((ExpressionToken)item.AndCondition, sourceAlias));
                    }

                    CreateTableStatement createTempTable =
                        Sql.CreateTemporaryTable(tempSourceAlias)
                        .As(Sql.Select.From(Sql.NameAs(sourceTable, sourceAlias)).Where(tempExpression));

                    VisitStatement(createTempTable);
                    State.WriteStatementTerminator();

                    InsertStatement insertStatement = Sql.Insert.Into(targetTable);

                    if ((((WhenNotMatchedTokenThenInsertToken)item).Values.Count == 0) ||
                        (((WhenNotMatchedTokenThenInsertToken)item).Columns.Count == 0) ||
                        (((WhenNotMatchedTokenThenInsertToken)item).Columns.Count != ((WhenNotMatchedTokenThenInsertToken)item).Values.Count))
                    {
                        insertStatement.Columns.Add(targetColumnOn);
                        insertStatement.From(Sql.Select.From(tempSourceAlias).Output(sourceColumnOn));
                    }
                    else
                    {
                        foreach (Name columnName in ((WhenNotMatchedTokenThenInsertToken)item).Columns)
                        {
                            insertStatement.Columns.Add(Sql.Name(columnName.LastPart));
                        }
                        SelectStatement fromSelect = Sql.Select.From(tempSourceAlias);
                        foreach (Token outputColumn in ((WhenNotMatchedTokenThenInsertToken)item).Values)
                        {
                            fromSelect.Output.Add(Sql.Name(((Name)outputColumn).LastPart));
                        }
                        insertStatement.From(fromSelect);
                    }

                    VisitStatement(insertStatement);
                    State.WriteStatementTerminator();
                    counter++;
                    VisitStatement(Sql.DropTable(tempSourceAlias, true));
                    State.WriteStatementTerminator();
                }
            }
        }