-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
319 lines (283 loc) · 6.67 KB
/
main.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package main
import (
"encoding/csv"
"flag"
"fmt"
"os"
"strconv"
"strings"
)
type SQLNode struct {
Type string
Value string
Left string
Operator string
Right string
}
type Table struct {
Name string
Headers []string
Rows [][]string
}
var printAST = flag.Bool("ast", false, "Print the Abstract Syntax Tree")
func main() {
// Define the flag
// Parse the flags
flag.Parse()
// Check if we have the correct number of arguments
if flag.NArg() != 1 {
fmt.Println("Usage: ./gosql [-ast] \"SQL QUERY\"")
fmt.Println("SELECT, FROM, WHERE, and LIMIT are supported.")
return
}
sqlQuery := flag.Arg(0)
// Parse the SQL query
ast, err := parseSQL(sqlQuery)
if err != nil {
fmt.Printf("Error parsing SQL: %v\n", err)
return
}
// If the -ast flag is set, print the AST
if *printAST {
fmt.Println("Abstract Syntax Tree:")
for _, node := range ast {
fmt.Printf("%+v\n", node)
}
}
result, err := executeQuery(ast)
if err != nil {
fmt.Printf("Error executing query: %v\n", err)
return
}
for _, row := range result {
fmt.Println(strings.Join(row, ", "))
}
}
func executeQuery(ast []SQLNode) ([][]string, error) {
var table *Table
var limit int = -1
var selectedColumns []string
var whereClause *SQLNode
for _, node := range ast {
switch node.Type {
case "SELECT":
if node.Value == "*" {
selectedColumns = nil // nil means select all columns
} else {
selectedColumns = strings.Split(node.Value, ", ")
}
case "FROM":
tableName := node.Value + ".csv"
loadedTable, err := loadCSVTable(tableName)
if err != nil {
return nil, fmt.Errorf("file '%s.csv' not found", node.Value)
}
table = loadedTable
case "WHERE":
whereClause = &node
case "LIMIT":
var err error
limit, err = strconv.Atoi(node.Value)
if err != nil {
return nil, fmt.Errorf("invalid LIMIT value: %s", node.Value)
}
if limit < 0 {
return nil, fmt.Errorf("LIMIT value must be non-negative")
}
}
}
if table == nil {
return nil, fmt.Errorf("no FROM clause found or table could not be loaded")
}
var columnIndices []int
if selectedColumns == nil {
// Select all columns
columnIndices = make([]int, len(table.Headers))
for i := range columnIndices {
columnIndices[i] = i
}
} else {
// Select specific columns
for _, col := range selectedColumns {
found := false
for i, header := range table.Headers {
if col == header {
columnIndices = append(columnIndices, i)
found = true
break
}
}
if !found {
return nil, fmt.Errorf("column %s not found in table", col)
}
}
}
// Prepare result with selected columns
result := make([][]string, 0, len(table.Rows)+1)
headerRow := make([]string, len(columnIndices))
for i, idx := range columnIndices {
headerRow[i] = table.Headers[idx]
}
result = append(result, headerRow)
// Add data rows with selected columns
for _, row := range table.Rows {
if whereClause != nil {
match, err := evaluateWhereClause(whereClause, table.Headers, row)
if err != nil {
return nil, err
}
if !match {
continue
}
}
if limit != -1 && len(result)-1 >= limit {
break
}
newRow := make([]string, len(columnIndices))
for i, idx := range columnIndices {
newRow[i] = row[idx]
}
result = append(result, newRow)
}
return result, nil
}
func evaluateWhereClause(node *SQLNode, headers []string, row []string) (bool, error) {
if node.Operator != "=" {
return false, fmt.Errorf("only equality (=) operator is supported in WHERE clause")
}
leftIndex := -1
for i, header := range headers {
if header == node.Left {
leftIndex = i
break
}
}
if leftIndex == -1 {
return false, fmt.Errorf("column %s not found in table", node.Left)
}
leftValue := row[leftIndex]
return leftValue == node.Right, nil
}
func loadCSVTable(filename string) (*Table, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
reader := csv.NewReader(file)
records, err := reader.ReadAll()
if err != nil {
return nil, err
}
if len(records) == 0 {
return nil, fmt.Errorf("empty CSV file")
}
// Uppercase all headers
for i, header := range records[0] {
records[0][i] = strings.ToUpper(header)
}
return &Table{
Name: strings.TrimSuffix(filename, ".csv"),
Headers: records[0],
Rows: records[1:],
}, nil
}
func parseSQL(sql string) ([]SQLNode, error) {
tokens := tokenize(sql)
ast := []SQLNode{}
for i := 0; i < len(tokens); {
token := strings.ToUpper(tokens[i])
switch token {
case "SELECT":
node := SQLNode{Type: token}
i++
values := []string{}
for i < len(tokens) && strings.ToUpper(tokens[i]) != "FROM" {
if tokens[i] != "," {
values = append(values, strings.ToUpper(tokens[i]))
}
i++
}
node.Value = strings.Join(values, ", ")
ast = append(ast, node)
case "FROM":
node := SQLNode{Type: token}
i++
values := []string{}
for i < len(tokens) && !isKeyword(tokens[i]) {
values = append(values, tokens[i])
i++
}
node.Value = strings.Join(values, " ")
ast = append(ast, node)
case "WHERE":
node := SQLNode{Type: token}
i++
if i+2 < len(tokens) {
node.Left = strings.ToUpper(tokens[i])
node.Operator = tokens[i+1]
node.Right = tokens[i+2]
node.Right = strings.Trim(node.Right, "'")
i += 3
}
ast = append(ast, node)
case "LIMIT":
node := SQLNode{Type: token}
i++
if i < len(tokens) {
node.Value = tokens[i]
i++
} else {
return nil, fmt.Errorf("LIMIT clause requires a value")
}
ast = append(ast, node)
default:
return nil, fmt.Errorf("unexpected token: %s", token)
}
}
return ast, nil
}
func tokenize(sql string) []string {
var tokens []string
var currentToken strings.Builder
inQuotes := false
for _, char := range sql {
switch {
case char == '\'':
inQuotes = !inQuotes
currentToken.WriteRune(char)
case char == ' ' && !inQuotes:
if currentToken.Len() > 0 {
tokens = append(tokens, currentToken.String())
currentToken.Reset()
}
case char == ',' && !inQuotes:
if currentToken.Len() > 0 {
tokens = append(tokens, currentToken.String())
currentToken.Reset()
}
tokens = append(tokens, ",")
case char == '=' && !inQuotes:
if currentToken.Len() > 0 {
tokens = append(tokens, currentToken.String())
currentToken.Reset()
}
tokens = append(tokens, "=")
default:
currentToken.WriteRune(char)
}
}
if currentToken.Len() > 0 {
tokens = append(tokens, currentToken.String())
}
return tokens
}
func isKeyword(token string) bool {
keywords := []string{"SELECT", "FROM", "WHERE", "LIMIT"}
for _, keyword := range keywords {
if strings.ToUpper(token) == keyword {
return true
}
}
return false
}