Пример #1
0
 public DataSetClass GetClass(NameWithSchema name)
 {
     //name = name.ToLower();
     if (Classes.ContainsKey(name)) return Classes[name];
     var tbl = _targetDatabase.FindTableLike(name.Schema, name.Name);
     if (tbl == null) throw new Exception("DBSH-00120 Unknown target table in data set:" + name);
     if (Classes.ContainsKey(tbl.FullName)) return Classes[tbl.FullName];
     var cls = new DataSetClass(this, tbl);
     Classes[tbl.FullName] = cls;
     cls.InitializeClass();
     return cls;
 }
Пример #2
0
 public DataSetInstance GetReferenceInstance(string refid, DataSetClass baseClass, DataSetClass referencedClass, string bindingColumn)
 {
     //int refid = Int32.Parse(srefid);
     if (!referencedClass.InstancesBySimpleKey.ContainsKey(refid))
     {
         referencedClass.ReportUndefinedReference(baseClass.TableName, bindingColumn, refid);
         //if (UndefinedReference != null)
         //{
         //    var ev = new UndefinedReferenceEventArgs();
         //    ev.BaseClass = baseClass;
         //    ev.ReferencedClass = referencedClass;
         //    ev.ReferencedValue = srefid;
         //    ev.BindingColumn = bindingColumn;
         //    UndefinedReference(this, ev);
         //    if (ev.NewReference != null) return ev.NewReference;
         //}
         return null;
     }
     var refobj = referencedClass.GetInstanceByIdentity(refid);
     return refobj;
 }
Пример #3
0
 public int? GetReferenceVariable(string srefid, DataSetClass baseClass, DataSetClass referencedClass, string bindingColumn)
 {
     var refobj = GetReferenceInstance(srefid, baseClass, referencedClass, bindingColumn);
     if (refobj != null) return refobj.IdVariable;
     return null;
 }
Пример #4
0
 internal void WriteColumnList(DataSetClass cls, StringBuilder sb)
 {
     bool was = false;
     for (int i = 0; i < cls.Columns.Length; i++)
     {
         if (cls.Columns[i] == cls.IdentityColumn) continue;
         if (cls.TargetTable.Columns[i].ComputedExpression != null) continue;
         if (was) sb.Append(",");
         sb.AppendFormat("[{0}]", cls.Columns[i]);
         was = true;
     }
 }
Пример #5
0
 private void WriteClassUpdates(SqlDumpWriter sdw, DataSetClass cls)
 {
     foreach (var inst in cls.AllInstances)
     {
         WriteInstanceUpdates(sdw, inst);
     }
 }
Пример #6
0
 private void WriteLookup(SqlDumpWriter sdw, DataSetClass cls)
 {
     foreach (var tuple in cls.LookupValues)
     {
         var sb = new StringBuilder();
         sb.Append("(");
         sb.AppendFormat("select top(1) tmain.[{0}] from [{1}] tmain ", cls.SimplePkCol, cls.TableName);
         var exprs = new List<string>();
         var rewr = new SqlRewriter(cls.TableName, this);
         for (int i = 0; i < cls.LookupFields.Length; i++) exprs.Add(rewr.Rewrite(cls.LookupFields[i]));
         rewr.WriteJoins(sb);
         sb.Append(" where ");
         bool was = false;
         for (int i = 0; i < exprs.Count; i++)
         {
             if (was) sb.Append(" and ");
             sb.AppendFormat("{0}=N'{1}'", exprs[i], tuple.Value[i]);
             was = true;
         }
         sb.Append(")");
         cls.LookupVariables[tuple.Key] = AssignVariable(sdw, sb.ToString());
     }
 }
Пример #7
0
        private int LoadMissing(DbConnection conn, DataSetClass cls)
        {
            var refValues = cls.GetMissingKeys();
            if (refValues.Count == 0) return 0;
            var sb = new StringBuilder();
            sb.Append($"select * from [{cls.TableName.Schema}].[{cls.TableName.Name}] where [{cls.SimplePkCol}] in (");
            bool was = false;
            foreach (string id in refValues)
            {
                if (was) sb.Append(",");
                sb.Append("'");
                sb.Append(id);
                sb.Append("'");
                was = true;
            }
            sb.Append(")");
            List<DataSetInstance> loaded;

            using (DbCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = sb.ToString();
                cmd.CommandTimeout = 3600;
                using (ICdlReader reader = _dda.AdaptReader(cmd.ExecuteReader()))
                {
                    loaded = DoLoadRows(reader, cls.TableName);
                }
            }
            //if (AddedMissingInstances != null)
            //{
            //    foreach (var ent in loaded)
            //    {
            //        var ev = new AddedMissingInstancesEventArgs
            //        {
            //            Class = cls,
            //            Instance = ent,
            //            Connection = conn,
            //        };
            //        AddedMissingInstances(this, ev);
            //    }
            //}
            return loaded.Count;
        }
Пример #8
0
        private void ProcessLookupValues(DbConnection conn, DataSetClass cls)
        {
            if (String.IsNullOrEmpty(cls.SimplePkCol))
            {
                throw new Exception(String.Format("Lookup column for table {0} is not defined", cls));
            }
            var refValues = GetAllReferences(cls);
            if (refValues.Count == 0) return;

            foreach (var row in cls.AllInstances)
            {
                string value = row.SimpleKeyValue;
                if (!refValues.Contains(value)) continue;

                string[] values = cls.LookupFieldIndexes.Select(x => x >= 0 ? row.Values[x].SafeToString() : null).ToArray();
                cls.LookupValues[value] = values;
            }
        }
Пример #9
0
 public HashSet<string> GetAllReferences(DataSetClass cls)
 {
     var refValues = new HashSet<string>();
     foreach (var rcls in Classes.Values)
     {
         foreach (var r in rcls.References)
         {
             if (r.ReferencedClass == cls)
             {
                 int colindex = Array.IndexOf(r.BaseClass.Columns, r.BindingColumn);
                 if (colindex >= 0)
                 {
                     foreach (var inst in r.BaseClass.AllInstances)
                     {
                         if (inst.Values[colindex] == null || inst.Values[colindex] == DBNull.Value) continue;
                         string refid = inst.Values[colindex].ToString();
                         refValues.Add(refid);
                     }
                 }
                 else
                 {
                     Warning("Class {0} doesn't contain binding column {1}", r.BaseClass, r.BindingColumn);
                 }
             }
         }
         //foreach (var fr in rcls.FulltextReferences)
         //{
         //    if (fr.ReferencedClass == cls)
         //    {
         //        int colindex = Array.IndexOf(fr.BaseClass.Columns, fr.BindingColumn);
         //        if (colindex >= 0)
         //        {
         //            var re = new Regex(fr.Regex);
         //            foreach (var inst in fr.BaseClass.AllInstaces)
         //            {
         //                if (inst.Values[colindex] == null || inst.Values[colindex] == DBNull.Value) continue;
         //                foreach (Match m in re.Matches(inst.Values[colindex].ToString()))
         //                {
         //                    int refid = Int32.Parse(m.Groups[fr.RegexGroup].Value);
         //                    refValues.Add(refid);
         //                }
         //            }
         //        }
         //        else
         //        {
         //            Warning("Class {0} doesn't contain binding column {1}", fr.BaseClass, fr.BindingColumn);
         //        }
         //    }
         //}
     }
     return refValues;
 }
Пример #10
0
 public DataSetInstance(DataSetClass cls, object[] values)
 {
     Class = cls;
     Values = values;
 }
Пример #11
0
 public DataSetClassReader(DataSetClass cls)
     : base(cls.TargetTable)
 {
     _cls = cls;
 }