public static RowType FindRow <RowType>(this IInMemoryTable <RowType> table, string[] pkcols, object[] pkvals)
     where RowType : class, IBedRecord
 {
     foreach (var row in table.Rows)
     {
         if (BedTool.EqualRecords(row.GetValuesByCols(pkcols), pkvals))
         {
             return(row);
         }
     }
     return(null);
 }
Exemple #2
0
        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);
        }