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 T Load <T>(XmlNode xnode, string pref = "") where T : MEDMObj { T p = null; string n = XFunc.GetAttr(xnode, "name", ""); if (n != "") { __ = $"{typeof(T).Name}: {n}"; if (pref != "") { n = $"{pref}.{n}"; } p = MainDic.GetObj <T>(n); try { p.SetValues(xnode); } catch (Exception e) { __ = $"error: {e}"; } } else { __ = $"error: Имя {typeof(T).Name} не задано."; } return(p); }
public override MEDMObj CreateObject(Type t) { /* * if (!IsLockUpdates) * { * MUpdateAdapter ua = new MUpdateAdapter(MUpdateOperation.Create, obj); * Updates.Add(ua); * } */ MEDMObj obj = null; obj = MainDic.CreateObj(t); MUpdateAdapter ua = new MUpdateAdapter(MUpdateOperation.Create, obj); Updates.Add(ua); //Save(null); LockUpdates++; try { //obj.SetId(GetNewId(t)); MainDic.AddObj(obj); } finally { LockUpdates--; } return(obj); }
public override void InitCfg(string filename) { MainTrace.Add(TraceType.Cfg, $"file => {Path.GetFileName(filename)} "); XmlDocument xdoc = XFunc.Load(filename); foreach (XmlNode cfgNode in xdoc.SelectNodes("descendant::cfg")) { foreach (XmlNode typeNode in cfgNode.SelectNodes("*")) { Type t = GetClassTypeByClassName(typeNode.Name); if (t != null) { MainTrace.Add(TraceType.Cfg, $"Class => {t.Name}"); foreach (XmlNode itemNode in typeNode.SelectNodes("*")) { LockUpdates++; try { MEDMObj obj = MainDic.GetObj(t, itemNode.Name); obj.SetValues(itemNode); MainTrace.Add(TraceType.Cfg, $"Item => {itemNode.Name} ({obj})"); } finally { LockUpdates--; } } } else { MainTrace.Add(TraceType.Error, $"Для узла конфигурации {typeNode.Name} класс не найден"); } } } }
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 InitUser(string login, string name, string password) { MEntity entity = SelectFirst <MEntity>($"select * from MEntity where EntityTypeId='USR' and code={AddParam(login)}"); if (entity == null) { entity = MainDic.GetObj <MEntity>(null); entity.Code = login; entity.Name = name; entity.EntityTypeId = "USR"; } MPassword pass = SelectFirst <MPassword>($"select * from MPassword where Id={AddParam(login)}"); if (pass == null) { pass = MainDic.GetObj <MPassword>(login); pass.PassCode = MFunc.GetSecurityHash(password); } Save(null); }
public void LoadStandart() { MEDMClass c = null; MEDMProp p = null; //---------------------------------------------------- c = MainDic.NewObj <MEDMClass>("MEDMIdObj"); c.Header = "Базовый класс для конфигураций"; p = MainDic.NewObj <MEDMProp>("MEDMIdObj.Id"); p.Header = "Id"; c.Props.Add(p); //---------------------------------------------------- c = MainDic.NewObj <MEDMClass>("MEDMNameObj"); c.Header = "Базовый класс для объектов с автоинкрементным Id"; p = MainDic.NewObj <MEDMProp>("MEDMNameObj.Id"); p.Header = "Id"; c.Props.Add(p); //---------------------------------------------------- c = MainDic.NewObj <MEDMClass>("MEDMGuidObj"); c.Header = "Базовый класс для объектов с Guid"; p = MainDic.NewObj <MEDMProp>("MEDMGuidObj.Id"); p.Header = "Id"; c.Props.Add(p); //---------------------------------------------------- c = MainDic.NewObj <MEDMClass>("MEDMCfgObj"); c.Header = "Базовый класс для конфигураций"; c.BaseId = "MEDMNameObj"; p = MainDic.NewObj <MEDMProp>("MEDMCfgObj.Id"); p.Header = "Наименование"; c.Props.Add(p); }
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); }