forked from mindstand/go-cypherdsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.go
96 lines (72 loc) · 2.03 KB
/
create.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
package go_cypherdsl
import (
"errors"
"fmt"
"strings"
)
func NewNode(builder *PathBuilder) (CreateQuery, error) {
if builder == nil {
return "", errors.New("builder can not be nil")
}
query, err := builder.ToCypher()
if err != nil {
return "", err
}
if query == "" {
return "", errors.New("query can not be empty")
}
return CreateQuery(query), nil
}
type IndexConfig struct {
Type string
Fields []string
}
func NewIndex(index *IndexConfig) (CreateQuery, error) {
if index == nil {
return "", errors.New("index can not be nil")
}
if index.Type == "" {
return "", errors.New("type can not be empty")
}
if index.Fields == nil {
return "", errors.New("fields can not be nil")
}
if len(index.Fields) == 0 {
return "", errors.New("fields can not be empty")
}
query := fmt.Sprintf("INDEX ON :%s(", index.Type)
for _, field := range index.Fields {
query += fmt.Sprintf("%s,", field)
}
return CreateQuery(strings.TrimSuffix(query, ",") + ")"), nil
}
type ConstraintConfig struct {
//specify the name of the variable for the constraint
Name string
//specify the type the action takes place on
Type string
//specify the field the action takes place on
Field string
//require field to be unique
Unique bool
//require field to show up
Exists bool
}
func NewConstraint(constraint *ConstraintConfig) (CreateQuery, error) {
if constraint == nil {
return "", errors.New("constraint can not be nil")
}
if constraint.Name == "" || constraint.Type == "" || constraint.Field == "" {
return "", errors.New("name, type and field can not be empty")
}
if constraint.Unique == constraint.Exists || (!constraint.Unique && !constraint.Exists) {
return "", errors.New("can only be unique or exists per call")
}
root := fmt.Sprintf("CONSTRAINT ON (%s:%s) ASSERT ", constraint.Name, constraint.Type)
if constraint.Unique {
root += fmt.Sprintf("%s.%s IS UNIQUE", constraint.Name, constraint.Field)
} else {
root += fmt.Sprintf("exists(%s.%s)", constraint.Name, constraint.Field)
}
return CreateQuery(root), nil
}