private KeyTypeAttributeState GetKeyTypeAttributeState(System.Reflection.PropertyInfo pi) { KeyTypeAttributeState state = new KeyTypeAttributeState(); CustomAttributeField[] arr = (CustomAttributeField[])pi.GetCustomAttributes(typeof(CustomAttributeField), false); for (int i = 0; i < arr.Length; i++) { if (arr[i].KeyType == KeyTypeAttribute.PrimaryKey) { state.IsPrimaryKey = true; } else if (arr[i].KeyType == KeyTypeAttribute.ForeignKey) { state.IsForeignKey = true; } else if (arr[i].KeyType == KeyTypeAttribute.QueryField) { state.IsQueryField = true; } else if (arr[i].KeyType == KeyTypeAttribute.OmitIfIsNull) { state.IsOmitIfIsNull = true; } } return(state); }
private System.Reflection.PropertyInfo[] GetValidProperties(lib.Class.Reflection r) { List <System.Reflection.PropertyInfo> l = new List <System.Reflection.PropertyInfo>(); for (int i = 0; i < r.Properties.Length; i++) { KeyTypeAttributeState state = GetKeyTypeAttributeState(r.Properties[i]); if (!r.IsDefaultValue(r.Properties[i]) && !state.IsQueryField) { l.Add(r.Properties[i]); } } return(l.ToArray()); }
public void Insert <T>(T entity, DbTransaction Transaction = null) { lib.Class.Reflection r = new lib.Class.Reflection(entity); if (r.Properties.Length == 0) { return; } string db_fields = ""; for (int i = 0; i < r.Properties.Length; i++) { KeyTypeAttributeState state = GetKeyTypeAttributeState(r.Properties[i]); bool IsDefaultValue = r.IsDefaultValue(r.Properties[i]); if (state.IsQueryField || (IsDefaultValue && state.IsOmitIfIsNull)) { continue; } if (!state.IsPrimaryKey && !(state.IsForeignKey && IsDefaultValue)) { db_fields += (string.IsNullOrEmpty(db_fields) ? "" : ",") + r.Properties[i].Name; } } string db_values = ""; for (int i = 0; i < r.Properties.Length; i++) { KeyTypeAttributeState state = GetKeyTypeAttributeState(r.Properties[i]); bool IsDefaultValue = r.IsDefaultValue(r.Properties[i]); if (state.IsQueryField || (IsDefaultValue && state.IsOmitIfIsNull)) { continue; } if (!state.IsPrimaryKey && !(state.IsForeignKey && IsDefaultValue)) { db_values += (string.IsNullOrEmpty(db_values) ? "" : ",") + GetDbValue(r, r.Properties[i], false); } } string sql = string.Format("{0} INSERT INTO {1} ({2}) VALUES ({3});", DbBase.DbSetDateFormat, typeof(T).Name, db_fields, db_values); DbBase.DbExecute(sql, Transaction); }
public void Update <T>(T entity, T Conditions, DbTransaction Transaction = null) { lib.Class.Reflection r = new lib.Class.Reflection(entity); if (r.Properties.Length == 0) { return; } System.Reflection.PropertyInfo[] PropsCondition = null; if (Conditions != null) { PropsCondition = GetValidProperties(new lib.Class.Reflection(Conditions)); } string db_fields_values = ""; for (int i = 0; i < r.Properties.Length; i++) { KeyTypeAttributeState state = GetKeyTypeAttributeState(r.Properties[i]); bool IsDefaultValue = r.IsDefaultValue(r.Properties[i]); if (state.IsQueryField || state.IsPrimaryKey || (IsDefaultValue && state.IsOmitIfIsNull)) { continue; } if (state.IsForeignKey && IsDefaultValue) { db_fields_values += (string.IsNullOrEmpty(db_fields_values) ? "" : ",") + string.Format("{0}=NULL", r.Properties[i].Name); } else { db_fields_values += (string.IsNullOrEmpty(db_fields_values) ? "" : ",") + string.Format("{0}={1}", r.Properties[i].Name, GetDbValue(r, r.Properties[i], false)); } } DbBase.DbExecute( string.Format("{0} UPDATE {1} SET {2} {3}", DbBase.DbSetDateFormat, typeof(T).Name, db_fields_values, Condition(Conditions)), Transaction); }