-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
121 lines (109 loc) · 3.29 KB
/
Copy pathmain.go
File metadata and controls
121 lines (109 loc) · 3.29 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"fmt"
lbug "github.com/LadybugDB/go-ladybug"
)
// To run this example with Option 2 (local library download), use:
//
// 1. Download libraries into this directory:
// go generate ./...
//
// 2. Build/Run with the system_ladybug tag:
// go run -tags system_ladybug main.go
//
// With Option 1 (go.work), no tag is needed - just: go run main.go
//go:generate sh -c "curl -fsSL https://raw.githubusercontent.com/LadybugDB/ladybug/refs/heads/main/scripts/download-liblbug.sh | LBUG_TARGET_DIR=$(pwd)/lib-ladybug bash"
//go:generate sh -c "[ \"$(uname)\" = Darwin ] && ln -sf liblbug.dylib lib-ladybug/liblbug.0.dylib || ln -sf liblbug.so lib-ladybug/liblbug.so.0"
/*
#cgo darwin LDFLAGS: -L${SRCDIR}/lib-ladybug -Wl,-rpath,${SRCDIR}/lib-ladybug
#cgo linux LDFLAGS: -L${SRCDIR}/lib-ladybug -Wl,-rpath,${SRCDIR}/lib-ladybug
#cgo windows LDFLAGS: -L${SRCDIR}/lib-ladybug
*/
import "C"
func main() {
// Use an in-memory database for demonstration.
dbPath := ":memory:"
// Open a database with default system configuration.
systemConfig := lbug.DefaultSystemConfig()
systemConfig.BufferPoolSize = 1024 * 1024 * 1024
db, err := lbug.OpenDatabase(dbPath, systemConfig)
if err != nil {
panic(err)
}
defer db.Close()
// Open a connection to the database.
conn, err := lbug.OpenConnection(db)
if err != nil {
panic(err)
}
defer conn.Close()
// Set up the schema and load data.
queries := []string{
"CREATE NODE TABLE User(name STRING, age INT64, PRIMARY KEY (name))",
"CREATE NODE TABLE City(name STRING, population INT64, PRIMARY KEY (name))",
"CREATE REL TABLE Follows(FROM User TO User, since INT64)",
"CREATE REL TABLE LivesIn(FROM User TO City)",
"COPY User FROM \"../dataset/demo-db/user.csv\"",
"COPY City FROM \"../dataset/demo-db/city.csv\"",
"COPY Follows FROM \"../dataset/demo-db/follows.csv\"",
"COPY LivesIn FROM \"../dataset/demo-db/lives-in.csv\"",
}
for _, query := range queries {
fmt.Println("Executing query:", query)
queryResult, err := conn.Query(query)
if err != nil {
panic(err)
}
defer queryResult.Close()
}
query := "MATCH (a:User)-[e:Follows]->(b:User) RETURN a.name, e.since, b.name"
println("Executing query:", query)
// Execute a query and print the result.
result, err := conn.Query(query)
if err != nil {
panic(err)
}
defer result.Close()
for result.HasNext() {
tuple, err := result.Next()
if err != nil {
panic(err)
}
defer tuple.Close()
// The result is a tuple, which can be converted to a slice or a map.
slice, err := tuple.GetAsSlice()
if err != nil {
panic(err)
}
fmt.Println(slice)
m, err := tuple.GetAsMap()
if err != nil {
panic(err)
}
fmt.Println(m)
}
// Execute a query with parameters.
query = "MATCH (a:User) WHERE a.name = $name RETURN a.age"
println("Executing query:", query)
preparedStatement, err := conn.Prepare(query)
if err != nil {
panic(err)
}
defer preparedStatement.Close()
args := map[string]interface{}{"name": "Adam"}
result, err = conn.Execute(preparedStatement, args)
if err != nil {
panic(err)
}
defer result.Close()
for result.HasNext() {
tuple, err := result.Next()
if err != nil {
panic(err)
}
defer tuple.Close()
// The tuple can also be converted to a string.
fmt.Print(tuple.GetAsString())
}
fmt.Println("All queries executed successfully.")
}