servicestack - Servicestack 自动查询自定义约定不适用于 PostgreSQL
问题描述
我已经定义了新的隐式约定
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"
});
解决方案
{Value}
替换为 db 参数,如果要更改 db 参数的值,则需要使用 ValueFormat,例如ValueFormat="{0} days"
.
要定义ValueFormat
隐式约定的格式,您需要注册 EndsWithConventions,例如:
autoQuery.EndsWithConventions.Add("WithinLastDays", new QueryDbFieldAttribute {
Template= "{Field} >= CURRENT_DATE + {Value}::interval",
ValueFormat= "{0} days ago"
});
另请注意,您可能CURRENT_DATE + interval
不想-
。
推荐阅读
- visualization - 使用 visreg2d 进行 Cox 回归
- smartsheet-api - 有没有办法使用 REST API 获取 SmartSheet 表单?
- android-emulator - Android模拟器不会启动找不到的东西
- html - 无法删除导航栏和横幅 css 之间的填充
- android - 在 NavigationView 抽屉中显示图像
- jenkins - 在步骤中找不到这样的 DSL 方法“publishHTML”
- c# - .NET MVC Razor 发布操作-“/”中的服务器错误
- c# - C# Serial Port 提供不需要的 IRP 消息
- java - 用户定义函数的困难
- regex - 如何正确使用 char negation [^ ] 来做单词非固定宽度向后看?