Repository navigation
sql reader在构建数据库表名时根据具体的数据库类型构建 - #704
Conversation
|
还是有点问题,mysql不加反引号遇到特殊字符也会报错,不能去掉 |
| switch queryType { | ||
| case TABLE: | ||
| sqls += "Select * From `" + table + "`;" | ||
| sqls += "Select * From " + getWrappedTableName(r.dbtype, table) |
There was a problem hiding this comment.
sqls += "Select * From " + getWrappedTableName(r.dbtype, table) + ";"
| func getWrappedTableName(dbtype string, table string) string { | ||
| switch dbtype { | ||
| case reader.ModeMySQL: | ||
| return "`" + table + "`;" |
| case reader.ModeMySQL: | ||
| return "`" + table + "`;" | ||
| case reader.ModeMSSQL, reader.ModePostgreSQL: | ||
| return "\"" + table + "\";" |
There was a problem hiding this comment.
这边加上双引号有什么用处吗,如果没有的话就直接return table
There was a problem hiding this comment.
postgres用双引号来解决表名大小写敏感 问题。不加的会一律默认会转为小写。以及特殊符号比如 - 需要用双引号包起来
| case reader.ModeMSSQL, reader.ModePostgreSQL: | ||
| return "\"" + table + "\";" | ||
| default: | ||
| return table + ";" |
There was a problem hiding this comment.
报个错会更符合现有逻辑,err = fmt.Errorf("%v mode not support in sql reader", dbtype)
|
|
||
| return sqls, nil | ||
| } | ||
| func getWrappedTableName(dbtype string, table string) string { |
|
|
||
| // 根据 queryType 获取表中所有记录或者表中所有数据的条数的sql语句 | ||
| func getRawSqls(queryType int, table string) (sqls string, err error) { | ||
| func (r *Reader) getRawSqls(queryType int, table string) (sqls string, err error) { |
There was a problem hiding this comment.
getWrappedTableName(r.dbtype, table)放这边会更好一点
|
|
||
| pgtests := []struct { | ||
| queryType int | ||
| expSQLs string |
There was a problem hiding this comment.
&reader可以放这里,或者 用参数形式把数据库dbtype放在getRawSqls里。再加一个getWrappedTableName小测试会更好
|
lgtm |
| case reader.ModeMySQL: | ||
| tableName = "`" + table + "`" | ||
| case reader.ModeMSSQL, reader.ModePostgreSQL: | ||
| tableName = "\"" + table + "\"" |
There was a problem hiding this comment.
就针对pg的加上双引号就行了,把MSSQL单独弄出来
There was a problem hiding this comment.
sqlserver 如果表名有横杆的话,也需要加双引号,否则会报语法错误,比如
rows ,err:=sql.Query("select * from log-2018-08-01") 会有问题
|
LGTM |

Fixes [issue number]
fix sql invalid in postgres