Skip to content

Commit c9d72b1

Browse files
committed
Added initial support for Live DBHub.io databases
1 parent d370c48 commit c9d72b1

File tree

813 files changed

+214
-324174
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

813 files changed

+214
-324174
lines changed

dbhub.go

Lines changed: 86 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ import (
1414
)
1515

1616
const (
17-
version = "0.0.2"
17+
version = "0.1.0"
1818
)
1919

2020
// New creates a new DBHub.io connection object. It doesn't connect to DBHub.io to do this. Connection only occurs
2121
// when subsequent functions (eg Query()) are called.
2222
func New(key string) (Connection, error) {
2323
c := Connection{
24-
APIKey: key,
25-
Server: "https://api.dbhub.io",
24+
APIKey: key,
25+
Server: "https://api.dbhub.io",
26+
VerifyServerCert: true,
2627
}
2728
return c, nil
2829
}
@@ -37,6 +38,11 @@ func (c *Connection) ChangeServer(s string) {
3738
c.Server = s
3839
}
3940

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+
4046
// Branches returns a list of all available branches of a database along with the name of the default branch
4147
func (c Connection) Branches(dbOwner, dbName string) (branches map[string]com.BranchEntry, defaultBranch string, err error) {
4248
// Prepare the API parameters
@@ -45,7 +51,7 @@ func (c Connection) Branches(dbOwner, dbName string) (branches map[string]com.Br
4551
// Fetch the list of branches and the default branch
4652
var response com.BranchListResponseContainer
4753
queryUrl := c.Server + "/v1/branches"
48-
err = sendRequestJSON(queryUrl, data, &response)
54+
err = sendRequestJSON(queryUrl, c.VerifyServerCert, data, &response)
4955

5056
// Extract information for return values
5157
branches = response.Branches
@@ -61,7 +67,7 @@ func (c Connection) Columns(dbOwner, dbName string, ident Identifier, table stri
6167

6268
// Fetch the list of columns
6369
queryUrl := c.Server + "/v1/columns"
64-
err = sendRequestJSON(queryUrl, data, &columns)
70+
err = sendRequestJSON(queryUrl, c.VerifyServerCert, data, &columns)
6571
return
6672
}
6773

@@ -72,19 +78,32 @@ func (c Connection) Commits(dbOwner, dbName string) (commits map[string]com.Comm
7278

7379
// Fetch the commits
7480
queryUrl := c.Server + "/v1/commits"
75-
err = sendRequestJSON(queryUrl, data, &commits)
81+
err = sendRequestJSON(queryUrl, c.VerifyServerCert, data, &commits)
7682
return
7783
}
7884

79-
// Databases returns the list of databases in your account
85+
// Databases returns the list of standard databases in your account
8086
func (c Connection) Databases() (databases []string, err error) {
8187
// Prepare the API parameters
8288
data := url.Values{}
8389
data.Set("apikey", c.APIKey)
8490

8591
// Fetch the list of databases
8692
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)
88107
return
89108
}
90109

@@ -95,7 +114,7 @@ func (c Connection) Delete(dbName string) (err error) {
95114

96115
// Delete the database
97116
queryUrl := c.Server + "/v1/delete"
98-
err = sendRequestJSON(queryUrl, data, nil)
117+
err = sendRequestJSON(queryUrl, c.VerifyServerCert, data, nil)
99118
if err != nil && err.Error() == "no rows in result set" { // Feels like a dodgy workaround
100119
err = fmt.Errorf("Unknown database\n")
101120
}
@@ -146,7 +165,7 @@ func (c Connection) Diff(dbOwnerA, dbNameA string, identA Identifier, dbOwnerB,
146165

147166
// Fetch the diffs
148167
queryUrl := c.Server + "/v1/diff"
149-
err = sendRequestJSON(queryUrl, data, &diffs)
168+
err = sendRequestJSON(queryUrl, c.VerifyServerCert, data, &diffs)
150169
return
151170
}
152171

@@ -157,21 +176,38 @@ func (c Connection) Download(dbOwner, dbName string, ident Identifier) (db io.Re
157176

158177
// Fetch the database file
159178
queryUrl := c.Server + "/v1/download"
160-
db, err = sendRequest(queryUrl, data)
179+
db, err = sendRequest(queryUrl, c.VerifyServerCert, data)
161180
if err != nil {
162181
return
163182
}
164183
return
165184
}
166185

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+
167203
// Indexes returns the list of indexes present in the database, along with the table they belong to
168204
func (c Connection) Indexes(dbOwner, dbName string, ident Identifier) (idx []com.APIJSONIndex, err error) {
169205
// Prepare the API parameters
170206
data := c.PrepareVals(dbOwner, dbName, ident)
171207

172208
// Fetch the list of indexes
173209
queryUrl := c.Server + "/v1/indexes"
174-
err = sendRequestJSON(queryUrl, data, &idx)
210+
err = sendRequestJSON(queryUrl, c.VerifyServerCert, data, &idx)
175211
return
176212
}
177213

@@ -182,11 +218,11 @@ func (c Connection) Metadata(dbOwner, dbName string) (meta com.MetadataResponseC
182218

183219
// Fetch the list of databases
184220
queryUrl := c.Server + "/v1/metadata"
185-
err = sendRequestJSON(queryUrl, data, &meta)
221+
err = sendRequestJSON(queryUrl, c.VerifyServerCert, data, &meta)
186222
return
187223
}
188224

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
190226
// url.Values container is then used for the requests to DBHub.io.
191227
func (c Connection) PrepareVals(dbOwner, dbName string, ident Identifier) (data url.Values) {
192228
// Prepare the API parameters
@@ -226,7 +262,7 @@ func (c Connection) Query(dbOwner, dbName string, ident Identifier, blobBase64 b
226262
// Run the query on the remote database
227263
var returnedData []com.DataRow
228264
queryUrl := c.Server + "/v1/query"
229-
err = sendRequestJSON(queryUrl, data, &returnedData)
265+
err = sendRequestJSON(queryUrl, c.VerifyServerCert, data, &returnedData)
230266
if err != nil {
231267
return
232268
}
@@ -272,7 +308,7 @@ func (c Connection) Releases(dbOwner, dbName string) (releases map[string]com.Re
272308

273309
// Fetch the releases
274310
queryUrl := c.Server + "/v1/releases"
275-
err = sendRequestJSON(queryUrl, data, &releases)
311+
err = sendRequestJSON(queryUrl, c.VerifyServerCert, data, &releases)
276312
return
277313
}
278314

@@ -283,7 +319,7 @@ func (c Connection) Tables(dbOwner, dbName string, ident Identifier) (tbl []stri
283319

284320
// Fetch the list of tables
285321
queryUrl := c.Server + "/v1/tables"
286-
err = sendRequestJSON(queryUrl, data, &tbl)
322+
err = sendRequestJSON(queryUrl, c.VerifyServerCert, data, &tbl)
287323
return
288324
}
289325

@@ -294,7 +330,7 @@ func (c Connection) Tags(dbOwner, dbName string) (tags map[string]com.TagEntry,
294330

295331
// Fetch the tags
296332
queryUrl := c.Server + "/v1/tags"
297-
err = sendRequestJSON(queryUrl, data, &tags)
333+
err = sendRequestJSON(queryUrl, c.VerifyServerCert, data, &tags)
298334
return
299335
}
300336

@@ -305,11 +341,11 @@ func (c Connection) Views(dbOwner, dbName string, ident Identifier) (views []str
305341

306342
// Fetch the list of views
307343
queryUrl := c.Server + "/v1/views"
308-
err = sendRequestJSON(queryUrl, data, &views)
344+
err = sendRequestJSON(queryUrl, c.VerifyServerCert, data, &views)
309345
return
310346
}
311347

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
313349
func (c Connection) Upload(dbName string, info UploadInformation, dbBytes *[]byte) (err error) {
314350
// Prepare the API parameters
315351
data := c.PrepareVals("", dbName, info.Ident)
@@ -357,7 +393,35 @@ func (c Connection) Upload(dbName string, info UploadInformation, dbBytes *[]byt
357393
// Upload the database
358394
var body io.ReadCloser
359395
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)
361425
if body != nil {
362426
defer body.Close()
363427
}
@@ -382,6 +446,6 @@ func (c Connection) Webpage(dbOwner, dbName string) (webPage com.WebpageResponse
382446

383447
// Fetch the releases
384448
queryUrl := c.Server + "/v1/webpage"
385-
err = sendRequestJSON(queryUrl, data, &webPage)
449+
err = sendRequestJSON(queryUrl, c.VerifyServerCert, data, &webPage)
386450
return
387451
}

go.mod

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/sqlitebrowser/go-dbhub
22

3-
go 1.14
3+
go 1.17
44

55
replace (
66
github.com/Sirupsen/logrus v1.0.5 => github.com/sirupsen/logrus v1.0.5
@@ -10,4 +10,43 @@ replace (
1010
github.com/Sirupsen/logrus v1.6.0 => github.com/sirupsen/logrus v1.6.0
1111
)
1212

13-
require github.com/sqlitebrowser/dbhub.io v0.0.14
13+
require github.com/sqlitebrowser/dbhub.io v0.1.1
14+
15+
require (
16+
github.com/BurntSushi/toml v0.3.1 // indirect
17+
github.com/aquilax/truncate v1.0.0 // indirect
18+
github.com/aymerick/douceur v0.2.0 // indirect
19+
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b // indirect
20+
github.com/go-ini/ini v1.56.0 // indirect
21+
github.com/go-playground/locales v0.13.0 // indirect
22+
github.com/go-playground/universal-translator v0.17.0 // indirect
23+
github.com/go-playground/validator/v10 v10.3.0 // indirect
24+
github.com/gopherjs/gopherjs v1.17.2 // indirect
25+
github.com/gorilla/css v1.0.0 // indirect
26+
github.com/gwenn/gosqlite v0.0.0-20200521090053-24878be1a237 // indirect
27+
github.com/gwenn/yacr v0.0.0-20200112083327-bbe82c1f4d60 // indirect
28+
github.com/jackc/pgx v2.11.0+incompatible // indirect
29+
github.com/jtolds/gls v4.20.0+incompatible // indirect
30+
github.com/leodido/go-urn v1.2.0 // indirect
31+
github.com/microcosm-cc/bluemonday v1.0.16 // indirect
32+
github.com/minio/minio-go v6.0.14+incompatible // indirect
33+
github.com/mitchellh/go-homedir v1.1.0 // indirect
34+
github.com/rabbitmq/amqp091-go v1.7.0 // indirect
35+
github.com/segmentio/ksuid v1.0.3 // indirect
36+
github.com/sergi/go-diff v1.1.0 // indirect
37+
github.com/shurcooL/go v0.0.0-20200502201357-93f07166e636 // indirect
38+
github.com/shurcooL/highlight_diff v0.0.0-20181222201841-111da2e7d480 // indirect
39+
github.com/shurcooL/highlight_go v0.0.0-20191220051317-782971ddf21b // indirect
40+
github.com/shurcooL/octicon v0.0.0-20191102190552-cbb32d6a785c // indirect
41+
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
42+
github.com/smartystreets/assertions v1.13.0 // indirect
43+
github.com/smtp2go-oss/smtp2go-go v1.0.2 // indirect
44+
github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d // indirect
45+
github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e // indirect
46+
github.com/sqlitebrowser/blackfriday v9.0.0+incompatible // indirect
47+
github.com/sqlitebrowser/github_flavored_markdown v0.0.0-20190120045821-b8cf8f054e47 // indirect
48+
golang.org/x/crypto v0.0.0-20220314234659-1baeb1ce4c0b // indirect
49+
golang.org/x/net v0.7.0 // indirect
50+
golang.org/x/sys v0.5.0 // indirect
51+
golang.org/x/text v0.8.0 // indirect
52+
)

0 commit comments

Comments
 (0)