public string ToHql(bool count) { var sb = new StringBuilder(); if (count) { sb.Append("select distinct civ.Id as Id").AppendLine(); } else { sb.Append("select distinct civ.Id as Id"); // add sort properties in the select foreach (var sort in _sortings) { var sortFactory = new DefaultHqlSortFactory(); sort.Item2(sortFactory); if (!sortFactory.Randomize) { sb.Append(", "); sb.Append(sort.Item1.Name).Append(".").Append(sortFactory.PropertyName); } else { // select distinct can't be used with newid() cacheable = false; sb.Replace("select distinct", "select "); } } sb.AppendLine(); } sb.Append("from ").Append(_from.TableName).Append(" as ").Append(_from.Name).AppendLine(); foreach (var join in _joins) { sb.Append(join.Item2.Type).Append(" ").Append(join.Item1.Name + "." + join.Item2.TableName).Append(" as ").Append(join.Item2.Name).AppendLine(); } // generating where clause if (_wheres.Any()) { sb.Append("where "); var expressions = new List <string>(); foreach (var where in _wheres) { var expressionFactory = new DefaultHqlExpressionFactory(); where.Item2(expressionFactory); expressions.Add(expressionFactory.Criterion.ToHql(where.Item1)); } sb.Append("(").Append(String.Join(") AND (", expressions.ToArray())).Append(")").AppendLine(); } // generating order by clause bool firstSort = true; foreach (var sort in _sortings) { if (!firstSort) { sb.Append(", "); } else { sb.Append("order by "); firstSort = false; } var sortFactory = new DefaultHqlSortFactory(); sort.Item2(sortFactory); if (sortFactory.Randomize) { string command = null; foreach (var sqlStatementProvider in _sqlStatementProviders) { if (!String.Equals(sqlStatementProvider.DataProvider, _shellSettings.DataProvider)) { continue; } command = sqlStatementProvider.GetStatement("random") ?? command; } if (command != null) { sb.Append(command); } } else { sb.Append(sort.Item1.Name).Append(".").Append(sortFactory.PropertyName); if (!sortFactory.Ascending) { sb.Append(" desc"); } } } // no order clause was specified, use a default sort order, unless it's a count // query hence it doesn't need one if (firstSort && !count) { sb.Append("order by civ.Id"); } return(sb.ToString()); }
public string ToHql(bool count) { var sb = new StringBuilder(); if (count) { sb.Append("select count(civ) ").AppendLine(); } else { sb.Append("select civ ").AppendLine(); } sb.Append("from ").Append(_from.TableName).Append(" as ").Append(_from.Name).AppendLine(); foreach (var join in _joins) { sb.Append(join.Item2.Type).Append(" ").Append(join.Item1.Name + "." + join.Item2.TableName).Append(" as ").Append(join.Item2.Name).AppendLine(); } // generating where clause if (_wheres.Any()) { sb.Append("where "); var expressions = new List <string>(); foreach (var where in _wheres) { var expressionFactory = new DefaultHqlExpressionFactory(); where.Item2(expressionFactory); expressions.Add(expressionFactory.Criterion.ToHql(where.Item1)); } sb.Append("(").Append(String.Join(") AND (", expressions.ToArray())).Append(")").AppendLine(); } // generating order by clause bool firstSort = true; foreach (var sort in _sortings) { if (!firstSort) { sb.Append(", "); } else { sb.Append("order by "); firstSort = false; } var sortFactory = new DefaultHqlSortFactory(); sort.Item2(sortFactory); if (sortFactory.Randomize) { sb.Append(" newid()"); } else { sb.Append(sort.Item1.Name).Append(".").Append(sortFactory.PropertyName); if (!sortFactory.Ascending) { sb.Append(" desc"); } } } return(sb.ToString()); }