-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
102 lines (93 loc) · 2.45 KB
/
main.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
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
package main
import (
"fmt"
"os"
"github.com/kuzudb/go-kuzu"
)
func main() {
dbPath := "example_db"
os.RemoveAll(dbPath)
// Open a database with default system configuration.
systemConfig := kuzu.DefaultSystemConfig()
systemConfig.BufferPoolSize = 1024 * 1024 * 1024
db, err := kuzu.OpenDatabase(dbPath, systemConfig)
if err != nil {
panic(err)
}
defer db.Close()
// Open a connection to the database.
conn, err := kuzu.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.")
}