public override void UpdateProperty(object sender, string propname, object oldvalue, object value, Type proptype, Type reftype, bool isVirtual) { base.UpdateProperty(sender, propname, oldvalue, value, proptype, reftype, isVirtual); if (sender is MObj && IsLockUpdates) { if (reftype != null && MainDic.ContainsKey(reftype)) { MainDic.AddFutureRef(reftype, value); } } else if (!isVirtual) { if (Updates.LastAdapter != null && Updates.LastAdapter.Obj == sender) { if (!Updates.LastAdapter.PropNames.Contains(propname)) { Updates.LastAdapter.PropNames.Add(propname); } } else if (sender is MEDMObj) { MUpdateAdapter ua = new MUpdateAdapter(MUpdateOperation.Update, (sender as MEDMObj)); ua.PropNames.Add(propname); Updates.Add(ua); } if (sender is MObj) { (sender as MObj).Invalidate(); } } }
public void SelectFromXML(IList list, Type t, XmlNode xroot, object id) { if (xroot != null) { try { bool f = MainDic.ContainsKey(t); { foreach (XmlNode xrow in xroot.ChildNodes) { if (xrow.Name == "row") { if (id == null || id.ToString() == "" || id.ToString().ToLower() == XFunc.GetAttr(xrow, "id", "")) { MObj obj = null; if (f) { obj = MainDic.CreateObj(t); } else { obj = Activator.CreateInstance(t) as MObj; if (obj == null) { throw new Exception($@"Тип ""{t}"" не наследует от MObj"); } obj.Model = this; } SetObjValues(obj, xrow); if (list != null) { list.Add(obj); } if (obj is MEDMObj) { MainDic.AddObj(obj as MEDMObj); } } } } } } finally { } } }
public override void UpdateFutureRef(Type dictype, string tablename, string idname, string idtype) { if (MainDic.ContainsKey(dictype) && MainDic.HasFutureRefs(dictype)) { string idlist = ""; bool f = false; foreach (object r in MainDic.GetFutureRefs(dictype)) { if (r != null) { if (f) { idlist += ','; } else { f = true; } switch (idtype.ToUpper()) { case "GUID": case "STRING": idlist += "'" + r.ToString() + "'"; break; default: idlist += r.ToString(); break; } } } MainDic.ClearFutureRefs(dictype); string sql = $"select * from [{tablename}] (nolock) where [{idname}] in ({idlist})"; Select(null, dictype, sql); } }
public IList Select(IList list, Type t, string sql, params object[] parms) { SqlConnection con = ConnectionPool.GetConnection(); try { parms = GetParamList(parms); int allCount = -1; IPaginationList plist = null; if (list is IPaginationList) { plist = (IPaginationList)(list); } DateTime begtime = DateTime.Now; SqlCommand com = new SqlCommand(sql, con); for (int i = 0; i < parms.Length - 1; i += 2) { if (parms[i] != null) { com.Parameters.Add(new SqlParameter(parms[i].ToString(), PrepareParam(parms[i + 1]))); } } // Для PaginationList select выполняем в 2 этапа сначала считаем кол-во а потом сам select c добавленным top if (plist != null) { if (plist.Top != "") { string sql1 = com.CommandText; int i = sql1.IndexOf('*'); int j = sql1.ToLower().IndexOf("order by"); if (i > 0 && (j < 0 || i < j)) { com.CommandText = sql1.Remove(j).Remove(i, 1).Insert(i, "count(*)"); allCount = Convert.ToInt32(com.ExecuteScalar()); com.CommandText = sql1.Insert(i, plist.Top + " "); } } } bool f = MainDic.ContainsKey(t); using (SqlDataReader rdr = com.ExecuteReader()) { while (rdr.Read()) { if (plist != null) { if ((plist.AllCount >= plist.StartPos || plist.StartPos == -1) && plist.AllCount < plist.StartPos + plist.PageSize || plist.StartPos == -1 || plist.PageSize == -1) { MObj obj = null; if (f) { obj = MainDic.CreateObj(t); } else { obj = Activator.CreateInstance(t) as MObj; if (obj == null) { throw new Exception($@"Тип ""{t}"" не наследует от MObj"); } obj.Model = this; } SetObjValues(obj, rdr); if (obj is MEDMObj) { MainDic.AddObj(obj as MEDMObj); } if (list != null) { list.Add(obj); } } plist.AllCount++; } else { MObj obj = null; if (f) { obj = MainDic.CreateObj(t); } else { obj = Activator.CreateInstance(t) as MObj; if (obj == null) { throw new Exception($@"Тип ""{t}"" не наследует от MObj"); } obj.Model = this; } SetObjValues(obj, rdr); if (obj is MEDMObj) { MainDic.AddObj(obj as MEDMObj); } if (list != null) { list.Add(obj); } } } } if (plist != null && allCount >= 0) { plist.AllCount = allCount; } MainTrace.Add(TraceType.Sql, $"SELECT => {com.CommandText}"); } finally { ConnectionPool.FreeConnection(con); } return(list); }