Appearance
ExecuteSQL
根据选择的数据库资源执行SQL语句,返回执行结果。
业务属性
属性 | 含义 | 说明 | 输入限定 | 示例 |
---|---|---|---|---|
enable | 是否启用该控件,未启用时不编译 | 是否编译 | 选择值 | 启用 |
name | 控件名称 | 控件输出值变量名 | 常量 | dataList |
text | 与控件关联的文本 | 设计页控件显示 | 常量 | queryData |
dsn | 数据库资源 | 可选已在资源配置中配置的关系型数据库资源 目前支持的数据库: MySQL 、ORACLE 、SqlServer | 选择值 | dsn1 |
sqlMethod | SQL方法 | SQL的执行方法: 单个查询:查询单行数据,获得单个实体 链表查询:查询多行数据,获得链表 分页查询:通过分页参数进行分页查询 存储过程:执行存储过程 更新-删除:执行数据变更操作:增删改,获得影响行数 | 选择值 | 链表查询 |
sql | SQL语句 | 执行的SQL语句 | 常量,sql中可用sqlParam的参数值 /流程变量 | #{sql} |
sqlParam | SQL参数 | sql中使用的参数 | key:常量 value: 常量 /流程变量 | key:a value:#{a} |
pageNo | 分页页码 | sqlMethod为分页查询时可设置 默认为1 | 常量 /流程变量 | 2 |
pageSize | 分页大小 | sqlMethod为分页查询时可设置 默认为30 | 常量 /流程变量 | #{pageSize} |
throwable | 发生异常时中断请求 | 启用:控件执行异常时立即中断请求,并返回错误 禁用:控件执行异常时将被忽略,并继续执行其后的流程 | 选择值 | 启用 |
分页参数pageNo、pageSize 输入说明
输入为流程变量时,变量必须为number或string类型(Lua类型)的数值,且不可为nil 变量
输入为常量时,必须为一个数值
输入数值带小数时,小数部分会被截取
输入为空时,pageNo默认为 1、pageSize默认为15
sqlParam使用说明 sqlParam的作用为sql执行时替换参数为具体值,配置参数的方式执行sql语句可防止sql注入式攻击 sql中参数的使用依赖sqlParam,而sqlParam依赖流程中的变量,请严格遵守其中的依赖关系,不要在sql语句中直接引用流程中的变量
在Sql中以下面的格式使用的变量,必须在sqlParam中有定义,否则抛出异常。
:p
:作为占位符,在执行sql时传入配置值作为参数的替换。使用方法如:
等值比较:
select * from table where id=:pid
模糊匹配:
Oracle:
select * from table where name like '%'||:pname||'%'
SqlServer:
select * from table where name like '%'+:pname+'%'
MySql:
select * from table where name like CONCAT('%',:pname,'%')
⚠️注意:sql Server数据库执行sql时,每个参数只允许被使用一次。如
select * from table where id=:id or id1=:id
是不合法的。例: sqlParam: p1="1" sql: select t.* from tableName where t.column1=:p1 执行语句为:select t.* from tableName where t.column1=? (执行时p1作为变量替换?)
例: sqlParam: p1="1" sql: select t.* from tableName where t.column1=:p1 执行语句为:select t.* from tableName where t.column1=? (执行时p1作为变量替换?)
:{join(p)}
:输入参数p
为数组,数组中元素作为占位符应用于sql 的 in () 语句中例: sqlParam: p1= {"v1","v2"} sql: select t.* from tableName t where t.column1 in (:{join(p)}) 执行语句为:select t.* from tableName t where t.column1 in (?,?) (执行时p1中的元素逐一作为变量替换 ?)
例: sqlParam: p1= {"v1","v2"} sql: select t.* from tableName t where t.column1 in (:{join(p)}) 执行语句为:select t.* from tableName t where t.column1 in (?,?) (执行时p1中的元素逐一作为变量替换 ?)
输出值
控件输出以控件属性name为名称的实体,该实体包含代表执行结果的属性result。result根据sqlMethod设置的sql方法的不同而不同,以下为不同执行结果result的类型、结构说明:
以下类型均为Lua类型
单个查询:result为table类型(当查询没有结果行时为空table),查询结果表的列作为result的属性。
当sql的查询结果有多行时,只返回第一行数据。
*result属性的Json格式数据结构为:
json{ "result":{"column1":"a","column2":1,...} }
{ "result":{"column1":"a","column2":1,...} }
链表查询:result为table类型的链表(当查询没有结果行时为空table),查询结果表的每一行视为链表的一个元素,每一列作为该链表元素的一个属性。
*result属性的Json格式数据结构为:
json{ "result":[ {"column1":"a","column2":1,...}, {"column1":"a","column2":1,...}, ... ] }
{ "result":[ {"column1":"a","column2":1,...}, {"column1":"a","column2":1,...}, ... ] }
分页查询:result为一个二次封装的table类型的实体,其具有以下字段:
- total:(类型:number)符合查询条件的数据总数。
- rows:(类型:table)查询结果链表
- pageNo:(类型:number)分页页码
- pageSize:(类型:number)分页大小
*result属性的Json格式数据结构为:
json{ "result": { "total": 100, "pageNo": 1, "pageSize": 5, "rows": [ {"column1":"a","column2":1,...}, {"column1":"a","column2":1,...}, ... ] } }
{ "result": { "total": 100, "pageNo": 1, "pageSize": 5, "rows": [ {"column1":"a","column2":1,...}, {"column1":"a","column2":1,...}, ... ] } }
存储过程:返回查询结果链表。
*result属性的Json格式数据结构为:
json{ "result":[ {"column1":"a","column2":1,...}, {"column1":"a","column2":1,...}, ... ] }
{ "result":[ {"column1":"a","column2":1,...}, {"column1":"a","column2":1,...}, ... ] }
更新-删除:result为number类型,数据改变行数。
*result属性的Json格式数据结构为:
json{ "result": 10 }
{ "result": 10 }
⚠️注意:暂不可通过本控件查询获得Oracle、MySql数据库的BLOB类型数据
结果列-输出属性 转换:
sql方法为“查询” 时,查询结果表中的列转为对应实体的属性,列名即为属性名。sql查询表结果中应避免存在两个相同名称的列,否则在结果表转换为输出对象时其中一列将会被丢弃。
使用示例
假如我们有一表test_table
,该表的数据如下:
id | status |
---|---|
100 | 1 |
200 | 1 |
ExecuteSQL控件返回值命名为result
,选择数据库资源test

