Subquery improvement for HqlExpressions (#8163)

This commit is contained in:
GiuseppeMusso-Laser
2019-02-28 21:21:49 +01:00
committed by Sébastien Ros
parent 3c4de528c4
commit a53d30dd6a
2 changed files with 56 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using NHibernate;
using NHibernate.Transform;
using Orchard.ContentManagement.Records;
@@ -543,6 +544,39 @@ namespace Orchard.ContentManagement {
Criterion = HqlRestrictions.InG(propertyName, values);
}
public void InSubquery(string propertyName, string subquery, Dictionary<string, object> parameters) {
string subqueryWithParameters = "";
Regex re = new Regex(@"(?<![^\s]):[^\s]+");
subqueryWithParameters = re.Replace(subquery, x => {
object param;
if (parameters.TryGetValue(x.ToString().TrimStart(':'), out param)) {
var typeCode = Type.GetTypeCode(param.GetType());
switch (typeCode) {
case TypeCode.String:
case TypeCode.DateTime:
return HqlRestrictions.FormatValue(param);
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Decimal:
case TypeCode.Double:
return FormatNumber(param);
default:
return "";
}
}
return "";
});
InSubquery(propertyName, subqueryWithParameters);
}
public void IsNull(string propertyName) {
Criterion = HqlRestrictions.IsNull(propertyName);
}
@@ -632,6 +666,21 @@ namespace Orchard.ContentManagement {
public void NaturalId() {
Criterion = HqlRestrictions.NaturalId();
}
private IHqlCriterion InSubquery(string propertyName, string subquery) {
if (string.IsNullOrWhiteSpace(subquery)) {
throw new ArgumentException("Subquery can't be empty", "subquery");
}
return new BinaryExpression("in", propertyName, "(" + subquery + ")");
}
private string FormatNumber(object value) {
decimal num;
if (Decimal.TryParse(value.ToString(), NumberStyles.Number, CultureInfo.InvariantCulture, out num))
return HqlRestrictions.FormatValue(value);
else
return "";
}
}
public enum HqlMatchMode {

View File

@@ -17,6 +17,13 @@ namespace Orchard.ContentManagement {
void In(string propertyName, object[] values);
void In(string propertyName, ICollection values);
void InG<T>(string propertyName, ICollection<T> values);
/// <summary>Search inside the results of a parameterized query.</summary>
/// <param name="propertyName">The property to compare.</param>
/// <param name="subquery">The subquery to evaluate. It can contain parameters.
/// A parameter is expressed as a word starting with ':' and no other character before.
/// (i.e. SELECT * FROM table WHERE column = :param)</param>
/// <param name="parameters">A dictionary containing the parameter names (without ':') as keys and their values.</param>
void InSubquery(string propertyName, string subquery, Dictionary<string, object> parameters);
void IsNull(string propertyName);
void EqProperty(string propertyName, string otherPropertyName);
void NotEqProperty(string propertyName, string otherPropertyName);