/// <summary> /// 转换1对多条件 /// </summary> /// <param name="whereCompiler"></param> /// <param name="property"></param> /// <param name="subText"></param> /// <param name="chainProperties"></param> /// <param name="propertyName"></param> /// <returns></returns> protected virtual WhereCompilerInfo GetSubWhereCompiler(WhereCompilerInfo whereCompiler, OrmPropertyInfo property, string subText, IList <OrmPropertyInfo> chainProperties, string propertyName) { var subWhereCompiler = new WhereCompilerInfo(property.Map.GetMapObject(), subText, new TableInfo { Joins = new Dictionary <string, JoinInfo>() }, new StringBuilder()) { Query = whereCompiler.Query }; Translate(subWhereCompiler); var lastIndex = propertyName.LastIndexOf('.'); if (lastIndex > -1) { propertyName = propertyName.Substring(0, lastIndex); } if (subWhereCompiler.Builder.Length > 0) { subWhereCompiler.Builder.Append(" and "); } subWhereCompiler.Builder.Insert(0, " where "); subWhereCompiler.Builder.Insert(0, string.Format("(select count(1) from {0} " , subWhereCompiler.GetJoinTable())); subWhereCompiler.Builder.AppendFormat("{0}={1})" , whereCompiler.GetFieldName(property.Map.ObjectProperty, propertyName) , subWhereCompiler.GetFieldName(property.Map.MapObjectProperty, null)); return(subWhereCompiler); }
/// <summary> /// 添加1对1关系map /// </summary> /// <param name="saveCompile"></param> /// <param name="property"></param> /// <param name="pValue"></param> protected virtual void AddOneToOneMap(SaveCompilerInfo saveCompile, OrmPropertyInfo property, object pValue) { var pk = saveCompile.SaveInfo.Entity.GetType().GetProperty(property.Map.ObjectProperty.PropertyName).GetValue(saveCompile.SaveInfo.Entity, null); FillForeignProperty(pValue, property, pk); AddMapInfo(saveCompile, property, pValue); }
/// <summary> /// 加载验证信息 /// </summary> /// <param name="infos"></param> /// <param name="ormProperty"></param> /// <param name="validation"></param> protected virtual void AppendValidationScript(IList <string> infos, OrmPropertyInfo ormProperty, ValidationInfo validation) { if (validation == null) { return; } var isEnable = true; if (ormProperty != null) { isEnable = SaveType == SaveType.Add && ormProperty.AllowAdd || SaveType == SaveType.Modify && ormProperty.AllowModify || SaveType == SaveType.Remove && ormProperty.AllowRemove; } if (!isEnable) { return; } var builder = new StringBuilder(); builder.Append("{Name:"); builder.AppendFormat("'{0}',Message:", validation.PropertName); builder.AppendFormat("'{0}',Rules:[{1}]", validation.Message, GetRulesArray(validation)); builder.Append("}"); infos.Add(builder.ToString()); }
/// <summary> /// 选择From读取方式 /// </summary> /// <param name="queryCompiler"></param> /// <param name="property"></param> /// <param name="propertyName"></param> /// <param name="isfromload"></param> protected virtual void SelectFromRead(QueryCompilerInfo queryCompiler, OrmPropertyInfo property, string propertyName, bool isfromload) { if (property.AllowRead && property.Map == null) { var asName = GetAsName(property, queryCompiler, propertyName); if (!string.IsNullOrEmpty(asName)) { queryCompiler.Builder.Append(asName); } } else if (property.Map != null && !property.Map.CheckRemote() && property.Map.IsGreedyLoad && queryCompiler.Query.IsGreedyLoad) { queryCompiler.AddJoin(property, propertyName); if (property.Map.MapType == OrmMapType.OneToMany) { AppendManySql(queryCompiler, null, property, propertyName, null); queryCompiler.Builder.AppendFormat(" as {0}_{1},", propertyName.Replace(".", "_"), queryCompiler.FieldCount); } else { AppendFromProperty(queryCompiler, property.Map.GetMapObject(), string.Format("{0}.{1}", propertyName, property.PropertyName), false); } } }
/// <summary> /// 添加OrmObject的Filed /// </summary> /// <param name="entity"></param> /// <param name="reader"></param> /// <param name="propertyName"></param> /// <param name="value"></param> /// <param name="obj"></param> protected virtual void SetOrmObjectField(object entity, IDataReader reader, string propertyName, object value, OrmObjectInfo obj) { var chainPropertys = obj.GetChainProperties(propertyName); OrmPropertyInfo property = chainPropertys.Count > 0 ? chainPropertys[chainPropertys.Count - 1] : null; if (property == null) { entity.SetProperty(propertyName, value); return; } if (!property.AllowRead) { return; } if (value == DBNull.Value && property.HasReadNullValue) { entity.SetProperty(propertyName, property.ReadNullValue); } else if (property.Map != null && property.Map.MapType == OrmMapType.OneToMany) { SetManyOrmProperty(entity, propertyName, value.ToString()); } else { entity.SetProperty(propertyName, value); } }
/// <summary> /// 得到延迟的加载条件 /// </summary> /// <param name="infos"></param> /// <param name="remoteQuery"></param> /// <param name="property"></param> /// <returns></returns> protected virtual IList <EntityInfo> GetRemoteInfos(IEnumerable <EntityInfo> infos, RemoteQueryInfo remoteQuery, OrmPropertyInfo property) { var query = GetRemoteQuery(infos, remoteQuery, property); var remoteInfos = GetInfos <IList <EntityInfo> >(property.Map.GetMapObject(), query, null); return(remoteInfos); }
/// <summary> /// 添加1对多关系map /// </summary> /// <param name="saveCompile"></param> /// <param name="property"></param> /// <param name="pValue"></param> protected virtual void AddOneToManyMap(SaveCompilerInfo saveCompile, OrmPropertyInfo property, object pValue) { System.Reflection.MethodInfo method = pValue.GetType().GetMethod("GetEnumerator"); object pk = saveCompile.SaveInfo.Entity.GetType().GetProperty(property.Map.ObjectProperty.PropertyName).GetValue(saveCompile.SaveInfo.Entity, null); if (method != null) { FillOneToManyMap(saveCompile, property, pValue, method, pk); } }
/// <summary> /// 得到map删除条件 /// </summary> /// <param name="saveCompile"></param> /// <param name="property"></param> /// <param name="where"></param> /// <returns></returns> protected virtual string GetRestoreMapWhere(SaveCompilerInfo saveCompile, OrmPropertyInfo property, string where) { var sbwhere = new StringBuilder(); sbwhere.AppendFormat(" where {0} in ( select ", property.Map.MapObjectProperty.FieldName); sbwhere.AppendFormat("{0} from ", property.Map.ObjectProperty.FieldName); sbwhere.AppendFormat("{0} {1} ", saveCompile.SaveInfo.SetTableName, where); AppendDefaultWhere(sbwhere, property.Map.GetMapObject()); return(sbwhere.ToString()); }
/// <summary> /// 对象 /// </summary> /// <param name="entitityKeys"></param> /// <param name="ormProperty"></param> /// <param name="context"></param> /// <param name="executor"></param> /// <param name="isLazyLoadExecute"></param> /// <param name="target"></param> /// <param name="type"></param> /// <param name="query"></param> public Proxy(IDictionary <object, object> entitityKeys, OrmPropertyInfo ormProperty, IContext context, IExecutor executor, bool isLazyLoadExecute, object target, Type type, QueryInfo query) : base(type) { EntitityKeys = entitityKeys; Target = target; OrmProperty = ormProperty; Context = context; Executor = executor; IsLazyLoadExecute = isLazyLoadExecute; IsInitialize = true; Query = query; }
/// <summary> /// 得到删除map的语句 /// </summary> /// <param name="saveCompiler"></param> /// <param name="property"></param> /// <param name="where"></param> /// <returns></returns> protected virtual string GetRestoreMapSql(SaveCompilerInfo saveCompiler, OrmPropertyInfo property, string where) { var sql = new StringBuilder(); var mapObject = property.Map.GetMapObject(); var tableName = saveCompiler.SaveInfo.GetSetTableName(mapObject); sql.AppendFormat("update {0} set {1} where ", tableName, property.Map.GetMapObject().RestoreMark); sql.AppendFormat("{0} in ( select ", property.Map.MapObjectProperty.FieldName); sql.AppendFormat("{0} from ", property.Map.ObjectProperty.FieldName); sql.AppendFormat("{0} {1}) ", saveCompiler.SaveInfo.SetTableName, where); return(sql.ToString()); }
/// <summary> /// 添加插入语句 /// </summary> /// <param name="saveCompile"></param> /// <param name="property"></param> /// <param name="sbName"></param> /// <param name="sbValue"></param> /// <param name="pname"></param> protected virtual void AppendAddSql(SaveCompilerInfo saveCompile, OrmPropertyInfo property, StringBuilder sbName, StringBuilder sbValue, string pname) { object pValue = saveCompile.SaveInfo.Entity.GetProperty(property.PropertyName); if (!IsAllowAdd(property, saveCompile.SaveInfo.Information, pValue)) { return; } sbName.AppendFormat("{0},", property.FieldName); sbValue.AppendFormat("{0}{1},", ParamterFlag, pname); AddParamter(saveCompile.Command, property, pname, pValue); }
/// <summary> /// 得到查询字段名称 /// </summary> /// <param name="property"></param> /// <param name="propertyName"></param> /// <returns></returns> public virtual string GetFieldName(OrmPropertyInfo property, string propertyName) { FieldCount++; if (property.IsCustom) { var lastIndex = propertyName.LastIndexOf('.'); var name = lastIndex < 0 ? propertyName : propertyName.Substring(0, lastIndex); return(string.Format(property.FieldName, GetAsName(name))); } return(string.Format("{0}.{1}", GetAsName(string.IsNullOrEmpty(propertyName)?propertyName: propertyName.Replace(string.Format(".{0}", property.PropertyName), "")), property.FieldName)); }
/// <summary> /// 执行1对多map /// </summary> /// <param name="saveCompile"></param> /// <param name="property"></param> /// <param name="pValue"></param> protected virtual void ModifyOneToManyMap(SaveCompilerInfo saveCompile, OrmPropertyInfo property, object pValue) { System.Reflection.MethodInfo method = pValue.GetType().GetMethod("GetEnumerator"); if (method != null) { var etor = (IEnumerator)method.Invoke(pValue, null); while (etor.MoveNext()) { ModifyMapInfo(saveCompile, property, etor.Current); } } }
/// <summary> /// 设置延迟属性 /// </summary> /// <param name="infos"></param> /// <param name="remoteInfos"></param> /// <param name="property"></param> protected virtual void SetRemoteProperies(IEnumerable <EntityInfo> infos, IList <EntityInfo> remoteInfos, OrmPropertyInfo property) { if (remoteInfos == null || remoteInfos.Count == 0) { return; } if (property.Map.MapType == OrmMapType.OneToOne) { foreach (var info in infos) { var id = info.GetProperty(property.Map.ObjectProperty.PropertyName); if (id == null) { continue; } foreach (var entityInfo in remoteInfos) { if (id.Equals(entityInfo.GetProperty(property.Map.MapObjectProperty.PropertyName))) { info.SetProperty(property.PropertyName, entityInfo); break; } } } } else { foreach (var info in infos) { var id = info.GetProperty(property.Map.ObjectProperty.PropertyName); if (id == null) { continue; } var arrayList = new ArrayList(); foreach (var entityInfo in remoteInfos) { if (id.Equals(entityInfo.GetProperty(property.Map.MapObjectProperty.PropertyName))) { arrayList.Add(entityInfo); } } var type = Type.GetType(property.Map.GetMapObject().ObjectName); if (type == null) { continue; } info.SetProperty(property.PropertyName, arrayList.ToArray(type)); } } }
/// <summary> /// 选择添加Map的执行方式 /// </summary> /// <param name="saveCompile"></param> /// <param name="property"></param> /// <param name="pValue"></param> protected virtual void SelectAddMap(SaveCompilerInfo saveCompile, OrmPropertyInfo property, object pValue) { switch (property.Map.MapType) { case OrmMapType.OneToOne: AddOneToOneMap(saveCompile, property, pValue); break; case OrmMapType.OneToMany: AddOneToManyMap(saveCompile, property, pValue); break; } }
/// <summary> /// 转换属性到字段 /// </summary> /// <param name="property"></param> /// <param name="queryCompiler"></param> /// <param name="propertyName"></param> protected virtual string GetAsName(OrmPropertyInfo property, QueryCompilerInfo queryCompiler, string propertyName) { if (!property.AllowRead) { return(null); } var builder = new StringBuilder(); builder.Append(queryCompiler.GetFieldName(property, propertyName)); builder.Append(" as "); builder.AppendFormat("{0}_{1}", propertyName.Replace(".", "_"), queryCompiler.FieldCount); builder.Append(","); return(builder.ToString()); }
/// <summary> /// 得到删除map的语句 /// </summary> /// <param name="saveCompiler"></param> /// <param name="property"></param> /// <param name="where"></param> /// <returns></returns> protected virtual string GetDeleteMapSql(SaveCompilerInfo saveCompiler, OrmPropertyInfo property, string where) { var sql = new StringBuilder(); var mapObject = property.Map.GetMapObject(); var tableName = saveCompiler.SaveInfo.GetSetTableName(mapObject); sql.Append(string.IsNullOrEmpty(property.Map.GetMapObject().RemoveMark) ? string.Format("delete from {0} ", tableName) : string.Format("update {0} set {1} ", tableName, property.Map.GetMapObject().RemoveMark)); sql.AppendFormat("where {0} in ( select ", property.Map.MapObjectProperty.FieldName); sql.AppendFormat("{0} from ", property.Map.ObjectProperty.FieldName); sql.AppendFormat("{0} {1}) ", saveCompiler.SaveInfo.SetTableName, where); return(sql.ToString()); }
/// <summary> /// 填充1对多关系map对象 /// </summary> /// <param name="saveCompile"/> /// <param name="property"></param> /// <param name="pValue"></param> /// <param name="method"></param> /// <param name="pk"></param> protected virtual void FillOneToManyMap(SaveCompilerInfo saveCompile, OrmPropertyInfo property, object pValue, System.Reflection.MethodInfo method, object pk) { var etor = (IEnumerator)method.Invoke(pValue, null); while (etor.MoveNext()) { if (etor.Current == null) { continue; } FillForeignProperty(etor.Current, property, pk); etor.Current.GetType().GetProperty(property.Map.MapObjectProperty.PropertyName).SetValue(etor.Current, pk, null); AddMapInfo(saveCompile, property, etor.Current); } }
/// <summary> /// 得到是否更新 /// </summary> /// <param name="property"></param> /// <param name="information"></param> /// <param name="pValue"></param> /// <returns></returns> protected virtual bool IsAllowModify(OrmPropertyInfo property, EntityInfo information, object pValue) { if (pValue == null) { return(false); } if (property.IsPrimaryKey || (information.Properties != null && !property.InProperties(information.Properties))) { return(false); } if (property.UnModifyValue != null && (property.HasUnModifyValue && (property.UnModifyValue.Equals(pValue)))) { return(false); } return(true); }
/// <summary> /// 添加连接表 /// </summary> /// <param name="property"></param> /// <param name="propertyName"></param> public virtual void AddJoin(OrmPropertyInfo property, string propertyName) { var index = propertyName.LastIndexOf('.'); var lastName = propertyName.Substring(0, index); if (!Table.Joins.ContainsKey(propertyName)) { Table.Joins.Add(propertyName, new JoinInfo { AsFieldName = property.Map.MapObjectProperty.FieldName, AsName = Table.CreateAsName(), JoinName = GetAsName(lastName), JoinFieldName = property.Map.ObjectProperty.FieldName, Object = property.Map.GetMapObject(), Map = property.Map }); } }
/// <summary> /// 添加参数 /// </summary> /// <param name="command"></param> /// <param name="property"></param> /// <param name="name"></param> /// <param name="value"></param> protected override void AddParamter(DbCommand command, OrmPropertyInfo property, string name, object value) { var pa = new SqlParameter(string.Format("@{0}", name), value); if (property != null) { if (property.Length != 0) { pa.Size = property.Length; } if (!string.IsNullOrEmpty(property.FieldType)) { pa.SqlDbType = (SqlDbType)Enum.Parse(typeof(SqlDbType), property.FieldType); } } ResetValue(pa); command.Parameters.Add(pa); }
/// <summary> /// 填充外键 /// </summary> /// <param name="info"></param> /// <param name="property"></param> /// <param name="pk"></param> protected virtual void FillForeignProperty(object info, OrmPropertyInfo property, object pk) { var propertyType = info.GetType() .GetProperties() .FirstOrDefault(it => it.Name.Equals(property.Map.MapObjectProperty.PropertyName)); if (propertyType == null) { return; } if (propertyType.PropertyType.IsValueType && !pk.Equals(0) || !propertyType.PropertyType.IsValueType && pk != null) { return; } propertyType.SetValue(info, pk, null); }
/// <summary> /// 拼接Set语句 /// </summary> /// <param name="saveCompile"></param> /// <param name="property"></param> /// <param name="sql"></param> /// <param name="pname"></param> protected virtual void AppendModifySetSql(SaveCompilerInfo saveCompile, OrmPropertyInfo property, StringBuilder sql, string pname) { object pValue = saveCompile.SaveInfo.Entity.GetProperty(property.PropertyName); if (!IsAllowModify(property, saveCompile.SaveInfo.Information, pValue)) { return; } if (property.IsVersion && saveCompile.SaveInfo.IsSetVersion) { sql.AppendFormat("{0}={0}+1,", saveCompile.SaveInfo.Object.VersionProperty.FieldName); } else { sql.AppendFormat("{0}={1}{2},", property.FieldName, ParamterFlag, pname); AddParamter(saveCompile.Command, property, pname, pValue); } }
protected override string GetManyFieldName(OrmPropertyInfo property, string propertName, QueryCompilerInfo selectComplier, QueryCompilerInfo subSelectComplier) { var name = propertName.Replace(string.Format(".{0}", property.PropertyName), ""); var feildName = selectComplier.GetFieldName(property.Map.ObjectProperty, name); if (subSelectComplier.SubQuery != null) { subSelectComplier.Chainon = string.Format("{0}.{1}={2}", "{0}", property.Map.MapObjectProperty.FieldName, feildName); } var builder = new StringBuilder(); builder.AppendFormat("(select {0} from {1} where {2}.{3}={4}", subSelectComplier.Builder, subSelectComplier.GetJoinTable() , subSelectComplier.Table.AsName, property.Map.MapObjectProperty.FieldName, feildName); builder.AppendFormat(" for xml path('{0}'),TYPE)", propertName); return(builder.ToString()); }
/// <summary> /// 得到查询对象 /// </summary> /// <param name="infos"></param> /// <param name="ormProperty"></param> protected virtual QueryInfo GetLazyLoadQuery(IEnumerable <EntityInfo> infos, OrmPropertyInfo ormProperty) { var ids = infos.Select(it => it.GetProperty(ormProperty.Map.ObjectProperty.PropertyName)).ToList(); var query = new QueryInfo { IsLazyLoad = true }; query.From(ormProperty.Map.GetMapObject().ObjectName); if (ids.Count == 0) { query.Where(string.Format("{0}==@MapId", ormProperty.Map.MapObjectProperty.PropertyName)) .SetParameter("MapId", ids[0]); } else { query.Where(string.Format("@MapIds.Contains({0})", ormProperty.Map.MapObjectProperty.PropertyName)) .SetParameter("MapIds", ids.ToArray()); } return(query); }
/// <summary> /// 更新map /// </summary> /// <param name="saveCompile"></param> /// <param name="property"></param> protected virtual void ConvertModifyMap(SaveCompilerInfo saveCompile, OrmPropertyInfo property) { if (property.Map == null || !property.Map.IsModify) { return; } var p = saveCompile.SaveInfo.Entity.GetType().GetProperties().FirstOrDefault(it => it.Name.Equals(property.PropertyName)); if (p == null) { return; } var pValue = p.GetValue(saveCompile.SaveInfo.Entity, null); if (pValue != null && !saveCompile.IsInMap(property)) { saveCompile.MapProperties.Add(property); SelectModifyMap(saveCompile, pValue, property); } }
/// <summary> /// 添加map信息 /// </summary> /// <param name="saveCompiler"></param> /// <param name="property"></param> /// <param name="entity"></param> protected virtual void AddMapInfo(SaveCompilerInfo saveCompiler, OrmPropertyInfo property, object entity) { if (((EntityInfo)entity).SaveType != SaveType.Modify || saveCompiler.ContentEntities.Contains(entity)) { return; } var mapSaveCompiler = new SaveCompilerInfo { Command = saveCompiler.Command, MapProperties = saveCompiler.MapProperties, ContentEntities = saveCompiler.ContentEntities, SaveInfo = new SaveInfo { Entity = entity, Information = (EntityInfo)entity, Object = property.Map.GetMapObject() } }; AddInfo(mapSaveCompiler); }
/// <summary> /// 拼接json /// </summary> /// <param name="obj"></param> /// <param name="properties"></param> /// <param name="name"></param> /// <param name="value"></param> protected virtual void AppendJsonObject(OrmObjectInfo obj, IDictionary <string, object> properties, string name, object value) { var propertyName = GetPropertyName(name); var chainPropertys = obj.GetChainProperties(propertyName); OrmPropertyInfo property = chainPropertys.Count > 0 ? chainPropertys[chainPropertys.Count - 1] : null; if (property == null) { return; } if (!property.AllowRead) { return; } if (value == DBNull.Value && property.HasReadNullValue) { value = property.ReadNullValue; } if (value == DBNull.Value) { return; } SetPropertyDictionary(properties, propertyName); var names = propertyName.Split('.'); var tempProperties = GetPropertyDictionary(properties, names); if (property.Map != null && property.Map.MapType == OrmMapType.OneToMany) { if (!tempProperties.ContainsKey(names[names.Length - 1])) { tempProperties.Add(names[names.Length - 1], GetManyProperties(property.Map.GetMapObject(), value.ToString())); } } else if (!tempProperties.ContainsKey(names[names.Length - 1])) { tempProperties.Add(names[names.Length - 1], GetPropertyTypeByName(obj, propertyName).TryConvertValue(value)); } }
/// <summary> /// 添加map信息 /// </summary> /// <param name="saveCompiler"></param> /// <param name="entity"></param> /// <param name="property"></param> protected virtual void ModifyMapInfo(SaveCompilerInfo saveCompiler, OrmPropertyInfo property, object entity) { var mapModel = ((EntityInfo)entity); if (((EntityInfo)entity).SaveType != SaveType.Modify || saveCompiler.ContentEntities.Contains(entity)) { return; } var mapSaveCompiler = new SaveCompilerInfo { Command = saveCompiler.Command, MapProperties = saveCompiler.MapProperties, ContentEntities = saveCompiler.ContentEntities, SaveInfo = new SaveInfo { Entity = entity, TableIndex = saveCompiler.SaveInfo.TableIndex, Information = mapModel, Object = property.Map.GetMapObject() } }; ModifyInfo(mapSaveCompiler); }
/// <summary> /// 得到一对多查询字段 /// </summary> /// <param name="property"></param> /// <param name="propertName"></param> /// <param name="selectComplier"></param> /// <param name="subSelectComplier"></param> /// <returns></returns> protected abstract string GetManyFieldName(OrmPropertyInfo property, string propertName, QueryCompilerInfo selectComplier, QueryCompilerInfo subSelectComplier);