-
Notifications
You must be signed in to change notification settings - Fork 7
/
stmt_database.go
307 lines (269 loc) · 8.38 KB
/
stmt_database.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package gocosmos
import (
"context"
"database/sql/driver"
"errors"
"fmt"
"strconv"
)
// StmtCreateDatabase implements "CREATE DATABASE" statement.
//
// Syntax:
//
// CREATE DATABASE [IF NOT EXISTS] <db-name> [WITH RU|MAXRU=ru]
//
// - ru: an integer specifying CosmosDB's database throughput expressed in RU/s. Supply either RU or MAXRU, not both!
//
// - If "IF NOT EXISTS" is specified, Exec will silently swallow the error "409 Conflict".
type StmtCreateDatabase struct {
*Stmt
dbName string
ifNotExists bool
ru, maxru int
}
// String implements fmt.Stringer/String.
//
// @Available since v1.1.0
func (s *StmtCreateDatabase) String() string {
return fmt.Sprintf(`StmtCreateDatabase{Stmt: %s, db: %q, if_not_exists: %t, ru: %d, maxru: %d}`,
s.Stmt, s.dbName, s.ifNotExists, s.ru, s.maxru)
}
func (s *StmtCreateDatabase) parse(withOptsStr string) error {
if err := s.Stmt.parseWithOpts(withOptsStr); err != nil {
return err
}
for k, v := range s.withOpts {
switch k {
case "RU":
ru, err := strconv.ParseInt(v, 10, 32)
if err != nil || ru < 0 {
return fmt.Errorf("invalid RU value: %s", v)
}
s.ru = int(ru)
case "MAXRU":
maxru, err := strconv.ParseInt(v, 10, 32)
if err != nil || maxru < 0 {
return fmt.Errorf("invalid RU value: %s", v)
}
s.maxru = int(maxru)
default:
return fmt.Errorf("invalid query, parsing error at WITH %s=%s", k, v)
}
}
return nil
}
func (s *StmtCreateDatabase) validate() error {
if s.ru > 0 && s.maxru > 0 {
return errors.New("only one of RU or MAXRU should be specified")
}
return nil
}
// Query implements driver.Stmt/Query.
// This function is not implemented, use Exec instead.
func (s *StmtCreateDatabase) Query(_ []driver.Value) (driver.Rows, error) {
return nil, ErrQueryNotSupported
}
// Exec implements driver.Stmt/Exec.
func (s *StmtCreateDatabase) Exec(args []driver.Value) (driver.Result, error) {
return s.ExecContext(context.Background(), _valuesToNamedValues(args))
}
// ExecContext implements driver.StmtExecContext/ExecContext.
//
// @Available since v1.1.0
func (s *StmtCreateDatabase) ExecContext(_ context.Context, args []driver.NamedValue) (driver.Result, error) {
if len(args) != 0 {
return nil, fmt.Errorf("expected 0 input value, got %d", len(args))
}
// TODO: pass ctx to REST API client
restResult := s.conn.restClient.CreateDatabase(DatabaseSpec{Id: s.dbName, Ru: s.ru, MaxRu: s.maxru})
ignoreErrorCode := 0
if s.ifNotExists {
ignoreErrorCode = 409
}
result := buildResultNoResultSet(&restResult.RestResponse, true, restResult.Rid, ignoreErrorCode)
return result, result.err
}
/*----------------------------------------------------------------------*/
// StmtAlterDatabase implements "ALTER DATABASE" statement.
//
// Syntax:
//
// ALTER DATABASE <db-name> WITH RU|MAXRU=<ru>
//
// - ru: an integer specifying CosmosDB's database throughput expressed in RU/s. Supply either RU or MAXRU, not both!
//
// Available since v0.1.1
type StmtAlterDatabase struct {
*Stmt
dbName string
ru, maxru int
}
// String implements fmt.Stringer/String.
//
// @Available since v1.1.0
func (s *StmtAlterDatabase) String() string {
return fmt.Sprintf(`StmtAlterDatabase{Stmt: %s, db: %q, ru: %d, maxru: %d}`,
s.Stmt, s.dbName, s.ru, s.maxru)
}
func (s *StmtAlterDatabase) parse(withOptsStr string) error {
if err := s.Stmt.parseWithOpts(withOptsStr); err != nil {
return err
}
for k, v := range s.withOpts {
switch k {
case "RU":
ru, err := strconv.ParseInt(v, 10, 32)
if err != nil || ru < 0 {
return fmt.Errorf("invalid RU value: %s", v)
}
s.ru = int(ru)
case "MAXRU":
maxru, err := strconv.ParseInt(v, 10, 32)
if err != nil || maxru < 0 {
return fmt.Errorf("invalid RU value: %s", v)
}
s.maxru = int(maxru)
default:
return fmt.Errorf("invalid query, parsing error at WITH %s=%s", k, v)
}
}
return nil
}
func (s *StmtAlterDatabase) validate() error {
if (s.ru <= 0 && s.maxru <= 0) || (s.ru > 0 && s.maxru > 0) {
return errors.New("only one of RU or MAXRU must be specified")
}
return nil
}
// Query implements driver.Stmt/Query.
// This function is not implemented, use Exec instead.
func (s *StmtAlterDatabase) Query(_ []driver.Value) (driver.Rows, error) {
return nil, ErrQueryNotSupported
}
// Exec implements driver.Stmt/Exec.
func (s *StmtAlterDatabase) Exec(args []driver.Value) (driver.Result, error) {
return s.ExecContext(context.Background(), _valuesToNamedValues(args))
}
// ExecContext implements driver.StmtExecContext/ExecContext.
//
// @Available since v1.1.1
func (s *StmtAlterDatabase) ExecContext(_ context.Context, args []driver.NamedValue) (driver.Result, error) {
if len(args) != 0 {
return nil, fmt.Errorf("expected 0 input value, got %d", len(args))
}
// TODO: pass ctx to REST API client
getResult := s.conn.restClient.GetDatabase(s.dbName)
if err := getResult.Error(); err != nil {
switch getResult.StatusCode {
case 403:
return nil, ErrForbidden
case 404:
return nil, ErrNotFound
}
return nil, err
}
restResult := s.conn.restClient.ReplaceOfferForResource(getResult.Rid, s.ru, s.maxru)
result := buildResultNoResultSet(&restResult.RestResponse, true, restResult.Rid, 0)
return result, result.err
}
/*----------------------------------------------------------------------*/
// StmtDropDatabase implements "DROP DATABASE" statement.
//
// Syntax:
//
// DROP DATABASE [IF EXISTS] <db-name>
//
// - If "IF EXISTS" is specified, Exec will silently swallow the error "404 Not Found".
type StmtDropDatabase struct {
*Stmt
dbName string
ifExists bool
}
// String implements fmt.Stringer/String.
//
// @Available since v1.1.0
func (s *StmtDropDatabase) String() string {
return fmt.Sprintf(`StmtDropDatabase{Stmt: %s, db: %q, if_exists: %t}`,
s.Stmt, s.dbName, s.ifExists)
}
func (s *StmtDropDatabase) validate() error {
return nil
}
// Query implements driver.Stmt/Query.
// This function is not implemented, use Exec instead.
func (s *StmtDropDatabase) Query(_ []driver.Value) (driver.Rows, error) {
return nil, ErrQueryNotSupported
}
// Exec implements driver.Stmt/Exec.
func (s *StmtDropDatabase) Exec(args []driver.Value) (driver.Result, error) {
return s.ExecContext(context.Background(), _valuesToNamedValues(args))
}
// ExecContext implements driver.StmtExecContext/ExecContext.
//
// @Available since v1.1.0
func (s *StmtDropDatabase) ExecContext(_ context.Context, args []driver.NamedValue) (driver.Result, error) {
if len(args) != 0 {
return nil, fmt.Errorf("expected 0 input value, got %d", len(args))
}
// TODO: pass ctx to REST API client
restResult := s.conn.restClient.DeleteDatabase(s.dbName)
ignoreErrorCode := 0
if s.ifExists {
ignoreErrorCode = 404
}
result := buildResultNoResultSet(&restResult.RestResponse, false, "", ignoreErrorCode)
return result, result.err
}
/*----------------------------------------------------------------------*/
// StmtListDatabases implements "LIST DATABASES" statement.
//
// Syntax:
//
// LIST DATABASES|DATABASE
type StmtListDatabases struct {
*Stmt
}
// String implements fmt.Stringer/String.
//
// @Available since v1.1.0
func (s *StmtListDatabases) String() string {
return fmt.Sprintf(`StmtListDatabases{Stmt: %s}`, s.Stmt)
}
func (s *StmtListDatabases) validate() error {
return nil
}
// Exec implements driver.Stmt/Exec.
// This function is not implemented, use Query instead.
func (s *StmtListDatabases) Exec(_ []driver.Value) (driver.Result, error) {
return nil, ErrExecNotSupported
}
// Query implements driver.Stmt/Query.
func (s *StmtListDatabases) Query(args []driver.Value) (driver.Rows, error) {
return s.QueryContext(context.Background(), _valuesToNamedValues(args))
}
// QueryContext implements driver.StmtQueryContext/QueryContext.
//
// @Available since v1.1.0
func (s *StmtListDatabases) QueryContext(_ context.Context, args []driver.NamedValue) (driver.Rows, error) {
if len(args) != 0 {
return nil, fmt.Errorf("expected 0 input value, got %d", len(args))
}
// TODO: pass ctx to REST API client
restResult := s.conn.restClient.ListDatabases()
result := &ResultResultSet{
err: restResult.Error(),
columnList: []string{"id", "_rid", "_ts", "_self", "_etag", "_colls", "_users"},
}
if result.err == nil {
result.count = len(restResult.Databases)
result.rows = make([]DocInfo, result.count)
for i, db := range restResult.Databases {
result.rows[i] = db.toMap()
}
}
switch restResult.StatusCode {
case 403:
result.err = ErrForbidden
}
return result, result.err
}