forked from mindstand/go-cypherdsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
with.go
71 lines (56 loc) · 1.19 KB
/
with.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
package go_cypherdsl
import (
"errors"
"fmt"
"strings"
)
//with is basically a sort
type WithConfig struct {
Parts []WithPart
}
func (w *WithConfig) ToString() (string, error) {
if w.Parts == nil || len(w.Parts) == 0 {
return "", errors.New("parts can not be empty")
}
query := ""
for _, part := range w.Parts {
partQuery, err := part.ToString()
if err != nil {
return "", err
}
query += fmt.Sprintf("%s, ", partQuery)
}
return strings.TrimSuffix(query, ", "), nil
}
//todo distinct
type WithPart struct {
Function *FunctionConfig
Name string
Field string
As string
}
func (wp *WithPart) ToString() (string, error) {
query := ""
var err error
if wp.Function != nil {
//make sure nothing else is defined
if wp.Name != "" || wp.Field != "" {
return "", errors.New("can not define name or field with a function")
}
query, err = wp.Function.ToString()
if err != nil {
return "", err
}
} else if wp.Name != "" {
query = wp.Name
if wp.Field != "" {
query += fmt.Sprintf(".%s", wp.Field)
}
} else {
return "", errors.New("must define a function or name")
}
if wp.As != "" {
query += fmt.Sprintf(" AS %s", wp.As)
}
return query, nil
}