1 Star 1 Fork 0

amuluze/amutool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
db.go 1.31 KB
一键复制 编辑 原始数据 按行查看 历史
jialong.wang 提交于 2023-12-07 15:39 . feat: add clickhousex
// Package clickhousex
// Date: 2023/12/6 18:08
// Author: Amu
// Description:
package clickhousex
import (
"fmt"
"gorm.io/driver/clickhouse"
"gorm.io/gorm"
)
type DB struct {
*gorm.DB
}
func NewDB(opts ...Option) (*DB, error) {
opt := &option{
Debug: false,
MaxIdleConns: 50,
MaxOpenConns: 100,
DialTimeout: 10,
ConnMaxLifeTime: 3600,
}
for _, o := range opts {
o(opt)
}
conn := getConn(opt)
fmt.Printf("conn: %#v\n", conn)
db, err := gorm.Open(clickhouse.New(clickhouse.Config{
Conn: conn, // initialize with existing database conn
DisableDatetimePrecision: true,
DontSupportRenameColumn: true,
DontSupportEmptyDefaultValue: false,
SkipInitializeWithVersion: false,
DefaultGranularity: 3,
DefaultCompression: "LZ4",
DefaultIndexType: "minmax",
DefaultTableEngineOpts: "ENGINE=MergeTree() ORDER BY tuple()",
}), &gorm.Config{})
return &DB{db}, err
}
func (db *DB) Close() {
if db != nil {
conn, err := db.DB.DB()
if err != nil {
return
}
err = conn.Close()
if err != nil {
return
}
}
}
func (db *DB) RunInTransaction(fn func(tx *gorm.DB) error) error {
return db.Transaction(fn)
}
func (db *DB) AutoMigrate(models ...interface{}) error {
return db.DB.AutoMigrate(models...)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/amuluze/amutool.git
git@gitee.com:amuluze/amutool.git
amuluze
amutool
amutool
6d4b89917063

搜索帮助