Connect to a MySQL server, addr can be ip:port, or a unix socket domain like /var/sock. Accepts a series of configuration functions as a variadic argument.
(addr string, user string, password string, dbName string, options ...func(*Conn))
| 52 | // Connect to a MySQL server, addr can be ip:port, or a unix socket domain like /var/sock. |
| 53 | // Accepts a series of configuration functions as a variadic argument. |
| 54 | func Connect(addr string, user string, password string, dbName string, options ...func(*Conn)) (*Conn, error) { |
| 55 | proto := getNetProto(addr) |
| 56 | |
| 57 | c := new(Conn) |
| 58 | |
| 59 | var err error |
| 60 | conn, err := net.DialTimeout(proto, addr, 10*time.Second) |
| 61 | if err != nil { |
| 62 | return nil, errors.Trace(err) |
| 63 | } |
| 64 | |
| 65 | if c.tlsConfig != nil { |
| 66 | c.Conn = packet.NewTLSConn(conn) |
| 67 | } else { |
| 68 | c.Conn = packet.NewConn(conn) |
| 69 | } |
| 70 | |
| 71 | c.user = user |
| 72 | c.password = password |
| 73 | c.db = dbName |
| 74 | c.proto = proto |
| 75 | |
| 76 | // use default charset here, utf-8 |
| 77 | c.charset = DEFAULT_CHARSET |
| 78 | |
| 79 | // Apply configuration functions. |
| 80 | for i := range options { |
| 81 | options[i](c) |
| 82 | } |
| 83 | |
| 84 | if err = c.handshake(); err != nil { |
| 85 | return nil, errors.Trace(err) |
| 86 | } |
| 87 | |
| 88 | return c, nil |
| 89 | } |
| 90 | |
| 91 | func (c *Conn) handshake() error { |
| 92 | var err error |
no test coverage detected