-
Notifications
You must be signed in to change notification settings - Fork 0
/
neo4j_builder.go
118 lines (107 loc) · 2.53 KB
/
neo4j_builder.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package neo4j_extended
import (
"errors"
"strconv"
)
//nodeOp any operation that does work with nodes-rel system
func nodeOp(key string, node *NeoNode, req *NeoRequest) (err error) {
cypherkey := key + " "
cypher, err := node.toCypher(req)
if err != nil {
return err
}
cypher = cypherkey + cypher
req.multiCypher = append(req.multiCypher, cypher)
return nil
}
//addFields adds Fields to cypher string
func getFieldsCypher(r *NeoRequest, fields *[]NeoField) (out string, err error) {
if fields == nil {
return "", nil
}
out = "{"
for _, field := range *fields {
//Check if parameter with same Name already exists
fieldName := field.Name
_, ok := r.params[field.Name]
i := 1
for ok {
field.Name = field.Name + strconv.Itoa(i)
i += 1
_, ok = r.params[field.Name]
}
//Default supported types
switch val := field.Val.(type) {
case int:
field.Val = val
if val != 0 {
out += fieldName + ": $" + field.Name + ", "
r.addParam(field)
}
case string:
field.Val = val
if val != "" {
out += fieldName + ": $" + field.Name + ", "
r.addParam(field)
}
case float64, float32:
field.Val = val
if val != 0 {
out += fieldName + ": $" + field.Name + ", "
r.addParam(field)
}
case []string:
field.Val = val
if len(val) > 0 {
out += fieldName + ": $" + field.Name + ", "
r.addParam(field)
}
default:
return "", errors.New("@AddCreate: wrong type")
}
}
out = trailingComma(out)
out += "}"
return out, nil
}
//add adds a cypher line
func (n *NeoRequest) add(cypher string, param NeoField) {
n.addCypher(cypher)
n.addParam(param)
}
//addCypher adds a cypher
func (n *NeoRequest) addCypher(cypher string) {
n.multiCypher = append(n.multiCypher, cypher)
}
//addCypherAll adds multiple cypher
func (n *NeoRequest) addCypherAll(multicypher []string) {
n.multiCypher = append(n.multiCypher, multicypher...)
}
//addParam adds a prameter
func (n *NeoRequest) addParam(param NeoField) {
n.params[param.Name] = param.Val
}
//addParamAll adds multiple params
func (n *NeoRequest) addParamAll(params []NeoField) {
for _, param := range params {
n.addParam(param)
}
}
//MultiCypherToCypher takes a cyper reduces it to one
func MultiCypherToCypher(multiCypher []string) (cypher string) {
for _, singleCypher := range multiCypher {
cypher = cypher + singleCypher + " "
}
return cypher
}
//trailingComma removes ", "
func trailingComma(s string) string {
//TODO Edge Case > 2 Analyse
if len(s) > 2 {
if s[len(s)-2:] == ", " {
s = s[:len(s)-2]
}
return s
}
return s
}