Skip to content

Commit 5f6571a

Browse files
committed
readme: add examples of usage SQL in connector
Added examples of using SQL queries in example_test.go for compiling the future documentation from sources. Follows up #62 Relates to #123
1 parent 1d2992e commit 5f6571a

File tree

1 file changed

+132
-2
lines changed

1 file changed

+132
-2
lines changed

example_test.go

+132-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tarantool_test
22

33
import (
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() {
7377
func 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"})
@@ -372,3 +377,128 @@ func ExampleSpace() {
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+
//
386+
// See the [protocol](https://www.tarantool.io/en/doc/latest/dev_guide/internals/box_protocol/#responses-for-sql)
387+
// explanation for details.
388+
func ExampleSQL() {
389+
// Tarantool supports SQL since version 2.0.0
390+
isLess, _ := test_helpers.IsTarantoolVersionLess(2, 0, 0)
391+
if isLess {
392+
return
393+
}
394+
server := "127.0.0.1:3013"
395+
opts := tarantool.Opts{
396+
Timeout: 500 * time.Millisecond,
397+
Reconnect: 1 * time.Second,
398+
MaxReconnects: 3,
399+
User: "test",
400+
Pass: "test",
401+
}
402+
client, err := tarantool.Connect(server, opts)
403+
if err != nil {
404+
fmt.Errorf("Failed to connect: %s", err.Error())
405+
}
406+
407+
resp, err := client.Execute("CREATE TABLE SQL_TEST (id INTEGER PRIMARY KEY, name STRING)", []interface{}{})
408+
fmt.Println("Execute")
409+
fmt.Println("Error", err)
410+
fmt.Println("Code", resp.Code)
411+
fmt.Println("Data", resp.Data)
412+
fmt.Println("MetaData", resp.MetaData)
413+
fmt.Println("SQL Info", resp.SQLInfo)
414+
415+
// there are 4 options to pass named parameters to an SQL query
416+
// the simple map:
417+
sqlBind1 := map[string]interface{}{
418+
"id": 1,
419+
"name": "test",
420+
}
421+
422+
// any type of structure
423+
sqlBind2 := struct {
424+
Id int
425+
Name string
426+
}{1, "test"}
427+
428+
// it is possible to use []tarantool.KeyValueBind
429+
sqlBind3 := []interface{}{
430+
tarantool.KeyValueBind{"id", 1},
431+
tarantool.KeyValueBind{"name", "test"},
432+
}
433+
434+
// or []interface{} slice with tarantool.KeyValueBind items inside
435+
sqlBind4 := []tarantool.KeyValueBind{
436+
{"id", 1},
437+
{"name", "test"},
438+
}
439+
440+
// the next usage
441+
resp, err = client.Execute("SELECT id FROM SQL_TEST WHERE id=:id AND name=:name", sqlBind1)
442+
fmt.Println("Execute")
443+
fmt.Println("Error", err)
444+
fmt.Println("Code", resp.Code)
445+
fmt.Println("Data", resp.Data)
446+
fmt.Println("MetaData", resp.MetaData)
447+
fmt.Println("SQL Info", resp.SQLInfo)
448+
449+
// the same as
450+
resp, err = client.Execute("SELECT id FROM SQL_TEST WHERE id=:id AND name=:name", sqlBind2)
451+
fmt.Println("Execute")
452+
fmt.Println("Error", err)
453+
fmt.Println("Code", resp.Code)
454+
fmt.Println("Data", resp.Data)
455+
fmt.Println("MetaData", resp.MetaData)
456+
fmt.Println("SQL Info", resp.SQLInfo)
457+
458+
// the same as
459+
resp, err = client.Execute("SELECT id FROM SQL_TEST WHERE id=:id AND name=:name", sqlBind3)
460+
fmt.Println("Execute")
461+
fmt.Println("Error", err)
462+
fmt.Println("Code", resp.Code)
463+
fmt.Println("Data", resp.Data)
464+
fmt.Println("MetaData", resp.MetaData)
465+
fmt.Println("SQL Info", resp.SQLInfo)
466+
467+
// the same as
468+
resp, err = client.Execute("SELECT id FROM SQL_TEST WHERE id=:id AND name=:name", sqlBind4)
469+
fmt.Println("Execute")
470+
fmt.Println("Error", err)
471+
fmt.Println("Code", resp.Code)
472+
fmt.Println("Data", resp.Data)
473+
fmt.Println("MetaData", resp.MetaData)
474+
fmt.Println("SQL Info", resp.SQLInfo)
475+
476+
// the way to pass positional arguments to an SQL query
477+
resp, err = client.Execute("SELECT id FROM SQL_TEST WHERE id=? AND name=?", []interface{}{2, "test"})
478+
fmt.Println("Execute")
479+
fmt.Println("Error", err)
480+
fmt.Println("Code", resp.Code)
481+
fmt.Println("Data", resp.Data)
482+
fmt.Println("MetaData", resp.MetaData)
483+
fmt.Println("SQL Info", resp.SQLInfo)
484+
485+
// the way to pass SQL expression with using custom packing/unpacking for a type
486+
var res []Tuple
487+
sqlInfo, metaData, err := client.ExecuteTyped("SELECT id, name, name FROM SQL_TEST WHERE id=?", []interface{}{2}, &res)
488+
fmt.Println("ExecuteTyped")
489+
fmt.Println("Error", err)
490+
fmt.Println("Data", res)
491+
fmt.Println("MetaData", metaData)
492+
fmt.Println("SQL Info", sqlInfo)
493+
494+
// for using different types of parameters (positioned/named), collect all items in []interface{}
495+
// all "named" items must be passed with tarantool.KeyValueBind{}
496+
resp, err = client.Execute("SELECT id FROM SQL_TEST WHERE id=:id AND name=?",
497+
[]interface{}{tarantool.KeyValueBind{"id", 1}, "test"})
498+
fmt.Println("Execute")
499+
fmt.Println("Error", err)
500+
fmt.Println("Code", resp.Code)
501+
fmt.Println("Data", resp.Data)
502+
fmt.Println("MetaData", resp.MetaData)
503+
fmt.Println("SQL Info", resp.SQLInfo)
504+
}

0 commit comments

Comments
 (0)