更新时间:2023-05-17 GMT+08:00

SQL语法

数据后端与各数据库的SQL语法差异

  • 如果需要把后端服务请求中携带的参数传递给SQL,使用${参数名}的方式传递,其中String类型的参数需要用单引号括起来,int类型的参数则不需要。

    如以下示例,name为String类型参数,id为int类型参数。

    select * from table01 where name='${name}' and id=${id}
  • 参数可以在后端服务请求的Headers、Parameters或者Body中传递。
  • 如果SQL中的字符串含关键字,需要对字符串转义。

    如某个字段名为delete,则SQL需要按如下格式写:

    select `delete` from table01
  • 如果配置数据后端时勾选了“预编译”,在使用传入的参数进行模糊匹配查询时,若匹配字段中带有字符“%”,则需要使用concat函数进行拼接。

    如以下示例,name为String类型参数。

    select * from table01 where name like concat('%',${name})

若数据源开启了“预编译”,且在SQL语句中同时引用多种数据类型的后端请求参数时,系统会默认把传入的参数转换为String类型。因此在执行SQL语句时,需要调用相应的函数对非String类型参数进行数据类型转换。

以上面的name(String类型)和id(int类型)参数为例,在同时传入SQL语句时,id参数会被转换为String类型,需要在SQL语句中,使用转换函数把id参数再转换回int类型。此处以cast()函数为例,不同数据库使用的转换函数会有所不同。

select * from table01 where name='${name}' and id=cast('${id}' as int)

SQL查询样例(update、insert等命令类似)

  • 带参数查询

    指把后端服务请求中携带的参数(Headers、Parameters或者Body参数)传递给SQL,为SQL语句提供灵活的条件查询或数据处理能力。

    • GET、DELETE方法的API,从请求URL中获取参数。
    • POST、PUT方法的API,从Body中获取参数。注意:Body体为application/x-www-form-urlencoded格式。
    select * from table01 where 1=1 and col01 = ${param01};
  • 可选参数查询
    select * from table01 where 1=1 [and col01 = ${param01}] [and col02 = ${param02}]
  • IN查询
    select * from table01 where 1=1 and col01 in ('${param01}','${param02}');
  • UNION查询
    默认删除重复数据,如需返回全部数据,使用关键字:union all
    select * from table01 
    union [all | distinct]
    select * from table02;
  • 嵌套查询
    select * from table01 where 1=1 and col01 in (select col02 from table02 where col03 is not null);

NoSQL(MongoDB、Redis等)兼容的源生命令

  • Redis数据源支持的命令:

    GET、HGET、HGETALL、LRANGE、SMEMBERS、ZRANGE、ZREVRANGE、SET、LPUSH、SADD、ZADD、HMSET、DEL

  • MongoDB数据源支持的命令:

    find

NoSQL样例

  • 插入String类型的key,value从请求参数中获取。
    set hello ${parm01}
  • 查询String类型的key
    get hello