Skip to content

Commit 17fb94d

Browse files
committed
api: add Execute methods to ConnectionPool
The patch adds missed Execute, ExecuteTyped and ExecuteAsync methods to the ConnectionPool type. Part of #176
1 parent d4905f5 commit 17fb94d

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
1313
- Support queue 1.2.0 (#177)
1414
- ConnectionHandler interface for handling changes of connections in
1515
ConnectionPool (#178)
16+
- Execute, ExecuteTyped and ExecuteAsync methods to ConnectionPool (#176)
1617

1718
### Changed
1819

connection_pool/connection_pool.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,16 @@ func (connPool *ConnectionPool) Eval(expr string, args interface{}, userMode Mod
385385
return conn.Eval(expr, args)
386386
}
387387

388+
// Execute passes sql expression to Tarantool for execution.
389+
func (connPool *ConnectionPool) Execute(expr string, args interface{}, userMode Mode) (resp *tarantool.Response, err error) {
390+
conn, err := connPool.getNextConnection(userMode)
391+
if err != nil {
392+
return nil, err
393+
}
394+
395+
return conn.Execute(expr, args)
396+
}
397+
388398
// GetTyped performs select (with limit = 1 and offset = 0)
389399
// to box space and fills typed result.
390400
func (connPool *ConnectionPool) GetTyped(space, index interface{}, key interface{}, result interface{}, userMode ...Mode) (err error) {
@@ -495,6 +505,16 @@ func (connPool *ConnectionPool) EvalTyped(expr string, args interface{}, result
495505
return conn.EvalTyped(expr, args, result)
496506
}
497507

508+
// ExecuteTyped passes sql expression to Tarantool for execution.
509+
func (connPool *ConnectionPool) ExecuteTyped(expr string, args interface{}, result interface{}, userMode Mode) (tarantool.SQLInfo, []tarantool.ColumnMetaData, error) {
510+
conn, err := connPool.getNextConnection(userMode)
511+
if err != nil {
512+
return tarantool.SQLInfo{}, nil, err
513+
}
514+
515+
return conn.ExecuteTyped(expr, args, result)
516+
}
517+
498518
// SelectAsync sends select request to Tarantool and returns Future.
499519
func (connPool *ConnectionPool) SelectAsync(space, index interface{}, offset, limit, iterator uint32, key interface{}, userMode ...Mode) *tarantool.Future {
500520
conn, err := connPool.getConnByMode(ANY, userMode)
@@ -607,6 +627,17 @@ func (connPool *ConnectionPool) EvalAsync(expr string, args interface{}, userMod
607627
return conn.EvalAsync(expr, args)
608628
}
609629

630+
// ExecuteAsync sends sql expression to Tarantool for execution and returns
631+
// Future.
632+
func (connPool *ConnectionPool) ExecuteAsync(expr string, args interface{}, userMode Mode) *tarantool.Future {
633+
conn, err := connPool.getNextConnection(userMode)
634+
if err != nil {
635+
return newErrorFuture(err)
636+
}
637+
638+
return conn.ExecuteAsync(expr, args)
639+
}
640+
610641
// Do sends the request and returns a future.
611642
// For requests that belong to an only one connection (e.g. Unprepare or ExecutePrepared)
612643
// the argument of type Mode is unused.

connection_pool/connection_pool_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,67 @@ func TestEval(t *testing.T) {
642642
require.Falsef(t, val, "expected `false` with mode `RW`")
643643
}
644644

645+
type Member struct {
646+
id uint
647+
val string
648+
}
649+
650+
func (m *Member) DecodeMsgpack(d *decoder) error {
651+
var err error
652+
var l int
653+
if l, err = d.DecodeArrayLen(); err != nil {
654+
return err
655+
}
656+
if l != 2 {
657+
return fmt.Errorf("array len doesn't match: %d", l)
658+
}
659+
if m.id, err = d.DecodeUint(); err != nil {
660+
return err
661+
}
662+
if m.val, err = d.DecodeString(); err != nil {
663+
return err
664+
}
665+
return nil
666+
}
667+
668+
func TestExecute(t *testing.T) {
669+
test_helpers.SkipIfSQLUnsupported(t)
670+
671+
roles := []bool{false, true, false, false, true}
672+
673+
err := test_helpers.SetClusterRO(servers, connOpts, roles)
674+
require.Nilf(t, err, "fail to set roles for cluster")
675+
676+
connPool, err := connection_pool.Connect(servers, connOpts)
677+
require.Nilf(t, err, "failed to connect")
678+
require.NotNilf(t, connPool, "conn is nil after Connect")
679+
680+
defer connPool.Close()
681+
682+
request := "SELECT NAME0, NAME1 FROM SQL_TEST WHERE NAME0 == 1;"
683+
// Execute
684+
resp, err := connPool.Execute(request, []interface{}{}, connection_pool.ANY)
685+
require.Nilf(t, err, "failed to Execute")
686+
require.NotNilf(t, resp, "response is nil after Execute")
687+
require.GreaterOrEqualf(t, len(resp.Data), 1, "response.Data is empty after Execute")
688+
require.Equalf(t, len(resp.Data[0].([]interface{})), 2, "unexpected response")
689+
690+
// ExecuteTyped
691+
mem := []Member{}
692+
info, _, err := connPool.ExecuteTyped(request, []interface{}{}, &mem, connection_pool.ANY)
693+
require.Nilf(t, err, "failed to ExecuteTyped")
694+
require.Equalf(t, info.AffectedCount, uint64(0), "unexpected info.AffectedCount")
695+
require.Equalf(t, len(mem), 1, "wrong count of results")
696+
697+
// ExecuteAsync
698+
fut := connPool.ExecuteAsync(request, []interface{}{}, connection_pool.ANY)
699+
resp, err = fut.Get()
700+
require.Nilf(t, err, "failed to ExecuteAsync")
701+
require.NotNilf(t, resp, "response is nil after ExecuteAsync")
702+
require.GreaterOrEqualf(t, len(resp.Data), 1, "response.Data is empty after ExecuteAsync")
703+
require.Equalf(t, len(resp.Data[0].([]interface{})), 2, "unexpected response")
704+
}
705+
645706
func TestRoundRobinStrategy(t *testing.T) {
646707
roles := []bool{false, true, false, false, true}
647708

connection_pool/const.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Default mode for each request table:
77
---------- --------------
88
| call | no default |
99
| eval | no default |
10+
| execute | no default |
1011
| ping | no default |
1112
| insert | RW |
1213
| delete | RW |
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//go:build !go_tarantool_msgpack_v5
2+
// +build !go_tarantool_msgpack_v5
3+
4+
package connection_pool_test
5+
6+
import (
7+
"gopkg.in/vmihailenco/msgpack.v2"
8+
)
9+
10+
type decoder = msgpack.Decoder
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//go:build go_tarantool_msgpack_v5
2+
// +build go_tarantool_msgpack_v5
3+
4+
package connection_pool_test
5+
6+
import (
7+
"github.com/vmihailenco/msgpack/v5"
8+
)
9+
10+
type decoder = msgpack.Decoder

0 commit comments

Comments
 (0)