@@ -14,15 +14,16 @@ import (
14
14
)
15
15
16
16
const (
17
- version = "0.0.2 "
17
+ version = "0.1.0 "
18
18
)
19
19
20
20
// New creates a new DBHub.io connection object. It doesn't connect to DBHub.io to do this. Connection only occurs
21
21
// when subsequent functions (eg Query()) are called.
22
22
func New (key string ) (Connection , error ) {
23
23
c := Connection {
24
- APIKey : key ,
25
- Server : "https://api.dbhub.io" ,
24
+ APIKey : key ,
25
+ Server : "https://api.dbhub.io" ,
26
+ VerifyServerCert : true ,
26
27
}
27
28
return c , nil
28
29
}
@@ -37,6 +38,11 @@ func (c *Connection) ChangeServer(s string) {
37
38
c .Server = s
38
39
}
39
40
41
+ // ChangeVerifyServerCert changes whether to verify the server provided https certificate. Useful for testing and development.
42
+ func (c * Connection ) ChangeVerifyServerCert (b bool ) {
43
+ c .VerifyServerCert = b
44
+ }
45
+
40
46
// Branches returns a list of all available branches of a database along with the name of the default branch
41
47
func (c Connection ) Branches (dbOwner , dbName string ) (branches map [string ]com.BranchEntry , defaultBranch string , err error ) {
42
48
// Prepare the API parameters
@@ -45,7 +51,7 @@ func (c Connection) Branches(dbOwner, dbName string) (branches map[string]com.Br
45
51
// Fetch the list of branches and the default branch
46
52
var response com.BranchListResponseContainer
47
53
queryUrl := c .Server + "/v1/branches"
48
- err = sendRequestJSON (queryUrl , data , & response )
54
+ err = sendRequestJSON (queryUrl , c . VerifyServerCert , data , & response )
49
55
50
56
// Extract information for return values
51
57
branches = response .Branches
@@ -61,7 +67,7 @@ func (c Connection) Columns(dbOwner, dbName string, ident Identifier, table stri
61
67
62
68
// Fetch the list of columns
63
69
queryUrl := c .Server + "/v1/columns"
64
- err = sendRequestJSON (queryUrl , data , & columns )
70
+ err = sendRequestJSON (queryUrl , c . VerifyServerCert , data , & columns )
65
71
return
66
72
}
67
73
@@ -72,19 +78,32 @@ func (c Connection) Commits(dbOwner, dbName string) (commits map[string]com.Comm
72
78
73
79
// Fetch the commits
74
80
queryUrl := c .Server + "/v1/commits"
75
- err = sendRequestJSON (queryUrl , data , & commits )
81
+ err = sendRequestJSON (queryUrl , c . VerifyServerCert , data , & commits )
76
82
return
77
83
}
78
84
79
- // Databases returns the list of databases in your account
85
+ // Databases returns the list of standard databases in your account
80
86
func (c Connection ) Databases () (databases []string , err error ) {
81
87
// Prepare the API parameters
82
88
data := url.Values {}
83
89
data .Set ("apikey" , c .APIKey )
84
90
85
91
// Fetch the list of databases
86
92
queryUrl := c .Server + "/v1/databases"
87
- err = sendRequestJSON (queryUrl , data , & databases )
93
+ err = sendRequestJSON (queryUrl , c .VerifyServerCert , data , & databases )
94
+ return
95
+ }
96
+
97
+ // DatabasesLive returns the list of Live databases in your account
98
+ func (c Connection ) DatabasesLive () (databases []string , err error ) {
99
+ // Prepare the API parameters
100
+ data := url.Values {}
101
+ data .Set ("apikey" , c .APIKey )
102
+ data .Set ("live" , "true" )
103
+
104
+ // Fetch the list of databases
105
+ queryUrl := c .Server + "/v1/databases"
106
+ err = sendRequestJSON (queryUrl , c .VerifyServerCert , data , & databases )
88
107
return
89
108
}
90
109
@@ -95,7 +114,7 @@ func (c Connection) Delete(dbName string) (err error) {
95
114
96
115
// Delete the database
97
116
queryUrl := c .Server + "/v1/delete"
98
- err = sendRequestJSON (queryUrl , data , nil )
117
+ err = sendRequestJSON (queryUrl , c . VerifyServerCert , data , nil )
99
118
if err != nil && err .Error () == "no rows in result set" { // Feels like a dodgy workaround
100
119
err = fmt .Errorf ("Unknown database\n " )
101
120
}
@@ -146,7 +165,7 @@ func (c Connection) Diff(dbOwnerA, dbNameA string, identA Identifier, dbOwnerB,
146
165
147
166
// Fetch the diffs
148
167
queryUrl := c .Server + "/v1/diff"
149
- err = sendRequestJSON (queryUrl , data , & diffs )
168
+ err = sendRequestJSON (queryUrl , c . VerifyServerCert , data , & diffs )
150
169
return
151
170
}
152
171
@@ -157,21 +176,38 @@ func (c Connection) Download(dbOwner, dbName string, ident Identifier) (db io.Re
157
176
158
177
// Fetch the database file
159
178
queryUrl := c .Server + "/v1/download"
160
- db , err = sendRequest (queryUrl , data )
179
+ db , err = sendRequest (queryUrl , c . VerifyServerCert , data )
161
180
if err != nil {
162
181
return
163
182
}
164
183
return
165
184
}
166
185
186
+ // Execute executes a SQL statement (INSERT, UPDATE, DELETE) on the chosen database.
187
+ func (c Connection ) Execute (dbOwner , dbName string , sql string ) (rowsChanged int , err error ) {
188
+ // Prepare the API parameters
189
+ data := c .PrepareVals (dbOwner , dbName , Identifier {})
190
+ data .Set ("sql" , base64 .StdEncoding .EncodeToString ([]byte (sql )))
191
+
192
+ // Run the query on the remote database
193
+ var execResponse com.ExecuteResponseContainer
194
+ queryUrl := c .Server + "/v1/execute"
195
+ err = sendRequestJSON (queryUrl , c .VerifyServerCert , data , & execResponse )
196
+ if err != nil {
197
+ return
198
+ }
199
+ rowsChanged = execResponse .RowsChanged
200
+ return
201
+ }
202
+
167
203
// Indexes returns the list of indexes present in the database, along with the table they belong to
168
204
func (c Connection ) Indexes (dbOwner , dbName string , ident Identifier ) (idx []com.APIJSONIndex , err error ) {
169
205
// Prepare the API parameters
170
206
data := c .PrepareVals (dbOwner , dbName , ident )
171
207
172
208
// Fetch the list of indexes
173
209
queryUrl := c .Server + "/v1/indexes"
174
- err = sendRequestJSON (queryUrl , data , & idx )
210
+ err = sendRequestJSON (queryUrl , c . VerifyServerCert , data , & idx )
175
211
return
176
212
}
177
213
@@ -182,11 +218,11 @@ func (c Connection) Metadata(dbOwner, dbName string) (meta com.MetadataResponseC
182
218
183
219
// Fetch the list of databases
184
220
queryUrl := c .Server + "/v1/metadata"
185
- err = sendRequestJSON (queryUrl , data , & meta )
221
+ err = sendRequestJSON (queryUrl , c . VerifyServerCert , data , & meta )
186
222
return
187
223
}
188
224
189
- // PrepareVals creates a url.Values container holding the API key, database owner, name, and database identifier. The
225
+ // PrepareVals creates an url.Values container holding the API key, database owner, name, and database identifier. The
190
226
// url.Values container is then used for the requests to DBHub.io.
191
227
func (c Connection ) PrepareVals (dbOwner , dbName string , ident Identifier ) (data url.Values ) {
192
228
// Prepare the API parameters
@@ -226,7 +262,7 @@ func (c Connection) Query(dbOwner, dbName string, ident Identifier, blobBase64 b
226
262
// Run the query on the remote database
227
263
var returnedData []com.DataRow
228
264
queryUrl := c .Server + "/v1/query"
229
- err = sendRequestJSON (queryUrl , data , & returnedData )
265
+ err = sendRequestJSON (queryUrl , c . VerifyServerCert , data , & returnedData )
230
266
if err != nil {
231
267
return
232
268
}
@@ -272,7 +308,7 @@ func (c Connection) Releases(dbOwner, dbName string) (releases map[string]com.Re
272
308
273
309
// Fetch the releases
274
310
queryUrl := c .Server + "/v1/releases"
275
- err = sendRequestJSON (queryUrl , data , & releases )
311
+ err = sendRequestJSON (queryUrl , c . VerifyServerCert , data , & releases )
276
312
return
277
313
}
278
314
@@ -283,7 +319,7 @@ func (c Connection) Tables(dbOwner, dbName string, ident Identifier) (tbl []stri
283
319
284
320
// Fetch the list of tables
285
321
queryUrl := c .Server + "/v1/tables"
286
- err = sendRequestJSON (queryUrl , data , & tbl )
322
+ err = sendRequestJSON (queryUrl , c . VerifyServerCert , data , & tbl )
287
323
return
288
324
}
289
325
@@ -294,7 +330,7 @@ func (c Connection) Tags(dbOwner, dbName string) (tags map[string]com.TagEntry,
294
330
295
331
// Fetch the tags
296
332
queryUrl := c .Server + "/v1/tags"
297
- err = sendRequestJSON (queryUrl , data , & tags )
333
+ err = sendRequestJSON (queryUrl , c . VerifyServerCert , data , & tags )
298
334
return
299
335
}
300
336
@@ -305,11 +341,11 @@ func (c Connection) Views(dbOwner, dbName string, ident Identifier) (views []str
305
341
306
342
// Fetch the list of views
307
343
queryUrl := c .Server + "/v1/views"
308
- err = sendRequestJSON (queryUrl , data , & views )
344
+ err = sendRequestJSON (queryUrl , c . VerifyServerCert , data , & views )
309
345
return
310
346
}
311
347
312
- // Upload uploads a new database, or a new revision of a database
348
+ // Upload uploads a new standard database, or a new revision of a database
313
349
func (c Connection ) Upload (dbName string , info UploadInformation , dbBytes * []byte ) (err error ) {
314
350
// Prepare the API parameters
315
351
data := c .PrepareVals ("" , dbName , info .Ident )
@@ -357,7 +393,35 @@ func (c Connection) Upload(dbName string, info UploadInformation, dbBytes *[]byt
357
393
// Upload the database
358
394
var body io.ReadCloser
359
395
queryUrl := c .Server + "/v1/upload"
360
- body , err = sendUpload (queryUrl , & data , dbBytes )
396
+ body , err = sendUpload (queryUrl , c .VerifyServerCert , & data , dbBytes )
397
+ if body != nil {
398
+ defer body .Close ()
399
+ }
400
+ if err != nil {
401
+ if body != nil {
402
+ // If there's useful error info in the returned JSON, return that as the error message
403
+ var z JSONError
404
+ err = json .NewDecoder (body ).Decode (& z )
405
+ if err != nil {
406
+ return
407
+ }
408
+ err = fmt .Errorf ("%s" , z .Msg )
409
+ }
410
+ }
411
+ return
412
+ }
413
+
414
+ // UploadLive uploads a new Live database
415
+ func (c Connection ) UploadLive (dbName string , dbBytes * []byte ) (err error ) {
416
+ // Prepare the API parameters
417
+ data := c .PrepareVals ("" , dbName , Identifier {})
418
+ data .Del ("dbowner" ) // The upload function always stores the database in the account of the API key user
419
+ data .Set ("live" , "true" )
420
+
421
+ // Upload the database
422
+ var body io.ReadCloser
423
+ queryUrl := c .Server + "/v1/upload"
424
+ body , err = sendUpload (queryUrl , c .VerifyServerCert , & data , dbBytes )
361
425
if body != nil {
362
426
defer body .Close ()
363
427
}
@@ -382,6 +446,6 @@ func (c Connection) Webpage(dbOwner, dbName string) (webPage com.WebpageResponse
382
446
383
447
// Fetch the releases
384
448
queryUrl := c .Server + "/v1/webpage"
385
- err = sendRequestJSON (queryUrl , data , & webPage )
449
+ err = sendRequestJSON (queryUrl , c . VerifyServerCert , data , & webPage )
386
450
return
387
451
}
0 commit comments