@@ -14,15 +14,16 @@ import (
1414)
1515
1616const (
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.
2222func 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
4147func (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
8086func (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
168204func (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.
191227func (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
313349func (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}
0 commit comments