Skip to content

Commit 16d9f1b

Browse files
authored
feat: fix query mode & update readme (#10)
* revert: dns modes mode names should match api server define * docs: readme * docs: update readme
1 parent 2c5a00d commit 16d9f1b

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed

README.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ Pure Go [OpenMLDB](https://github.com/4paradigm/OpenMLDB) driver for database/sq
55

66
## Features
77

8+
- Lightweight
9+
- Pure Go implementation, No C-bindings
10+
- Connection over HTTP
11+
- Full OpenMLDB SQL support, work with online and offline mode
12+
- Numeric, bool, string, date, timestamp data type support
13+
814
## Requirements
915

1016
- OpenMLDB with all components version >= 0.6.2
@@ -19,7 +25,7 @@ go get github.com/4paradigm/openmldb-go-sdk
1925
## Data Source Name (DSN)
2026

2127
```
22-
openmldb://<API_SERVER_HOST>:<API_SERVER_PORT>/<DB_NAME>
28+
openmldb://<API_SERVER_HOST>:<API_SERVER_PORT>/<DB_NAME>?mode=<MODE_NAME>
2329
```
2430

2531
For example, to open a database to `test_db` by api server at `127.0.0.1:8080`:
@@ -28,6 +34,60 @@ db, err := sql.Open("openmldb", "openmldb://127.0.0.1:8080/test_db")
2834
```
2935

3036
`<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.
37+
DSN parameters (the `?mode=<MODE_NAME>` part) are optional.
38+
39+
40+
### Query Mode (Optional)
41+
42+
The execution mode for OpenMLDB, defined as `mode=<MODE_NAME>`, default to `online`, available values are:
43+
- `online`: online preview mode
44+
- `offsync`: offline mode with system variable `sync_job = true`
45+
- `offasync`: offline mode with system variable `sync_job = false`
46+
47+
48+
## Data type support
49+
50+
int16, int32, int64, float, double, bool, date, timestamp and string types in OpenMLDB SQL are supported.
51+
Since Go types are flexible by design, you may choose any type in Go by your favor, as long as that type implements
52+
[sql#Scanner](https://pkg.go.dev/database/sql#Scanner) interface.
53+
54+
For example, a SQL string type, can be represented in Go with `string`, `sql.NullString`, `sql.Null[string]`, `string` is able
55+
to represent SQL string when it is not NULL, error reported if you want to save a NULL value into string, while the later two types
56+
are able to save all SQL string, regardless nullable:
57+
58+
```go
59+
import (
60+
"database/sql"
61+
)
62+
63+
// ...
64+
65+
{
66+
var s string
67+
err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&s)
68+
// err returned from Scan if NULL value returned from query
69+
}
70+
71+
72+
{
73+
var s sql.NullString
74+
err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&s)
75+
// NullString is safe for query returns NULL
76+
// ...
77+
78+
if s.Valid {
79+
// use s.String
80+
} else {
81+
// NULL value
82+
}
83+
}
84+
```
85+
86+
### Timestamp and date support
87+
88+
We use `time.Time` internally represents SQL timestamp and date type, so you can choose whatever type that is
89+
scannable from `time.Time`, like `sql.NullTime`, or simply `time.Time` itself.
90+
3191

3292
## Getting Start
3393

conn.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,26 @@ type queryMode string
3838

3939
func (m queryMode) String() string {
4040
switch m {
41-
case ModeOffline:
42-
return "offline"
43-
case ModeOnline:
44-
return "online"
41+
case ModeOffsync:
42+
return "offsync"
43+
case ModeOffasync:
44+
return "offasync"
4545
default:
4646
return "unknown"
4747
}
4848
}
4949

5050
const (
51-
ModeOffline queryMode = "offline"
52-
ModeOnline queryMode = "online"
51+
ModeOffsync queryMode = "offsync"
52+
ModeOffasync queryMode = "offasync"
53+
ModeOnline queryMode = "online"
5354
// TODO(someone): "request"
5455
)
5556

5657
var allQueryMode = map[string]queryMode{
57-
"offline": ModeOffline,
58-
"online": ModeOnline,
58+
"offsync": ModeOffsync,
59+
"offasync": ModeOffasync,
60+
"online": ModeOnline,
5961
}
6062

6163
type conn struct {

driver_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ func Test_parseDsn(t *testing.T) {
1717
}{
1818
{"openmldb://127.0.0.1:8080/test_db", "127.0.0.1:8080", "test_db", ModeOnline, nil},
1919
{"openmldb://127.0.0.1:8080/test_db?mode=online", "127.0.0.1:8080", "test_db", ModeOnline, nil},
20-
{"openmldb://127.0.0.1:8080/test_db?mode=offline", "127.0.0.1:8080", "test_db", ModeOffline, nil},
20+
{"openmldb://127.0.0.1:8080/test_db?mode=offasync", "127.0.0.1:8080", "test_db", ModeOffasync, nil},
21+
{"openmldb://127.0.0.1:8080/test_db?mode=offsync", "127.0.0.1:8080", "test_db", ModeOffsync, nil},
2122
{"openmldb://127.0.0.1:8080/test_db?mode=unknown", "127.0.0.1:8080", "test_db", "", errors.New("")},
2223
} {
2324
host, db, mode, err := parseDsn(tc.dsn)

0 commit comments

Comments
 (0)