コード例 #1
0
        public DataScript GetBaseModifyScript()
        {
            DataScript res = new DataScript();

            DmlfColumnRef[] wherecols = GetBaseWhereCols();
            foreach (var row in Rows)
            {
                if (row.RowState == BedRowState.Unchanged)
                {
                    continue;
                }
                // modified rows in multitable views are solved in GetLinkedDataScript()
                if (row.RowState == BedRowState.Modified && ResultFields != null && ResultFields.IsMultiTable())
                {
                    continue;
                }
                string[] changed        = row.GetChangedColumns(false);
                string[] changedNotNull = row.GetChangedColumns(true);
                if (changed.Length == 0 && row.RowState != BedRowState.Deleted)
                {
                    continue;
                }
                switch (row.RowState)
                {
                case BedRowState.Added:
                    res.Insert(changedNotNull, row.GetValuesByCols(changedNotNull));
                    break;

                case BedRowState.Modified:
                    res.Update(wherecols.GetNames(), row.Original.GetValuesByCols(wherecols, ResultFields), changed, row[changed]);
                    break;

                case BedRowState.Deleted:
                    res.Delete(wherecols.GetNames(), row.Original.GetValuesByCols(wherecols, ResultFields));
                    break;
                }
            }
            return(res);
        }
コード例 #2
0
ファイル: DbDiff_Table.cs プロジェクト: janproch/datadmin
        public static DataScript AlterFixedData(InMemoryTable oldData, InMemoryTable newData, InMemoryTableOperation ops, DbDiffOptions opts)
        {
            if (newData == null)
            {
                return(null);
            }
            IPrimaryKey newpk = newData.Structure.FindConstraint <IPrimaryKey>();

            if (newpk == null)
            {
                return(null);
            }
            string[]   newcolnames = newData.Structure.Columns.GetNames();
            DataScript script      = new DataScript();

            if (oldData == null)
            {
                foreach (var row in newData.Rows)
                {
                    script.Insert(newcolnames, row.GetValues());
                }
            }
            else
            {
                if (ops != null)
                {
                    oldData = new InMemoryTable(oldData, ops);
                }
                IPrimaryKey oldpk = oldData.Structure.FindConstraint <IPrimaryKey>();
                if (oldpk == null)
                {
                    return(null);
                }
                string[] newpkcols   = newpk.Columns.GetNames();
                string[] oldpkcols   = oldpk.Columns.GetNames();
                string[] newnopkcols = newData.Structure.GetNoPkColumns().GetNames();
                string[] oldnopkcols = oldData.Structure.GetNoPkColumns().GetNames();

                foreach (var row in newData.Rows)
                {
                    object[]        newpkvals = row.GetValuesByCols(newpkcols);
                    ArrayDataRecord oldrow    = oldData.FindRow(oldpkcols, newpkvals);
                    if (oldrow != null)
                    {
                        object[] oldnopkvals = oldrow.GetValuesByCols(oldnopkcols);
                        object[] newnopkvals = row.GetValuesByCols(newnopkcols);
                        if (BedTool.EqualRecords(oldnopkvals, newnopkvals))
                        {
                            continue;
                        }
                        // UPDATE
                        List <string> changedcols = new List <string>();
                        List <object> changedvals = new List <object>();
                        for (int i = 0; i < oldnopkcols.Length; i++)
                        {
                            if (!BedTool.EqualValues(oldnopkvals[i], newnopkvals[i]))
                            {
                                changedcols.Add(newnopkcols[i]);
                                changedvals.Add(newnopkvals[i]);
                            }
                        }
                        script.Update(newpkcols.ToArray(), newpkvals.ToArray(), changedcols.ToArray(), changedvals.ToArray());
                    }
                    else
                    {
                        script.Insert(newcolnames, row.GetValues());
                    }
                }
            }
            if (script.IsEmpty())
            {
                return(null);
            }
            return(script);
        }