单个查询
sqlMethod选择 单个查询
sql输入:
sql
select * from test_table where id = '100'
select * from test_table where id = '100'

使用输出控件输出查询结果:
data:
json
{"data": "#{result}"}
{"data": "#{result}"}

使用curl请求接口:
$ curl http://localhost:6636/api/executeSql
$ curl http://localhost:6636/api/executeSql
接口返回:
json
{
"data":
{
"result":
{
"status": "1",
"id": "100"
}
}
}
{
"data":
{
"result":
{
"status": "1",
"id": "100"
}
}
}
查询
sqlMethod选择 链表查询
sql输入:
sql
select * from test_table where status = '1'
select * from test_table where status = '1'

使用curl请求接口:
$ curl http://localhost:6636/api/executeSql
$ curl http://localhost:6636/api/executeSql
接口返回:
json
{
"data":
{
"result":
[
{
"status": "1",
"id": "100"
},
{
"status": "1",
"id": "200"
}
]
}
}
{
"data":
{
"result":
[
{
"status": "1",
"id": "100"
},
{
"status": "1",
"id": "200"
}
]
}
}
分页查询
sqlMethod选择 分页查询
sql输入:
sql
select * from test_table where status = :sql_status
select * from test_table where status = :sql_status
配置sql参数

输入分页参数 #{body.pageNo}
、#{body.pageSize}

使用curl请求接口:
$ curl http://localhost:6636/api/executeSql -d "{\"status\":1, \"pageNo\":1, \"pageSize\":15}"
$ curl http://localhost:6636/api/executeSql -d "{\"status\":1, \"pageNo\":1, \"pageSize\":15}"
接口返回:
json
{
"data":
{
"result":
{
"rows":
[
{
"status": "1",
"id": "100"
},
{
"status": "1",
"id": "200"
}
],
"pageSize": 15,
"pageNo": 1,
"total": 2
}
}
}
{
"data":
{
"result":
{
"rows":
[
{
"status": "1",
"id": "100"
},
{
"status": "1",
"id": "200"
}
],
"pageSize": 15,
"pageNo": 1,
"total": 2
}
}
}
更新-删除
sqlMethod选择 更新-删除
sql输入:
sql
delete from test_table where id = '100'
delete from test_table where id = '100'

使用curl请求接口:
$ curl http://localhost:6636/api/executeSql
$ curl http://localhost:6636/api/executeSql
接口返回:
json
{
"data":
{
"result": 1
}
}
{
"data":
{
"result": 1
}
}