如何使用日期过滤器与MuleSoft的OData(2.0版)

问题描述 投票:0回答:1

我使用MuleSoft(3.9)的OData(2.0)RAML和通过查询到Oracle数据库。添加日期过滤器在&$filter=START_DATE le datetime'2016-01-01T11:00:00'抛出数据库错误的网址:

SQL命令无法正常结束。

如何日期过滤器被添加到的OData肾错构瘤?

该数据库查询被作为select....where START_DATE <= datetime'2016-01-01T11:00:00'产生。我们是否需要使用to_date显式转换?

odata raml mulesoft
1个回答
0
投票

请使用以下变量的OData过滤器解析到SQL过滤器

%var odataFilterToSQLFilter = (odataFilter) -> 

    (( odataFilter replace "eq null" with "is null" 
     replace "ne null" with "is not null" 
     replace " eq " with " = " 
     replace " ne " with " != " 
     replace " gt " with " > " 
     replace " lt " with " < " 
     replace " ge " with " >= " 
     replace " le " with " <= " 
     replace " and " with " AND " 
     replace " or " with " OR " ) splitBy " " map  (
         ("TO_DATE('" ++ (($ replace "datetime'" with "" ) replace "T" with " ") ++ ",'yyyy-MM-dd HH24:MI:SS')") when $ as :string contains "datetime" otherwise $
     )) joinBy " "

%var toSQLWhere = (odataFilter) -> (" WHERE " ++ odataFilterToSQLFilter(odataFilter)) unless odataFilter == null otherwise ""

---
"SELECT " ++ generateSqlFields(filters.select) ++ " FROM $remoteEntityName"
 ++ ( 
    (toSQLWhere(filters.filter))
© www.soinside.com 2019 - 2024. All rights reserved.