From 01e20253b4431cafd38c9b26d80d708bcc6d12c4 Mon Sep 17 00:00:00 2001 From: aceforeverd Date: Wed, 24 Apr 2024 13:54:40 +0000 Subject: [PATCH] feat: simplify mode, only 'online' & 'offline' --- README.md | 2 ++ conn.go | 40 +++++++++++++++++++++++----------------- driver.go | 2 +- driver_test.go | 2 +- go.mod | 2 +- go.sum | 9 ++------- 6 files changed, 30 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index de761a6..b116e11 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,8 @@ For example, to open a database to `test_db` by api server at `127.0.0.1:8080`: db, err := sql.Open("openmldb", "openmldb://127.0.0.1:8080/test_db") ``` +`` is mandatory in DSN, and at this time (version 0.2.0), you must ensure the database `` created before open go connection. + ## Getting Start ```go diff --git a/conn.go b/conn.go index 13550a5..22a97d5 100644 --- a/conn.go +++ b/conn.go @@ -12,6 +12,7 @@ import ( "strings" ) +// compile time validation that our types implements the expected interfaces var ( _ interfaces.Conn = (*conn)(nil) @@ -35,10 +36,8 @@ type queryMode string func (m queryMode) String() string { switch m { - case ModeOffsync: - return "offsync" - case ModeOffasync: - return "offasync" + case ModeOffline: + return "offline" case ModeOnline: return "online" default: @@ -47,15 +46,14 @@ func (m queryMode) String() string { } const ( - ModeOffsync queryMode = "offsync" - ModeOffasync queryMode = "offasync" - ModeOnline queryMode = "online" + ModeOffline queryMode = "offline" + ModeOnline queryMode = "online" + // TODO(someone): "request" ) var allQueryMode = map[string]queryMode{ - "offsync": ModeOffsync, - "offasync": ModeOffasync, - "online": ModeOnline, + "offline": ModeOffline, + "online": ModeOnline, } type conn struct { @@ -81,7 +79,9 @@ type respDataRows struct { i int } -// Columns returns the names of the columns. The number of +// Columns implements driver.Rows. +// +// Returns the names of the columns. The number of // columns of the result is inferred from the length of the // slice. If a particular column name isn't known, an empty // string should be returned for that entry. @@ -89,13 +89,17 @@ func (r respDataRows) Columns() []string { return make([]string, len(r.Schema)) } -// Close closes the rows iterator. +// Close implements driver.Rows. +// +// closes the rows iterator. func (r *respDataRows) Close() error { r.i = len(r.Data) return nil } -// Next is called to populate the next row of data into +// Next implements driver.Rows. +// +// called to populate the next row of data into // the provided slice. The provided slice will be the same // size as the Columns() are wide. // @@ -196,7 +200,7 @@ func parseRespFromJson(respBody io.Reader) (*queryResp, error) { return &r, nil } -func (c *conn) query(ctx context.Context, sql string, parameters ...interfaces.Value) (rows interfaces.Rows, err error) { +func (c *conn) execute(ctx context.Context, sql string, parameters ...interfaces.Value) (rows interfaces.Rows, err error) { if c.closed { return nil, interfaces.ErrBadConn } @@ -206,6 +210,8 @@ func (c *conn) query(ctx context.Context, sql string, parameters ...interfaces.V return nil, err } + // POST endpoint/dbs/ is capable of all SQL, though it looks like + // a query API returns rows req, err := http.NewRequestWithContext( ctx, "POST", @@ -250,7 +256,7 @@ func (c *conn) Begin() (interfaces.Tx, error) { // Ping implements driver.Pinger. func (c *conn) Ping(ctx context.Context) error { - _, err := c.query(ctx, "SELECT 1") + _, err := c.execute(ctx, "SELECT 1") return err } @@ -274,7 +280,7 @@ func (c *conn) ExecContext(ctx context.Context, query string, args []interfaces. for i, arg := range args { parameters[i] = arg.Value } - if _, err := c.query(ctx, query, parameters...); err != nil { + if _, err := c.execute(ctx, query, parameters...); err != nil { return nil, err } return interfaces.ResultNoRows, nil @@ -286,5 +292,5 @@ func (c *conn) QueryContext(ctx context.Context, query string, args []interfaces for i, arg := range args { parameters[i] = arg.Value } - return c.query(ctx, query, parameters...) + return c.execute(ctx, query, parameters...) } diff --git a/driver.go b/driver.go index 5c76269..396cf6b 100644 --- a/driver.go +++ b/driver.go @@ -9,10 +9,10 @@ import ( "strings" ) + func init() { sql.Register("openmldb", &driver{}) } - var ( _ interfaces.Driver = (*driver)(nil) _ interfaces.DriverContext = (*driver)(nil) diff --git a/driver_test.go b/driver_test.go index 8ced88c..56c426b 100644 --- a/driver_test.go +++ b/driver_test.go @@ -17,7 +17,7 @@ func Test_parseDsn(t *testing.T) { }{ {"openmldb://127.0.0.1:8080/test_db", "127.0.0.1:8080", "test_db", ModeOnline, nil}, {"openmldb://127.0.0.1:8080/test_db?mode=online", "127.0.0.1:8080", "test_db", ModeOnline, nil}, - {"openmldb://127.0.0.1:8080/test_db?mode=offasync", "127.0.0.1:8080", "test_db", ModeOffasync, nil}, + {"openmldb://127.0.0.1:8080/test_db?mode=offline", "127.0.0.1:8080", "test_db", ModeOffline, nil}, {"openmldb://127.0.0.1:8080/test_db?mode=unknown", "127.0.0.1:8080", "test_db", "", errors.New("")}, } { host, db, mode, err := parseDsn(tc.dsn) diff --git a/go.mod b/go.mod index 9ac3cbd..e57fa16 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/4paradigm/openmldb-go-sdk go 1.18 -require github.com/stretchr/testify v1.8.0 +require github.com/stretchr/testify v1.9.0 require ( github.com/davecgh/go-spew v1.1.1 // indirect diff --git a/go.sum b/go.sum index 5164829..60ce688 100644 --- a/go.sum +++ b/go.sum @@ -1,15 +1,10 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=