Skip to content

Commit 4b94c40

Browse files
committed
Each function now accepts a branch, commit, release, and/or tag
1 parent 308ec74 commit 4b94c40

File tree

3 files changed

+69
-32
lines changed

3 files changed

+69
-32
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ A Go library for accessing and using SQLite databases stored remotely on DBHub.i
1313

1414
### Still to do
1515

16-
* Add commit identifier parameters (commit id, branch, tag) to each call
1716
* Tests for each function
1817
* Retrieve index details for a database
1918
* Return the list of available databases

dbhub.go

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
const (
14-
version = "0.0.1"
14+
version = "0.0.2"
1515
)
1616

1717
// New creates a new DBHub.io connection object. It doesn't connect to DBHub.io to do this. Connection only occurs
@@ -35,12 +35,9 @@ func (c *Connection) ChangeServer(s string) {
3535
}
3636

3737
// Columns returns the column information for a given table or view
38-
func (c Connection) Columns(dbowner, dbname, table string) (columns []com.APIJSONColumn, err error) {
38+
func (c Connection) Columns(dbOwner, dbName string, ident Identifier, table string) (columns []com.APIJSONColumn, err error) {
3939
// Prepare the API parameters
40-
data := url.Values{}
41-
data.Set("apikey", c.APIKey)
42-
data.Set("dbowner", dbowner)
43-
data.Set("dbname", dbname)
40+
data := c.PrepareVals(dbOwner, dbName, ident)
4441
data.Set("table", table)
4542

4643
// Fetch the list of columns
@@ -51,16 +48,38 @@ func (c Connection) Columns(dbowner, dbname, table string) (columns []com.APIJSO
5148

5249
// Diff returns the differences between two commits of two databases, or if the details on the second database are left empty,
5350
// between two commits of the same database. You can also specify the merge strategy used for the generated SQL statements.
54-
func (c Connection) Diff(dbOwnerA string, dbNameA string, commitA string, dbOwnerB string, dbNameB string, commitB string, merge MergeStrategy) (diffs com.Diffs, err error) {
51+
func (c Connection) Diff(dbOwnerA, dbNameA string, identA Identifier, dbOwnerB, dbNameB string, identB Identifier, merge MergeStrategy) (diffs com.Diffs, err error) {
5552
// Prepare the API parameters
5653
data := url.Values{}
5754
data.Set("apikey", c.APIKey)
5855
data.Set("dbowner_a", dbOwnerA)
5956
data.Set("dbname_a", dbNameA)
60-
data.Set("commit_a", commitA)
57+
if identA.Branch != "" {
58+
data.Set("branch_a", identA.Branch)
59+
}
60+
if identA.CommitID != "" {
61+
data.Set("commit_a", identA.CommitID)
62+
}
63+
if identA.Release != "" {
64+
data.Set("release_a", identA.Release)
65+
}
66+
if identA.Tag != "" {
67+
data.Set("tag_a", identA.Tag)
68+
}
6169
data.Set("dbowner_b", dbOwnerB)
6270
data.Set("dbname_b", dbNameB)
63-
data.Set("commit_b", commitB)
71+
if identB.Branch != "" {
72+
data.Set("branch_b", identB.Branch)
73+
}
74+
if identB.CommitID != "" {
75+
data.Set("commit_b", identB.CommitID)
76+
}
77+
if identB.Release != "" {
78+
data.Set("release_b", identB.Release)
79+
}
80+
if identB.Tag != "" {
81+
data.Set("tag_b", identB.Tag)
82+
}
6483
if merge == PreservePkMerge {
6584
data.Set("merge", "preserve_pk")
6685
} else if merge == NewPkMerge {
@@ -76,28 +95,45 @@ func (c Connection) Diff(dbOwnerA string, dbNameA string, commitA string, dbOwne
7695
}
7796

7897
// Indexes returns the list of indexes present in the database, along with the table they belong to
79-
func (c Connection) Indexes(dbowner, dbname string) (idx map[string]string, err error) {
98+
func (c Connection) Indexes(dbOwner, dbName string, ident Identifier) (idx map[string]string, err error) {
8099
// Prepare the API parameters
81-
data := url.Values{}
82-
data.Set("apikey", c.APIKey)
83-
data.Set("dbowner", dbowner)
84-
data.Set("dbname", dbname)
100+
data := c.PrepareVals(dbOwner, dbName, ident)
85101

86102
// Fetch the list of indexes
87103
queryUrl := c.Server + "/v1/indexes"
88104
err = sendRequest(queryUrl, data, &idx)
89105
return
90106
}
91107

108+
// PrepareVals creates a url.Values container holding the API key, database owner, name, and database identifier. The
109+
// url.Values container is then used for the requests to DBHub.io.
110+
func (c Connection) PrepareVals(dbOwner, dbName string, ident Identifier) (data url.Values) {
111+
// Prepare the API parameters
112+
data = url.Values{}
113+
data.Set("apikey", c.APIKey)
114+
data.Set("dbowner", dbOwner)
115+
data.Set("dbname", dbName)
116+
if ident.Branch != "" {
117+
data.Set("branch", ident.Branch)
118+
}
119+
if ident.CommitID != "" {
120+
data.Set("commit", ident.CommitID)
121+
}
122+
if ident.Release != "" {
123+
data.Set("release", ident.Release)
124+
}
125+
if ident.Tag != "" {
126+
data.Set("tag", ident.Tag)
127+
}
128+
return
129+
}
130+
92131
// Query runs a SQL query (SELECT only) on the chosen database, returning the results.
93132
// The "blobBase64" boolean specifies whether BLOB data fields should be base64 encoded in the output, or just skipped
94133
// using an empty string as a placeholder.
95-
func (c Connection) Query(dbowner, dbname string, blobBase64 bool, sql string) (out Results, err error) {
134+
func (c Connection) Query(dbOwner, dbName string, ident Identifier, blobBase64 bool, sql string) (out Results, err error) {
96135
// Prepare the API parameters
97-
data := url.Values{}
98-
data.Set("apikey", c.APIKey)
99-
data.Set("dbowner", dbowner)
100-
data.Set("dbname", dbname)
136+
data := c.PrepareVals(dbOwner, dbName, ident)
101137
data.Set("sql", base64.StdEncoding.EncodeToString([]byte(sql)))
102138

103139
// Run the query on the remote database
@@ -122,8 +158,8 @@ func (c Connection) Query(dbowner, dbname string, blobBase64 bool, sql string) (
122158
// BLOB data is optionally Base64 encoded, or just skipped (using an empty string as placeholder)
123159
if blobBase64 {
124160
// Safety check. Make sure we've received a string
125-
if _, ok := l.Value.(string); ok {
126-
oneRow.Fields = append(oneRow.Fields, base64.StdEncoding.EncodeToString([]byte(l.Value.(string))))
161+
if s, ok := l.Value.(string); ok {
162+
oneRow.Fields = append(oneRow.Fields, base64.StdEncoding.EncodeToString([]byte(s)))
127163
} else {
128164
oneRow.Fields = append(oneRow.Fields, fmt.Sprintf("unexpected data type '%T' for returned BLOB", l.Value))
129165
}
@@ -143,12 +179,9 @@ func (c Connection) Query(dbowner, dbname string, blobBase64 bool, sql string) (
143179
}
144180

145181
// Tables returns the list of tables in the database
146-
func (c Connection) Tables(dbowner, dbname string) (tbl []string, err error) {
182+
func (c Connection) Tables(dbOwner, dbName string, ident Identifier) (tbl []string, err error) {
147183
// Prepare the API parameters
148-
data := url.Values{}
149-
data.Set("apikey", c.APIKey)
150-
data.Set("dbowner", dbowner)
151-
data.Set("dbname", dbname)
184+
data := c.PrepareVals(dbOwner, dbName, ident)
152185

153186
// Fetch the list of tables
154187
queryUrl := c.Server + "/v1/tables"
@@ -157,12 +190,9 @@ func (c Connection) Tables(dbowner, dbname string) (tbl []string, err error) {
157190
}
158191

159192
// Views returns the list of views in the database
160-
func (c Connection) Views(dbowner, dbname string) (views []string, err error) {
193+
func (c Connection) Views(dbOwner, dbName string, ident Identifier) (views []string, err error) {
161194
// Prepare the API parameters
162-
data := url.Values{}
163-
data.Set("apikey", c.APIKey)
164-
data.Set("dbowner", dbowner)
165-
data.Set("dbname", dbname)
195+
data := c.PrepareVals(dbOwner, dbName, ident)
166196

167197
// Fetch the list of views
168198
queryUrl := c.Server + "/v1/views"

types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ type Connection struct {
66
Server string `json:"server"`
77
}
88

9+
// Identifier holds information used to identify a specific commit, tag, release, or the head of a specific branch
10+
type Identifier struct {
11+
Branch string `json:"branch"`
12+
CommitID string `json:"commit_id"`
13+
Release string `json:"release"`
14+
Tag string `json:"tag"`
15+
}
16+
917
// ResultRow is used for returning the results of a SQL query as a slice of strings
1018
type ResultRow struct {
1119
Fields []string

0 commit comments

Comments
 (0)