private static void AppendHqlClauses(QueryBuildingContext queryContext, StringBuilder sb) { sb.Append(" from "); sb.Append(queryContext.RootClass.FullName); sb.Append(" ent"); // add joins foreach (var join in queryContext.Joins) { sb.Append($" {GetJoinTypeString(join.JoinType)} join {join.Reference} {join.Alias}"); if (!string.IsNullOrWhiteSpace(join.Condition)) { sb.Append(" on "); sb.Append(join.Condition); } } if (queryContext.FilterCriteria.FilterClauses.Count > 0) { sb.Append(" where "); bool hasClause = false; foreach (var clause in queryContext.FilterCriteria.FilterClauses) { if (hasClause) { sb.Append(" and "); } sb.Append("("); sb.Append(clause); sb.Append(")"); hasClause = true; } } }
public static IQuery CreateQuery(this ISession session, QueryBuildingContext queryContext) { var sb = new StringBuilder(); sb.Append("select ent "); AppendHqlClauses(queryContext, sb); if (!string.IsNullOrEmpty(queryContext.OrderBy)) { sb.Append(" order by "); sb.Append(queryContext.OrderBy.Replace("ascending", "asc").Replace("descending", "desc")); } var q = session.CreateQuery(sb.ToString()); TransferHqlParameters(q, queryContext.FilterCriteria); return(q); }
public static IQuery CreateQueryCount(this ISession session, System.Type entityType, QueryBuildingContext queryContext) { var sb = new StringBuilder(); sb.Append("select count(*) "); AppendHqlClauses(queryContext, sb); var q = session.CreateQuery(sb.ToString()); TransferHqlParameters(q, queryContext.FilterCriteria); return(q); }