func providerConfigure(d *schema.ResourceData) (interface{}, error) { var username = d.Get("username").(string) var password = d.Get("password").(string) var endpoint = d.Get("endpoint").(string) proto := "tcp" if endpoint[0] == '/' { proto = "unix" } // mysqlts is the thread-safe implementation of mymysql, so we can // safely re-use the same connection between multiple parallel // operations. conn := mysqlts.New(proto, "", endpoint, username, password) err := conn.Connect() if err != nil { return nil, err } major, minor, patch, err := mysqlVersion(conn) if err != nil { return nil, err } return &providerConfiguration{ Conn: conn, VersionMajor: major, VersionMinor: minor, VersionPatch: patch, }, nil }
func OpenDB() { file, err := os.Open("config.json") if err != nil { panic(err) } var config ConfigFile decoder := json.NewDecoder(file) err = decoder.Decode(&config) if err != nil { panic(err) } gConn = thrsafe.New("tcp", config.Mysql.LocalAddr, config.Mysql.Address, config.Mysql.User, config.Mysql.Password, config.Mysql.Database) err = gConn.Connect() if err != nil { panic(err) } }
func NewDatabase(address string, username string, password string, dbname string, prefix string) (*Database, error) { db := new(Database) db.connection = mysql.New("tcp", "", address, username, password, dbname) err := db.connection.Connect() if err != nil { return nil, err } db.connection.Register("set names utf8") db.open = true if prefix == nil { prefix = "" } db.prefix = prefix return db, nil }
package database import ( "github.com/ziutek/mymysql/thrsafe" "log" "os" ) var ( // MySQL Connection Handler Db = thrsafe.New(db_proto, "", db_addr, db_user, db_pass, db_name) ) func MysqlError(err error) (ret bool) { ret = (err != nil) if ret { log.Println("MySQL error: ", err) } return } func MysqlErrExit(err error) { if MysqlError(err) { os.Exit(1) } }