-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapScanCASexample.go
57 lines (46 loc) · 1.27 KB
/
mapScanCASexample.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"fmt"
"github.com/gocql/gocql"
)
func main() {
/*
create table example.my_lwt_table(pk int, version int, value text, PRIMARY KEY(pk));
*/
cluster := gocql.NewCluster("127.0.0.1", "127.0.0.2", "127.0.0.3")
cluster.Keyspace = "example"
cluster.ProtoVersion = 4
session, err := cluster.CreateSession()
if err != nil {
panic(err)
}
defer session.Close()
err = session.Query(`INSERT INTO example.my_lwt_table(pk, version, value) VALUES(?, ?, ?)`, 1, 1, "a").Exec()
if err != nil {
panic(err)
}
m := make(map[string]interface{})
applied, err := session.Query(`UPDATE example.my_lwt_table SET value=? WHERE pk=? IF version=?`, "b", 1, 0).MapScanCAS(m)
if err != nil {
panic(err)
}
fmt.Println(applied, m)
var value string
err = session.Query(`SELECT value FROM example.my_lwt_table WHERE pk=?`, 1).Scan(&value)
if err != nil {
panic(err)
}
fmt.Println(value)
m = make(map[string]interface{})
applied, err = session.Query(`UPDATE example.my_lwt_table SET value=? WHERE pk=? IF version=?`, "b", 1, 1).MapScanCAS(m)
if err != nil {
panic(err)
}
fmt.Println(applied, m)
var value2 string
err = session.Query(`SELECT value FROM example.my_lwt_table WHERE pk=?`, 1).Scan(&value2)
if err != nil {
panic(err)
}
fmt.Println(value2)
}