forked from mindstand/go-cypherdsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
return.go
86 lines (67 loc) · 1.43 KB
/
return.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
package go_cypherdsl
import (
"errors"
"fmt"
"strings"
)
type ReturnPart struct {
Name string
Type string
Alias string
Function *FunctionConfig
Literal interface{}
BooleanExpression WhereQuery
Path string
}
func (r *ReturnPart) ToString() (string, error) {
if r.Function != nil {
query, err := r.Function.ToString()
if err != nil {
return "", err
}
if r.Alias != "" {
query += fmt.Sprintf(" AS %s", r.Alias)
}
return query, nil
}
//handle literal
if r.Literal != nil {
return cypherizeInterface(r.Literal)
}
//handle boolean expression
if r.BooleanExpression != "" {
return string(r.BooleanExpression), nil
}
if r.Path != "" {
return r.Path, nil
}
if r.Name == "" {
return "", errors.New("name can not be empty")
}
//handle standard return
query := r.Name
if r.Type != "" {
query += fmt.Sprintf(".%s", r.Type)
}
if r.Alias != "" {
query += fmt.Sprintf(" AS %s", r.Alias)
}
return query, nil
}
func NewReturnClause(distinct bool, parts ...ReturnPart) (ReturnQuery, error) {
if len(parts) == 0 {
return "", errors.New("parts can not be empty")
}
query := "RETURN "
if distinct {
query += "DISTINCT "
}
for _, part := range parts {
partStr, err := part.ToString()
if err != nil {
return "", err
}
query += fmt.Sprintf("%s, ", partStr)
}
return ReturnQuery(strings.TrimSuffix(query, ", ")), nil
}