Skip to content

Commit 341b74d

Browse files
committed
readme: add examples of usage SQL in connector
Added examples of using SQL queries in connector with different types of arguments in README.md. Follows up #62
1 parent 5911024 commit 341b74d

File tree

1 file changed

+106
-3
lines changed

1 file changed

+106
-3
lines changed

README.md

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,6 @@ import (
184184
)
185185

186186
func main() {
187-
spaceNo := uint32(512)
188-
indexNo := uint32(0)
189-
190187
server := "127.0.0.1:3013"
191188
opts := tarantool.Opts{
192189
Timeout: 500 * time.Millisecond,
@@ -285,6 +282,112 @@ func main() {
285282
}
286283
```
287284

285+
To use SQL to query a tarantool instance, call `Execute`.
286+
287+
Pay attention that with different types of queries (DDL, DQL, DML etc.)
288+
some fields of the response structure (`MetaData` and `InfoAutoincrementIds` in `SQLInfo`) may be nil.
289+
290+
See the [protocol](https://www.tarantool.io/en/doc/latest/dev_guide/internals/box_protocol/#responses-for-sql) explanation for details.
291+
292+
```go
293+
package main
294+
295+
import (
296+
"github.com/tarantool/go-tarantool"
297+
"log"
298+
"time"
299+
)
300+
301+
func main() {
302+
server := "127.0.0.1:3013"
303+
opts := tarantool.Opts{
304+
Timeout: 500 * time.Millisecond,
305+
Reconnect: 1 * time.Second,
306+
MaxReconnects: 3,
307+
User: "test",
308+
Pass: "test",
309+
}
310+
client, err := tarantool.Connect(server, opts)
311+
if err != nil {
312+
log.Fatalf("Failed to connect: %s", err.Error())
313+
}
314+
315+
resp, err := client.Execute("CREATE TABLE SQL_TEST (id INTEGER PRIMARY KEY, name STRING)")
316+
log.Println("Execute")
317+
log.Println("Error", err)
318+
log.Println("Code", resp.Code)
319+
log.Println("Data", resp.Data)
320+
log.Println("MetaData", resp.MetaData)
321+
log.Println("SQL Info", resp.SQLInfo)
322+
323+
// there are 3 options to pass named parameters to an SQL query
324+
sqlBind1 := map[string]interface{} {
325+
"id": 1,
326+
"name": "test",
327+
}
328+
329+
sqlBind2 := struct {
330+
Id int
331+
Name string
332+
} {1, "test"}
333+
334+
type kv struct {
335+
Key string
336+
Value interface{}
337+
}
338+
339+
sqlBind3 := []kv {
340+
kv{"id", 1},
341+
kv{"name", "test"},
342+
}
343+
344+
// the next usage
345+
resp, err = client.Execute("SELECT id FROM SQL_TEST WHERE id=:id AND name=:name", sqlBind1)
346+
log.Println("Execute")
347+
log.Println("Error", err)
348+
log.Println("Code", resp.Code)
349+
log.Println("Data", resp.Data)
350+
log.Println("MetaData", resp.MetaData)
351+
log.Println("SQL Info", resp.SQLInfo)
352+
353+
// the same as
354+
resp, err = client.Execute("SELECT id FROM SQL_TEST WHERE id=:id AND name=:name", sqlBind2)
355+
log.Println("Execute")
356+
log.Println("Error", err)
357+
log.Println("Code", resp.Code)
358+
log.Println("Data", resp.Data)
359+
log.Println("MetaData", resp.MetaData)
360+
log.Println("SQL Info", resp.SQLInfo)
361+
362+
// the same as
363+
resp, err = client.Execute("SELECT id FROM SQL_TEST WHERE id=:id AND name=:name", sqlBind3)
364+
log.Println("Execute")
365+
log.Println("Error", err)
366+
log.Println("Code", resp.Code)
367+
log.Println("Data", resp.Data)
368+
log.Println("MetaData", resp.MetaData)
369+
log.Println("SQL Info", resp.SQLInfo)
370+
371+
// there are 2 options to pass positional arguments to an SQL query
372+
resp, err = client.Execute("SELECT id FROM SQL_TEST WHERE id=? AND name=?", 1, "test")
373+
log.Println("Execute")
374+
log.Println("Error", err)
375+
log.Println("Code", resp.Code)
376+
log.Println("Data", resp.Data)
377+
log.Println("MetaData", resp.MetaData)
378+
log.Println("SQL Info", resp.SQLInfo)
379+
380+
// the same as
381+
resp, err = client.Execute("SELECT id FROM SQL_TEST WHERE id=? AND name=?", []interface{}{2, "test"})
382+
log.Println("Execute")
383+
log.Println("Error", err)
384+
log.Println("Code", resp.Code)
385+
log.Println("Data", resp.Data)
386+
log.Println("MetaData", resp.MetaData)
387+
log.Println("SQL Info", resp.SQLInfo)
388+
}
389+
```
390+
288391
To enable support of UUID in msgpack with [google/uuid](https://github.com/google/uuid),
289392
import tarantool/uuid submodule.
290393
```go

0 commit comments

Comments
 (0)