// Get a connection. Notice that this shall not be called from initialization thread. func New() *mysql.Client { mutex.Lock() db := cachedb now := time.Now() if db != nil { // There was a cached db connection, use it. cachedb = nil } if db != nil && now.Sub(released) > 1e11 { // The saved db was too old, discard it as we don't trust it // TODO: Better would be a way to test if it is still valid, but it must not take time. db = nil // log.Println("Discard the old db") } mutex.Unlock() if db == nil { // There was no local cache to be used var err error db, err = mysql.DialTCP(dbLicenseDatabase, dbDatabaseLogin, dbDatabasePassword, dbDatabaseName) if err != nil { countFailure++ if countFailure == maxRetries { log.Println(err, "(last warning)") } else if countFailure < maxRetries { log.Println(err) } } else { countFailure = 0 } } return db }
func trymysql() { db, err := mysql.DialTCP("localhost", "tim", "letmein", "tim") if err != nil { os.Exit(1) } err = db.Query("select name, age, length(name) namelen from tabletest;") if err != nil { os.Exit(1) } result, err := db.StoreResult() if err != nil { os.Exit(1) } info, err := gemini.LoadTableFromMySQL(result) if err != nil { fmt.Println(err) os.Exit(1) } tables := make(gemini.TableSet) tables["people"] = info fmt.Printf("%v\n", *tables["people"]) }
func (d *MySQLDriver) Open(dsn string) (driver.Conn, error) { hostport, user, passwd, dbname, err := parseDSN(dsn) if err != nil { return nil, err } //fmt.Printf("host=%s user=%s pass=%s db=%s\n", hostport, user, passwd, dbname) client, err := mysql.DialTCP(hostport, user, passwd, dbname) if err != nil { return nil, err } return &MySQLConn{client}, nil }