-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
128 lines (104 loc) · 2.66 KB
/
server.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
package main
import (
"bufio"
"fmt"
"net"
"strings"
)
type Table struct {
Name string
Columns []string
Rows [][]string
}
var database = make(map[string]*Table)
func main() {
listener, err := net.Listen("tcp", ":8080")
if err != nil {
fmt.Println("Error when start the server: ", err)
return
}
defer listener.Close()
fmt.Println("Server is running at 8080...")
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Connection error ", err)
continue
}
go handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
defer conn.Close()
fmt.Println("CLient connected", conn.RemoteAddr())
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
query := scanner.Text()
fmt.Println("Query received: ", query)
response := processQuery(query)
conn.Write([]byte(response + "\n"))
}
fmt.Println("Client disconnected: ", conn.RemoteAddr())
}
func processQuery(query string) string {
query = strings.TrimSpace(query)
parts := strings.Fields(query)
if len(parts) == 0 {
return "Error: query is empty"
}
command := strings.ToUpper(parts[0])
switch command {
case "CREATE":
return createTable(parts[1:])
case "INSERT":
return insertIntoTable(parts[1:])
case "SELECT":
return selectFromTable(parts[1:])
default:
return "Error: command not found"
}
}
func createTable(args []string) string {
if len(args) < 2 || args[0] != "TABLE" {
return "Error: args are invalid, Use: CREATE TABLE name (col1, col2, ..."
}
tableName := args[1]
columns := args[2:]
database[tableName] = &Table{
Name: tableName,
Columns: columns,
Rows: [][]string{},
}
return fmt.Sprintf("Table '%s' created with columns: %v", tableName, columns)
}
func insertIntoTable(args []string) string {
if len(args) < 4 || args[0] != "INTO" || args[2] != "VALUES" {
return "Error: query invalid, Use INSERT INTO name VALUES (value1, value2, ....)"
}
tableName := args[1]
values := args[3:]
table, exists := database[tableName]
if !exists {
return fmt.Sprintf("Error: Table '%s' not found", tableName)
}
if len(values) != len(table.Columns) {
return fmt.Sprintf("Error: number of values does not match")
}
table.Rows = append(table.Rows, values)
return "insert success fully"
}
func selectFromTable(args []string) string {
if len(args) < 3 || args[0] !="*" || args[1] != "FROM" {
return "Error: invalid query, use: SELECT * FROM name"
}
tableName := args[2]
table, exits := database[tableName]
if !exits {
return fmt.Sprintf("Error: table '%s' not found", tableName)
}
result := fmt.Sprintf("Table: %s\nColumns: %v\n", tableName, table.Columns)
for _, row := range table.Rows {
result += fmt.Sprintf("%v\n", row)
}
return result
}