mssql

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

MGIN MsSQL 插件 (github.com/maczh/mssql)

MGIN 的 Microsoft SQL Server 数据库插件, 基于 GORM v2 (gorm.io/gorm + gorm.io/driver/sqlserver), 参照 maczh/mgin 的 db/mysql 插件 编写, 但作为独立模块 (github.com/maczh/mssql), 不依赖 maczh/mgin 内部包, 与 maczh/hive、maczh/hbase 风格一致。

特性

  • 单连接 / 多库 (multidb) 两种模式
  • 连接池配置 (最大连接、空闲连接、超时、存活时间)
  • GORM Debug 模式 (打印 SQL)
  • 查询二级缓存: 进程内内存缓存 (go-gorm/caches Easer) 或可选 Redis 缓存
  • 生命周期 Init / Check / Close 与自动重连, 完全兼容 MGIN 插件契约

安装

go get github.com/maczh/mssql

依赖 (由 go mod 自动解析):

  • gorm.io/gorm v1.31.0
  • gorm.io/driver/sqlserver v1.6.1
  • github.com/go-gorm/caches/v4 v4.0.5
  • github.com/knadh/koanf v1.5.0
  • github.com/redis/go-redis/v9 (可选 Redis 缓存)
  • github.com/sadlil/gologger

使用方式

import "github.com/maczh/mssql"

// 注册到 MGIN (全局实例 MsSQL)
app.MGin.UsePlugin("mssql", mssql.MsSQL)

// 本地配置 (go.config.used 含 mssql; go.config.prefix 建议设为 mssql)
// 配置中心下发 mssql-test.yml, 由 MGIN 注入到插件 Init([]byte)
获取连接并执行查询 (GORM v2)
db, err := mssql.MsSQL.GetConnection()          // 单连接
// 或指定库名: mssql.MsSQL.GetConnection("db1")

// 原生 SQL
var users []User
db.Raw("SELECT id, name FROM t_user WITH (NOLOCK) WHERE age > ?", 18).Scan(&users)

// GORM 模型
db.Where("name = ?", "maczh").First(&user)
db.Create(&user)
db.Save(&user)
db.Delete(&user, 1)

// 多库模式按库名取连接
db1, _ := mssql.MsSQL.GetConnection("db1")

配置

单连接
go:
  config:
    used: mssql
    prefix: mssql
  data:
    mssql: "sqlserver://sa:Your_password123@localhost:1433?database=test&encrypt=disable"
    mssql_debug: false
    mssql_cache: false
    mssql_pool:
      max: 100
      total: 10
      timeout: 60
      life: 60

DSN 格式: sqlserver://user:password@host:port?database=dbname&encrypt=... 本地/开发环境常加 encrypt=disable (SQL Server 默认要求加密连接)。

多库 (multidb)
go:
  data:
    mssql_multidb: true
    mssql_dbNames: db1,db2
    mssql_db1: "sqlserver://sa:pwd1@host1:1433?database=db1&encrypt=disable"
    mssql_db2: "sqlserver://sa:pwd2@host2:1433?database=db2&encrypt=disable"

多库模式下通过 mssql.MsSQL.GetConnection("db1") 取对应连接; IsMultiDB() 判断是否多库, ListConnNames() 返回已注册的连接名。

查询缓存

// 进程内内存缓存 (需配置 go.data.mssql_cache: true)
mssql.MsSQL.UseCacheWithRedis(cache)

// 或用外部 Redis 客户端 (如 MGIN redis 插件) 作为二级缓存
// mssql.MsSQL.UseCacheWithRedis(rdb)
  • 缓存开关: go.data.mssql_cache: true
  • 可选 Redis 地址: go.data.mssql_cache_redis: "127.0.0.1:6379"
  • 过期秒数: go.data.mssql_cache_expired (默认 300)

API 速览

方法 说明
Init([]byte) 用 yml 原文初始化连接
Check() error 健康检查 + 自动重连
Close() 关闭所有连接
GetConnection(dbName ...string) (*gorm.DB, error) 获取 GORM 连接
IsMultiDB() bool 是否多库模式
ListConnNames() []string 已注册连接名
UseCache() bool 启用内存/Redis 查询缓存
UseCacheWithRedis(rdb) bool 用外部 Redis 客户端启用缓存

与 mysql 插件的差异

  • 模块独立 (github.com/maczh/mssql), 不引入 maczh/mgin 内部 config/redis 包; Redis 缓存改为接受外部传入的 redis.UniversalClient (UseCacheWithRedis)。
  • Redis 客户端使用 github.com/redis/go-redis/v9 (mysql 插件在 mgin 内用的是 v7), RedisCacher 逻辑一致。
  • 其余配置键、连接池、Debug、multidb、生命周期方法均与 mysql 插件保持一致 (go.data.mssql*)。

测试

go test ./... -run TestMssql -v -timeout 90s

无可达 SQL Server 时测试自动 SKIP (先 TCP 探活端口, 再限时建连)。

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MsSQL = &mssqlClient{}

MsSQL 是提供给 MGIN 注册的全局插件实例: app.MGin.UsePlugin("mssql", mssql.MsSQL)

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址