Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:support db Ping #71

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (dc *DatabendConn) PrepareContext(ctx context.Context, query string) (drive
return dc.prepare(query)
}

func buildDatabendConn(ctx context.Context, config Config) (*DatabendConn, error) {
func BuildDatabendConn(ctx context.Context, config Config) (*DatabendConn, error) {
dc := &DatabendConn{
ctx: ctx,
cfg: &config,
Expand Down Expand Up @@ -206,3 +206,11 @@ func (dc *DatabendConn) Rollback() error {
dc.Close()
return nil
}

func (dc *DatabendConn) Ping() error {
_, err := dc.Query("SELECT VERSION()", []driver.Value{})
if err != nil {
return err
}
return nil
}
2 changes: 1 addition & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (d DatabendDriver) OpenWithConfig(
config Config) (
driver.Conn, error) {
logger.Info("OpenWithConfig")
dc, err := buildDatabendConn(ctx, config)
dc, err := BuildDatabendConn(ctx, config)
if err != nil {
return nil, err
}
Expand Down
6 changes: 1 addition & 5 deletions stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ package godatabend
import (
"context"
"database/sql/driver"
"regexp"

ldriver "github.com/databendcloud/databend-go/lib/driver"
"github.com/pkg/errors"
)

var (
splitInsertRe = regexp.MustCompile(`(?si)(.+\s*VALUES)\s*(\(.+\))`)
ldriver "github.com/databendcloud/databend-go/lib/driver"
)

type databendStmt struct {
Expand Down
27 changes: 19 additions & 8 deletions tests/main_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package tests

import (
"context"
"database/sql"
"fmt"
"os"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -33,19 +33,25 @@ func TestDatabendSuite(t *testing.T) {

type DatabendTestSuite struct {
suite.Suite
db *sql.DB
table string
r *require.Assertions
db *sql.DB
databendConn *dc.DatabendConn
table string
r *require.Assertions
}

func (s *DatabendTestSuite) SetupSuite() {
var err error

dsn := os.Getenv("TEST_DATABEND_DSN")
//dsn := os.Getenv("TEST_DATABEND_DSN")
dsn := "http://databend:databend@localhost:8000/default?sslmode=disable"
s.NotEmpty(dsn)

s.db, err = sql.Open("databend", dsn)
s.Nil(err)
cfg, err := dc.ParseDSN(dsn)
s.Nil(err)
s.databendConn, err = dc.BuildDatabendConn(context.TODO(), *cfg)
s.Nil(err)

err = s.db.Ping()
s.Nil(err)
Expand Down Expand Up @@ -80,13 +86,18 @@ func (s *DatabendTestSuite) TearDownTest() {
s.r.Nil(err)
}

func (s *DatabendTestSuite) TestPing() {
err := s.databendConn.Ping()
s.r.Nil(err)
}

func (s *DatabendTestSuite) TestDesc() {
rows, err := s.db.Query("DESC " + s.table)
s.r.Nil(err)

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 +174,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
Loading