feat(database): 添加ClickHouse数据库驱动支持

- 新增ClickHouse数据库驱动实现
- 更新数据库驱动注册包,添加对MySQL、PostgreSQL、SQL Server、Oracle、SQLite和ClickHouse的支持
- 修改Database函数返回类型从gdb.DB到DB
- 添加go.mod和go.sum依赖管理文件
This commit is contained in:
maguodong
2026-04-08 11:02:33 +08:00
parent 2a50b54e7d
commit d01b2973df
11 changed files with 433 additions and 11 deletions
+61
View File
@@ -0,0 +1,61 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package clickhouse implements database.Driver for ClickHouse database.
package clickhouse
import (
"database/sql"
"fmt"
_ "github.com/ClickHouse/clickhouse-go/v2"
"git.magicany.cc/black1552/gin-base/database"
)
// Driver is the driver for ClickHouse database.
type Driver struct {
*database.Core
}
const (
quoteChar = `"`
)
func init() {
if err := database.Register("clickhouse", New()); err != nil {
panic(err)
}
}
// New creates and returns a driver that implements database.Driver for ClickHouse.
func New() database.Driver {
return &Driver{}
}
// New creates and returns a database object for ClickHouse.
func (d *Driver) New(core *database.Core, node *database.ConfigNode) (database.DB, error) {
return &Driver{
Core: core,
}, nil
}
// GetChars returns the security char for ClickHouse.
func (d *Driver) GetChars() (charLeft string, charRight string) {
return quoteChar, quoteChar
}
// Open creates and returns an underlying sql.DB object for ClickHouse.
func (d *Driver) Open(config *database.ConfigNode) (*sql.DB, error) {
var source string
if config.Link != "" {
source = config.Link
} else {
source = fmt.Sprintf("clickhouse://%s:%s@%s:%s/%s",
config.User, config.Pass, config.Host, config.Port, config.Name)
}
return sql.Open("clickhouse", source)
}
+20 -8
View File
@@ -4,15 +4,27 @@
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package drivers provides database drivers registration for GF ORM.
// This package automatically registers common database drivers when imported.
// Package drivers provides database drivers registration for our custom ORM.
// This package automatically registers all supported database drivers when imported.
// Supported databases: MySQL, MariaDB, PostgreSQL, SQL Server, Oracle, SQLite, ClickHouse
package drivers
import (
_ "github.com/gogf/gf/contrib/drivers/clickhouse/v2"
_ "github.com/gogf/gf/contrib/drivers/mssql/v2"
_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
_ "github.com/gogf/gf/contrib/drivers/oracle/v2"
_ "github.com/gogf/gf/contrib/drivers/pgsql/v2"
_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
// MySQL & MariaDB driver
_ "git.magicany.cc/black1552/gin-base/database/drivers/mysql"
// PostgreSQL driver
_ "git.magicany.cc/black1552/gin-base/database/drivers/pgsql"
// SQL Server driver
_ "git.magicany.cc/black1552/gin-base/database/drivers/mssql"
// Oracle driver
_ "git.magicany.cc/black1552/gin-base/database/drivers/oracle"
// SQLite driver (pure Go implementation, no CGO required)
_ "git.magicany.cc/black1552/gin-base/database/drivers/sqlite"
// ClickHouse driver
_ "git.magicany.cc/black1552/gin-base/database/drivers/clickhouse"
)
+61
View File
@@ -0,0 +1,61 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package mssql implements database.Driver for Microsoft SQL Server.
package mssql
import (
"database/sql"
"fmt"
_ "github.com/microsoft/go-mssqldb"
"git.magicany.cc/black1552/gin-base/database"
)
// Driver is the driver for SQL Server database.
type Driver struct {
*database.Core
}
const (
quoteChar = `"`
)
func init() {
if err := database.Register("mssql", New()); err != nil {
panic(err)
}
}
// New creates and returns a driver that implements database.Driver for SQL Server.
func New() database.Driver {
return &Driver{}
}
// New creates and returns a database object for SQL Server.
func (d *Driver) New(core *database.Core, node *database.ConfigNode) (database.DB, error) {
return &Driver{
Core: core,
}, nil
}
// GetChars returns the security char for SQL Server.
func (d *Driver) GetChars() (charLeft string, charRight string) {
return quoteChar, quoteChar
}
// Open creates and returns an underlying sql.DB object for SQL Server.
func (d *Driver) Open(config *database.ConfigNode) (*sql.DB, error) {
var source string
if config.Link != "" {
source = config.Link
} else {
source = fmt.Sprintf("sqlserver://%s:%s@%s:%s?database=%s",
config.User, config.Pass, config.Host, config.Port, config.Name)
}
return sql.Open("sqlserver", source)
}
+83
View File
@@ -0,0 +1,83 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package mysql implements database.Driver for MySQL database.
package mysql
import (
"database/sql"
"fmt"
"strings"
_ "github.com/go-sql-driver/mysql"
"git.magicany.cc/black1552/gin-base/database"
)
// Driver is the driver for MySQL database.
type Driver struct {
*database.Core
}
const (
quoteChar = "`"
)
func init() {
var (
err error
driverObj = New()
)
// Register for both mysql and mariadb (compatible protocol)
if err = database.Register("mysql", driverObj); err != nil {
panic(err)
}
if err = database.Register("mariadb", driverObj); err != nil {
panic(err)
}
}
// New creates and returns a driver that implements database.Driver for MySQL.
func New() database.Driver {
return &Driver{}
}
// New creates and returns a database object for MySQL.
// It implements the interface of database.Driver for extra database driver installation.
func (d *Driver) New(core *database.Core, node *database.ConfigNode) (database.DB, error) {
return &Driver{
Core: core,
}, nil
}
// GetChars returns the security char for MySQL database.
func (d *Driver) GetChars() (charLeft string, charRight string) {
return quoteChar, quoteChar
}
// Open creates and returns an underlying sql.DB object for MySQL.
func (d *Driver) Open(config *database.ConfigNode) (*sql.DB, error) {
var (
source string
username = config.User
password = config.Pass
protocol = "tcp"
dbName = config.Name
params = make([]string, 0)
)
if config.Extra != "" {
params = append(params, config.Extra)
}
// Default params
params = append(params, "loc=Local", "parseTime=true")
source = fmt.Sprintf("%s:%s@%s(%s:%s)/%s?%s",
username, password, protocol, config.Host, config.Port, dbName, strings.Join(params, "&"))
return sql.Open("mysql", source)
}
+61
View File
@@ -0,0 +1,61 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package oracle implements database.Driver for Oracle database.
package oracle
import (
"database/sql"
"fmt"
_ "github.com/sijms/go-ora/v2"
"git.magicany.cc/black1552/gin-base/database"
)
// Driver is the driver for Oracle database.
type Driver struct {
*database.Core
}
const (
quoteChar = `"`
)
func init() {
if err := database.Register("oracle", New()); err != nil {
panic(err)
}
}
// New creates and returns a driver that implements database.Driver for Oracle.
func New() database.Driver {
return &Driver{}
}
// New creates and returns a database object for Oracle.
func (d *Driver) New(core *database.Core, node *database.ConfigNode) (database.DB, error) {
return &Driver{
Core: core,
}, nil
}
// GetChars returns the security char for Oracle.
func (d *Driver) GetChars() (charLeft string, charRight string) {
return quoteChar, quoteChar
}
// Open creates and returns an underlying sql.DB object for Oracle.
func (d *Driver) Open(config *database.ConfigNode) (*sql.DB, error) {
var source string
if config.Link != "" {
source = config.Link
} else {
source = fmt.Sprintf("oracle://%s:%s@%s:%s/%s",
config.User, config.Pass, config.Host, config.Port, config.Name)
}
return sql.Open("oracle", source)
}
+71
View File
@@ -0,0 +1,71 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package pgsql implements database.Driver for PostgreSQL database.
package pgsql
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
"git.magicany.cc/black1552/gin-base/database"
)
// Driver is the driver for PostgreSQL database.
type Driver struct {
*database.Core
}
const (
quoteChar = `"`
)
func init() {
if err := database.Register("pgsql", New()); err != nil {
panic(err)
}
}
// New creates and returns a driver that implements database.Driver for PostgreSQL.
func New() database.Driver {
return &Driver{}
}
// New creates and returns a database object for PostgreSQL.
// It implements the interface of database.Driver for extra database driver installation.
func (d *Driver) New(core *database.Core, node *database.ConfigNode) (database.DB, error) {
return &Driver{
Core: core,
}, nil
}
// GetChars returns the security char for PostgreSQL database.
func (d *Driver) GetChars() (charLeft string, charRight string) {
return quoteChar, quoteChar
}
// Open creates and returns an underlying sql.DB object for PostgreSQL.
func (d *Driver) Open(config *database.ConfigNode) (*sql.DB, error) {
var (
source string
username = config.User
password = config.Pass
host = config.Host
port = config.Port
dbName = config.Name
)
source = fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
host, port, username, password, dbName)
if config.Extra != "" {
source += " " + config.Extra
}
return sql.Open("postgres", source)
}
+58
View File
@@ -0,0 +1,58 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package sqlite implements database.Driver for SQLite database.
package sqlite
import (
"database/sql"
_ "modernc.org/sqlite"
"git.magicany.cc/black1552/gin-base/database"
)
// Driver is the driver for SQLite database.
type Driver struct {
*database.Core
}
const (
quoteChar = "`"
)
func init() {
if err := database.Register("sqlite", New()); err != nil {
panic(err)
}
}
// New creates and returns a driver that implements database.Driver for SQLite.
func New() database.Driver {
return &Driver{}
}
// New creates and returns a database object for SQLite.
func (d *Driver) New(core *database.Core, node *database.ConfigNode) (database.DB, error) {
return &Driver{
Core: core,
}, nil
}
// GetChars returns the security char for SQLite.
func (d *Driver) GetChars() (charLeft string, charRight string) {
return quoteChar, quoteChar
}
// Open creates and returns an underlying sql.DB object for SQLite.
func (d *Driver) Open(config *database.ConfigNode) (*sql.DB, error) {
// For SQLite, use the Name field as the database file path
dbName := config.Name
if dbName == "" {
dbName = ":memory:"
}
return sql.Open("sqlite", dbName)
}