gf/database/gdb/gdb_driver_mysql.go

144 lines
4.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"
"errors"
"fmt"
"github.com/gogf/gf/internal/intlog"
"github.com/gogf/gf/text/gregex"
"github.com/gogf/gf/text/gstr"
2019-07-29 21:01:19 +08:00
2020-03-08 00:17:42 +08:00
_ "github.com/go-sql-driver/mysql"
2017-11-23 10:21:28 +08:00
)
2020-03-08 00:17:42 +08:00
// DriverMysql is the driver for mysql database.
type DriverMysql struct {
*Core
}
// New creates and returns a database object for mysql.
// It implements the interface of gdb.Driver for extra database driver installation.
2020-03-08 00:17:42 +08:00
func (d *DriverMysql) New(core *Core, node *ConfigNode) (DB, error) {
return &DriverMysql{
Core: core,
}, nil
2017-11-23 10:21:28 +08:00
}
// Open creates and returns a underlying sql.DB object for mysql.
// Note that it converts time.Time argument to local timezone in default.
2020-03-08 00:17:42 +08:00
func (d *DriverMysql) Open(config *ConfigNode) (*sql.DB, error) {
2019-06-19 09:06:52 +08:00
var source string
if config.LinkInfo != "" {
source = config.LinkInfo
// Custom changing the schema in runtime.
if config.Name != "" {
source, _ = gregex.ReplaceString(`/([\w\.\-]+)+`, "/"+config.Name, source)
}
2019-06-19 09:06:52 +08:00
} else {
2020-01-07 22:14:32 +08:00
source = fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s?charset=%s&multiStatements=true&parseTime=true",
2020-01-07 22:14:32 +08:00
config.User, config.Pass, config.Host, config.Port, config.Name, config.Charset,
)
2019-06-19 09:06:52 +08:00
}
intlog.Printf("Open: %s", source)
2020-03-08 00:17:42 +08:00
if db, err := sql.Open("mysql", source); err == nil {
2019-06-19 09:06:52 +08:00
return db, nil
} else {
return nil, err
}
2017-11-23 10:21:28 +08:00
}
2020-03-08 00:17:42 +08:00
// GetChars returns the security char for this type of database.
func (d *DriverMysql) GetChars() (charLeft string, charRight string) {
2019-06-19 09:06:52 +08:00
return "`", "`"
2017-11-23 10:21:28 +08:00
}
// HandleSqlBeforeCommit handles the sql before posts it to database.
func (d *DriverMysql) HandleSqlBeforeCommit(link Link, sql string, args []interface{}) (string, []interface{}) {
return sql, args
2019-06-19 09:06:52 +08:00
}
// Tables retrieves and returns the tables of current schema.
// It's mainly used in cli tool chain for automatically generating the models.
2020-03-08 00:17:42 +08:00
func (d *DriverMysql) Tables(schema ...string) (tables []string, err error) {
var result Result
2020-03-08 00:17:42 +08:00
link, err := d.DB.GetSlave(schema...)
if err != nil {
return nil, err
}
2020-03-08 00:17:42 +08:00
result, err = d.DB.DoGetAll(link, `SHOW TABLES`)
if err != nil {
return
}
for _, m := range result {
for _, v := range m {
tables = append(tables, v.String())
}
}
return
}
// TableFields retrieves and returns the fields information of specified table of current
// schema.
//
// Note that it returns a map containing the field name and its corresponding fields.
// As a map is unsorted, the TableField struct has a "Index" field marks its sequence in
// the fields.
//
// It's using cache feature to enhance the performance, which is never expired util the
// process restarts.
2020-03-08 00:17:42 +08:00
func (d *DriverMysql) TableFields(table string, schema ...string) (fields map[string]*TableField, err error) {
charL, charR := d.GetChars()
table = gstr.Trim(table, charL+charR)
if gstr.Contains(table, " ") {
return nil, errors.New("function TableFields supports only single table operations")
}
2020-03-08 00:17:42 +08:00
checkSchema := d.schema.Val()
if len(schema) > 0 && schema[0] != "" {
checkSchema = schema[0]
}
v, _ := internalCache.GetOrSetFunc(
fmt.Sprintf(`mysql_table_fields_%s_%s@group:%s`, table, checkSchema, d.GetGroup()),
func() (interface{}, error) {
2020-09-27 00:15:11 +08:00
var (
result Result
link *sql.DB
)
2020-03-08 00:17:42 +08:00
link, err = d.DB.GetSlave(checkSchema)
if err != nil {
return nil, err
}
2020-03-08 00:17:42 +08:00
result, err = d.DB.DoGetAll(
link,
2020-03-08 00:17:42 +08:00
fmt.Sprintf(`SHOW FULL COLUMNS FROM %s`, d.DB.QuoteWord(table)),
)
if err != nil {
return nil, err
}
fields = make(map[string]*TableField)
for i, m := range result {
fields[m["Field"].String()] = &TableField{
Index: i,
Name: m["Field"].String(),
Type: m["Type"].String(),
Null: m["Null"].Bool(),
Key: m["Key"].String(),
Default: m["Default"].Val(),
Extra: m["Extra"].String(),
Comment: m["Comment"].String(),
}
}
return fields, nil
}, 0)
if err == nil {
fields = v.(map[string]*TableField)
}
return
}