mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-01-19 17:51:45 +08:00
Subquery improvement for HqlExpressions (#8163)
This commit is contained in:
committed by
Sébastien Ros
parent
3c4de528c4
commit
a53d30dd6a
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user