diff --git a/README.md b/README.md index b845572..1f81350 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/column_details/main.go b/examples/column_details/main.go index a80c26c..13e2165 100644 --- a/examples/column_details/main.go +++ b/examples/column_details/main.go @@ -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) } diff --git a/examples/diff_commits/main.go b/examples/diff_commits/main.go index ff73fc7..ceaaa84 100644 --- a/examples/diff_commits/main.go +++ b/examples/diff_commits/main.go @@ -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 { diff --git a/examples/list_indexes/main.go b/examples/list_indexes/main.go index fd56928..f343664 100644 --- a/examples/list_indexes/main.go +++ b/examples/list_indexes/main.go @@ -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) } diff --git a/examples/list_tables/main.go b/examples/list_tables/main.go index 48287bf..a670d77 100644 --- a/examples/list_tables/main.go +++ b/examples/list_tables/main.go @@ -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) } diff --git a/examples/list_views/main.go b/examples/list_views/main.go index f1c6f87..a5d70c8 100644 --- a/examples/list_views/main.go +++ b/examples/list_views/main.go @@ -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) } diff --git a/examples/sql_query/main.go b/examples/sql_query/main.go index 1c6e1e8..25aa90b 100644 --- a/examples/sql_query/main.go +++ b/examples/sql_query/main.go @@ -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`)