Skip to content

Commit

Permalink
fix: code examples (#73)
Browse files Browse the repository at this point in the history
* fix: code examples

* fix test ci
  • Loading branch information
hantmac authored Oct 20, 2023
1 parent 0aa66ff commit 3395417
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
58 changes: 44 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,23 @@ If the create table SQL is `CREATE TABLE test (
you can use the next code to batch insert data:

```go
conn, err := sql.Open("databend", dsn)
package main

import (
"database/sql"
"fmt"

_ "github.com/databendcloud/databend-go"
)

func main() {
conn, err := sql.Open("databend", "http://databend:databend@localhost:8000/default?sslmode=disable")
tx, err := conn.Begin()
if err != nil {
fmt.Println(err)
}
batch, err := conn.Prepare(fmt.Sprintf("INSERT INTO %s VALUES", "test"))
for i := 0; i < 10; i++ {
batch, err := tx.Prepare(fmt.Sprintf("INSERT INTO %s VALUES", "test"))
for i := 0; i < 10; i++ {
_, err = batch.Exec(
"1234",
"2345",
Expand All @@ -102,18 +113,27 @@ for i := 0; i < 10; i++ {
"2021-01-01 00:00:00",
)
}
err = conn.Commit()
err = tx.Commit()
}
```

## Querying Row/s
Querying a single row can be achieved using the QueryRow method. This returns a *sql.Row, on which Scan can be invoked with pointers to variables into which the columns should be marshaled.

```go
dsn, cfg, err := getDSN()
if err != nil {
log.Fatalf("failed to create DSN from Config: %v, err: %v", cfg, err)
}
conn, err := sql.Open("databend", dsn)
package main

import (
"database/sql"
"fmt"

_ "github.com/databendcloud/databend-go"
)

func main() {
// create table data (col1 uint8, col2 string);
// insert into data values(1,'col2');
conn, err := sql.Open("databend", "http://databend:databend@localhost:8000/default?sslmode=disable")
if err != nil {
fmt.Println(err)
}
Expand All @@ -126,16 +146,25 @@ dsn, cfg, err := getDSN()
fmt.Println(err)
}
fmt.Println(col2)
}
```

Iterating multiple rows requires the Query method. This returns a *sql.Rows struct on which Next can be invoked to iterate through the rows. QueryContext equivalent allows passing of a context.

```go
dsn, cfg, err := getDSN()
if err != nil {
log.Fatalf("failed to create DSN from Config: %v, err: %v", cfg, err)
}
conn, err := sql.Open("databend", dsn)
package main

import (
"database/sql"
"fmt"

_ "github.com/databendcloud/databend-go"
)

func main() {
// create table data (col1 uint8, col2 string);
// insert into data values(1,'col2');
conn, err := sql.Open("databend", "http://databend:databend@localhost:8000/default?sslmode=disable")
if err != nil {
fmt.Println(err)
}
Expand All @@ -150,6 +179,7 @@ dsn, cfg, err := getDSN()
}
fmt.Println(col2)
}
}
```

## Compatibility
Expand Down
6 changes: 3 additions & 3 deletions tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (s *DatabendTestSuite) TestDesc() {

result, err := scanValues(rows)
s.r.Nil(err)
s.r.Equal([][]interface{}{[]interface{}{"i64", "BIGINT", "NO", "0", ""}, []interface{}{"u64", "BIGINT UNSIGNED", "NO", "0", ""}, []interface{}{"f64", "DOUBLE", "NO", "0", ""}, []interface{}{"s", "VARCHAR", "NO", "", ""}, []interface{}{"s2", "VARCHAR", "NO", "", ""}, []interface{}{"a16", "ARRAY(INT16)", "NO", "[]", ""}, []interface{}{"a8", "ARRAY(UINT8)", "NO", "[]", ""}, []interface{}{"d", "DATE", "NO", "", ""}, []interface{}{"t", "TIMESTAMP", "NO", "", ""}}, result)
s.r.Equal([][]interface{}{[]interface{}{"i64", "BIGINT", "YES", "NULL", ""}, []interface{}{"u64", "BIGINT UNSIGNED", "YES", "NULL", ""}, []interface{}{"f64", "DOUBLE", "YES", "NULL", ""}, []interface{}{"s", "VARCHAR", "YES", "NULL", ""}, []interface{}{"s2", "VARCHAR", "YES", "NULL", ""}, []interface{}{"a16", "ARRAY(INT16)", "YES", "NULL", ""}, []interface{}{"a8", "ARRAY(UINT8)", "YES", "NULL", ""}, []interface{}{"d", "DATE", "YES", "NULL", ""}, []interface{}{"t", "TIMESTAMP", "YES", "NULL", ""}}, result)
rows.Close()
}

Expand Down Expand Up @@ -163,12 +163,12 @@ func (s *DatabendTestSuite) TestExec() {
}{
{
fmt.Sprintf("INSERT INTO %s (i64) VALUES (?)", s.table),
fmt.Sprintf("SELECT i64 FROM %s WHERE i64=?", s.table),
"",
[]interface{}{int64(1)},
},
{
fmt.Sprintf("INSERT INTO %s (i64, u64) VALUES (?, ?)", s.table),
fmt.Sprintf("SELECT i64, u64 FROM %s WHERE i64=? AND u64=?", s.table),
"",
[]interface{}{int64(2), uint64(12)},
},
{
Expand Down

0 comments on commit 3395417

Please sign in to comment.