internal void DeleteFormat <T>(string format, object[] values) { MdoReflector reflector = MdoReflector.get(typeof(T)); String sql = "DELETE FROM " + reflector.TableName + FormatToWhereSQL(format, values); ExeUpdateSQL(sql); }
/// <summary> /// 取得某个类型的Reflection信息对象(在这里被称为Reflector) /// </summary> /// <param name="type"></param> /// <returns></returns> public static MdoReflector get(Type type) { if (!cache.ContainsKey(type)) { cache[type] = new MdoReflector(type); } return(cache[type]); }
public static string GetDeleteSQL(Object obj) { Type type = obj.GetType(); MdoReflector reflector = MdoReflector.get(type); String sql = "DELETE FROM " + reflector.TableName + " WHERE " + KeySQL(obj); return(sql); }
public void NotifyAllPropertiesChanged() { MdoReflector mrf = MdoReflector.get(typeof(T)); foreach (MdoField mf in mrf.Fields.Values) { NotifyPropertyChanged(mf.Name); } }
public void AlterTableAddColumn(Type type, string propName, object defaultValue) { MdoReflector reflector = MdoReflector.get(type); MdoField f = reflector.Fields[propName]; string sql = string.Format("ALTER TABLE {0} ADD COLUMN {1} {2} DEFAULT {3}", reflector.TableName, propName.ToUpper(), SQLTypeName(f), GetSQLSafeValue(defaultValue)); ExeUpdateSQL(sql); }
public override string ToString() { string s = typeof(T).Name + ": "; MdoReflector mrf = MdoReflector.get(typeof(T)); foreach (MdoField mf in mrf.Fields.Values) { s += mf.Name + "=" + mf.getValue(this) + " "; } return(s); }
public bool Exists <T>(T o) { MdoReflector reflector = MdoReflector.get(typeof(T)); String sql = "SELECT * FROM " + reflector.TableName + " WHERE " + KeySQL(o); bool isExists = false; ExeQuerySQL(sql, delegate(DbDataReader reader) { isExists = reader.Read(); }); return(isExists); }
public int CountFormat <T>(string format, object[] values) { Type type = typeof(T); string sql = "SELECT COUNT(*) FROM " + MdoReflector.get(typeof(T)).TableName + FormatToWhereSQL(format, values); int count = 0; ExeQuerySQL(sql, delegate(DbDataReader reader) { reader.Read(); count = reader.GetInt32(0); }); return(count); }
/// <summary> /// 从src复制所有属性的值到的dest。属性以src中标MdoFieldAttribute的属性为基准 /// </summary> /// <param name="dest"></param> /// <param name="src"></param> public static void CopyMdoPropertiesOfSrc(object dest, object src) { Type destType = dest.GetType(); Type srcType = src.GetType(); MdoReflector mrf = MdoReflector.get(srcType); foreach (MdoField mf in mrf.Fields.Values) { object value = mf.getValue(src); object newValue; PropertyInfo pi = destType.GetProperty(mf.Name); if (pi != null) { if (!ConvertValueType(value, out newValue, mf.Type, pi.PropertyType)) { continue; } pi.SetValue(dest, newValue, null); continue; } FieldInfo fi = destType.GetField(mf.Name); if (fi != null) { if (!ConvertValueType(value, out newValue, mf.Type, fi.FieldType)) { continue; } fi.SetValue(dest, newValue); continue; } //PropertyInfo pi; //FieldInfo fi; //GetPropertyOrFieldInfo(destType, mf.Name, out pi, out fi); //if (pi != null) //{ // if (ConvertValueType(value, out newValue, mf.Type, pi.PropertyType)) // pi.SetValue(dest, newValue, null); //}else if (fi != null) //{ // if (ConvertValueType(value, out newValue, mf.Type, fi.FieldType)) // fi.SetValue(dest, newValue); //} } }
public virtual string ToKeyString() { string s = typeof(T).Name + ": "; MdoReflector mrf = MdoReflector.get(typeof(T)); foreach (MdoField mf in mrf.Fields.Values) { if (mf.IsKey) { s += mf.Name + "=" + mf.getValue(this) + " "; } } return(s); }
/// <summary> /// 获得由一个对象的主键值组成的key /// </summary> /// <param name="o"></param> /// <returns></returns> private string getKey(Object o) { string sql = "FROM " + o.GetType() + " WHERE "; MdoReflector reflector = MdoReflector.get(o.GetType()); foreach (MdoField f in reflector.Fields.Values) { if (!f.IsKey) { continue; } sql += f.Name.ToUpper() + '=' + f.getValue(o); sql += " AND "; } //去掉结尾的 " AND " return(sql.Remove(sql.Length - 5)); }
/// <summary> /// 获得由一个对象的主键值组成的SQL查询条件 /// </summary> /// <param name="o"></param> /// <returns></returns> public static String KeySQL(Object o) { String sql = ""; MdoReflector reflector = MdoReflector.get(o.GetType()); foreach (MdoField f in reflector.DulpexFields) { if (!f.IsKey) { continue; } sql += f.Name.ToUpper() + '=' + GetSQLSafeValue(f.getValue(o)); sql += " AND "; } //去掉结尾的 " AND " return(sql.Remove(sql.Length - 5)); }
public void SetValuesFrom(object src, bool isNotifyAll) { if (!isNotifyAll) { SetValuesFrom(src); return; } if (isNotifyAll) { List <string> changedProps = new List <string>(); MdoReflector.CopyMdoPropertiesOfDest(this, src, changedProps); foreach (string propName in changedProps) { NotifyPropertyChanged(propName); } } }
public T Load <T>(T mdo) { string key = getKey(mdo); object cachedObj = cache.get(key); if (cachedObj != null) { MdoReflector.CopyMdoPropertiesOfDest(mdo, cachedObj); return((T)mdo); } try { realStore.Load(mdo); cache.put(key, mdo); return(mdo); }catch (NotExistException <T> e) { throw e; } }
/// <summary> /// 从数据库中,根据对象的o的主键值,载入对象的其他字段 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="o"></param> public T Load <T>(T o) { MdoReflector reflector = MdoReflector.get(typeof(T)); String sql = "SELECT * FROM " + reflector.TableName + " WHERE " + KeySQL(o); ExeQuerySQL(sql, delegate(DbDataReader reader) { if (reader.Read()) { foreach (MdoField f in reflector.DulpexFields) { readToField(reader, o, f); } } else { throw new NotExistException <T>(o); } }); return(o); }
/// <summary> /// 根据类型获得创建数据表的SQL /// </summary> /// <param name="type"></param> /// <returns></returns> public static string GetCreateTableSQL(Type type) { MdoReflector reflector = MdoReflector.get(type); // 创建主键的ALTER TABLE 代码 String keySQL = "PRIMARY KEY ("; String sql = "CREATE TABLE " + reflector.TableName + " ("; bool hasKey = false; foreach (MdoField f in reflector.DulpexFields) { sql += f.Name.ToUpper() + " "; sql += SQLTypeName(f) + " "; if (f.IsKey) { sql += "NOT NULL"; } sql += ","; if (f.IsKey) { hasKey = true; keySQL += f.Name.ToUpper() + ","; } } if (!hasKey) { throw new Exception(String.Format("Mdo派生类{0}没有定义primary key属性", type.Name)); } keySQL = keySQL.TrimEnd(',') + ")"; sql += keySQL + ");"; return(sql); }
public MdoList <T> FindFormat <T>(string format, params object[] values) where T : Mdo <T> { MdoList <T> objs = new MdoList <T>(); Type type = typeof(T); string sql = "SELECT * FROM " + MdoReflector.get(typeof(T)).TableName + FormatToWhereSQL(format, values); ExeQuerySQL(sql, delegate(DbDataReader reader) { List <MdoField> setterFields = MdoReflector.get(type).SetterFields; while (reader.Read()) { T o = (T)Activator.CreateInstance(type); foreach (MdoField f in setterFields) { readToField(reader, o, f); } o.DeepLoad(); objs.Add(o); } }); return(objs); }
public static String GetInsertSQL(Object obj) { Type type = obj.GetType(); MdoReflector reflector = MdoReflector.get(type); String table = reflector.TableName; String sql = "INSERT INTO " + table + " ("; // 字段名列表 foreach (MdoField f in reflector.DulpexFields) { sql += f.Name.ToUpper() + ","; } //去掉最后一个逗号 sql = sql.TrimEnd(','); sql += ") VALUES ("; // 数值列表 foreach (MdoField f in reflector.DulpexFields) { Object v = f.getValue(obj); // 主键字段的数值不能为null if (v == null && f.IsKey) { throw new NullKeyException(f); } sql += GetSQLSafeValue(v) + ","; } //去掉最后一个逗号 sql = sql.TrimEnd(','); sql += (");"); return(sql); }
public static string GetDropTableSQL(Type type) { MdoReflector reflector = MdoReflector.get(type); return("DROP TABLE " + reflector.TableName); }
public void CopyPropertiesTo(object dest) { MdoReflector.CopyMdoPropertiesOfSrc(dest, this); }
/// <summary> /// 从src复制所有属性的值到的dest。属性以dest中标MdoFieldAttribute的属性为基准 /// </summary> /// <param name="dest"></param> /// <param name="src"></param> /// <param name="changedProps">将有变化的属性值放入此集合中</param> public static void CopyMdoPropertiesOfDest(object dest, object src, ICollection <string> changedProps) { Type destType = dest.GetType(); Type srcType = src.GetType(); MdoReflector mrf = MdoReflector.get(destType); foreach (MdoField mf in mrf.Fields.Values) { object newV; Type typeOfNewV; PropertyInfo pi = srcType.GetProperty(mf.Name); if (pi != null) { newV = pi.GetValue(src, null); typeOfNewV = pi.PropertyType; } else { FieldInfo fi = srcType.GetField(mf.Name); if (fi == null) { continue; } newV = fi.GetValue(src); typeOfNewV = fi.FieldType; } //PropertyInfo pi; //FieldInfo fi; //GetPropertyOrFieldInfo(srcType, mf.Name, out pi, out fi); //if (pi != null) //{ // newV = pi.GetValue(src, null); // typeOfNewV = pi.PropertyType; //} //else if (fi != null) //{ // newV = fi.GetValue(src); // typeOfNewV = fi.FieldType; //} //else // continue; object destValue; if (!ConvertValueType(newV, out destValue, typeOfNewV, mf.Type)) { continue; } object oldV = mf.getValue(dest); mf.setValue(dest, destValue); //找出变化了的属性 if (changedProps != null) { IComparable newCompV = destValue as IComparable; IComparable oldCompV = oldV as IComparable; if ((destValue == null && oldV != null) || ((destValue != null && oldV == null))) { changedProps.Add(mf.Name); } else if (newCompV != null && oldCompV != null && newCompV.CompareTo(oldCompV) != 0) { changedProps.Add(mf.Name); } } } }
public void SetValuesFrom(object src) { MdoReflector.CopyMdoPropertiesOfDest(this, src); }
private void PutInCache <T>(T mdo) { MdoReflector reflector = MdoReflector.get(typeof(T)); cache.put(getKey(mdo), mdo); }
public IDictionary <string, object> ToDictionary() { MdoReflector reflector = MdoReflector.get(typeof(T)); return(reflector.getDictionary(this)); }
public object GetPropertyValue(string propertyName) { MdoReflector mrf = MdoReflector.get(typeof(T)); return(mrf.Fields[propertyName].getValue(this)); }
private string getUpdateSQL <T>(T o, string setSQL) { return("UPDATE " + MdoReflector.get(typeof(T)).TableName + " SET " + setSQL + " WHERE " + KeySQL(o)); }