Ejemplo n.º 1
0
        public void GetCommands(DmlfBatch res, DatabaseInfo db, IDialectDataAdapter dda, ICdlValueConvertor converter)
        {
            var cmd = new DmlfInsert();
            cmd.InsertTarget = TargetTable;

            var table = db.FindTable(TargetTable);
            if (table == null) return;

            var autoinc = table.FindAutoIncrementColumn();
            bool isAutoInc = autoinc != null && Values.Any(x => x.Column == autoinc.Name);

            GetValues(cmd.Columns, Values, table, dda, converter);

            if (isAutoInc)
            {
                res.AllowIdentityInsert(table.FullName, true);
            }

            res.Commands.Add(cmd);

            if (isAutoInc)
            {
                res.AllowIdentityInsert(table.FullName, false);
            }
        }
Ejemplo n.º 2
0
        public void GetCommands(DmlfBatch res, DatabaseInfo db, ChangeSetModel model, bool allowCascade = true)
        {
            if (allowCascade && (DeleteReferencesCascade || model.DeleteReferencesCascade))
            {
                var refs = GenerateCascadeDeletions(db);
                foreach (var item in refs)
                {
                    item.GetCommands(res, db, model, false);
                }
            }

            if (model.DeleteReferencesCascade && model.DeleteSkipList != null && model.DeleteSkipList.Contains(TargetTable)) return;
            var cmd = new DmlfDelete();
            cmd.DeleteTarget = new DmlfSource
            {
                TableOrView = TargetTable,
                LinkedInfo = LinkedInfo,
                Alias = "basetbl",
            };
            cmd.From.Add(new DmlfFromItem
            {
                Source = cmd.DeleteTarget,
            });
            if (!GetConditions(cmd, this, Conditions, db)) return;
            res.Commands.Add(cmd);
        }
Ejemplo n.º 3
0
        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);
            }
        }
Ejemplo n.º 4
0
        public void GetCommands(DmlfBatch res, DatabaseInfo db, ChangeSetModel model, IDialectDataAdapter dda, ICdlValueConvertor converter)
        {
            var table = db.FindTable(TargetTable);
            if (table == null) return;

            if ((UpdateReferences || model.UpdateReferences) && IsUpdatingPk(db))
            {
                var refs = GenerateCascadeUpdates(db);
                foreach (var item in refs)
                {
                    item.GetCommands(res, db, model, dda, converter);
                }
            }

            if (!IsDuplicatingIdentity(db, model))
            {
                var cmd = new DmlfUpdate();
                cmd.UpdateTarget = new DmlfSource
                    {
                        TableOrView = TargetTable,
                        LinkedInfo = LinkedInfo,
                        Alias = "basetbl",
                    };
                cmd.From.Add(new DmlfFromItem
                    {
                        Source = cmd.UpdateTarget,
                    });
                if (GetConditions(cmd, this, Conditions, db))
                {
                    GetValues(cmd.Columns, Values, table, dda, converter);
                    res.Commands.Add(cmd);
                }
            }
        }
Ejemplo n.º 5
0
        public void GetDeleteCommands(DmlfBatch res, DatabaseInfo db, ChangeSetModel model)
        {
            var table = db.FindTable(TargetTable);
            if (table == null) return;

            if (IsDuplicatingIdentity(db, model))
            {
                var del = new DmlfDelete();
                del.DeleteTarget = new DmlfSource
                    {
                        TableOrView = table.FullName,
                        Alias = "basetbl",
                    };
                del.SingleFrom = new DmlfFromItem
                    {
                        Source = del.DeleteTarget
                    };
                GetConditions(del, this, Conditions, db);

                res.Commands.Add(del);
            }
        }
Ejemplo n.º 6
0
        //public void SaveToXml(XmlElement xml)
        //{
        //    foreach (var elem in Inserts)
        //    {
        //        elem.SaveToXml(xml.AddChild("Insert"));
        //    }
        //    foreach (var elem in Updates)
        //    {
        //        elem.SaveToXml(xml.AddChild("Update"));
        //    }
        //    foreach (var elem in Deletes)
        //    {
        //        elem.SaveToXml(xml.AddChild("Delete"));
        //    }
        //}

        //private void DumpTarget(ISqlDumper dmp, ChangeSetItem item)
        //{
        //    string linkedInfoStr = item.LinkedInfo != null ? item.LinkedInfo.ToString() : "";
        //    dmp.Put("%s%f", linkedInfoStr, item.TargetTable);
        //}

        //private void DumpWhere(ISqlDumper dmp, ChangeSetItem item, List<ChangeSetCondition> conditions, DatabaseInfo db)
        //{
        //    dmp.Put("^ where ");
        //    bool wasCond = false;
        //    foreach(var cond in conditions)
        //    {
        //        if (wasCond) dmp.Put(" ^and ");
        //        wasCond = true;
        //        DumpCondition(dmp, item, cond, db);
        //    }
        //}

        public DmlfBatch GetCommands(DatabaseInfo db, IDatabaseFactory factory)
        {
            var disableFks = new HashSet<Tuple<NameWithSchema, string>>();
            var dda = factory.CreateDataAdapter();
            var converter = new CdlValueConvertor(new DataFormatSettings());

            foreach (var upd in Updates)
            {
                if (upd.DisableReferencedForeignKeys || upd.UpdateReferences || DisableReferencedForeignKeys || UpdateReferences)
                {
                    var table = db.FindTable(upd.TargetTable);
                    if (table == null) continue;
                    foreach (var fk in table.GetReferences())
                    {
                        disableFks.Add(Tuple.Create(fk.OwnerTable.FullName, fk.ConstraintName));
                    }
                }
            }

            var res = new DmlfBatch();

            foreach (var fk in disableFks) res.DisableConstraint(fk.Item1, fk.Item2, true);

            foreach (var ins in Inserts)
            {
                ins.GetCommands(res, db, dda, converter);
            }

            foreach (var upd in Updates)
            {
                upd.GetInsertCommands(res, db, this, dda, converter);
            }

            foreach (var upd in Updates)
            {
                upd.GetCommands(res, db, this, dda, converter);
            }

            foreach (var upd in Updates)
            {
                upd.GetDeleteCommands(res, db, this);
            }

            foreach (var del in Deletes)
            {
                del.GetCommands(res, db, this);
            }

            foreach (var fk in disableFks) res.DisableConstraint(fk.Item1, fk.Item2, false);

            res.Commands.ForEach(x =>
                {
                    var cmd = x as DmlfCommandBase;
                    if (cmd != null) cmd.SimplifyFromAliases();
                });

            return res;
        }