forked from cockroachdb/cockroach
/
conn.go
175 lines (153 loc) · 4.58 KB
/
conn.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License. See the AUTHORS file
// for names of contributors.
//
// Author: Peter Mattis (peter@cockroachlabs.com)
package driver
import (
"database/sql/driver"
"errors"
"fmt"
"strings"
"github.com/cockroachdb/cockroach/util"
)
var _ driver.Conn = &conn{}
var _ driver.Queryer = &conn{}
var _ driver.Execer = &conn{}
// conn implements the sql/driver.Conn interface. Note that conn is assumed to
// be stateful and is not used concurrently by multiple goroutines; See
// https://golang.org/pkg/database/sql/driver/#Conn.
type conn struct {
sender Sender
session []byte
beginTransaction bool
}
func (c *conn) Close() error {
return nil
}
func (c *conn) Prepare(query string) (driver.Stmt, error) {
return &stmt{conn: c, stmt: query}, nil
}
func (c *conn) Begin() (driver.Tx, error) {
c.beginTransaction = true
return &tx{conn: c}, nil
}
func (c *conn) Exec(stmt string, args []driver.Value) (driver.Result, error) {
result, err := c.internalQuery(stmt, args)
if err != nil {
return nil, err
}
switch t := result.GetUnion().(type) {
case nil:
return nil, nil
case *Response_Result_DDL_:
return driver.ResultNoRows, nil
case *Response_Result_RowsAffected:
return driver.RowsAffected(int(t.RowsAffected)), nil
case *Response_Result_Rows_:
return driver.RowsAffected(len(t.Rows.Rows)), nil
default:
return nil, util.Errorf("unexpected result %s of type %T", t, t)
}
}
func (c *conn) Query(stmt string, args []driver.Value) (driver.Rows, error) {
result, err := c.internalQuery(stmt, args)
if err != nil {
return nil, err
}
driverRows := &rows{}
switch t := result.GetUnion().(type) {
case *Response_Result_Rows_:
resultRows := t.Rows
driverRows.columns = resultRows.Columns
driverRows.rows = make([][]driver.Value, 0, len(resultRows.Rows))
for _, row := range resultRows.Rows {
values := make([]driver.Value, 0, len(row.Values))
for _, datum := range row.Values {
val, err := datum.Value()
if err != nil {
return nil, err
}
values = append(values, val)
}
driverRows.rows = append(driverRows.rows, values)
}
}
return driverRows, nil
}
func (c *conn) internalQuery(stmt string, args []driver.Value) (*Response_Result, error) {
if c.beginTransaction {
stmt = "BEGIN TRANSACTION; " + stmt
c.beginTransaction = false
}
dArgs := make([]Datum, 0, len(args))
for _, arg := range args {
datum, err := makeDatum(arg)
if err != nil {
return nil, err
}
dArgs = append(dArgs, datum)
}
return c.send(stmt, dArgs)
}
// send sends the statement to the server.
func (c *conn) send(stmt string, dArgs []Datum) (*Response_Result, error) {
args := Request{
Session: c.session,
Sql: stmt,
Params: dArgs,
}
// Forget the session state, and use the one provided in the server
// response for the next request.
c.session = nil
resp, err := c.sender.Send(args)
if err != nil {
return nil, err
}
// Set the session state even if the server returns an application error.
// The server is responsible for constructing the correct session state
// and sending it back.
c.session = resp.Session
// Check for any application errors.
// TODO(vivek): We might want to bunch all errors found here into
// a single error.
for _, result := range resp.Results {
if result.Error != nil {
return nil, errors.New(*result.Error)
}
}
// Only use the last result.
if index := len(resp.Results); index != 0 {
return &resp.Results[index-1], nil
}
return nil, nil
}
// Execute all the URL settings against the db to create
// the correct session state.
func (c *conn) applySettings(params map[string]string) error {
var commands []string
for _, setting := range []struct {
name string
operator string
}{
{name: "DATABASE", operator: "="},
{name: "TIME ZONE", operator: ""},
} {
if val, ok := params[strings.ToLower(strings.Replace(setting.name, " ", "_", -1))]; ok {
commands = append(commands, fmt.Sprintf("SET %s %s \"%s\"", setting.name, setting.operator, val))
}
}
_, err := c.Exec(strings.Join(commands, ";"), nil)
return err
}