RuleGo RuleGo
🏠首页
  • 快速入门
  • 规则链
  • 标准组件
  • 扩展组件
  • 自定义组件
  • 可视化
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • 触发器
  • 高级主题
  • 性能
  • 标准组件
  • 扩展组件
  • 自定义组件
  • 组件市场
  • 概述
  • 快速入门
  • 路由
  • DSL
  • API
  • Options
  • 组件
🔥编辑器 (opens new window)
  • 可视化编辑器 (opens new window)
  • RuleGo-Server (opens new window)
  • ❓问答

    • FAQ
💖支持
👥加入社区
  • Github (opens new window)
  • Gitee (opens new window)
  • GitCode (opens new window)
  • 更新日志 (opens new window)
  • English
  • 简体中文
🏠首页
  • 快速入门
  • 规则链
  • 标准组件
  • 扩展组件
  • 自定义组件
  • 可视化
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • 触发器
  • 高级主题
  • 性能
  • 标准组件
  • 扩展组件
  • 自定义组件
  • 组件市场
  • 概述
  • 快速入门
  • 路由
  • DSL
  • API
  • Options
  • 组件
🔥编辑器 (opens new window)
  • 可视化编辑器 (opens new window)
  • RuleGo-Server (opens new window)
  • ❓问答

    • FAQ
💖支持
👥加入社区
  • Github (opens new window)
  • Gitee (opens new window)
  • GitCode (opens new window)
  • 更新日志 (opens new window)
  • English
  • 简体中文

广告采用随机轮播方式显示 ❤️成为赞助商
  • 快速入门

  • 规则链

  • 标准组件

    • 标准组件概述
    • 过滤器

    • 动作

    • 转换器

    • 外部的

      • HTTP客户端
      • MQTT客户端
      • 发送邮件
      • 数据库客户端
        • GO第三方数据库驱动包
        • 配置
        • Relation Type
        • 执行结果
        • 配置示例
        • 应用示例
      • ssh
      • TCP/UDP客户端
    • 流

  • 扩展组件

  • 自定义组件

  • 组件市场

  • 可视化

  • AOP

  • 触发器

  • 高级主题

  • RuleGo-Server

  • 问题

目录

数据库客户端

dbClient组件:通过标准sql接口对数据库进行增删修改查操作。内置支持mysql和postgres数据库,可以执行SQL查询、更新、插入和删除操作。

组件基于Go标准库的database/sql接口实现,因此也支持任何实现了该接口的第三方数据库驱动。使用第三方驱动时需要在代码中导入相应的包。

例如,要使用TDengine (opens new window)时代码示例如下:

import (
    _ "github.com/taosdata/driver-go/v3/taosRestful"
)
1
2
3

# GO第三方数据库驱动包

以下是使用第三方数据库驱动包的示例:

驱动包名称 数据库类型 导入路径 driverName配置 dsn配置
TDengine TDengine "github.com/taosdata/driver-go/v3/taosRestful" taosRestful root:root@tcp(127.0.0.1:6030)/test
Microsoft SQL Server Microsoft SQL Server "github.com/denisenkom/go-mssqldb" mssql server=127.0.0.1;user id=root;password=root;database=test
Oracle Database Oracle Database "github.com/godror/godror" oracle username/password@//127.0.0.1:1521/test
Snowflake Snowflake "github.com/snowflakedb/gosnowflake" snowflake ACCOUNT=account_name;USER=user_name;PASSWORD=password;DATABASE=database_name;WAREHOUSE=warehouse_name
ClickHouse ClickHouse "github.com/ClickHouse/clickhouse-go" clickhouse tcp://127.0.0.1:9000?username=root&password=root&database=test
Vertica Vertica "github.com/vertica/vertica-sql-go" vertica vertica://127.0.0.1:5433/test?username=root&password=root
MySQL MySQL "github.com/go-sql-driver/mysql" mysql root:root@tcp(127.0.0.1:3306)/test
PostgreSQL PostgreSQL "github.com/lib/pq" postgres user:password@tcp(127.0.0.1:5432)/test 或者 user= password= host=127.0.0.1 port=5432 dbname=test sslmode=disable

# 配置

