Skip to content

Commit

Permalink
feat: simplify mode, only 'online' & 'offline'
Browse files Browse the repository at this point in the history
  • Loading branch information
aceforeverd committed Apr 24, 2024
1 parent c046517 commit 01e2025
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 27 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
```

`<DB_NAME>` is mandatory in DSN, and at this time (version 0.2.0), you must ensure the database `<DB_NAME>` created before open go connection.

## Getting Start

```go
Expand Down
40 changes: 23 additions & 17 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
)

// compile time validation that our types implements the expected interfaces
var (
_ interfaces.Conn = (*conn)(nil)

Expand All @@ -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:
Expand All @@ -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 {
Expand All @@ -81,21 +79,27 @@ 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.
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.
//
Expand Down Expand Up @@ -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
}
Expand All @@ -206,6 +210,8 @@ func (c *conn) query(ctx context.Context, sql string, parameters ...interfaces.V
return nil, err
}

// POST endpoint/dbs/<db_name> is capable of all SQL, though it looks like
// a query API returns rows
req, err := http.NewRequestWithContext(
ctx,
"POST",
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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...)
}
2 changes: 1 addition & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"strings"
)


func init() {
sql.Register("openmldb", &driver{})
}

var (
_ interfaces.Driver = (*driver)(nil)
_ interfaces.DriverContext = (*driver)(nil)
Expand Down
2 changes: 1 addition & 1 deletion driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=

0 comments on commit 01e2025

Please sign in to comment.