Skip to content

Commit 6249bdd

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 06b42c3 commit 6249bdd

File tree

1 file changed

+105
-3
lines changed

1 file changed

+105
-3
lines changed

example_test.go

+105-3
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"
@@ -42,7 +43,7 @@ func ExampleConnection_Select() {
4243
return
4344
}
4445
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)})
4647
if err != nil {
4748
fmt.Printf("error in select is %v", err)
4849
return
@@ -68,7 +69,7 @@ func ExampleConnection_SelectTyped() {
6869
}
6970
defer conn.Close()
7071
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)
7273
if err != nil {
7374
fmt.Printf("error in select is %v", err)
7475
return
@@ -86,7 +87,7 @@ func ExampleConnection_SelectTyped() {
8687
}
8788

8889
func Example() {
89-
spaceNo := uint32(512)
90+
spaceNo := uint32(517)
9091
indexNo := uint32(0)
9192

9293
server := "127.0.0.1:3013"
@@ -222,3 +223,104 @@ func Example() {
222223
// Fut 2 Error <nil>
223224
// Fut 2 Data [[15 val 15 bla]]
224225
}
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

Comments
 (0)