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