|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/tarantool/go-tarantool/v2" |
| 7 | + _ "github.com/tarantool/go-tarantool/v2/datetime" |
| 8 | + _ "github.com/tarantool/go-tarantool/v2/decimal" |
| 9 | + _ "github.com/tarantool/go-tarantool/v2/uuid" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +func main() { |
| 14 | + // Connect to the database |
| 15 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 16 | + defer cancel() |
| 17 | + dialer := tarantool.NetDialer{ |
| 18 | + Address: "127.0.0.1:3301", |
| 19 | + User: "sampleuser", |
| 20 | + Password: "123456", |
| 21 | + } |
| 22 | + opts := tarantool.Opts{ |
| 23 | + Timeout: time.Second, |
| 24 | + } |
| 25 | + |
| 26 | + conn, err := tarantool.Connect(ctx, dialer, opts) |
| 27 | + if err != nil { |
| 28 | + fmt.Println("Connection refused:", err) |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + // Interact with the database |
| 33 | + // ... |
| 34 | + // Insert data |
| 35 | + tuples := [][]interface{}{ |
| 36 | + {1, "Roxette", 1986}, |
| 37 | + {2, "Scorpions", 1965}, |
| 38 | + {3, "Ace of Base", 1987}, |
| 39 | + {4, "The Beatles", 1960}, |
| 40 | + } |
| 41 | + var futures []*tarantool.Future |
| 42 | + for _, tuple := range tuples { |
| 43 | + request := tarantool.NewInsertRequest("bands").Tuple(tuple) |
| 44 | + futures = append(futures, conn.Do(request)) |
| 45 | + } |
| 46 | + fmt.Println("Inserted tuples:") |
| 47 | + for _, future := range futures { |
| 48 | + result, err := future.Get() |
| 49 | + if err != nil { |
| 50 | + fmt.Println("Got an error:", err) |
| 51 | + } else { |
| 52 | + fmt.Println(result) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Select by primary key |
| 57 | + data, err := conn.Do( |
| 58 | + tarantool.NewSelectRequest("bands"). |
| 59 | + Limit(10). |
| 60 | + Iterator(tarantool.IterEq). |
| 61 | + Key([]interface{}{uint(1)}), |
| 62 | + ).Get() |
| 63 | + if err != nil { |
| 64 | + fmt.Println("Got an error:", err) |
| 65 | + } |
| 66 | + fmt.Println("Tuple selected by the primary key value:", data) |
| 67 | + |
| 68 | + // Select by secondary key |
| 69 | + data, err = conn.Do( |
| 70 | + tarantool.NewSelectRequest("bands"). |
| 71 | + Index("band"). |
| 72 | + Limit(10). |
| 73 | + Iterator(tarantool.IterEq). |
| 74 | + Key([]interface{}{"The Beatles"}), |
| 75 | + ).Get() |
| 76 | + if err != nil { |
| 77 | + fmt.Println("Got an error:", err) |
| 78 | + } |
| 79 | + fmt.Println("Tuple selected by the secondary key value:", data) |
| 80 | + |
| 81 | + // Update |
| 82 | + data, err = conn.Do( |
| 83 | + tarantool.NewUpdateRequest("bands"). |
| 84 | + Key(tarantool.IntKey{2}). |
| 85 | + Operations(tarantool.NewOperations().Assign(1, "Pink Floyd")), |
| 86 | + ).Get() |
| 87 | + if err != nil { |
| 88 | + fmt.Println("Got an error:", err) |
| 89 | + } |
| 90 | + fmt.Println("Updated tuple:", data) |
| 91 | + |
| 92 | + // Upsert |
| 93 | + data, err = conn.Do( |
| 94 | + tarantool.NewUpsertRequest("bands"). |
| 95 | + Tuple([]interface{}{uint(5), "The Rolling Stones", 1962}). |
| 96 | + Operations(tarantool.NewOperations().Assign(1, "The Doors")), |
| 97 | + ).Get() |
| 98 | + if err != nil { |
| 99 | + fmt.Println("Got an error:", err) |
| 100 | + } |
| 101 | + |
| 102 | + // Replace |
| 103 | + data, err = conn.Do( |
| 104 | + tarantool.NewReplaceRequest("bands"). |
| 105 | + Tuple([]interface{}{1, "Queen", 1970}), |
| 106 | + ).Get() |
| 107 | + if err != nil { |
| 108 | + fmt.Println("Got an error:", err) |
| 109 | + } |
| 110 | + fmt.Println("Replaced tuple:", data) |
| 111 | + |
| 112 | + // Delete |
| 113 | + data, err = conn.Do( |
| 114 | + tarantool.NewDeleteRequest("bands"). |
| 115 | + Key([]interface{}{uint(5)}), |
| 116 | + ).Get() |
| 117 | + if err != nil { |
| 118 | + fmt.Println("Got an error:", err) |
| 119 | + } |
| 120 | + fmt.Println("Deleted tuple:", data) |
| 121 | + |
| 122 | + // Call |
| 123 | + data, err = conn.Do( |
| 124 | + tarantool.NewCallRequest("get_bands_older_than").Args([]interface{}{1966}), |
| 125 | + ).Get() |
| 126 | + if err != nil { |
| 127 | + fmt.Println("Got an error:", err) |
| 128 | + } |
| 129 | + fmt.Println("Stored procedure result:", data) |
| 130 | + |
| 131 | + // Close connection |
| 132 | + conn.CloseGraceful() |
| 133 | + fmt.Println("Connection is closed") |
| 134 | +} |
0 commit comments