From 618e784627e2d0c0883f92c1708de3107296a0f9 Mon Sep 17 00:00:00 2001 From: Conor Branagan Date: Sun, 11 Dec 2016 12:49:48 -0500 Subject: [vtable] Add pure Go example of GitHub repo vtable. --- _example/vtable/main.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 _example/vtable/main.go (limited to '_example/vtable/main.go') diff --git a/_example/vtable/main.go b/_example/vtable/main.go new file mode 100644 index 0000000..442069f --- /dev/null +++ b/_example/vtable/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "database/sql" + "fmt" + "github.com/mattn/go-sqlite3" + "log" +) + +func main() { + sql.Register("sqlite3_with_extensions", &sqlite3.SQLiteDriver{ + ConnectHook: func(conn *sqlite3.SQLiteConn) error { + return conn.CreateModule("github", githubModule{}) + }, + }) + db, err := sql.Open("sqlite3_with_extensions", ":memory:") + if err != nil { + log.Fatal(err) + } + defer db.Close() + + _, err = db.Exec("create virtual table repo using github(id, full_name, description, html_url)") + if err != nil { + log.Fatal(err) + } + + rows, err := db.Query("select id, full_name, description, html_url from repo") + if err != nil { + log.Fatal(err) + } + defer rows.Close() + for rows.Next() { + var id, full_name, description, html_url string + rows.Scan(&id, &full_name, &description, &html_url) + fmt.Printf("%s: %s\n\t%s\n\t%s\n\n", id, full_name, description, html_url) + } +} -- cgit v1.2.3 From a9d61d54c6d919d7f627743daa26e54687244566 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Sun, 5 Mar 2017 20:49:45 +0900 Subject: use pointer receiver --- _example/vtable/main.go | 5 +++-- _example/vtable/vtable.go | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to '_example/vtable/main.go') diff --git a/_example/vtable/main.go b/_example/vtable/main.go index 442069f..2f3b8a4 100644 --- a/_example/vtable/main.go +++ b/_example/vtable/main.go @@ -3,14 +3,15 @@ package main import ( "database/sql" "fmt" - "github.com/mattn/go-sqlite3" "log" + + "github.com/mattn/go-sqlite3" ) func main() { sql.Register("sqlite3_with_extensions", &sqlite3.SQLiteDriver{ ConnectHook: func(conn *sqlite3.SQLiteConn) error { - return conn.CreateModule("github", githubModule{}) + return conn.CreateModule("github", &githubModule{}) }, }) db, err := sql.Open("sqlite3_with_extensions", ":memory:") diff --git a/_example/vtable/vtable.go b/_example/vtable/vtable.go index 40b460f..eb80e2a 100644 --- a/_example/vtable/vtable.go +++ b/_example/vtable/vtable.go @@ -19,7 +19,7 @@ type GithubRepo struct { type githubModule struct { } -func (m githubModule) Create(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) { +func (m *githubModule) Create(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) { err := c.DeclareVTab(fmt.Sprintf(` CREATE TABLE %s ( id INT, @@ -33,11 +33,11 @@ func (m githubModule) Create(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab return &ghRepoTable{}, nil } -func (m githubModule) Connect(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) { +func (m *githubModule) Connect(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) { return m.Create(c, args) } -func (m githubModule) DestroyModule() {} +func (m *githubModule) DestroyModule() {} type ghRepoTable struct { repos []GithubRepo -- cgit v1.2.3 From f9e79c0a39fae626821dd44da2372ded64b4f8d4 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Sun, 5 Mar 2017 20:52:55 +0900 Subject: golint --- _example/vtable/main.go | 6 +++--- _example/vtable/vtable.go | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) (limited to '_example/vtable/main.go') diff --git a/_example/vtable/main.go b/_example/vtable/main.go index 2f3b8a4..aad8dda 100644 --- a/_example/vtable/main.go +++ b/_example/vtable/main.go @@ -31,8 +31,8 @@ func main() { } defer rows.Close() for rows.Next() { - var id, full_name, description, html_url string - rows.Scan(&id, &full_name, &description, &html_url) - fmt.Printf("%s: %s\n\t%s\n\t%s\n\n", id, full_name, description, html_url) + var id, fullName, description, htmlURL string + rows.Scan(&id, &fullName, &description, &htmlURL) + fmt.Printf("%s: %s\n\t%s\n\t%s\n\n", id, fullName, description, htmlURL) } } diff --git a/_example/vtable/vtable.go b/_example/vtable/vtable.go index eb80e2a..1d6d824 100644 --- a/_example/vtable/vtable.go +++ b/_example/vtable/vtable.go @@ -9,11 +9,11 @@ import ( "github.com/mattn/go-sqlite3" ) -type GithubRepo struct { +type githubRepo struct { ID int `json:"id"` FullName string `json:"full_name"` Description string `json:"description"` - HtmlURL string `json:"html_url"` + HTMLURL string `json:"html_url"` } type githubModule struct { @@ -40,7 +40,7 @@ func (m *githubModule) Connect(c *sqlite3.SQLiteConn, args []string) (sqlite3.VT func (m *githubModule) DestroyModule() {} type ghRepoTable struct { - repos []GithubRepo + repos []githubRepo } func (v *ghRepoTable) Open() (sqlite3.VTabCursor, error) { @@ -55,7 +55,7 @@ func (v *ghRepoTable) Open() (sqlite3.VTabCursor, error) { return nil, err } - repos := make([]GithubRepo, 0) + var repos []githubRepo if err := json.Unmarshal(body, &repos); err != nil { return nil, err } @@ -71,7 +71,7 @@ func (v *ghRepoTable) Destroy() error { return nil } type ghRepoCursor struct { index int - repos []GithubRepo + repos []githubRepo } func (vc *ghRepoCursor) Column(c *sqlite3.SQLiteContext, col int) error { @@ -83,7 +83,7 @@ func (vc *ghRepoCursor) Column(c *sqlite3.SQLiteContext, col int) error { case 2: c.ResultText(vc.repos[vc.index].Description) case 3: - c.ResultText(vc.repos[vc.index].HtmlURL) + c.ResultText(vc.repos[vc.index].HTMLURL) } return nil } -- cgit v1.2.3