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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
package query
import (
"fmt"
"strings"
)
const MAX_CLAUSE_DEPTH int = 16
func (stmt Statement) Compile(b *strings.Builder) (*string, error) {
if stmt.Negated {
b.WriteString("NOT ")
}
switch stmt.Category {
case CAT_TITLE:
b.WriteString("title ")
case CAT_AUTHOR:
b.WriteString("author ")
case CAT_DATE:
b.WriteString("date ")
case CAT_FILETIME:
b.WriteString("fileTime ")
case CAT_TAGS:
b.WriteString("tags ")
case CAT_LINKS:
b.WriteString("links ")
default:
return nil, &CompileError{
fmt.Sprint("unknown or invalid category ", stmt.Category.String()),
}
}
switch stmt.Operator {
case OP_EQ:
if stmt.Category.IsSet() {
b.WriteString("IN ")
} else {
b.WriteString("= ")
}
case OP_AP:
b.WriteString("LIKE ")
case OP_NE:
b.WriteString("!= ")
case OP_LT:
b.WriteString("< ")
case OP_LE:
b.WriteString("<= ")
case OP_GE:
b.WriteString(">= ")
case OP_GT:
b.WriteString("> ")
default:
return nil, &CompileError{
fmt.Sprint("unknown or invalid operand ", stmt.Operator.String()),
}
}
switch stmt.Value.Type() {
case VAL_STR:
s, ok := stmt.Value.(StringValue)
if !ok {
panic(CompileError{"type corruption in string value"})
}
b.WriteString("(?) ")
return &s.S, nil
case VAL_DATETIME:
dt, ok := stmt.Value.(DatetimeValue)
if !ok {
panic(CompileError{"type corruption in datetime value"})
}
fmt.Fprint(b, dt.D.Unix(), " ")
default:
return nil, &CompileError{
fmt.Sprint("unknown or invalid value type ", stmt.Value.Type()),
}
}
return nil, nil
}
func (stmts Statements) Compile(b *strings.Builder, delim string) ([]string, error) {
var args []string
// TODO: handle meta category
for i, stmt := range stmts {
if i != 0 {
b.WriteString(delim)
}
b.WriteByte(' ')
arg, err := stmt.Compile(b)
if err != nil {
return nil, err
} else if arg != nil {
args = append(args, *arg)
}
}
return args, nil
}
func (root Clause) Compile() (string, []string, error) {
if d := root.Depth(); d > MAX_CLAUSE_DEPTH {
return "", nil, &CompileError{
fmt.Sprint("exceeded maximum clause depth of 8: ", d),
}
}
b := strings.Builder{}
args, err := root.buildCompile(&b)
if err != nil {
return "", nil, err
}
return b.String(), args, nil
}
func (c Clause) buildCompile(b *strings.Builder) ([]string, error) {
b.WriteString("( ")
var delim string
switch cop := c.Operator; cop {
case COP_AND:
delim = "AND"
case COP_OR:
delim = "OR"
default:
return nil, &CompileError{fmt.Sprint("invalid clause operator ", cop)}
}
args, err := c.Statements.Compile(b, delim)
if err != nil {
return nil, err
}
for _, clause := range c.Clauses {
b.WriteString(delim)
b.WriteByte(' ')
newArgs, err := clause.buildCompile(b)
if err != nil {
return nil, err
} else if newArgs != nil {
args = append(args, newArgs...)
}
}
b.WriteString(") ")
return args, nil
}
|