Skip to content

Commit

Permalink
Add ShowSpaces and Execute for Session (#315)
Browse files Browse the repository at this point in the history
Add Show spaces and for

Co-authored-by: Anqi <[email protected]>
  • Loading branch information
haoxins and Nicole00 authored Feb 27, 2024
1 parent 5135309 commit 5bfccb7
Show file tree
Hide file tree
Showing 3 changed files with 48 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
21 changes: 21 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,24 @@ 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)
}
assert.LessOrEqual(t, 1, len(spaceNames))
}

0 comments on commit 5bfccb7

Please sign in to comment.