Categorygithub.com/iyuangang/oracle-sql-runner
module
1.0.1-beta
Repository: https://github.com/iyuangang/oracle-sql-runner.git
Documentation: pkg.go.dev

# README

Oracle SQL Runner

Build codecov Release Go Report Card License

Oracle SQL Runner 是一个高性能的 Oracle SQL 脚本执行工具,支持并行执行、错误重试、PL/SQL 块等特性。

特性

  • 支持并行执行 SQL 语句
  • 自动识别和处理 PL/SQL 块
  • 智能错误重试机制
  • 详细的执行日志和性能指标
  • 支持查询结果显示
  • 跨平台支持 (Linux, Windows, macOS)

安装

从二进制安装

Releases 页面下载对应平台的二进制文件。

从源码构建

需要 Go 1.22 或更高版本:

git clone https://github.com/iyuangang/oracle-sql-runner.git
cd oracle-sql-runner
go build ./cmd/sql-runner

配置

创建 config.json 配置文件:

{
  "databases": {
    "prod": {
      "name": "生产环境",
      "user": "your_username",
      "password": "your_password",
      "host": "localhost",
      "port": 1521,
      "service": "ORCLPDB1",
      "max_connections": 5,
      "idle_timeout": 300
    }
  },
  "max_retries": 3,
  "max_concurrent": 5,
  "batch_size": 1000,
  "timeout": 30,
  "log_level": "info",
  "log_file": "logs/sql-runner.log"
}

配置说明

  • databases: 数据库配置列表
    • name: 数据库描述
    • user: 用户名
    • password: 密码
    • host: 主机地址
    • port: 端口号
    • service: 服务名
    • max_connections: 最大连接数
    • idle_timeout: 空闲超时时间(秒)
  • max_retries: 最大重试次数
  • max_concurrent: 最大并发执行数
  • batch_size: 批处理大小
  • timeout: SQL 执行超时时间(秒)
  • log_level: 日志级别 (debug/info/warn/error)
  • log_file: 日志文件路径

使用方法

基本用法

sql-runner -f script.sql -d prod

命令行参数

Usage:
  sql-runner [flags]

Flags:
  -c, --config string    配置文件路径 (默认 "config.json")
  -d, --database string  数据库名称
  -f, --file string      SQL文件路径
  -h, --help            帮助信息
  -v, --verbose         显示详细信息
      --version         版本信息

SQL 文件格式

支持三种类型的 SQL 语句:

  1. 普通查询
SELECT * FROM employees;
  1. DML/DDL 语句
CREATE TABLE test_table (
    id NUMBER PRIMARY KEY,
    name VARCHAR2(100)
);
  1. PL/SQL 块
CREATE OR REPLACE PROCEDURE test_proc AS
BEGIN
    DBMS_OUTPUT.PUT_LINE('Hello World');
END;
/

日志输出

日志以 JSON 格式输出,包含详细的执行信息:

{
    "time": "2024-11-26T09:46:38.7720375+08:00",
    "level": "INFO",
    "source": {
        "function": "core.(*Executor).ExecuteFile",
        "file": "internal/core/executor.go",
        "line": 123
    },
    "msg": "SQL文件执行完成",
    "success": 33,
    "failed": 1,
    "duration": 3827597300
}

开发

运行测试

# 运行单元测试
go test ./...

# 运行集成测试
go test -tags=integration ./...

构建发布版本

make release

许可证

本项目采用 MIT 许可证,详见 LICENSE 文件。

贡献

欢迎提交 Issue 和 Pull Request!

鸣谢

  • godror 提供了 Oracle 数据库的 Go 驱动
  • cobra 提供了命令行解析库
  • slog 提供了高性能的日志库

# Packages

No description provided by the author
No description provided by the author
No description provided by the author