@@ -2,6 +2,7 @@ package tarantool_test
2
2
3
3
import (
4
4
"fmt"
5
+ "github.com/tarantool/go-tarantool/test_helpers"
5
6
"time"
6
7
7
8
"github.com/tarantool/go-tarantool"
@@ -42,7 +43,7 @@ func ExampleConnection_Select() {
42
43
return
43
44
}
44
45
defer conn .Close ()
45
- resp , err := conn .Select (512 , 0 , 0 , 100 , tarantool .IterEq , []interface {}{uint (1111 )})
46
+ resp , err := conn .Select (517 , 0 , 0 , 100 , tarantool .IterEq , []interface {}{uint (1111 )})
46
47
if err != nil {
47
48
fmt .Printf ("error in select is %v" , err )
48
49
return
@@ -68,7 +69,7 @@ func ExampleConnection_SelectTyped() {
68
69
}
69
70
defer conn .Close ()
70
71
var res []Tuple
71
- err = conn .SelectTyped (512 , 0 , 0 , 100 , tarantool .IterEq , tarantool.IntKey {1111 }, & res )
72
+ err = conn .SelectTyped (517 , 0 , 0 , 100 , tarantool .IterEq , tarantool.IntKey {1111 }, & res )
72
73
if err != nil {
73
74
fmt .Printf ("error in select is %v" , err )
74
75
return
@@ -86,7 +87,7 @@ func ExampleConnection_SelectTyped() {
86
87
}
87
88
88
89
func Example () {
89
- spaceNo := uint32 (512 )
90
+ spaceNo := uint32 (517 )
90
91
indexNo := uint32 (0 )
91
92
92
93
server := "127.0.0.1:3013"
@@ -222,3 +223,104 @@ func Example() {
222
223
// Fut 2 Error <nil>
223
224
// Fut 2 Data [[15 val 15 bla]]
224
225
}
226
+
227
+ // To use SQL to query a tarantool instance, call `Execute`.
228
+ //
229
+ // Pay attention that with different types of queries (DDL, DQL, DML etc.)
230
+ // some fields of the response structure (`MetaData` and `InfoAutoincrementIds` in `SQLInfo`) may be nil.
231
+ //
232
+ // See the [protocol](https://www.tarantool.io/en/doc/latest/dev_guide/internals/box_protocol/#responses-for-sql)
233
+ // explanation for details.
234
+ func ExampleSQL () {
235
+ // Tarantool supports SQL since version 2.0.0
236
+ isLess , _ := test_helpers .IsTarantoolVersionLess (2 , 0 , 0 )
237
+ if isLess {
238
+ return
239
+ }
240
+ server := "127.0.0.1:3013"
241
+ opts := tarantool.Opts {
242
+ Timeout : 500 * time .Millisecond ,
243
+ Reconnect : 1 * time .Second ,
244
+ MaxReconnects : 3 ,
245
+ User : "test" ,
246
+ Pass : "test" ,
247
+ }
248
+ client , err := tarantool .Connect (server , opts )
249
+ if err != nil {
250
+ fmt .Errorf ("Failed to connect: %s" , err .Error ())
251
+ }
252
+
253
+ resp , err := client .Execute ("CREATE TABLE SQL_TEST (id INTEGER PRIMARY KEY, name STRING)" , []interface {}{})
254
+ fmt .Println ("Execute" )
255
+ fmt .Println ("Error" , err )
256
+ fmt .Println ("Code" , resp .Code )
257
+ fmt .Println ("Data" , resp .Data )
258
+ fmt .Println ("MetaData" , resp .MetaData )
259
+ fmt .Println ("SQL Info" , resp .SQLInfo )
260
+
261
+ // there are 3 options to pass named parameters to an SQL query
262
+ sqlBind1 := map [string ]interface {}{
263
+ "id" : 1 ,
264
+ "name" : "test" ,
265
+ }
266
+
267
+ sqlBind2 := struct {
268
+ Id int
269
+ Name string
270
+ }{1 , "test" }
271
+
272
+ type kv struct {
273
+ Key string
274
+ Value interface {}
275
+ }
276
+
277
+ sqlBind3 := []kv {
278
+ kv {"id" , 1 },
279
+ kv {"name" , "test" },
280
+ }
281
+
282
+ // the next usage
283
+ resp , err = client .Execute ("SELECT id FROM SQL_TEST WHERE id=:id AND name=:name" , sqlBind1 )
284
+ fmt .Println ("Execute" )
285
+ fmt .Println ("Error" , err )
286
+ fmt .Println ("Code" , resp .Code )
287
+ fmt .Println ("Data" , resp .Data )
288
+ fmt .Println ("MetaData" , resp .MetaData )
289
+ fmt .Println ("SQL Info" , resp .SQLInfo )
290
+
291
+ // the same as
292
+ resp , err = client .Execute ("SELECT id FROM SQL_TEST WHERE id=:id AND name=:name" , sqlBind2 )
293
+ fmt .Println ("Execute" )
294
+ fmt .Println ("Error" , err )
295
+ fmt .Println ("Code" , resp .Code )
296
+ fmt .Println ("Data" , resp .Data )
297
+ fmt .Println ("MetaData" , resp .MetaData )
298
+ fmt .Println ("SQL Info" , resp .SQLInfo )
299
+
300
+ // the same as
301
+ resp , err = client .Execute ("SELECT id FROM SQL_TEST WHERE id=:id AND name=:name" , sqlBind3 )
302
+ fmt .Println ("Execute" )
303
+ fmt .Println ("Error" , err )
304
+ fmt .Println ("Code" , resp .Code )
305
+ fmt .Println ("Data" , resp .Data )
306
+ fmt .Println ("MetaData" , resp .MetaData )
307
+ fmt .Println ("SQL Info" , resp .SQLInfo )
308
+
309
+ // there are 2 options to pass positional arguments to an SQL query
310
+ resp , err = client .Execute ("SELECT id FROM SQL_TEST WHERE id=? AND name=?" , 1 , "test" )
311
+ fmt .Println ("Execute" )
312
+ fmt .Println ("Error" , err )
313
+ fmt .Println ("Code" , resp .Code )
314
+ fmt .Println ("Data" , resp .Data )
315
+ fmt .Println ("MetaData" , resp .MetaData )
316
+ fmt .Println ("SQL Info" , resp .SQLInfo )
317
+
318
+ // the same as
319
+ resp , err = client .Execute ("SELECT id FROM SQL_TEST WHERE id=? AND name=?" , []interface {}{2 , "test" })
320
+ fmt .Println ("Execute" )
321
+ fmt .Println ("Error" , err )
322
+ fmt .Println ("Code" , resp .Code )
323
+ fmt .Println ("Data" , resp .Data )
324
+ fmt .Println ("MetaData" , resp .MetaData )
325
+ fmt .Println ("SQL Info" , resp .SQLInfo )
326
+ }
0 commit comments