Update example for the newly required Identifier structure

Also update and expand out the README
This commit is contained in:
Justin Clift
2020-08-03 23:03:37 +10:00
parent 80d7e68a79
commit 683f6bde36
7 changed files with 69 additions and 19 deletions

View File

@@ -31,15 +31,19 @@ A Go library for accessing and using SQLite databases stored remotely on DBHub.i
### Example code
#### Create a new DBHub.io API object
```
// Create a new DBHub.io API object
db, err := dbhub.New("YOUR_API_KEY_HERE")
if err != nil {
log.Fatal(err)
}
```
// Retrieve the list of tables in the remote database
tables, err := db.Tables("justinclift", "Join Testing.sqlite")
#### Retrieve the list of tables in a remote database
```
// Run the `Tables()` function on the new API object
tables, err := db.Tables("justinclift", "Join Testing.sqlite", dbhub.Identifier{Branch: "master"})
if err != nil {
log.Fatal(err)
}
@@ -49,9 +53,22 @@ fmt.Println("Tables:")
for _, j := range tables {
fmt.Printf(" * %s\n", j)
}
```
// Run a SQL query on the remote database
r, err := db.Query("justinclift", "Join Testing.sqlite", false,
##### Output
```
Tables:
* table1
* table2
```
#### Run a SQL query on a remote database
```
// Do we want to display BLOBs as base64?
showBlobs := false
// Run the query
r, err := db.Query("justinclift", "Join Testing.sqlite", dbhub.Identifier{Branch: "master"}, showBlobs,
`SELECT table1.Name, table2.value
FROM table1 JOIN table2
USING (id)
@@ -63,17 +80,50 @@ fmt.Printf("Query results (JSON):\n\t%v\n", r)
fmt.Println()
```
### Output
##### Output
```
Tables:
* table1
* table2
Query results (JSON):
{[{[Foo 5]} {[Bar 10]} {[Baz 15]} {[Blumph 12.5000]} {[Blargo 8]} {[Batty 3]}]}
```
#### Generate and display diff for two commits of a remote database
```
// The databases we want to see differences for
db1Owner := "justinclift"
db1Name := "Join Testing.sqlite"
db1Commit := dbhub.Identifier{CommitID: "c82ba65add364427e9af3f540be8bf98e8cd6bdb825b07c334858e816c983db0"}
db2Owner := ""
db2Name := ""
db2Commit := dbhub.Identifier{CommitID: "adf78104254ece17ff40dab80ae800574fa5d429a4869792a64dcf2027cd9cd9"}
// Create the diff
diffs, err := db.Diff(db1Owner, db1Name, db1Commit, db2Owner, db2Name, db2Commit, dbhub.PreservePkMerge)
if err != nil {
log.Fatal(err)
}
// Display the diff
fmt.Printf("SQL statements for turning the first commit into the second:\n")
for _, i := range diffs.Diff {
if i.Schema != nil {
fmt.Printf("%s\n", i.Schema.Sql)
}
for _, j := range i.Data {
fmt.Printf("%s\n", j.Sql)
}
}
```
##### Output
```
SQL statements for turning the first commit into the second:
CREATE VIEW joinedView AS
SELECT table1.Name, table2.value
FROM table1 JOIN table2
USING (id)
ORDER BY table1.id;
```
### Further examples
* [SQL Query](https://github.com/sqlitebrowser/go-dbhub/blob/master/examples/sql_query/main.go) - Run a SQL query, return the results as JSON

View File

@@ -16,7 +16,7 @@ func main() {
// Retrieve the column info for a table or view in the remote database
table := "table1"
columns, err := db.Columns("justinclift", "Join Testing.sqlite", table)
columns, err := db.Columns("justinclift", "Join Testing.sqlite", dbhub.Identifier{Branch: "master"}, table)
if err != nil {
log.Fatal(err)
}

View File

@@ -17,8 +17,8 @@ func main() {
// Retrieve the differences between two commmits of the same database
user := "justinclift"
database := "DB4S download stats.sqlite"
commit1 := "34cbeebfc347a09406707f4220cd40f60778692523d2e7d227ccd92f4125c9ea"
commit2 := "bc6a07955811d86db79e9b4f7fdc3cb2360d40da793066510d792588a8bf8de2"
commit1 := dbhub.Identifier{CommitID: "34cbeebfc347a09406707f4220cd40f60778692523d2e7d227ccd92f4125c9ea"}
commit2 := dbhub.Identifier{CommitID: "bc6a07955811d86db79e9b4f7fdc3cb2360d40da793066510d792588a8bf8de2"}
mergeMode := dbhub.PreservePkMerge
diffs, err := db.Diff(user, database, commit1, "", "", commit2, mergeMode)
if err != nil {

View File

@@ -15,7 +15,7 @@ func main() {
}
// Retrieve the list of indexes in the remote database
indexes, err := db.Indexes("justinclift", "Join Testing.sqlite")
indexes, err := db.Indexes("justinclift", "Join Testing.sqlite", dbhub.Identifier{Branch: "master"})
if err != nil {
log.Fatal(err)
}

View File

@@ -15,7 +15,7 @@ func main() {
}
// Retrieve the list of tables in the remote database
tables, err := db.Tables("justinclift", "Join Testing.sqlite")
tables, err := db.Tables("justinclift", "Join Testing.sqlite", dbhub.Identifier{Branch: "master"})
if err != nil {
log.Fatal(err)
}

View File

@@ -15,7 +15,7 @@ func main() {
}
// Retrieve the list of views in the remote database
views, err := db.Views("justinclift", "Join Testing.sqlite")
views, err := db.Views("justinclift", "Join Testing.sqlite", dbhub.Identifier{Branch: "master"})
if err != nil {
log.Fatal(err)
}

View File

@@ -16,8 +16,8 @@ func main() {
// Run a query on the remote database
showBlobs := false
r, err := db.Query("justinclift", "Join Testing.sqlite", showBlobs,
`SELECT table1.Name, table2.value
r, err := db.Query("justinclift", "Join Testing.sqlite", dbhub.Identifier{Branch: "master"},
showBlobs, `SELECT table1.Name, table2.value
FROM table1 JOIN table2
USING (id)
ORDER BY table1.id`)