我已经定义了新的隐式约定
autoQuery.ImplicitConventions.Add("%WithinLastDays", "{Field} > NOW() - INTERVAL '{Value} days'");
问题是对于 postgres 连接,查询被错误地翻译成
WHERE "TABLE"."FIELD" > NOW() - INTERVAL ':0 days'
并且它不会将参数发送到数据库。在内置约定的情况下,它可以正常工作。
更新
我试图定义 EndsWithConvention 但有同样的问题 - 参数没有传递给 pgsql 引擎(但是它在 SqlExpression 中可用)
autoQuery.EndsWithConventions
.Add("WithinLastDays", new QueryDbFieldAttribute() { Template= "{Field} >= CURRENT_DATE - {Value}", ValueFormat= "interval '{0} days ago'" });
autoQuery.EndsWithConventions
.Add("WithinLastDays", new QueryDbFieldAttribute() { Template= "{Field} >= CURRENT_DATE - interval '{Value}'", ValueFormat= "{0} days ago" });
更新 2 以下定义导致 PostgresException: 42601: błąd składni w lub blisko "$1" (抱歉波兰语错误)
autoQuery.EndsWithConventions.Add("WithinLastDays", new QueryDbFieldAttribute() { Template= "{Field} >= CURRENT_DATE - interval {Value}", ValueFormat= "{0} days ago" });
生成的查询是
SELECT here columns
FROM table
WHERE table."publication_date" >= CURRENT_DATE - interval $1
LIMIT 100
更新 3
autoQuery.EndsWithConventions.Add("WithinLastDays", new QueryDbFieldAttribute() { Template= "{Field} >= CURRENT_DATE - {Value}", ValueFormat= "interval {0} 'days ago'" });
生成
SELECT ...
FROM ...
WHERE ...."publication_date" >= CURRENT_DATE - $1
并发出 PostgresException: 42883: operator doesn't exist: date - text
这是 dto 定义
[Route("/search/tenders")]
public class FindTenders : QueryDb<TenderSearchResult>
{
public int? PublicationDateWithinLastDays { get; set; }
}
模型:
public class EntitiySearchResult
{
public DateTime PublicationDate { get; set; }
}
最终解决方案 @mythz 解决了注册问题和在我的原始查询中使用间隔子句的问题。从现在开始,以下定义可以很好地获取过去 X 天内的记录。谢谢@mythz
var autoQuery = new AutoQueryFeature() { MaxLimit = 100 };
autoQuery.EndsWithConventions.Add("WithinLastDays", new QueryDbFieldAttribute
{
Template = "{Field} >= CURRENT_DATE + {Value}::interval",
ValueFormat = "{0} days ago"
});