我定义了新的隐式约定
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: 运算符不存在:日期 - 文本
这是 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"
});
最佳答案
{Value}
被替换为 db 参数,如果您想更改 db 参数的值,您 need to use ValueFormat ,例如ValueFormat="{0} 天"
。
要定义 ValueFormat
格式隐式约定,您需要 register an EndsWithConventions ,例如:
autoQuery.EndsWithConventions.Add("WithinLastDays", new QueryDbFieldAttribute {
Template= "{Field} >= CURRENT_DATE + {Value}::interval",
ValueFormat= "{0} days ago"
});
另请注意,您可能需要 CURRENT_DATE + 间隔
而不是 -
。
关于Servicestack 自动查询自定义约定不适用于 PostgreSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50030994/