private DmlfInsertSelect CompileInsert()
        {
            var res = new DmlfInsertSelect();
            res.TargetTable = TargetTableSqlName;
            res.LinkedInfo = TargetLinkedInfo;
            res.Select = new DmlfSelect();
            res.Select.From.Add(SourceJoinModel.SourceToRefsJoin);

            foreach (var col in TargetColumns)
            {
                res.TargetColumns.Add(col.Name);
                var expr = col.CreateSourceExpression(SourceJoinModel, RequiresGrouping && !col.IsKey && !col.IsRestriction);
                res.Select.Columns.Add(new DmlfResultField
                {
                    Expr = expr,
                });
            }

            if (RequiresGrouping)
            {
                res.Select.GroupBy = new DmlfGroupByCollection();
                foreach (var col in TargetColumns.Where(x => x.IsKey || (x.IsRestriction && x.IsReference)))
                {
                    var expr = col.CreateSourceExpression(SourceJoinModel, false);
                    res.Select.GroupBy.Add(new DmlfGroupByItem
                    {
                        Expr = expr,
                    });
                }
            }

            CreateNotNullConditions(res.Select);

            var existSelect = new DmlfSelect();
            existSelect.SingleFrom.Source = new DmlfSource
            {
                TableOrView = TargetTableSqlName,
                Alias = "tested",
                LinkedInfo = TargetLinkedInfo,
            };
            existSelect.SelectAll = true;
            CreateKeyCondition(existSelect, "tested");
            CreateLifetimeConditions(existSelect, "tested");
            CreateFilterConditions(res.Select);

            res.Select.AddAndCondition(new DmlfNotExistCondition { Select = existSelect });

            return res;
        }
        public void GetInsertCommands(DmlfBatch res, DatabaseInfo db, ChangeSetModel model, IDialectDataAdapter dda, ICdlValueConvertor converter)
        {
            var table = db.FindTable(TargetTable);
            if (table == null) return;

            if (IsDuplicatingIdentity(db, model))
            {
                res.AllowIdentityInsert(table.FullName, true);
                var insert = new DmlfInsertSelect
                    {
                        TargetTable = table.FullName,
                    };
                insert.Select = new DmlfSelect();
                insert.Select.SingleFrom = new DmlfFromItem
                    {
                        Source = new DmlfSource
                            {
                                TableOrView = table.FullName,
                                Alias = "basetbl",
                            }
                    };

                GetConditions(insert.Select, this, Conditions, db);

                foreach (var col in table.Columns)
                {
                    var valcol = Values.FirstOrDefault(x => x.Column == col.Name);
                    insert.TargetColumns.Add(col.Name);
                    if (valcol == null)
                    {
                        insert.Select.Columns.Add(DmlfResultField.BuildFromColumn(col.Name, insert.Select.SingleFrom.Source));
                    }
                    else
                    {
                        insert.Select.Columns.Add(new DmlfResultField
                            {
                                Expr = new DmlfLiteralExpression
                                    {
                                        Value = valcol.Value,
                                    }
                            });
                    }
                }
                res.Commands.Add(insert);
                res.AllowIdentityInsert(table.FullName, false);
            }
        }