-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
95 lines (80 loc) · 1.59 KB
/
query.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
package main
import (
"strconv"
"strings"
)
type Query interface {
Execute(Database) []string
}
type EmptyQuery struct {
}
func (q EmptyQuery) Execute(_ Database) []string {
return []string{}
}
type ListQuery struct {
Key string
}
func (q ListQuery) Execute(database Database) []string {
return database.List(q.Key)
}
type SelectQuery struct {
Key string
}
func (q SelectQuery) Execute(database Database) []string {
return database.Select(q.Key)
}
type UpdateQuery struct {
Key string
Value string
}
func (q UpdateQuery) Execute(database Database) []string {
database.Update(q.Key, q.Value)
return []string{}
}
type IncrementQuery struct {
Key string
Value float64
}
func (q IncrementQuery) Execute(database Database) []string {
database.Increment(q.Key, q.Value)
return []string{}
}
type AppendQuery struct {
Key string
Value string
}
func (q AppendQuery) Execute(database Database) []string {
database.Append(q.Key, q.Value)
return []string{}
}
func ParseQuery(query string) Query {
i := strings.Index(query, " ")
if i < 0 {
return EmptyQuery{}
}
operation := query[:i]
key := query[i+1:]
if operation == "list" {
return ListQuery{key}
}
if operation == "select" {
return SelectQuery{key}
}
i = strings.Index(key, " ")
if i < 0 {
return EmptyQuery{}
}
value := key[i+1:]
key = key[:i]
if operation == "update" {
return UpdateQuery{key, value}
}
if operation == "increment" {
numericValue, _ := strconv.ParseFloat(value, 64)
return IncrementQuery{key, numericValue}
}
if operation == "append" {
return AppendQuery{key, value}
}
return EmptyQuery{}
}