func New(proto, laddr, raddr, user, passwd string, db ...string) *Conn { return &Conn{ Raw: mysql.New(proto, laddr, raddr, user, passwd, db...), MaxRetries: 7, } }
// Open new connection. The uri need to have the following syntax: // // [PROTOCOL_SPECFIIC*]DBNAME/USER/PASSWD // // where protocol spercific part may be empty (this means connection to // local server using default protocol). Currently possible forms: // // DBNAME/USER/PASSWD // unix:SOCKPATH*DBNAME/USER/PASSWD // unix:SOCKPATH,OPTIONS*DBNAME/USER/PASSWD // tcp:ADDR*DBNAME/USER/PASSWD // tcp:ADDR,OPTIONS*DBNAME/USER/PASSWD // // OPTIONS can contain comma separated list of options in form: // opt1=VAL1,opt2=VAL2,boolopt3,boolopt4 // Currently implemented options: // laddr - local address/port (eg. 1.2.3.4:0) // timeout - connect timeout in format accepted by time.ParseDuration func (d *Driver) Open(uri string) (driver.Conn, error) { cfg := *d // copy default configuration pd := strings.SplitN(uri, "*", 2) if len(pd) == 2 { // Parse protocol part of URI p := strings.SplitN(pd[0], ":", 2) if len(p) != 2 { return nil, errors.New("Wrong protocol part of URI") } cfg.proto = p[0] options := strings.Split(p[1], ",") cfg.raddr = options[0] for _, o := range options[1:] { kv := strings.SplitN(o, "=", 2) var k, v string if len(kv) == 2 { k, v = kv[0], kv[1] } else { k, v = o, "true" } switch k { case "laddr": cfg.laddr = v case "timeout": cfg.timeout = v default: return nil, errors.New("Unknown option: " + k) } } // Remove protocol part pd = pd[1:] } // Parse database part of URI dup := strings.SplitN(pd[0], "/", 3) if len(dup) != 3 { return nil, errors.New("Wrong database part of URI") } cfg.db = dup[0] cfg.user = dup[1] cfg.passwd = dup[2] // Establish the connection c := conn{mysql.New( cfg.proto, cfg.laddr, cfg.raddr, cfg.user, cfg.passwd, cfg.db, )} if cfg.timeout != "" { to, err := time.ParseDuration(cfg.timeout) if err != nil { return nil, err } c.my.SetTimeout(to) } for _, q := range cfg.initCmds { c.my.Register(q) // Register initialisation commands } if err := c.my.Connect(); err != nil { return nil, errFilter(err) } c.my.NarrowTypeSet(true) c.my.FullFieldInfo(false) return &c, nil }