gf/database/gdb/gdb_mysql.go

47 lines
1.2 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
}
2020-01-07 22:14:32 +08:00
// Open creates and returns a underlying database connection with given configuration.
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 {
2020-01-07 22:14:32 +08:00
source = fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s?charset=%s&multiStatements=true&parseTime=true&loc=Local",
config.User, config.Pass, config.Host, config.Port, config.Name, config.Charset,
)
2019-06-19 09:06:52 +08:00
}
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
}
2020-01-06 20:43:59 +08:00
// getChars returns the quote chars for field.
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
}
2020-01-06 20:43:59 +08:00
// handleSqlBeforeExec handles the sql before posts it to database.
2020-01-07 22:14:32 +08:00
func (db *dbMysql) handleSqlBeforeExec(sql string) string {
return sql
2019-06-19 09:06:52 +08:00
}