Skip to content

Commit

Permalink
Add ShowSpaces and Execute for Session
Browse files Browse the repository at this point in the history
Add Show spaces and for
  • Loading branch information
haoxins committed Feb 4, 2024
1 parent a010d20 commit 7dffe66
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
26 changes: 26 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,32 @@ func (session *Session) ExecuteJsonWithParameter(stmt string, params map[string]
return resp.([]byte), err
}

func (session *Session) ExecuteAndCheck(stmt string) (*ResultSet, error) {
rs, err := session.Execute(stmt)
if err != nil {
return nil, err
}

if !rs.IsSucceed() {
errMsg := rs.GetErrorMsg()
return nil, fmt.Errorf("Fail to execute query. %s", errMsg)
}

return rs, nil
}

func (session *Session) ShowSpaces() ([]SpaceName, error) {
rs, err := session.ExecuteAndCheck("SHOW SPACES;")
if err != nil {
return nil, err
}

var names []SpaceName
rs.Scan(&names)

return names, nil
}

func (session *Session) reConnect() error {
newConnection, err := session.connPool.getIdleConn()
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions session_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,7 @@ func (pool *SessionPool) ExecuteAndCheck(q string) (*ResultSet, error) {
return nil, err
}

errCode := rs.GetErrorCode()
if errCode != ErrorCode_SUCCEEDED {
if !rs.IsSucceed() {
errMsg := rs.GetErrorMsg()
return nil, fmt.Errorf("Fail to execute query. %s", errMsg)
}
Expand Down
23 changes: 23 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package nebula_go

import (
"context"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -117,3 +118,25 @@ func TestSession_Recover(t *testing.T) {
}
assert.Equal(t, 1, pool.getActiveConnCount()+pool.getIdleConnCount())
}

func TestSession_ShowSpaces(t *testing.T) {
config := GetDefaultConf()
host := HostAddress{address, port}
pool, err := NewConnectionPool([]HostAddress{host}, config, DefaultLogger{})
if err != nil {
t.Fatal(err)
}

sess, err := pool.GetSession("root", "nebula")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 1, pool.getActiveConnCount()+pool.getIdleConnCount())

spaceNames, err := sess.ShowSpaces()
if err != nil {
t.Fatal(err)
}
fmt.Println("spaces:")
fmt.Prinnln(spaceNames)

Check failure on line 141 in session_test.go

View workflow job for this annotation

GitHub Actions / go-client-ssl (1.17)

undefined: fmt.Prinnln

Check failure on line 141 in session_test.go

View workflow job for this annotation

GitHub Actions / go-client (1.19)

undefined: fmt.Prinnln

Check failure on line 141 in session_test.go

View workflow job for this annotation

GitHub Actions / go-client-ssl (1.19)

undefined: fmt.Prinnln

Check failure on line 141 in session_test.go

View workflow job for this annotation

GitHub Actions / go-client-ssl (1.18)

undefined: fmt.Prinnln

Check failure on line 141 in session_test.go

View workflow job for this annotation

GitHub Actions / go-client (1.16)

undefined: fmt.Prinnln

Check failure on line 141 in session_test.go

View workflow job for this annotation

GitHub Actions / go-client (1.20)

undefined: fmt.Prinnln

Check failure on line 141 in session_test.go

View workflow job for this annotation

GitHub Actions / go-client (1.21)

undefined: fmt.Prinnln

Check failure on line 141 in session_test.go

View workflow job for this annotation

GitHub Actions / go-client (1.18)

undefined: fmt.Prinnln

Check failure on line 141 in session_test.go

View workflow job for this annotation

GitHub Actions / go-client-ssl (1.20)

undefined: fmt.Prinnln

Check failure on line 141 in session_test.go

View workflow job for this annotation

GitHub Actions / go-client-ssl (1.21)

undefined: fmt.Prinnln

Check failure on line 141 in session_test.go

View workflow job for this annotation

GitHub Actions / go-client-ssl (1.16)

undefined: fmt.Prinnln

Check failure on line 141 in session_test.go

View workflow job for this annotation

GitHub Actions / go-client (1.17)

undefined: fmt.Prinnln
}

0 comments on commit 7dffe66

Please sign in to comment.