Skip to content

Commit 43be0da

Browse files
committed
Remove common dependency from library code
This removes the dependency from the dbhub.io/common module from the library code by copying the required data types into the library. This might look like a lot of code duplication, however it makes the two projects more independent by not using data structures some of which are solely for internal use and depending on implementation details on the server side. Another benefit is that users of the library do not have to import the entire dbhub.io project including all of its dependencies into their project.
1 parent 238e706 commit 43be0da

File tree

2 files changed

+173
-16
lines changed

2 files changed

+173
-16
lines changed

dbhub.go

+14-16
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"io"
1010
"net/url"
1111
"time"
12-
13-
com "github.com/sqlitebrowser/dbhub.io/common"
1412
)
1513

1614
const (
@@ -44,12 +42,12 @@ func (c *Connection) ChangeVerifyServerCert(b bool) {
4442
}
4543

4644
// Branches returns a list of all available branches of a database along with the name of the default branch
47-
func (c Connection) Branches(dbOwner, dbName string) (branches map[string]com.BranchEntry, defaultBranch string, err error) {
45+
func (c Connection) Branches(dbOwner, dbName string) (branches map[string]BranchEntry, defaultBranch string, err error) {
4846
// Prepare the API parameters
4947
data := c.PrepareVals(dbOwner, dbName, Identifier{})
5048

5149
// Fetch the list of branches and the default branch
52-
var response com.BranchListResponseContainer
50+
var response BranchListResponseContainer
5351
queryUrl := c.Server + "/v1/branches"
5452
err = sendRequestJSON(queryUrl, c.VerifyServerCert, data, &response)
5553

@@ -60,7 +58,7 @@ func (c Connection) Branches(dbOwner, dbName string) (branches map[string]com.Br
6058
}
6159

6260
// Columns returns the column information for a given table or view
63-
func (c Connection) Columns(dbOwner, dbName string, ident Identifier, table string) (columns []com.APIJSONColumn, err error) {
61+
func (c Connection) Columns(dbOwner, dbName string, ident Identifier, table string) (columns []APIJSONColumn, err error) {
6462
// Prepare the API parameters
6563
data := c.PrepareVals(dbOwner, dbName, ident)
6664
data.Set("table", table)
@@ -72,7 +70,7 @@ func (c Connection) Columns(dbOwner, dbName string, ident Identifier, table stri
7270
}
7371

7472
// Commits returns the details of all commits for a database
75-
func (c Connection) Commits(dbOwner, dbName string) (commits map[string]com.CommitEntry, err error) {
73+
func (c Connection) Commits(dbOwner, dbName string) (commits map[string]CommitEntry, err error) {
7674
// Prepare the API parameters
7775
data := c.PrepareVals(dbOwner, dbName, Identifier{})
7876

@@ -123,7 +121,7 @@ func (c Connection) Delete(dbName string) (err error) {
123121

124122
// Diff returns the differences between two commits of two databases, or if the details on the second database are left empty,
125123
// between two commits of the same database. You can also specify the merge strategy used for the generated SQL statements.
126-
func (c Connection) Diff(dbOwnerA, dbNameA string, identA Identifier, dbOwnerB, dbNameB string, identB Identifier, merge MergeStrategy) (diffs com.Diffs, err error) {
124+
func (c Connection) Diff(dbOwnerA, dbNameA string, identA Identifier, dbOwnerB, dbNameB string, identB Identifier, merge MergeStrategy) (diffs Diffs, err error) {
127125
// Prepare the API parameters
128126
data := url.Values{}
129127
data.Set("apikey", c.APIKey)
@@ -190,7 +188,7 @@ func (c Connection) Execute(dbOwner, dbName string, sql string) (rowsChanged int
190188
data.Set("sql", base64.StdEncoding.EncodeToString([]byte(sql)))
191189

192190
// Run the query on the remote database
193-
var execResponse com.ExecuteResponseContainer
191+
var execResponse ExecuteResponseContainer
194192
queryUrl := c.Server + "/v1/execute"
195193
err = sendRequestJSON(queryUrl, c.VerifyServerCert, data, &execResponse)
196194
if err != nil {
@@ -201,7 +199,7 @@ func (c Connection) Execute(dbOwner, dbName string, sql string) (rowsChanged int
201199
}
202200

203201
// Indexes returns the list of indexes present in the database, along with the table they belong to
204-
func (c Connection) Indexes(dbOwner, dbName string, ident Identifier) (idx []com.APIJSONIndex, err error) {
202+
func (c Connection) Indexes(dbOwner, dbName string, ident Identifier) (idx []APIJSONIndex, err error) {
205203
// Prepare the API parameters
206204
data := c.PrepareVals(dbOwner, dbName, ident)
207205

@@ -212,7 +210,7 @@ func (c Connection) Indexes(dbOwner, dbName string, ident Identifier) (idx []com
212210
}
213211

214212
// Metadata returns the metadata (branches, releases, tags, commits, etc) for the database
215-
func (c Connection) Metadata(dbOwner, dbName string) (meta com.MetadataResponseContainer, err error) {
213+
func (c Connection) Metadata(dbOwner, dbName string) (meta MetadataResponseContainer, err error) {
216214
// Prepare the API parameters
217215
data := c.PrepareVals(dbOwner, dbName, Identifier{})
218216

@@ -260,7 +258,7 @@ func (c Connection) Query(dbOwner, dbName string, ident Identifier, blobBase64 b
260258
data.Set("sql", base64.StdEncoding.EncodeToString([]byte(sql)))
261259

262260
// Run the query on the remote database
263-
var returnedData []com.DataRow
261+
var returnedData []DataRow
264262
queryUrl := c.Server + "/v1/query"
265263
err = sendRequestJSON(queryUrl, c.VerifyServerCert, data, &returnedData)
266264
if err != nil {
@@ -274,10 +272,10 @@ func (c Connection) Query(dbOwner, dbName string, ident Identifier, blobBase64 b
274272
var oneRow ResultRow
275273
for _, l := range j {
276274
switch l.Type {
277-
case com.Float, com.Integer, com.Text:
275+
case Float, Integer, Text:
278276
// Float, integer, and text fields are added to the output
279277
oneRow.Fields = append(oneRow.Fields, fmt.Sprint(l.Value))
280-
case com.Binary:
278+
case Binary:
281279
// BLOB data is optionally Base64 encoded, or just skipped (using an empty string as placeholder)
282280
if blobBase64 {
283281
// Safety check. Make sure we've received a string
@@ -302,7 +300,7 @@ func (c Connection) Query(dbOwner, dbName string, ident Identifier, blobBase64 b
302300
}
303301

304302
// Releases returns the details of all releases for a database
305-
func (c Connection) Releases(dbOwner, dbName string) (releases map[string]com.ReleaseEntry, err error) {
303+
func (c Connection) Releases(dbOwner, dbName string) (releases map[string]ReleaseEntry, err error) {
306304
// Prepare the API parameters
307305
data := c.PrepareVals(dbOwner, dbName, Identifier{})
308306

@@ -324,7 +322,7 @@ func (c Connection) Tables(dbOwner, dbName string, ident Identifier) (tbl []stri
324322
}
325323

326324
// Tags returns the details of all tags for a database
327-
func (c Connection) Tags(dbOwner, dbName string) (tags map[string]com.TagEntry, err error) {
325+
func (c Connection) Tags(dbOwner, dbName string) (tags map[string]TagEntry, err error) {
328326
// Prepare the API parameters
329327
data := c.PrepareVals(dbOwner, dbName, Identifier{})
330328

@@ -440,7 +438,7 @@ func (c Connection) UploadLive(dbName string, dbBytes *[]byte) (err error) {
440438
}
441439

442440
// Webpage returns the URL of the database file in the webUI. eg. for web browsers
443-
func (c Connection) Webpage(dbOwner, dbName string) (webPage com.WebpageResponseContainer, err error) {
441+
func (c Connection) Webpage(dbOwner, dbName string) (webPage WebpageResponseContainer, err error) {
444442
// Prepare the API parameters
445443
data := c.PrepareVals(dbOwner, dbName, Identifier{})
446444

server_types.go

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package dbhub
2+
3+
import (
4+
"time"
5+
)
6+
7+
type APIJSONColumn struct {
8+
Cid int `json:"column_id"`
9+
Name string `json:"name"`
10+
DataType string `json:"data_type"`
11+
NotNull bool `json:"not_null"`
12+
DfltValue string `json:"default_value"`
13+
Pk int `json:"primary_key"`
14+
}
15+
16+
type APIJSONIndexColumn struct {
17+
CID int `json:"id"`
18+
Name string `json:"name"`
19+
}
20+
21+
type APIJSONIndex struct {
22+
Name string `json:"name"`
23+
Table string `json:"table"`
24+
Columns []APIJSONIndexColumn `json:"columns"`
25+
}
26+
27+
type BranchEntry struct {
28+
Commit string `json:"commit"`
29+
CommitCount int `json:"commit_count"`
30+
Description string `json:"description"`
31+
}
32+
33+
type BranchListResponseContainer struct {
34+
Branches map[string]BranchEntry `json:"branches"`
35+
DefaultBranch string `json:"default_branch"`
36+
}
37+
38+
type CommitEntry struct {
39+
AuthorEmail string `json:"author_email"`
40+
AuthorName string `json:"author_name"`
41+
CommitterEmail string `json:"committer_email"`
42+
CommitterName string `json:"committer_name"`
43+
ID string `json:"id"`
44+
Message string `json:"message"`
45+
OtherParents []string `json:"other_parents"`
46+
Parent string `json:"parent"`
47+
Timestamp time.Time `json:"timestamp"`
48+
Tree DBTree `json:"tree"`
49+
}
50+
51+
type ValType int
52+
53+
const (
54+
Binary ValType = iota
55+
Image
56+
Null
57+
Text
58+
Integer
59+
Float
60+
)
61+
62+
type DataValue struct {
63+
Name string
64+
Type ValType
65+
Value interface{}
66+
}
67+
68+
type DataRow []DataValue
69+
70+
type DBTree struct {
71+
ID string `json:"id"`
72+
Entries []DBTreeEntry `json:"entries"`
73+
}
74+
75+
type DBTreeEntryType string
76+
77+
const (
78+
TREE DBTreeEntryType = "tree"
79+
DATABASE = "db"
80+
LICENCE = "licence"
81+
)
82+
83+
type DBTreeEntry struct {
84+
EntryType DBTreeEntryType `json:"entry_type"`
85+
LastModified time.Time `json:"last_modified"`
86+
LicenceSHA string `json:"licence"`
87+
Name string `json:"name"`
88+
Sha256 string `json:"sha256"`
89+
Size int64 `json:"size"`
90+
}
91+
92+
type ExecuteResponseContainer struct {
93+
RowsChanged int `json:"rows_changed"`
94+
Status string `json:"status"`
95+
}
96+
97+
type MetadataResponseContainer struct {
98+
Branches map[string]BranchEntry `json:"branches"`
99+
Commits map[string]CommitEntry `json:"commits"`
100+
DefBranch string `json:"default_branch"`
101+
Releases map[string]ReleaseEntry `json:"releases"`
102+
Tags map[string]TagEntry `json:"tags"`
103+
WebPage string `json:"web_page"`
104+
}
105+
106+
type ReleaseEntry struct {
107+
Commit string `json:"commit"`
108+
Date time.Time `json:"date"`
109+
Description string `json:"description"`
110+
ReleaserEmail string `json:"email"`
111+
ReleaserName string `json:"name"`
112+
Size int64 `json:"size"`
113+
}
114+
115+
type TagEntry struct {
116+
Commit string `json:"commit"`
117+
Date time.Time `json:"date"`
118+
Description string `json:"description"`
119+
TaggerEmail string `json:"email"`
120+
TaggerName string `json:"name"`
121+
}
122+
123+
type WebpageResponseContainer struct {
124+
WebPage string `json:"web_page"`
125+
}
126+
127+
type DiffType string
128+
129+
const (
130+
ActionAdd DiffType = "add"
131+
ActionDelete DiffType = "delete"
132+
ActionModify DiffType = "modify"
133+
)
134+
135+
type SchemaDiff struct {
136+
ActionType DiffType `json:"action_type"`
137+
Sql string `json:"sql,omitempty"`
138+
Before string `json:"before"`
139+
After string `json:"after"`
140+
}
141+
142+
type DataDiff struct {
143+
ActionType DiffType `json:"action_type"`
144+
Sql string `json:"sql,omitempty"`
145+
Pk []DataValue `json:"pk"`
146+
DataBefore []interface{} `json:"data_before,omitempty"`
147+
DataAfter []interface{} `json:"data_after,omitempty"`
148+
}
149+
150+
type DiffObjectChangeset struct {
151+
ObjectName string `json:"object_name"`
152+
ObjectType string `json:"object_type"`
153+
Schema *SchemaDiff `json:"schema,omitempty"`
154+
Data []DataDiff `json:"data,omitempty"`
155+
}
156+
157+
type Diffs struct {
158+
Diff []DiffObjectChangeset `json:"diff"`
159+
}

0 commit comments

Comments
 (0)