feat(database): 添加ClickHouse数据库驱动支持
- 新增ClickHouse数据库驱动实现 - 更新数据库驱动注册包,添加对MySQL、PostgreSQL、SQL Server、Oracle、SQLite和ClickHouse的支持 - 修改Database函数返回类型从gdb.DB到DB - 添加go.mod和go.sum依赖管理文件
This commit is contained in:
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user