Skip to content

Commit 2d7a3fd

Browse files
committed
sql: add minimal sql support
This patch adds the support of SQL in connector. Added all required constants in const.go for encoding SQL in msgpack. Fixes #62
1 parent bec9f72 commit 2d7a3fd

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

connector.go

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Connector interface {
1717
Call(functionName string, args interface{}) (resp *Response, err error)
1818
Call17(functionName string, args interface{}) (resp *Response, err error)
1919
Eval(expr string, args interface{}) (resp *Response, err error)
20+
Execute(expr string, args interface{}) (resp *Response, err error)
2021

2122
GetTyped(space, index interface{}, key interface{}, result interface{}) (err error)
2223
SelectTyped(space, index interface{}, offset, limit, iterator uint32, key interface{}, result interface{}) (err error)

const.go

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const (
1111
EvalRequest = 8
1212
UpsertRequest = 9
1313
Call17Request = 10
14+
ExecuteRequest = 11
1415
PingRequest = 64
1516
SubscribeRequest = 66
1617

@@ -29,6 +30,10 @@ const (
2930
KeyDefTuple = 0x28
3031
KeyData = 0x30
3132
KeyError = 0x31
33+
KeyMetaData = 0x32
34+
KeySQLText = 0x40
35+
KeySQLBind = 0x41
36+
KeySQLInfo = 0x42
3237

3338
// https://github.com/fl00r/go-tarantool-1.6/issues/2
3439

multi/multi.go

+4
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ func (connMulti *ConnectionMulti) Eval(expr string, args interface{}) (resp *tar
294294
return connMulti.getCurrentConnection().Eval(expr, args)
295295
}
296296

297+
func (connMulti *ConnectionMulti) Execute(expr string, args interface{}) (resp *tarantool.Response, err error) {
298+
return connMulti.getCurrentConnection().Execute(expr, args)
299+
}
300+
297301
func (connMulti *ConnectionMulti) GetTyped(space, index interface{}, key interface{}, result interface{}) (err error) {
298302
return connMulti.getCurrentConnection().GetTyped(space, index, key, result)
299303
}

request.go

+37
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tarantool
22

33
import (
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
124132
type 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+
353390
func (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

Comments
 (0)