github.com/zhongdalu/gf@v1.0.0/g/database/gdb/gdb_mysql.go (about) 1 // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/zhongdalu/gf. 6 7 package gdb 8 9 import ( 10 "database/sql" 11 "fmt" 12 _ "github.com/zhongdalu/gf/third/github.com/gf-third/mysql" 13 ) 14 15 // 数据库链接对象 16 type dbMysql struct { 17 *dbBase 18 } 19 20 // 创建SQL操作对象,内部采用了lazy link处理 21 func (db *dbMysql) Open(config *ConfigNode) (*sql.DB, error) { 22 var source string 23 if config.LinkInfo != "" { 24 source = config.LinkInfo 25 } else { 26 source = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&multiStatements=true", 27 config.User, config.Pass, config.Host, config.Port, config.Name, config.Charset) 28 } 29 if db, err := sql.Open("gf-mysql", source); err == nil { 30 return db, nil 31 } else { 32 return nil, err 33 } 34 } 35 36 // 获得关键字操作符 37 func (db *dbMysql) getChars() (charLeft string, charRight string) { 38 return "`", "`" 39 } 40 41 // 在执行sql之前对sql进行进一步处理 42 func (db *dbMysql) handleSqlBeforeExec(query string) string { 43 return query 44 }