我可以在不使用Where子句的情况下创建自定义表达式吗?
-
29-09-2019 - |
题
虽然我已经在上一个问题中使用本机查询解决了这个问题。我现在想知道是否可以创建一个自定义表达式,该自定义表达式在不使用Where子句的情况下可以在标准中使用?我之所以不想要该子句,因为Oracle是 connect by ... start with ...
(这里) 陈述。我跟着 这 页面开始我的开始。但是,这将生成像 select * from foo where connect by start with
...
这是我正在使用的。从生成的东西看,我可以说它正在生成正确的语句减去Where子句。
public class StartWithConnectByCriteria : AbstractCriterion
{
public StartWithConnectByCriteria(string parentName, string parentValue, string childName)
{
ParentName = parentName;
ParentValue = parentValue;
ChildName = childName;
}
public string ParentName { get; set; }
public string ParentValue { get; set; }
public string ChildName { get; set; }
public IProjection P { get; set; }
public override IProjection[] GetProjections()
{
if(P != null)
{
return new IProjection[] {P};
}
return null;
}
public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
return
CriterionUtil.GetTypedValues(criteriaQuery, criteria, P, ParentName, ParentValue.ToString());
}
public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery,
IDictionary<string, IFilter> enabledFilters)
{
var sqlBuilder = new SqlStringBuilder();
SqlString[] parentColumnNames = CriterionUtil.GetColumnNames(ParentName,
P, criteriaQuery,
criteria, enabledFilters);
SqlString parentColumnName = parentColumnNames[0];
SqlString[] childColumnNames = CriterionUtil.GetColumnNames(ChildName,
P, criteriaQuery,
criteria, enabledFilters);
SqlString childColumnName = childColumnNames[0];
criteriaQuery.AddUsedTypedValues(GetTypedValues(criteria, criteriaQuery));
sqlBuilder
.Add("start with " + parentColumnName + " = '" + ParentValue + "'")
.Add(" connect by prior " + childColumnName + " = " + parentColumnName);
return sqlBuilder.ToSqlString();
}
public override string ToString()
{
return "";
}
}
我这样使用。
StartWithConnectByCriteria criterion =
new StartWithConnectByCriteria(
"parent",
"parent_value",
"child");
DetachedCriteria dc = DetachedCriteria.For<NormalUpstream>("nu")
.Add(criterion);
我有一种与 .Add()
从分离有限仪,但不能100%确定。不幸的是,我似乎找不到有关创建自定义表达式的文档。
编辑:现在我想到的是我在吠叫错误的树。虽然这不是至关重要的(我已经有一个不错的实现)。我仍然有兴趣了解如何进一步自定义NHIBERNATE。
编辑2:由于开箱即用,不支持Oracle的专有功能,因此 start with ... connect by
. 。我正在尝试通过添加本机支持它来了解有关扩展NHIBERNATE的更多信息。我知道我可以用自定义方言注册这些功能。但是我想知道是否可以作为标准实现它,因此我可以将其与其他标准查询一起使用。我发布的代码工作正常且正确创建有效的SQL,但是当我在我的标准中添加startwithConnectBycriteria时,NHIBERNATE会发出诸如 select this_.id from table where start with ... connect by
. 。这是一个无效的查询,因为该子句不属于Where。
这是我期望Nhibernate生成的查询。
select
random_column
from
table
start with parent_id = 'parent_node_id'
connect by prior child_up_id = parent_id
注意如何没有 where
此查询中的子句。然而, start with ... connect by
仍然可以与 where clause
. 。您可以阅读有关这些关键字的工作方式的更多信息 这里.
解决方案
我不知道现有的NHIBERNATE语法是否允许这样做,但是有一个用于层次查询的ANSI标准语法可能被证明有用。我相信它仅在11R2及以上工作,因此,我不确定它对您是否有用。看 递归子查询重构 了解更多信息。