class TableInfo<T> where T : new() { public TableInfo() { Type type = typeof(T); TableAttribute tableattr = type.GetCustomAttributes(false).Where(attr => attr.GetType() == typeof(TableAttribute)).SingleOrDefault() as TableAttribute; if (tableattr != null) TableName = tableattr.Name; else TableName = type.Name; PRopertyInfo[] infos = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); foreach (PropertyInfo info in infos) { object[] attributes = info.GetCustomAttributes(false); if (!attributes.Where(attr => attr.GetType() == typeof(IgnoreAttribute)).Any()) { KeyAttribute key = attributes.Where(attr => attr.GetType() == typeof(KeyAttribute)).SingleOrDefault() as KeyAttribute; bool isKey = key != null; if (isKey && key.IsIdentity) IdentityProperty = info.Name; ColumnAttribute columnMap = attributes.Where(attr => attr.GetType() == typeof(ColumnAttribute)).SingleOrDefault() as ColumnAttribute; string columnName = info.Name; string queryName = columnName; if (columnMap != null) { columnName = columnMap.Name; queryName = columnMap.QueryName; } columns.Add(new ColumnMap(columnName, info.Name, queryName, isKey, !string.IsNullOrEmpty(IdentityProperty))); delegateSet[info.Name] = info.SetValue; delegateGet[info.Name] = info.GetValue; selectText = "select " + GetColumnList() + " from " + TableName; var s = InsertParams(); insertText = "insert into " + TableName + "(" + s[0] + ") values(" + s[1] + ")"; s = SetParams(); updateText = "update " + TableName + " set " + s[0] + " where " + s[1]; } } } public string TableName { get; set; } string IdentityProperty = ""; string selectText; string insertText; string updateText; List<ColumnMap> columns = new List<ColumnMap>(); Dictionary<string, Action<object, object, object[]>> delegateSet = new Dictionary<string, Action<object, object, object[]>>(); Dictionary<string, Func<object, object[], object>> delegateGet = new Dictionary<string, Func<object, object[], object>>(); Dictionary<string, int> readerIndex = new Dictionary<string, int>(); class ColumnMap { public ColumnMap() { } public ColumnMap(string propertyName) { ColumnName = propertyName; PropertyName = propertyName; QueryName = propertyName; IsPerimaryKey = false; } public ColumnMap(string propertyName, bool isPerimaryKey) : this(propertyName) { IsPerimaryKey = isPerimaryKey; } //public ColumnMap(string propertyName, bool isPerimaryKey,bool isIdentity) // : this(propertyName, isPerimaryKey) //{ // IsIdentity = isIdentity; //} public ColumnMap(string columnName, string propertyName, string queryName, bool isPerimaryKey,bool isIdentity) : this(columnName,isPerimaryKey) { ColumnName = columnName; QueryName = queryName; } public string ColumnName { get; set; } public string PropertyName { get; set; } public string QueryName { get; set; } public bool IsPerimaryKey { get; set; } public bool IsIdentity { get; set; } } public string SelectText { get { return selectText; } } string GetColumnList() { List<string> columnList = new List<string>(); foreach (ColumnMap map in columns) { if (map.ColumnName == map.QueryName) columnList.Add(map.ColumnName); else columnList.Add(map.ColumnName + " as " + map.QueryName); } return SSGClass.DataConvert.JoinList(columnList); } public string DeleteText { get { return "delete from " + TableName; } } public SSGClass.ExpressOpr GetDeleteExpress(T entity) { SSGClass.ExpressOpr exp = null; var keys = columns.Where(col => col.IsPerimaryKey == true); if (keys.Any()) { foreach (var keyColumn in keys) { if (exp == null) exp = SSGClass.ExpressOpr.where(keyColumn.ColumnName, Op.Equal, delegateGet[keyColumn.PropertyName].Invoke(entity, null)); else exp += SSGClass.ExpressOpr.and(keyColumn.ColumnName, Op.Equal, delegateGet[keyColumn.PropertyName].Invoke(entity, null)); } } else { foreach (var keyColumn in columns) { if (exp == null) exp = SSGClass.ExpressOpr.where(keyColumn.ColumnName, Op.Equal, delegateGet[keyColumn.PropertyName].Invoke(entity, null)); else exp += SSGClass.ExpressOpr.and(keyColumn.ColumnName, Op.Equal, delegateGet[keyColumn.PropertyName].Invoke(entity, null)); } } return exp; } public string InsertText { get { return insertText; } } public Dictionary<string, object> GetEntityPatams(T entity) { Dictionary<string, object> paramValues = new Dictionary<string,object>(); foreach (ColumnMap map in columns) { if (!string.IsNullOrEmpty(map.ColumnName)) paramValues.Add("@" + map.ColumnName, delegateGet[map.PropertyName].Invoke(entity, null)); } return paramValues; } string[] InsertParams() { string[] param = new string[2]; List<string> columnList = new List<string>(); List<string> paramList = new List<string>(); foreach (ColumnMap map in columns) { if (!string.IsNullOrEmpty(map.ColumnName)) { columnList.Add(map.ColumnName); paramList.Add("@" + map.ColumnName); } } param[0] = SSGClass.DataConvert.JoinList(columnList); param[1] = SSGClass.DataConvert.JoinList(paramList); return param; } public string UpdateText { get { return updateText; } } string[] SetParams() { string[] param = new string[2]; List<string> columnList = new List<string>(); List<string> paramList = new List<string>(); foreach (ColumnMap map in columns) { if (map.IsPerimaryKey) paramList.Add(map.ColumnName + " = @" + map.ColumnName); else columnList.Add(map.ColumnName + " = @" + map.ColumnName); } param[0] = SSGClass.DataConvert.JoinList(columnList); param[1] = SSGClass.DataConvert.JoinList(paramList); return param; } public T GenEntity(System.Data.IDataReader reader) { T entity = new T(); foreach (ColumnMap map in columns) { int index;
新闻热点
疑难解答