Extract parseInt

This commit is contained in:
Jinzhu 2019-12-05 23:13:54 +08:00
parent 0f387db5b8
commit 11e2819f44
2 changed files with 12 additions and 19 deletions

View File

@ -142,20 +142,16 @@ func (s commonDialect) CurrentDatabase() (name string) {
// LimitAndOffsetSQL return generated SQL with Limit and Offset
func (s commonDialect) LimitAndOffsetSQL(limit, offset interface{}) (sql string, err error) {
if limit != nil {
parsedLimit, err := s.parseInt(limit)
if err != nil {
if parsedLimit, err := s.parseInt(limit); err != nil {
return "", err
}
if parsedLimit >= 0 {
} else if parsedLimit >= 0 {
sql += fmt.Sprintf(" LIMIT %d", parsedLimit)
}
}
if offset != nil {
parsedOffset, err := s.parseInt(offset)
if err != nil {
if parsedOffset, err := s.parseInt(offset); err != nil {
return "", err
}
if parsedOffset >= 0 {
} else if parsedOffset >= 0 {
sql += fmt.Sprintf(" OFFSET %d", parsedOffset)
}
}

View File

@ -168,25 +168,22 @@ func (s mssql) CurrentDatabase() (name string) {
return
}
func (mssql) LimitAndOffsetSQL(limit, offset interface{}) (sql string, err error) {
parseInt := func(value interface{}) (int64, error) {
func parseInt(value interface{}) (int64, error) {
return strconv.ParseInt(fmt.Sprint(value), 0, 0)
}
}
func (mssql) LimitAndOffsetSQL(limit, offset interface{}) (sql string, err error) {
if offset != nil {
parsedOffset, err := parseInt(offset)
if err != nil {
if parsedOffset, err := parseInt(offset); err != nil {
return "", err
}
if parsedOffset >= 0 {
} else if parsedOffset >= 0 {
sql += fmt.Sprintf(" OFFSET %d ROWS", parsedOffset)
}
}
if limit != nil {
parsedLimit, err := parseInt(limit)
if err != nil {
if parsedLimit, err := parseInt(limit); err != nil {
return "", err
}
if parsedLimit >= 0 {
} else if parsedLimit >= 0 {
if sql == "" {
// add default zero offset
sql += " OFFSET 0 ROWS"