/// <summary> /// 解析条件 /// </summary> /// <param name="whereCompiler"></param> /// <returns></returns> public virtual void Translate(WhereCompilerInfo whereCompiler) { whereCompiler.Builder = new StringBuilder(); if (!string.IsNullOrEmpty(whereCompiler.Exp)) { AppendSql(whereCompiler); } var builder = new StringBuilder(); if (whereCompiler.IsSaveWhere) { var tableName = whereCompiler.SaveCompiler.SaveInfo.SetTableName; builder.Append("exists(select * from "); builder.Append(whereCompiler.GetJoinTable(whereCompiler.SaveCompiler.SaveInfo)); builder.AppendFormat(" where {0}.{1}={2}" , tableName, whereCompiler.Object.PrimaryProperty.FieldName , whereCompiler.GetFieldName(whereCompiler.Object.PrimaryProperty, null)); if (whereCompiler.Builder.Length > 0) { builder.Append(" and "); builder.Append(whereCompiler.Builder); } builder.Append(")"); whereCompiler.Builder = builder; } }
/// <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> /// 设置查询的条件 /// </summary> /// <param name="whereCompiler"></param> /// <param name="chainProperties"></param> /// <param name="propertyName"></param> protected virtual string GetFeildNameAndAddJoins(WhereCompilerInfo whereCompiler, IList <OrmPropertyInfo> chainProperties, string propertyName) { var property = chainProperties[chainProperties.Count - 1]; if (whereCompiler.Table.Joins != null) { whereCompiler.AddJoins(chainProperties); } var fieldName = whereCompiler.GetFieldName(property, propertyName); return(fieldName); }
/// <summary> /// 解析All函数 /// </summary> /// <param name="whereCompiler"></param> /// <param name="match"></param> /// <param name="key"></param> protected virtual void ReplaceAll(WhereCompilerInfo whereCompiler, Match match, string key) { var lastIndex = Regex.Match(match.Value, PropertyPattern).Value.LastIndexOf('.'); var propertyName = match.Value.Trim().Substring(0, lastIndex); var chainProperties = whereCompiler.Object.GetChainProperties(propertyName); var property = chainProperties[chainProperties.Count - 1]; var subWhere = TranslateManyWhere(whereCompiler, match, propertyName, chainProperties); string mapTableName = whereCompiler.Table.Joins != null?whereCompiler.Query.GetGetTableName(property.Map.GetMapObject()) : whereCompiler.SaveCompiler.SaveInfo.GetSetTableName(property.Map.GetMapObject()); var allWhere = string.Format("=(select count(1) from {0} _AllCountTable where _AllCountTable.{1}={2})" , mapTableName, property.Map.MapObjectProperty.FieldName , whereCompiler.GetFieldName(property.Map.ObjectProperty, propertyName)); whereCompiler.Builder.Append(subWhere.Builder); whereCompiler.Builder.Append(allWhere); }