gf/database/gdb/gdb_mysql.go

46 lines
1.1 KiB
Go
Raw Normal View History

// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
2017-12-29 16:03:30 +08:00
//
// 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.
2017-12-29 16:03:30 +08:00
2017-11-23 10:21:28 +08:00
package gdb
import (
"database/sql"
"fmt"
2019-07-29 21:01:19 +08:00
_ "github.com/gf-third/mysql"
2017-11-23 10:21:28 +08:00
)
// 数据库链接对象
2018-12-14 18:35:51 +08:00
type dbMysql struct {
2019-06-19 09:06:52 +08:00
*dbBase
2017-11-23 10:21:28 +08:00
}
// 创建SQL操作对象内部采用了lazy link处理
2019-06-19 09:06:52 +08:00
func (db *dbMysql) Open(config *ConfigNode) (*sql.DB, error) {
var source string
if config.LinkInfo != "" {
source = config.LinkInfo
} else {
2019-10-25 19:54:02 +08:00
source = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&multiStatements=true&parseTime=true&loc=Local",
2019-06-19 09:06:52 +08:00
config.User, config.Pass, config.Host, config.Port, config.Name, config.Charset)
}
if db, err := sql.Open("gf-mysql", source); err == nil {
return db, nil
} else {
return nil, err
}
2017-11-23 10:21:28 +08:00
}
2018-12-14 18:35:51 +08:00
// 获得关键字操作符
2019-06-19 09:06:52 +08:00
func (db *dbMysql) getChars() (charLeft string, charRight string) {
return "`", "`"
2017-11-23 10:21:28 +08:00
}
// 在执行sql之前对sql进行进一步处理
2018-12-14 18:35:51 +08:00
func (db *dbMysql) handleSqlBeforeExec(query string) string {
2019-06-19 09:06:52 +08:00
return query
}