Skip to content

sql reader在构建数据库表名时根据具体的数据库类型构建 - #704

Merged
wonderflow merged 7 commits into
qiniu:masterfrom
xxh2000:fix_sql_bug
Aug 13, 2018
Merged

wonderflow merged 7 commits into
qiniu:masterfrom
xxh2000:fix_sql_bug

Conversation

@xxh2000

@xxh2000 xxh2000 commented Aug 9, 2018

Copy link
Copy Markdown
Contributor

Fixes [issue number]

fix sql invalid in postgres

@xxh2000

xxh2000 commented Aug 10, 2018

Copy link
Copy Markdown
Contributor Author

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

Comment thread reader/sql/sql.go Outdated
switch queryType {
case TABLE:
sqls += "Select * From `" + table + "`;"
sqls += "Select * From " + getWrappedTableName(r.dbtype, table)

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

Comment thread reader/sql/sql.go Outdated
func getWrappedTableName(dbtype string, table string) string {
switch dbtype {
case reader.ModeMySQL:
return "`" + table + "`;"

@redHJ redHJ Aug 10, 2018 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

return "" + table + ""

Comment thread reader/sql/sql.go Outdated
case reader.ModeMySQL:
return "`" + table + "`;"
case reader.ModeMSSQL, reader.ModePostgreSQL:
return "\"" + table + "\";"

@redHJ redHJ Aug 10, 2018 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

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

Choose a reason for hiding this comment

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

好的,明白了,谢谢~

Comment thread reader/sql/sql.go Outdated
case reader.ModeMSSQL, reader.ModePostgreSQL:
return "\"" + table + "\";"
default:
return table + ";"

@redHJ redHJ Aug 10, 2018 •

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok

Comment thread reader/sql/sql.go Outdated

return sqls, nil
}
func getWrappedTableName(dbtype string, table string) string {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

只拿table会不会更清晰点呢

Comment thread reader/sql/sql.go

// 根据 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
Copy Markdown
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)放这边会更好一点

Comment thread reader/sql/sql_test.go

pgtests := []struct {
queryType int
expSQLs string

Copy link
Copy Markdown
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

xxh2000 commented Aug 10, 2018

Copy link
Copy Markdown
Contributor Author

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

image

@redHJ

redHJ commented Aug 13, 2018

Copy link
Copy Markdown
Collaborator

lgtm

Comment thread reader/sql/sql.go
case reader.ModeMySQL:
tableName = "`" + table + "`"
case reader.ModeMSSQL, reader.ModePostgreSQL:
tableName = "\"" + table + "\""

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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