├── .gitignore ├── LICENSE ├── README.md ├── client.go ├── go.mod ├── rqlite.go └── rqlite_test.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 rqlite 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rqlite-go 2 | rqlite-go is a Go database/sql driver for rqlite. It is still in development and not yet ready for use. 3 | 4 | There is an alternative, fully-functional [Go client library available](https://github.com/rqlite/gorqlite). 5 | -------------------------------------------------------------------------------- /client.go: -------------------------------------------------------------------------------- 1 | package rqlite 2 | 3 | import ( 4 | "errors" 5 | "io" 6 | "net/http" 7 | "net/url" 8 | ) 9 | 10 | type Node struct { 11 | u *url.URL 12 | c *http.Client 13 | 14 | statusURL string 15 | queryURL string 16 | executeURL string 17 | } 18 | 19 | func NewNode(u *url.URL) *Node { 20 | c := &http.Client{ 21 | CheckRedirect: func(req *http.Request, via []*http.Request) error { 22 | return http.ErrUseLastResponse 23 | }, 24 | } 25 | 26 | return &Node{ 27 | u: u, 28 | c: c, 29 | statusURL: u.String() + "/status", 30 | queryURL: u.String() + "/db/query", 31 | executeURL: u.String() + "/db/execute", 32 | } 33 | } 34 | 35 | func (n *Node) Status() (io.ReadCloser, error) { 36 | resp, err := n.c.Get(n.statusURL) 37 | if err != nil { 38 | return nil, err 39 | } 40 | if resp.StatusCode != http.StatusOK { 41 | return nil, errors.New("status endpoint not found") 42 | } 43 | return resp.Body, nil 44 | } -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/rqlite/rqlite-go 2 | 3 | go 1.17 4 | -------------------------------------------------------------------------------- /rqlite.go: -------------------------------------------------------------------------------- 1 | package rqlite 2 | 3 | import ( 4 | "context" 5 | "database/sql" 6 | "database/sql/driver" 7 | "net/url" 8 | "strings" 9 | ) 10 | 11 | func init() { 12 | sql.Register("rqlite", &RQLiteDriver{}) 13 | } 14 | 15 | // RQLiteDriver implements driver.Driver. 16 | type RQLiteDriver struct { 17 | } 18 | 19 | // Open opens database and returns a new connection. 20 | func (d *RQLiteDriver) Open(p string) (driver.Conn, error) { 21 | u, err := url.Parse(strings.TrimRight(p, "/")) 22 | if err != nil { 23 | return nil, err 24 | } 25 | 26 | node := NewNode(u) 27 | _, err = node.Status() 28 | if err != nil { 29 | return nil, err 30 | } 31 | 32 | return &RQLiteConn{ 33 | node: node, 34 | }, nil 35 | } 36 | 37 | type RQLiteConn struct { 38 | node *Node 39 | } 40 | 41 | func (c *RQLiteConn) Prepare(query string) (driver.Stmt, error) { 42 | return nil, nil 43 | } 44 | 45 | func (c *RQLiteConn) Close() error { 46 | return nil 47 | } 48 | 49 | func (c *RQLiteConn) Begin() (driver.Tx, error) { 50 | return nil, nil 51 | } 52 | 53 | func (c *RQLiteConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) { 54 | return nil, nil 55 | } -------------------------------------------------------------------------------- /rqlite_test.go: -------------------------------------------------------------------------------- 1 | package rqlite 2 | 3 | import ( 4 | "database/sql" 5 | "fmt" 6 | "testing" 7 | "net/http" 8 | "net/http/httptest" 9 | ) 10 | 11 | func Test_Init(t *testing.T) { 12 | if len(sql.Drivers()) != 1 || sql.Drivers()[0] != "rqlite" { 13 | t.Fatal("wrong list of registered Drivers") 14 | } 15 | } 16 | 17 | func Test_SimpleOpen(t *testing.T) { 18 | _, err := sql.Open("rqlite", "http://example.com") 19 | if err != nil { 20 | t.Fatal("failed to perform simple DB Open") 21 | } 22 | } 23 | 24 | func Test_SimplePingFail(t *testing.T) { 25 | db, err := sql.Open("rqlite", "http://example.com") 26 | if err != nil { 27 | t.Fatal("failed to perform simple DB Open") 28 | } 29 | if db.Ping() == nil { 30 | t.Fatal("successfully pinged non-existent DB") 31 | } 32 | } 33 | 34 | func Test_SimplePingOK(t *testing.T) { 35 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 36 | fmt.Fprintln(w, "OK") 37 | })) 38 | defer ts.Close() 39 | 40 | db, err := sql.Open("rqlite", ts.URL) 41 | if err != nil { 42 | t.Fatal("failed to perform simple DB Open") 43 | } 44 | if db.Ping() != nil { 45 | t.Fatal("failed to ping mock DB") 46 | } 47 | } --------------------------------------------------------------------------------