@@ -2,6 +2,7 @@ package tarantool
22
33import (
44 "errors"
5+ "reflect"
56 "time"
67
78 "gopkg.in/vmihailenco/msgpack.v2"
@@ -120,6 +121,13 @@ func (conn *Connection) Eval(expr string, args interface{}) (resp *Response, err
120121 return conn .EvalAsync (expr , args ).Get ()
121122}
122123
124+ // Execute passes sql expression for execution.
125+ //
126+ // It is equal to conn.ExecuteAsync(space, tuple).Get().
127+ func (conn * Connection ) Execute (expr string , args interface {}) (resp * Response , err error ) {
128+ return conn .ExecuteAsync (expr , args ).Get ()
129+ }
130+
123131// single used for conn.GetTyped for decode one tuple
124132type single struct {
125133 res interface {}
@@ -346,10 +354,39 @@ func (conn *Connection) EvalAsync(expr string, args interface{}) *Future {
346354 })
347355}
348356
357+ // ExecuteAsync sends a sql expression for execution and returns Future.
358+ func (conn * Connection ) ExecuteAsync (expr string , args interface {}) * Future {
359+ future := conn .newFuture (ExecuteRequest )
360+ bind := makeSQLBind (args )
361+ return future .send (conn , func (enc * msgpack.Encoder ) error {
362+ enc .EncodeMapLen (2 )
363+ enc .EncodeUint64 (KeySQLText )
364+ enc .EncodeString (expr )
365+ enc .EncodeUint64 (KeySQLBind )
366+ return enc .Encode (bind )
367+ })
368+ }
369+
349370//
350371// private
351372//
352373
374+ func makeSQLBind (from interface {}) interface {} {
375+ val := reflect .ValueOf (from )
376+ arr := []map [string ]interface {}{}
377+
378+ if val .Kind () == reflect .Map {
379+ for _ , e := range val .MapKeys () {
380+ mp := map [string ]interface {}{}
381+ v := val .MapIndex (e )
382+ t := v .Interface ()
383+ mp [":" + e .String ()] = t
384+ arr = append (arr , mp )
385+ }
386+ }
387+ return arr
388+ }
389+
353390func (fut * Future ) pack (h * smallWBuf , enc * msgpack.Encoder , body func (* msgpack.Encoder ) error ) (err error ) {
354391 rid := fut .requestId
355392 hl := h .Len ()
0 commit comments