This repository has been archived by the owner on Jan 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdriver.go
74 lines (62 loc) · 1.61 KB
/
driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2013 Julien Schmidt. All rights reserved.
// http://www.julienschmidt.com
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
// Go SphinxQL Driver - A SphinxQL-Driver for Go's database/sql package
package sphinxql
import (
"database/sql"
"database/sql/driver"
"net"
"time"
)
type sphinxqlDriver struct{}
// Open new Connection.
// See https://github.com/Go-SQL-Driver/SphinxQL#dsn-data-source-name for how
// the DSN string is formated
func (d *sphinxqlDriver) Open(dsn string) (driver.Conn, error) {
var err error
// New sphinxqlConn
mc := new(sphinxqlConn)
mc.cfg = parseDSN(dsn)
// Connect to Server
if _, ok := mc.cfg.params["timeout"]; ok { // with timeout
var timeout time.Duration
timeout, err = time.ParseDuration(mc.cfg.params["timeout"])
if err == nil {
mc.netConn, err = net.DialTimeout(mc.cfg.net, mc.cfg.addr, timeout)
}
} else { // no timeout
mc.netConn, err = net.Dial(mc.cfg.net, mc.cfg.addr)
}
if err != nil {
return nil, err
}
mc.buf = newBuffer(mc.netConn)
// Reading Handshake Initialization Packet
err = mc.readInitPacket()
if err != nil {
return nil, err
}
// Send Client Authentication Packet
err = mc.writeAuthPacket()
if err != nil {
return nil, err
}
// Read Result Packet
err = mc.readResultOK()
if err != nil {
return nil, err
}
// Handle DSN Params
err = mc.handleParams()
if err != nil {
return nil, err
}
return mc, err
}
func init() {
sql.Register("sphinxql", &sphinxqlDriver{})
}