-
Notifications
You must be signed in to change notification settings - Fork 255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sql reader在构建数据库表名时根据具体的数据库类型构建 #704
Conversation
还是有点问题,mysql不加反引号遇到特殊字符也会报错,不能去掉 |
reader/sql/sql.go
Outdated
switch queryType { | ||
case TABLE: | ||
sqls += "Select * From `" + table + "`;" | ||
sqls += "Select * From " + getWrappedTableName(r.dbtype, table) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sqls += "Select * From " + getWrappedTableName(r.dbtype, table) + ";"
reader/sql/sql.go
Outdated
func getWrappedTableName(dbtype string, table string) string { | ||
switch dbtype { | ||
case reader.ModeMySQL: | ||
return "`" + table + "`;" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return "" + table + "
"
reader/sql/sql.go
Outdated
case reader.ModeMySQL: | ||
return "`" + table + "`;" | ||
case reader.ModeMSSQL, reader.ModePostgreSQL: | ||
return "\"" + table + "\";" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这边加上双引号有什么用处吗,如果没有的话就直接return table
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
postgres用双引号来解决表名大小写敏感 问题。不加的会一律默认会转为小写。以及特殊符号比如 - 需要用双引号包起来
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的,明白了,谢谢~
reader/sql/sql.go
Outdated
case reader.ModeMSSQL, reader.ModePostgreSQL: | ||
return "\"" + table + "\";" | ||
default: | ||
return table + ";" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
报个错会更符合现有逻辑,err = fmt.Errorf("%v mode not support in sql reader", dbtype)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
reader/sql/sql.go
Outdated
case DATABASE: | ||
default: | ||
return "", fmt.Errorf("%v queryType is not support get sql now", queryType) | ||
} | ||
|
||
return sqls, nil | ||
} | ||
func getWrappedTableName(dbtype string, table string) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
只拿table会不会更清晰点呢
reader/sql/sql.go
Outdated
@@ -1680,19 +1680,29 @@ func (r *Reader) getCheckAll(queryType int) (checkAll bool, err error) { | |||
} | |||
|
|||
// 根据 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getWrappedTableName(r.dbtype, table)放这边会更好一点
reader/sql/sql_test.go
Outdated
|
||
pgtests := []struct { | ||
queryType int | ||
expSQLs string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
&reader可以放这里,或者 用参数形式把数据库dbtype放在getRawSqls里。再加一个getWrappedTableName小测试会更好
lgtm |
case reader.ModeMySQL: | ||
tableName = "`" + table + "`" | ||
case reader.ModeMSSQL, reader.ModePostgreSQL: | ||
tableName = "\"" + table + "\"" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
就针对pg的加上双引号就行了,把MSSQL单独弄出来
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sqlserver 如果表名有横杆的话,也需要加双引号,否则会报语法错误,比如
rows ,err:=sql.Query("select * from log-2018-08-01")
会有问题
LGTM |
Fixes [issue number]
fix sql invalid in postgres