private static string GetUserTaskSelectFields(UserTaskFieldDefine returnFields) { ORMappingItemCollection mappings = ORMapping.GetMappingInfo <UserTask>(); StringBuilder strB = new StringBuilder(); if (returnFields != UserTaskFieldDefine.All) { foreach (EnumItemDescription desp in EnumItemDescriptionAttribute.GetDescriptionList(typeof(UserTaskFieldDefine))) { UserTaskFieldDefine enumItem = (UserTaskFieldDefine)desp.EnumValue; if ((enumItem & returnFields) != UserTaskFieldDefine.None) { ORMappingItem mappingItem = GetMappingFromPropertyName(enumItem.ToString(), mappings); if (mappingItem != null) { if (strB.Length > 0) { strB.Append(", "); } strB.Append(mappingItem.DataFieldName); } } } } else { strB.Append("*"); } return(strB.ToString()); }
private static object GetValueFromObjectDirectly(ORMappingItem item, object graph) { object data = GetMemberValueFromObject(item.MemberInfo, graph); if (data != null) { Type dataType = data.GetType(); if (dataType.IsEnum) { if (item.EnumUsage == EnumUsageTypes.UseEnumValue) { data = (int)data; } else { data = data.ToString(); } } else if (dataType == typeof(TimeSpan)) { data = ((TimeSpan)data).TotalSeconds; } } return(data); }
protected static string GetPropertyFieldName(string propertyName, ORMappingItemCollection mapping) { ORMappingItem item = mapping[propertyName]; (item != null).FalseThrow("不能在{0}的OR Mapping信息中找到属性{1}", mapping.TableName, propertyName); return(item.DataFieldName); }
private static ORMappingItem GetMappingItemFromIDType(UserTaskIDType idType) { ORMappingItemCollection mappings = ORMapping.GetMappingInfo <UserTask>(); ORMappingItem keyItem = GetMappingFromPropertyName(idType.ToString(), mappings); ExceptionHelper.FalseThrow(keyItem != null, "不能找到idType为{0}对应的字段", idType.ToString()); return(keyItem); }
private static ORMappingItem GetMappingFromPropertyName(string propertyName, ORMappingItemCollection mappings) { ORMappingItem result = null; foreach (ORMappingItem item in mappings) { if (item.PropertyName == propertyName) { result = item; break; } } return(result); }
private static string GetFieldName(ORMappingItemCollection mapping, SchemaPropertyDefine pd) { string result = pd.SnapshotFieldName; if (result.IsNullOrEmpty()) { result = pd.Name; ORMappingItem item = mapping.Find(m => m.PropertyName == pd.Name); if (item != null) { result = item.DataFieldName; } } return(result); }
public UserTaskCollection GetUserAccomplishedTasks(UserTaskIDType idType, UserTaskFieldDefine returnFields, bool nolock, params string[] ids) { ExceptionHelper.FalseThrow <ArgumentNullException>(ids != null, "ids"); UserTaskCollection utc = new UserTaskCollection(); if (ids.Length > 0) { ORMappingItem keyItem = GetMappingItemFromIDType(idType); InSqlClauseBuilder iBuilder = new InSqlClauseBuilder(); iBuilder.AppendItem(ids); string sql = string.Format("SELECT {0} FROM WF.USER_ACCOMPLISHED_TASK{1} WHERE {2} {3}", GetUserTaskSelectFields(returnFields), nolock ? "(NOLOCK)" : string.Empty, GetMappingItemFromIDType(idType).DataFieldName, iBuilder.ToSqlStringWithInOperator(TSqlBuilder.Instance)); using (DbContext dbi = DbHelper.GetDBContext(GetConnectionName())) { Database db = DatabaseFactory.Create(dbi); using (IDataReader dr = db.ExecuteReader(CommandType.Text, sql)) { while (dr.Read()) { UserTask ut = new UserTask(); ORMapping.DataReaderToObject(dr, ut); utc.Add(ut); } } } } return(utc); }
private static object GetValueFromObject(ORMappingItem item, object graph) { object data = null; if (string.IsNullOrEmpty(item.SubClassPropertyName)) { data = GetValueFromObjectDirectly(item, graph); if (item.EncryptProperty) { data = EncryptPropertyValue(data); } } else { if (graph != null) { System.Reflection.MemberInfo mi = TypePropertiesWithNonPublicCacheQueue.Instance.GetPropertyInfoDirectly(graph.GetType(), item.PropertyName); if (mi == null) { mi = graph.GetType().GetField(item.PropertyName, BindingFlags.Instance | BindingFlags.Public); } if (mi != null) { object subGraph = GetMemberValueFromObject(mi, graph); if (subGraph != null) { data = GetValueFromObjectDirectly(item, subGraph); } } } } return(data); }
/// <summary> /// 生成子表与父表联接查询语句 /// </summary> /// <param name="type">实体类型</param> /// <param name="whereAction">Where条件</param> /// <param name="orderByAction">OrderBy条件</param> /// <returns>SQL语句</returns> public static string GetSubClassSelectSql(Type type, Action <WhereSqlClauseBuilder> whereAction = null, Action <OrderBySqlClauseBuilder> orderByAction = null) { StringBuilder sqlStr = new StringBuilder(); //生成连接查询语句 ORMappingItemCollection mappings = ORMapping.GetMappingInfo(type); ORMappingItem primaryItem = mappings.Find(p => p.PrimaryKey); sqlStr.Append(String.Format(" SELECT * FROM {0} ", mappings.TableName)); //遍历父类,找出父表,生成SELECT语句 Type baseType = type.BaseType; while (baseType != typeof(object)) { //如果不存在TableMapping特性则跳过 if (baseType != null) { var tableMapping = (ORTableMappingAttribute)Attribute.GetCustomAttribute(baseType, typeof(ORTableMappingAttribute), true); if (tableMapping == null) { baseType = baseType.BaseType; continue; } //创建内联接查询语句 sqlStr.Append(String.Format(" INNER JOIN {0} ON {1}.{2} = {0}.{2} ", tableMapping.TableName, mappings.TableName, primaryItem.DataFieldName)); } if (baseType != null) { baseType = baseType.BaseType; } } //Where子句处理 if (whereAction != null) { //生成Where子句 WhereSqlClauseBuilder builder = new WhereSqlClauseBuilder(); whereAction(builder); //将Where子句添加到SQL字串 if (builder.Count > 0) { sqlStr.Append(String.Format(" WHERE {0} ", builder.ToSqlString(TSqlBuilder.Instance))); } } //OrderBy子句处理 if (orderByAction != null) { //生成OrderBy子句 OrderBySqlClauseBuilder orderByBuilder = new OrderBySqlClauseBuilder(); orderByAction(orderByBuilder); //将OrderBy子句添加到SQL字串 if (orderByBuilder.Count > 0) { sqlStr.Append(String.Format(" ORDER BY {0} ", orderByBuilder.ToSqlString(TSqlBuilder.Instance))); } } return(sqlStr.ToString()); }