/// <summary> /// 根据对象拼Insert语句 /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="graph">对象</param> /// <param name="mapping">映射关系</param> /// <param name="builder">生成Sql语句类型的Builder如TSqlBuilder或PlSqlBuilder</param> /// <param name="ignorProperties">忽略的字段</param> /// <returns>根据传入的对象和对象映射时需要忽略的字段以及类定义上的表名,生成完整的Insert语句</returns> public static string GetInsertSql <T>(T graph, ORMappingItemCollection mapping, ISqlBuilder builder, params string[] ignorProperties) { ExceptionHelper.FalseThrow <ArgumentNullException>(graph != null, "graph"); ExceptionHelper.FalseThrow <ArgumentNullException>(mapping != null, "mapping"); ExceptionHelper.FalseThrow <ArgumentNullException>(builder != null, "builder"); InsertSqlClauseBuilder insertBuilder = GetInsertSqlClauseBuilder(graph, mapping, ignorProperties); return(string.Format("INSERT INTO {0} {1} \r\n SELECT SCOPE_IDENTITY()", mapping.TableName, insertBuilder.ToSqlString(builder))); }
private static string GetDeleteSql <T>(T graph, ORMappingItemCollection mapping, ISqlBuilder builder, string[] ignorProperties) { ExceptionHelper.FalseThrow <ArgumentNullException>(graph != null, "graph"); ExceptionHelper.FalseThrow <ArgumentNullException>(mapping != null, "mapping"); ExceptionHelper.FalseThrow <ArgumentNullException>(builder != null, "builder"); WhereSqlClauseBuilder whereBuilder = GetWhereSqlClauseBuilderByPrimaryKey(graph, mapping); return(string.Format("DELETE FROM {0} WHERE {1}", mapping.TableName, whereBuilder.ToSqlString(builder))); }
private static void MergeMappingItems(ORMappingItemCollection dest, ORMappingItemCollection src) { foreach (ORMappingItem item in src) { if (dest.Contains(item.DataFieldName) == false) { dest.Add(item); } } }
public static string GetColumnName(System.Type type, string propName) { ORMappingItemCollection mapping = InnerGetMappingInfo(type); ORMappingItem propertyItem = mapping[propName]; if (propertyItem != null) { return(propertyItem.DataFieldName); } throw new ArgumentException("找不到属性" + propName + "所对应的表字段!"); }
/// <summary> /// 根据对象拼Where子句的方法 /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="graph">对象</param> /// <param name="mapping">映射关系</param> /// <param name="ignorProperties">忽略的字段</param> /// <returns>WhereSqlClauseBuilder,供拼Where子句使用</returns> /// <remarks> /// 根据传入的对象和对象映射时需要忽略的字段,返回WhereSqlClauseBuilder对象,以供后续拼Where子句使用 /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Data.SqlBuilder.Test\ORMappingTest.cs" region="GetWhereSqlClauseBuilder" lang="cs" title="根据传入的对象拼Where子句"/> /// <see cref="MCS.Library.Data.Builder.WhereSqlClauseBuilder"/> /// </remarks> public static WhereSqlClauseBuilder GetWhereSqlClauseBuilder <T>(T graph, ORMappingItemCollection mapping, params string[] ignorProperties) { ExceptionHelper.FalseThrow <ArgumentNullException>(graph != null, "graph"); ExceptionHelper.FalseThrow <ArgumentNullException>(mapping != null, "mapping"); WhereSqlClauseBuilder builder = new WhereSqlClauseBuilder(); FillSqlClauseBuilder(builder, graph, mapping, ClauseBindingFlags.Where, new DoSqlClauseBuilder <T>(DoWhereSqlClauseBuilder <T>), ignorProperties); return(builder); }
public static InsertSqlClauseBuilder GetInsertSqlClauseBuilder <T>(T graph, ORMappingItemCollection mapping, ref List <DbParameter> dbParams, params string[] ignorProperties) { ExceptionHelper.FalseThrow <ArgumentNullException>(graph != null, "graph"); ExceptionHelper.FalseThrow <ArgumentNullException>(mapping != null, "mapping"); InsertSqlClauseBuilder builder = new InsertSqlClauseBuilder(); FillSqlClauseBuilder(builder, graph, mapping, ClauseBindingFlags.Insert, new DoSqlClauseBuilderWithParams <T>(DoInsertUpdateSqlWithParamBuilder <T>), ignorProperties); return(builder); }
/// <summary> /// 复制Mapping的集合 /// </summary> /// <returns></returns> public ORMappingItemCollection Clone() { ORMappingItemCollection items = new ORMappingItemCollection(); items.tableName = this.tableName; foreach (ORMappingItem item in this) { items.Add(item.Clone()); } return(items); }
/// <summary> /// 根据对象拼Insert语句 /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="graph">对象</param> /// <param name="mapping">映射关系</param> /// <param name="builder">生成Sql语句类型的Builder如TSqlBuilder或PlSqlBuilder</param> /// <param name="ignorProperties">忽略的字段</param> /// <returns>根据传入的对象和对象映射时需要忽略的字段以及类定义上的表名,生成完整的Insert语句</returns> public static string GetUpdateSql <T>(T graph, ORMappingItemCollection mapping, ISqlBuilder builder, params string[] ignorProperties) { ExceptionHelper.FalseThrow <ArgumentNullException>(graph != null, "graph"); ExceptionHelper.FalseThrow <ArgumentNullException>(mapping != null, "mapping"); ExceptionHelper.FalseThrow <ArgumentNullException>(builder != null, "builder"); UpdateSqlClauseBuilder updateBuilder = GetUpdateSqlClauseBuilder(graph, mapping, ignorProperties); WhereSqlClauseBuilder whereBuilder = GetWhereSqlClauseBuilderByPrimaryKey(graph, mapping); return(string.Format("UPDATE {0} SET {1} WHERE {2}", mapping.TableName, updateBuilder.ToSqlString(builder), whereBuilder.ToSqlString(builder))); }
/// <summary> /// 获取Mapping信息,作为扩展使用 /// </summary> /// <param name="type"></param> /// <returns></returns> private static ORMappingItemCollection InnerGetMappingInfo(System.Type type) { ExceptionHelper.FalseThrow <ArgumentNullException>(type != null, "type"); ORMappingItemCollection result = null; if (ORMappingContextCache.Instance.TryGetValue(type, out result) == false) { result = ORMappingsCache.Instance.GetOrAddNewValue(type, (cache, key) => cache.Add(key, GetMappingItemCollection(type))); } return(result); }
private static ORMappingItemCollection GetMappingItemsBySubClass(RelativeAttributes attrs, MemberInfo sourceMI) { ORMappingItemCollection items = new ORMappingItemCollection(); System.Type subType = attrs.SubClassType != null ? attrs.SubClassType.Type : GetRealType(sourceMI); MemberInfo[] mis = GetTypeMembers(subType); foreach (SubClassORFieldMappingAttribute attr in attrs.SubClassFieldMappings) { MemberInfo mi = GetMemberInfoByName(attr.SubPropertyName, mis); if (mi != null) { if (items.Contains(attr.DataFieldName) == false) { ORMappingItem item = new ORMappingItem(); item.PropertyName = sourceMI.Name; item.SubClassPropertyName = attr.SubPropertyName; item.MemberInfo = mi; if (attrs.SubClassType != null) { item.SubClassTypeDescription = attrs.SubClassType.TypeDescription; } FillMappingItemByAttr(item, attr); items.Add(item); } } } foreach (SubClassSqlBehaviorAttribute attr in attrs.SubClassFieldSqlBehaviors) { ORMappingItem item = FindItemBySubClassPropertyName(attr.SubPropertyName, items); if (item != null) { FillMappingItemByBehaviorAttr(item, attr); } } return(items); }
private static void FillSqlClauseBuilder(SelectSqlClauseBuilder builder, ORMappingItemCollection mapping, ClauseBindingFlags bindingFlags, DoSelectClauseBuilder builderDelegate, params string[] ignorProperties) { foreach (ORMappingItem item in mapping) { if (Array.Exists <string>(ignorProperties, delegate(string target) { return(string.Compare(target, item.PropertyName, true) == 0); }) == false) { if ((item.BindingFlags & bindingFlags) != ClauseBindingFlags.None) { builderDelegate(builder, item); } } } }
private static ORMappingItemCollection CreateMappingItems(MemberInfo mi) { ORMappingItemCollection result = null; bool isDoMapping = false; RelativeAttributes attrs = null; if (mi.Name != "Item" && (GetRealType(mi).GetInterface("ICollection") == null || GetRealType(mi).Name.ToLower() == "byte[]")) //byte[] 对应sql的image类型 { attrs = GetRelativeAttributes(mi); if (attrs.NoMapping == null) { isDoMapping = true; } } if (isDoMapping == true) { if (attrs != null) { if (attrs.SubClassFieldMappings.Count > 0 || attrs.SubClassFieldSqlBehaviors.Count > 0) { result = GetMappingItemsBySubClass(attrs, mi); } else { result = GetMappingItems(attrs, mi); } } } else { result = new ORMappingItemCollection(); } return(result); }
/// <summary> /// /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="row">DataRow对象</param> /// <param name="items">映射关系</param> /// <param name="graph">对象</param> /// <param name="dod"></param> public static void DataRowToObject <T>(DataRow row, ORMappingItemCollection items, T graph, DataToObjectDeligations dod) { ExceptionHelper.FalseThrow <ArgumentNullException>(row != null, "row"); ExceptionHelper.FalseThrow <ArgumentNullException>(items != null, "items"); ExceptionHelper.FalseThrow <ArgumentNullException>(graph != null, "graph"); ExceptionHelper.FalseThrow <ArgumentNullException>(row.Table != null, "row.Table"); foreach (DataColumn column in row.Table.Columns) { if (items.Contains(column.ColumnName)) { ORMappingItem item = items[column.ColumnName]; System.Type realType = GetRealType(item.MemberInfo); object data = row[column]; if (Convertible(realType, data)) { SetValueToObject(item, graph, ConvertData(item, data), row, dod); } } } }
/// <summary> /// 将DataReader的值写入到对象中 /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="dr">IDataReader对象</param> /// <param name="items">映射关系</param> /// <param name="graph">对象</param> /// <param name="dod"></param> public static void DataReaderToObject <T>(IDataReader dr, ORMappingItemCollection items, T graph, DataToObjectDeligations dod) { ExceptionHelper.FalseThrow <ArgumentNullException>(dr != null, "dr"); ExceptionHelper.FalseThrow <ArgumentNullException>(items != null, "items"); ExceptionHelper.FalseThrow <ArgumentNullException>(graph != null, "graph"); DataTable schemaTable = dr.GetSchemaTable(); foreach (DataRow row in schemaTable.Rows) { string columnName = row["ColumnName"].ToString(); if (items.Contains(columnName)) { ORMappingItem item = items[row["ColumnName"].ToString()]; System.Type realType = GetRealType(item.MemberInfo); object data = dr[columnName]; if (Convertible(realType, data)) { SetValueToObject(item, graph, ConvertData(item, data), dr, dod); } } } }
/// <summary> /// 将DataReader的值写入到对象中 /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="dr">IDataReader对象</param> /// <param name="items">映射关系</param> /// <param name="graph">对象</param> public static void DataReaderToObject <T>(IDataReader dr, ORMappingItemCollection items, T graph) { DataReaderToObject(dr, items, graph, null); }
/// <summary> /// 将DataRow的值写入到对象中 /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="row">DataRow对象</param> /// <param name="items">映射关系</param> /// <param name="graph">对象</param> public static void DataRowToObject <T>(DataRow row, ORMappingItemCollection items, T graph) { DataRowToObject(row, items, graph, null); }
private static ORMappingItem FindItemBySubClassPropertyName(string subPropertyName, ORMappingItemCollection items) { ORMappingItem result = null; foreach (ORMappingItem item in items) { if (item.SubClassPropertyName == subPropertyName) { result = item; break; } } return(result); }
public static string GetDeleteSql <T>(T graph, ISqlBuilder builder, params string[] ignorProperties) { ORMappingItemCollection mapping = InnerGetMappingInfoByObject(graph); return(GetDeleteSql <T>(graph, mapping, builder, ignorProperties)); }
public static string GetSelectSql <T>(ISqlBuilder builder, params string[] ignorProperties) { ORMappingItemCollection mapping = InnerGetMappingInfo(typeof(T)); return(GetSelectSql(false, mapping, builder, ignorProperties)); }
private static SelectSqlClauseBuilder GetSelectSqlClauseBuilder(bool useTableName, ORMappingItemCollection mapping, params string[] ignorProperties) { ExceptionHelper.FalseThrow <ArgumentNullException>(mapping != null, "mapping"); SelectSqlClauseBuilder builder = new SelectSqlClauseBuilder(); if (useTableName) { builder.TableName = mapping.TableName; } FillSqlClauseBuilder(builder, mapping, ClauseBindingFlags.Select, new DoSelectClauseBuilder(DoSelectSqlClauseBuilder), ignorProperties); return(builder); }
public static string GetInsertSql <T>(T graph, ISqlBuilder builder, ref List <DbParameter> dbParams, params string[] ignorProperties) { ORMappingItemCollection mapping = InnerGetMappingInfoByObject(graph); return(GetInsertSql <T>(graph, mapping, builder, ref dbParams, ignorProperties)); }
public static string GetTableName(System.Type type) { ORMappingItemCollection mapping = InnerGetMappingInfo(type); return(mapping.TableName); }
public static string GetSelectSql(bool useTableName, System.Type type, ISqlBuilder builder, params string[] ignorProperties) { ORMappingItemCollection mapping = InnerGetMappingInfo(type); return(GetSelectSql(useTableName, mapping, builder, ignorProperties)); }