该组件允许通关过dsn字段复用共享的连接客户端。参考组件连接复用 。

字段 类型 说明 默认值
sql string SQL语句,可以使用组件配置变量 无
params 数组 SQL语句参数列表,可以使用组件配置变量 无
getOne bool 是否只返回一条记录,true:返回结构不是数组结构,false:返回数据是数组结构 无
poolSize int 连接池大小 无
driverName string 数据库驱动名称,mysql/postgres,或者其他数据库驱动类型 mysql
dsn string 数据库连接配置,参考sql.Open参数 无

# Relation Type

  • Success: 执行成功,把消息发送到Success链
  • Failure: 执行失败,把消息发送到Failure链

# 执行结果

  • Select:查询结果,替换到msg.Data,流转到下一个节点。
  • UPDATE, DELETE:结果存放在msg.Metadata中:
    • msg.Metadata.rowsAffected:影响多少行
    • msg.Data:内容不变
  • INSERT:结果存放在msg.Metadata中:
    • msg.Metadata.rowsAffected:影响多少行
    • msg.Metadata.lastInsertId:插入ID (如果有)
    • msg.Data:内容不变

# 配置示例

  {
  "id": "s1",
  "type": "dbClient",
  "name": "插入1条记录",
  "configuration": {
    "driverName":"mysql",
    "dsn":"root:root@tcp(127.0.0.1:3306)/test",
    "poolSize":5,
    "sql":"insert into users (id,name, age) values (?,?,?)",
    "params":["${metadata.id}", "${metadata.name}", "${metadata.age}"]
  }
 }
1
2
3
4
5
6
7
8
9
10
11
12

# 应用示例

应用示例参考:dbClient (opens new window)

{
  "ruleChain": {
	"id":"rule01",
    "name": "测试规则链",
	"root": true
  },
  "metadata": {
    "nodes": [
       {
        "id": "s1",
        "type": "dbClient",
        "name": "插入1条记录",
        "configuration": {
			"driverName":"mysql",
			"dsn":"root:root@tcp(127.0.0.1:3306)/test",
			"poolSize":5,
			"sql":"insert into users (id,name, age) values (?,?,?)",
			"params":["${metadata.id}", "${metadata.name}", "${metadata.age}"]
        }
      },
     {
        "id": "s2",
        "type": "dbClient",
        "name": "查询1条记录",
        "configuration": {
			"driverName":"mysql",
			"dsn":"root:root@tcp(127.0.0.1:3306)/test",
			"sql":"select * from users where id = ?",
			"params":["${metadata.id}"],
			"getOne":true
        }
      },
	  {
        "id": "s3",
        "type": "dbClient",
        "name": "查询多条记录,参数不使用占位符",
        "configuration": {
			"driverName":"mysql",
			"dsn":"root:root@tcp(127.0.0.1:3306)/test",
			"sql":"select * from users where age >= 18"
        }
      },
	  {
        "id": "s4",
        "type": "dbClient",
        "name": "更新记录,参数使用占位符",
        "configuration": {
			"driverName":"mysql",
			"dsn":"root:root@tcp(127.0.0.1:3306)/test",
			"sql":"update users set age = ? where id = ?",
			"params":["${metadata.updateAge}","${metadata.id}"]
        }
      },
	  {
        "id": "s5",
        "type": "dbClient",
        "name": "删除记录",
        "configuration": {
			"driverName":"mysql",
			"dsn":"root:root@tcp(127.0.0.1:3306)/test",
			"sql":"delete from users"
        }
      }
    ],
    "connections": [
     {
        "fromId": "s1",
        "toId": "s2",
        "type": "Success"
      },
	 {
		"fromId": "s2",
		"toId": "s3",
		"type": "Success"
	  },
	 {
		"fromId": "s3",
		"toId": "s4",
		"type": "Success"
	  },
	{
		"fromId": "s4",
		"toId": "s5",
		"type": "Success"
	  }
    ]
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
在 GitHub 上编辑此页 (opens new window)
上次更新: 2025/04/02, 01:29:50
发送邮件
ssh

← 发送邮件 ssh→

Theme by Vdoing | Copyright © 2023-2025 RuleGo Team | Apache 2.0 License

  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式