问题描述
我创建了以下NHibernate HQL生成器:
I have created the following NHibernate HQL generator:
public class ContainsGenerator : BaseHqlGeneratorForMethod {
public ContainsGenerator() {
SupportedMethods = new[] {
ReflectionHelper.GetMethodDefinition(() =>
MyExtensions.Contains(null, null))
};
}
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject,
ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder,
IHqlExpressionVisitor visitor) {
var exp = FormatExpression((string)((ConstantExpression)arguments[1]).Value);
return treeBuilder.BooleanMethodCall("CONTAINS", new[] {
visitor.Visit(arguments[0]).AsExpression(),
treeBuilder.Constant(exp)
});
}
private string FormatExpression(string exp) {
exp = exp.Replace("'", "''");
exp = exp.Replace("\"", "");
exp = (char)34 + Regex.Replace(exp,
"(AND NOT|AND|OR NOT|OR) ",
(char)34 + " $1 " + (char)34, RegexOptions.IgnoreCase)
+ (char)34;
return exp;
}
}
这用于执行全文搜索,以加快大型表的搜索速度.
This is used to do Full-Text searching to speed up searching over large tables.
与过去构建的此生成器和以前的生成器的唯一区别是,我调用FormatExpression将搜索表达式转换为SQL Server可以理解的正确格式.但这似乎是问题所在,因为尽管它在我第一次启动查询时起作用,但随后的搜索会生成相同的查询,并且传递给CONTAINS的第二个参数永远不会改变.例如,如果我说:
The only difference to this and previous generators I've built in the past is that I call FormatExpression to convert the search expression to the correct format that SQL Server understands. However this seems to be the problem because although it works the first time I fire the query, subsequent searches produces the same query and the second argument passed into CONTAINS never changes. For example If I say:
var products = session.Query<Product>().Where(p => p.Name.Contains("Test 1")).ToList();
它将产生以下查询:
现在,如果我说:
var products = session.Query<Product>().Where(p => p.Name.Contains("Test 2")).ToList();
它产生完全相同的查询.
It produces exactly the same query.
如果有人可以向我展示正确的方法,我将不胜感激.谢谢
I'd appreciate it if someone could show me the correct way to do this. Thanks
推荐答案
这是NHibernate中的一个已知错误:请参见 https://nhibernate.jira.com/browse/NH-2658 .
It is a known bug in NHibernate: see https://nhibernate.jira.com/browse/NH-2658.
这篇关于NHibernate HQL Generator缓存表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!