@@ -2,6 +2,7 @@ package tarantool_test
22
33import (
44 "fmt"
5+ "github.com/tarantool/go-tarantool/test_helpers"
56 "time"
67
78 "github.com/tarantool/go-tarantool"
@@ -31,7 +32,8 @@ func ExampleConnection_Select() {
3132 conn .Replace (spaceNo , []interface {}{uint (1111 ), "hello" , "world" })
3233 conn .Replace (spaceNo , []interface {}{uint (1112 ), "hallo" , "werld" })
3334
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+
3537 if err != nil {
3638 fmt .Printf ("error in select is %v" , err )
3739 return
@@ -53,7 +55,9 @@ func ExampleConnection_SelectTyped() {
5355 conn := example_connect ()
5456 defer conn .Close ()
5557 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+
5761 if err != nil {
5862 fmt .Printf ("error in select is %v" , err )
5963 return
@@ -73,6 +77,7 @@ func ExampleConnection_SelectTyped() {
7377func ExampleConnection_SelectAsync () {
7478 conn := example_connect ()
7579 defer conn .Close ()
80+ spaceNo := uint32 (517 )
7681
7782 conn .Insert (spaceNo , []interface {}{uint (16 ), "test" , "one" })
7883 conn .Insert (spaceNo , []interface {}{uint (17 ), "test" , "one" })
@@ -320,12 +325,12 @@ func ExampleSchema() {
320325 }
321326
322327 space1 := schema .Spaces ["test" ]
323- space2 := schema .SpacesById [514 ]
328+ space2 := schema .SpacesById [516 ]
324329 fmt .Printf ("Space 1 ID %d %s\n " , space1 .Id , space1 .Name )
325330 fmt .Printf ("Space 2 ID %d %s\n " , space2 .Id , space2 .Name )
326331 // 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
329334}
330335
331336// Example demonstrates how to retrieve information with space schema.
@@ -344,7 +349,7 @@ func ExampleSpace() {
344349
345350 // Access Space objects by name or ID.
346351 space1 := schema .Spaces ["test" ]
347- space2 := schema .SpacesById [514 ] // It's a map.
352+ space2 := schema .SpacesById [516 ] // It's a map.
348353 fmt .Printf ("Space 1 ID %d %s %s\n " , space1 .Id , space1 .Name , space1 .Engine )
349354 fmt .Printf ("Space 1 ID %d %t\n " , space1 .FieldsCount , space1 .Temporary )
350355
@@ -365,10 +370,132 @@ func ExampleSpace() {
365370 fmt .Printf ("SpaceField 2 %s %s\n " , spaceField2 .Name , spaceField2 .Type )
366371
367372 // Output:
368- // Space 1 ID 512 test memtx
373+ // Space 1 ID 517 test memtx
369374 // Space 1 ID 0 false
370375 // Index 0 primary
371376 // &{0 unsigned} &{2 string}
372377 // SpaceField 1 name0 unsigned
373378 // SpaceField 2 name3 unsigned
374379}
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