Skip to content

Commit 5d5ad20

Browse files
committed
否则特殊表名会出问题&返回数据类型修改
1 parent 2909ef3 commit 5d5ad20

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

cmd/text2sql/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ func main() {
4242
}
4343
fmt.Printf("sql: %s \n", sql)
4444
if run {
45-
fmt.Printf("result: %s \n", result)
45+
fmt.Printf("result: %+v \n", result)
4646
}
4747
}

mysql/mysql.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (x *Db) getDdl() ([]string, error) {
5959
ddls := make([]string, 0, len(list))
6060
for _, tableName := range list {
6161
result := map[string]interface{}{}
62-
if err := x.db.Raw(fmt.Sprintf("SHOW CREATE TABLE %s", tableName)).Scan(&result).Error; err != nil {
62+
if err := x.db.Raw(fmt.Sprintf("SHOW CREATE TABLE `%s`", tableName)).Scan(&result).Error; err != nil {
6363
return nil, fmt.Errorf("获取表%s的结构失败: %w", tableName, err)
6464
}
6565

@@ -101,7 +101,7 @@ func (x *Db) CheckSQL(sql string) error {
101101
}
102102

103103
// DoSQL 执行SQL查询并返回结果
104-
func (x *Db) DoSQL(sql string) (res map[string]interface{}, err error) {
104+
func (x *Db) DoSQL(sql string) (res []map[string]interface{}, err error) {
105105
// 创建只读事务
106106
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
107107
defer cancel()
@@ -117,7 +117,7 @@ func (x *Db) DoSQL(sql string) (res map[string]interface{}, err error) {
117117
tx.Set("gorm:query_option", "FOR READ ONLY")
118118

119119
// 执行查询
120-
res = map[string]interface{}{}
120+
res = []map[string]interface{}{}
121121
if err = tx.Raw(sql).Scan(&res).Error; err != nil {
122122
tx.Rollback()
123123
return

text2sql.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type Text2sql struct {
2727
}
2828

2929
// Do 执行文本到SQL的转换过程
30-
func (x *Text2sql) Do(question string) (sql string, runResult map[string]interface{}, err error) {
30+
func (x *Text2sql) Do(question string) (sql string, runResult []map[string]interface{}, err error) {
3131
// 参数验证和初始化
3232
if question == "" {
3333
return "", nil, errors.New("问题不能为空")

0 commit comments

Comments
 (0)