Skip to content

Commit 4b2f89c

Browse files
committed
fix bug in miniSQL!!
1 parent f109a4a commit 4b2f89c

5 files changed

Lines changed: 67 additions & 45 deletions

File tree

ClientServer/main.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ func main() {
1717
}
1818

1919
// ToDo:直接返回一个查询体,主要需要解析出来 ①查询的table名称 ②执行的操作类型
20-
func parser(input string) regionRPC.SQLRst {
20+
func parser(input string) (regionRPC.SQLRst, bool) {
2121
rst := regionRPC.SQLRst{}
2222
word := strings.Split(input, " ")
2323
// [检查]是否以分号结尾
2424
if input[len(input)-1] != ';' {
2525
fmt.Println("SQL语句没有以分号结尾!")
26+
return rst, false
2627
}
2728
// 通过第一个word判断操作类型
2829
if word[0] == "select" || word[0] == "insert" || word[0] == "delete" || word[0] == "update" {
@@ -60,6 +61,23 @@ func parser(input string) regionRPC.SQLRst {
6061
rst.SQLtype = "create_table"
6162
} else {
6263
fmt.Println("错误的操作符!")
64+
return rst, false
65+
}
66+
return rst, true
67+
}
68+
69+
// 真正尝试运行SQL的程序
70+
func runSQL(input string) {
71+
// 1. 得到解析后的SQL内容
72+
sqlRst, ok := parser(input)
73+
if ok {
74+
// 2. 尝试在本地查找相关的表
75+
for _, table := range global.TableCache {
76+
if table.Name == sqlRst.Table {
77+
fmt.Println("table '" + table.Name + "' 在本地有缓存")
78+
}
79+
}
80+
} else {
81+
fmt.Println("错误的SQL语句")
6382
}
64-
return rst
6583
}

ClientServer/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func Test_parser(t *testing.T) {
1616
"create table ttt;",
1717
}
1818
for i, sql := range testSQL {
19-
s := parser(sql)
19+
s, _ := parser(sql)
2020
fmt.Println("-------测试第" + strconv.Itoa(i) + "条语句-------")
2121
fmt.Println("SQL语句为:" + s.SQL)
2222
fmt.Println("SQL的类型为:" + s.SQLtype)

ClientServer/rpcManager/region/ClientService_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var client *CliServiceClient
1212
func TestMain(m *testing.M) {
1313
mylog.LogInputChan = mylog.LogStart()
1414
config.BuildConfig()
15-
client, _ = DialService("tcp", "10.162.19.119:"+config.Configs.Region_port)
15+
client, _ = DialService("tcp", "192.168.31.68:"+config.Configs.Region_port)
1616
m.Run()
1717
}
1818

RegionServer/src/RegionManagers/main.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
config "region/utils/ConfigSystem"
1010
mylog "region/utils/LogSystem"
1111
"region/utils/global"
12-
"strconv"
1312
)
1413

1514
func main() {
@@ -26,7 +25,13 @@ func main() {
2625
global.SQLOutput = make(chan string, 1)
2726
go miniSQL.Start(global.SQLInput, global.SQLOutput)
2827

29-
buildSQL()
28+
// buildSQL()
29+
30+
global.SQLInput <- "use database aaa;"
31+
fmt.Println(1)
32+
res := <-global.SQLOutput
33+
fmt.Println(2)
34+
fmt.Println(res)
3035

3136
// 注册rpc服务
3237
rpc.RpcM2R, _ = rpc.DialReportService("tcp", global.MasterIP+":"+config.Configs.Rpc_M2R_port)
@@ -41,20 +46,20 @@ func main() {
4146
}
4247
}
4348

44-
func buildSQL() {
45-
// global.SQLInput <- "create database aaa;"
46-
// res := <-global.SQLOutput
47-
// fmt.Println(res)
48-
// time.Sleep(3 * time.Second)
49-
fmt.Println(0)
50-
fmt.Println("管道的内容数量为:" + strconv.Itoa(len(global.SQLInput)))
51-
_, isOpen := <-global.SQLInput
52-
if !isOpen {
53-
fmt.Println("管道已被关闭")
54-
}
55-
global.SQLInput <- "use database aaa;"
56-
fmt.Println(1)
57-
res := <-global.SQLOutput
58-
fmt.Println(2)
59-
fmt.Println(res)
60-
}
49+
// func buildSQL() {
50+
// // global.SQLInput <- "create database aaa;"
51+
// // res := <-global.SQLOutput
52+
// // fmt.Println(res)
53+
// // time.Sleep(3 * time.Second)
54+
// fmt.Println(0)
55+
// fmt.Println("管道的内容数量为:" + strconv.Itoa(len(global.SQLInput)))
56+
// _, isOpen := <-global.SQLInput
57+
// if !isOpen {
58+
// fmt.Println("管道已被关闭")
59+
// }
60+
// global.SQLInput <- "use database aaa;"
61+
// fmt.Println(1)
62+
// res := <-global.SQLOutput
63+
// fmt.Println(2)
64+
// fmt.Println(res)
65+
// }

RegionServer/src/RegionManagers/miniSQL/start.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package miniSQL
22

33
import (
4-
"bufio"
54
"fmt"
65
"os"
76
"path/filepath"
@@ -104,11 +103,11 @@ func runShell(r chan<- error, in chan string, out chan string) {
104103
}
105104
_ = file.Close()
106105
}()
107-
s := bufio.NewScanner(file)
108-
for s.Scan() {
109-
//fmt.Println(s.Text())
110-
ll.AppendHistory(s.Text())
111-
}
106+
// s := bufio.NewScanner(file)
107+
// for s.Scan() {
108+
// //fmt.Println(s.Text())
109+
// ll.AppendHistory(s.Text())
110+
// }
112111
InitDB()
113112

114113
StatementChannel := make(chan types.DStatements, 500) //用于传输操作指令通道
@@ -119,22 +118,22 @@ func runShell(r chan<- error, in chan string, out chan string) {
119118
var beginSQLParse = false
120119
var sqlText = make([]byte, 0, 100)
121120
for { //each sql
122-
LOOP:
123-
beginSQLParse = false
124-
sqlText = sqlText[:0]
125-
var input string
126-
var err error
121+
// LOOP:
122+
// beginSQLParse = false
123+
// sqlText = sqlText[:0]
124+
// var input string
125+
// var err error
127126
for { //each line
128-
if beginSQLParse {
129-
input, err = ll.Prompt(secondPrompt)
130-
} else {
131-
input, err = ll.Prompt(firstPrompt)
132-
}
133-
if err != nil {
134-
if err == liner.ErrPromptAborted {
135-
goto LOOP
136-
}
137-
}
127+
// if beginSQLParse {
128+
// input, err = ll.Prompt(secondPrompt)
129+
// } else {
130+
// input, err = ll.Prompt(firstPrompt)
131+
// }
132+
// if err != nil {
133+
// if err == liner.ErrPromptAborted {
134+
// goto LOOP
135+
// }
136+
// }
138137
// trimInput := readBuffer()
139138
// t := <-in
140139
trimInput := <-in
@@ -143,7 +142,7 @@ func runShell(r chan<- error, in chan string, out chan string) {
143142
//fmt.Println(trimInput)
144143
//trimInput
145144
if len(trimInput) != 0 {
146-
ll.AppendHistory(input)
145+
// ll.AppendHistory(input)
147146
if !beginSQLParse && (trimInput == "quit" || strings.HasPrefix(trimInput, "quit;")) {
148147
close(StatementChannel)
149148
for _ = range FinishChannel {

0 commit comments

Comments
 (0)