Skip to content
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

Merged
merged 7 commits into from
Aug 13, 2018

Conversation

xxh2000
Copy link
Contributor

@xxh2000 xxh2000 commented Aug 9, 2018

Fixes [issue number]

fix sql invalid in postgres

@xxh2000
Copy link
Contributor Author

xxh2000 commented Aug 10, 2018

还是有点问题,mysql不加反引号遇到特殊字符也会报错,不能去掉

switch queryType {
case TABLE:
sqls += "Select * From `" + table + "`;"
sqls += "Select * From " + getWrappedTableName(r.dbtype, table)
Copy link
Collaborator

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) + ";"

func getWrappedTableName(dbtype string, table string) string {
switch dbtype {
case reader.ModeMySQL:
return "`" + table + "`;"
Copy link
Collaborator

@redHJ redHJ Aug 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return "" + table + ""

case reader.ModeMySQL:
return "`" + table + "`;"
case reader.ModeMSSQL, reader.ModePostgreSQL:
return "\"" + table + "\";"
Copy link
Collaborator

@redHJ redHJ Aug 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这边加上双引号有什么用处吗,如果没有的话就直接return table

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

postgres用双引号来解决表名大小写敏感 问题。不加的会一律默认会转为小写。以及特殊符号比如 - 需要用双引号包起来

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,明白了,谢谢~

case reader.ModeMSSQL, reader.ModePostgreSQL:
return "\"" + table + "\";"
default:
return table + ";"
Copy link
Collaborator

@redHJ redHJ Aug 10, 2018

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

只拿table会不会更清晰点呢

@@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getWrappedTableName(r.dbtype, table)放这边会更好一点


pgtests := []struct {
queryType int
expSQLs string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&reader可以放这里,或者 用参数形式把数据库dbtype放在getRawSqls里。再加一个getWrappedTableName小测试会更好

@xxh2000
Copy link
Contributor Author

xxh2000 commented Aug 10, 2018

为什么两个ci test 结果不一样

image

@redHJ
Copy link
Collaborator

redHJ commented Aug 13, 2018

lgtm

case reader.ModeMySQL:
tableName = "`" + table + "`"
case reader.ModeMSSQL, reader.ModePostgreSQL:
tableName = "\"" + table + "\""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

就针对pg的加上双引号就行了,把MSSQL单独弄出来

Copy link
Contributor Author

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") 会有问题

@wonderflow wonderflow changed the title Fix sql bug sql reader在构建数据库表名时根据具体的数据库类型构建 Aug 13, 2018
@wonderflow
Copy link
Contributor

LGTM

@wonderflow wonderflow merged commit 9732d12 into qiniu:master Aug 13, 2018
@xxh2000 xxh2000 deleted the fix_sql_bug branch August 27, 2018 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants