@@ -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"
@@ -31,7 +32,8 @@ func ExampleConnection_Select() {
31
32
conn .Replace (spaceNo , []interface {}{uint (1111 ), "hello" , "world" })
32
33
conn .Replace (spaceNo , []interface {}{uint (1112 ), "hallo" , "werld" })
33
34
34
- resp , err := conn .Select (512 , 0 , 0 , 100 , tarantool .IterEq , []interface {}{uint (1111 )})
35
+ resp , err := conn .Select (517 , 0 , 0 , 100 , tarantool .IterEq , []interface {}{uint (1111 )})
36
+
35
37
if err != nil {
36
38
fmt .Printf ("error in select is %v" , err )
37
39
return
@@ -53,7 +55,9 @@ func ExampleConnection_SelectTyped() {
53
55
conn := example_connect ()
54
56
defer conn .Close ()
55
57
var res []Tuple
56
- err := conn .SelectTyped (512 , 0 , 0 , 100 , tarantool .IterEq , tarantool.IntKey {1111 }, & res )
58
+
59
+ err := conn .SelectTyped (517 , 0 , 0 , 100 , tarantool .IterEq , tarantool.IntKey {1111 }, & res )
60
+
57
61
if err != nil {
58
62
fmt .Printf ("error in select is %v" , err )
59
63
return
@@ -73,6 +77,7 @@ func ExampleConnection_SelectTyped() {
73
77
func ExampleConnection_SelectAsync () {
74
78
conn := example_connect ()
75
79
defer conn .Close ()
80
+ spaceNo := uint32 (517 )
76
81
77
82
conn .Insert (spaceNo , []interface {}{uint (16 ), "test" , "one" })
78
83
conn .Insert (spaceNo , []interface {}{uint (17 ), "test" , "one" })
@@ -320,12 +325,12 @@ func ExampleSchema() {
320
325
}
321
326
322
327
space1 := schema .Spaces ["test" ]
323
- space2 := schema .SpacesById [514 ]
328
+ space2 := schema .SpacesById [516 ]
324
329
fmt .Printf ("Space 1 ID %d %s\n " , space1 .Id , space1 .Name )
325
330
fmt .Printf ("Space 2 ID %d %s\n " , space2 .Id , space2 .Name )
326
331
// Output:
327
- // Space 1 ID 512 test
328
- // Space 2 ID 514 schematest
332
+ // Space 1 ID 517 test
333
+ // Space 2 ID 516 schematest
329
334
}
330
335
331
336
// Example demonstrates how to retrieve information with space schema.
@@ -344,7 +349,7 @@ func ExampleSpace() {
344
349
345
350
// Access Space objects by name or ID.
346
351
space1 := schema .Spaces ["test" ]
347
- space2 := schema .SpacesById [514 ] // It's a map.
352
+ space2 := schema .SpacesById [516 ] // It's a map.
348
353
fmt .Printf ("Space 1 ID %d %s %s\n " , space1 .Id , space1 .Name , space1 .Engine )
349
354
fmt .Printf ("Space 1 ID %d %t\n " , space1 .FieldsCount , space1 .Temporary )
350
355
@@ -365,10 +370,132 @@ func ExampleSpace() {
365
370
fmt .Printf ("SpaceField 2 %s %s\n " , spaceField2 .Name , spaceField2 .Type )
366
371
367
372
// Output:
368
- // Space 1 ID 512 test memtx
373
+ // Space 1 ID 517 test memtx
369
374
// Space 1 ID 0 false
370
375
// Index 0 primary
371
376
// &{0 unsigned} &{2 string}
372
377
// SpaceField 1 name0 unsigned
373
378
// SpaceField 2 name3 unsigned
374
379
}
380
+
381
+ // To use SQL to query a tarantool instance, call Execute.
382
+ //
383
+ // Pay attention that with different types of queries (DDL, DQL, DML etc.)
384
+ // some fields of the response structure (MetaData and InfoAutoincrementIds in SQLInfo) may be nil.
385
+ func ExampleConnection_Execute () {
386
+ // Tarantool supports SQL since version 2.0.0
387
+ isLess , _ := test_helpers .IsTarantoolVersionLess (2 , 0 , 0 )
388
+ if isLess {
389
+ return
390
+ }
391
+ server := "127.0.0.1:3013"
392
+ opts := tarantool.Opts {
393
+ Timeout : 500 * time .Millisecond ,
394
+ Reconnect : 1 * time .Second ,
395
+ MaxReconnects : 3 ,
396
+ User : "test" ,
397
+ Pass : "test" ,
398
+ }
399
+ client , err := tarantool .Connect (server , opts )
400
+ if err != nil {
401
+ fmt .Printf ("Failed to connect: %s" , err .Error ())
402
+ }
403
+
404
+ resp , err := client .Execute ("CREATE TABLE SQL_TEST (id INTEGER PRIMARY KEY, name STRING)" , []interface {}{})
405
+ fmt .Println ("Execute" )
406
+ fmt .Println ("Error" , err )
407
+ fmt .Println ("Code" , resp .Code )
408
+ fmt .Println ("Data" , resp .Data )
409
+ fmt .Println ("MetaData" , resp .MetaData )
410
+ fmt .Println ("SQL Info" , resp .SQLInfo )
411
+
412
+ // there are 4 options to pass named parameters to an SQL query
413
+ // the simple map:
414
+ sqlBind1 := map [string ]interface {}{
415
+ "id" : 1 ,
416
+ "name" : "test" ,
417
+ }
418
+
419
+ // any type of structure
420
+ sqlBind2 := struct {
421
+ Id int
422
+ Name string
423
+ }{1 , "test" }
424
+
425
+ // it is possible to use []tarantool.KeyValueBind
426
+ sqlBind3 := []interface {}{
427
+ tarantool.KeyValueBind {Key : "id" , Value : 1 },
428
+ tarantool.KeyValueBind {Key : "name" , Value : "test" },
429
+ }
430
+
431
+ // or []interface{} slice with tarantool.KeyValueBind items inside
432
+ sqlBind4 := []tarantool.KeyValueBind {
433
+ {"id" , 1 },
434
+ {"name" , "test" },
435
+ }
436
+
437
+ // the next usage
438
+ resp , err = client .Execute ("SELECT id FROM SQL_TEST WHERE id=:id AND name=:name" , sqlBind1 )
439
+ fmt .Println ("Execute" )
440
+ fmt .Println ("Error" , err )
441
+ fmt .Println ("Code" , resp .Code )
442
+ fmt .Println ("Data" , resp .Data )
443
+ fmt .Println ("MetaData" , resp .MetaData )
444
+ fmt .Println ("SQL Info" , resp .SQLInfo )
445
+
446
+ // the same as
447
+ resp , err = client .Execute ("SELECT id FROM SQL_TEST WHERE id=:id AND name=:name" , sqlBind2 )
448
+ fmt .Println ("Execute" )
449
+ fmt .Println ("Error" , err )
450
+ fmt .Println ("Code" , resp .Code )
451
+ fmt .Println ("Data" , resp .Data )
452
+ fmt .Println ("MetaData" , resp .MetaData )
453
+ fmt .Println ("SQL Info" , resp .SQLInfo )
454
+
455
+ // the same as
456
+ resp , err = client .Execute ("SELECT id FROM SQL_TEST WHERE id=:id AND name=:name" , sqlBind3 )
457
+ fmt .Println ("Execute" )
458
+ fmt .Println ("Error" , err )
459
+ fmt .Println ("Code" , resp .Code )
460
+ fmt .Println ("Data" , resp .Data )
461
+ fmt .Println ("MetaData" , resp .MetaData )
462
+ fmt .Println ("SQL Info" , resp .SQLInfo )
463
+
464
+ // the same as
465
+ resp , err = client .Execute ("SELECT id FROM SQL_TEST WHERE id=:id AND name=:name" , sqlBind4 )
466
+ fmt .Println ("Execute" )
467
+ fmt .Println ("Error" , err )
468
+ fmt .Println ("Code" , resp .Code )
469
+ fmt .Println ("Data" , resp .Data )
470
+ fmt .Println ("MetaData" , resp .MetaData )
471
+ fmt .Println ("SQL Info" , resp .SQLInfo )
472
+
473
+ // the way to pass positional arguments to an SQL query
474
+ resp , err = client .Execute ("SELECT id FROM SQL_TEST WHERE id=? AND name=?" , []interface {}{2 , "test" })
475
+ fmt .Println ("Execute" )
476
+ fmt .Println ("Error" , err )
477
+ fmt .Println ("Code" , resp .Code )
478
+ fmt .Println ("Data" , resp .Data )
479
+ fmt .Println ("MetaData" , resp .MetaData )
480
+ fmt .Println ("SQL Info" , resp .SQLInfo )
481
+
482
+ // the way to pass SQL expression with using custom packing/unpacking for a type
483
+ var res []Tuple
484
+ sqlInfo , metaData , err := client .ExecuteTyped ("SELECT id, name, name FROM SQL_TEST WHERE id=?" , []interface {}{2 }, & res )
485
+ fmt .Println ("ExecuteTyped" )
486
+ fmt .Println ("Error" , err )
487
+ fmt .Println ("Data" , res )
488
+ fmt .Println ("MetaData" , metaData )
489
+ fmt .Println ("SQL Info" , sqlInfo )
490
+
491
+ // for using different types of parameters (positioned/named), collect all items in []interface{}
492
+ // all "named" items must be passed with tarantool.KeyValueBind{}
493
+ resp , err = client .Execute ("SELECT id FROM SQL_TEST WHERE id=:id AND name=?" ,
494
+ []interface {}{tarantool.KeyValueBind {"id" , 1 }, "test" })
495
+ fmt .Println ("Execute" )
496
+ fmt .Println ("Error" , err )
497
+ fmt .Println ("Code" , resp .Code )
498
+ fmt .Println ("Data" , resp .Data )
499
+ fmt .Println ("MetaData" , resp .MetaData )
500
+ fmt .Println ("SQL Info" , resp .SQLInfo )
501
+ }
0 commit comments