-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdriver.go
152 lines (120 loc) · 2.96 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package taos
import (
"database/sql/driver"
"errors"
"io/ioutil"
"log"
"net/http"
// "strings"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/valyala/fasthttp"
)
// Driver mydb driver for implement database/sql/driver
type Driver struct {
cfg *config
cli *http.Client
token string
}
func init() {
log.Println("driver is call ")
}
// Open for implement driver interface
func (driver *Driver) Open(name string) (driver.Conn, error) {
// log.Println("exec open driver")
cfg, err := parseDSN(name)
if err != nil {
return nil, err
}
driver.cfg = cfg
// 简单的http client配置,todo: http client 连接池管理http连接
t := http.DefaultTransport.(*http.Transport).Clone()
t.MaxIdleConns = 100
t.MaxConnsPerHost = 100
t.MaxIdleConnsPerHost = 100
driver.cli = &http.Client{
Timeout: 30 * time.Second,
Transport: t,
}
token, err := driver.login()
if err != nil {
return nil, err
}
driver.token = token
return &conn{
drv: driver,
}, nil
}
func (driver *Driver) login() (string, error) {
url := "http://" +
driver.cfg.getUri() +
"/rest/login/" +
driver.cfg.user + "/" +
driver.cfg.passwd
res, err := driver.doGet(url)
if nil != err {
return "", err
}
any := jsoniter.Get(res)
status := any.Get("status").ToString()
if status != "succ" {
return "", errors.New("[" + any.Get("code").ToString() +
"]" + any.Get("desc").ToString())
}
return any.Get("desc").ToString(), nil
}
func (driver *Driver) useDB() error {
_, err := driver.query("use " + driver.cfg.dbName)
return err
}
func (driver *Driver) query(sql string) ([]byte, error) {
url := "http://" +
driver.cfg.getUri() +
"/rest/sqlt/" +
driver.cfg.dbName
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
req.SetRequestURI(url)
// fasthttp does not automatically request a gzipped response.
// We must explicitly ask for it.
// req.Header.Set("Accept-Encoding", "gzip")
req.Header.Add("Authorization", "Taosd "+driver.token)
req.Header.Add("Content-Type", "text/plain")
req.Header.SetMethod(fasthttp.MethodPost)
req.SetBody([]byte(sql))
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(resp)
err := fasthttp.Do(req, resp)
if err != nil {
// log.Printf("Client get failed: %s\n", err)
return nil, err
}
// Do we need to decompress the response?
// contentEncoding := resp.Header.Peek("Content-Encoding")
// var body []byte
// if bytes.EqualFold(contentEncoding, []byte("gzip")) {
// // log.Println("Unzipping...")
// body, _ = resp.BodyGunzip()
// } else {
// body = resp.Body()
// }
// log.Println(string(body))
return resp.Body(), nil
}
func (driver *Driver) doGet(urlStr string) ([]byte, error) {
method := "GET"
req, err := http.NewRequest(method, urlStr, nil)
if err != nil {
return nil, err
}
res, err := driver.cli.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
return body, nil
}