Skip to content

Commit

Permalink
Extract parseInt
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Dec 5, 2019
1 parent 0f387db commit 11e2819
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
12 changes: 4 additions & 8 deletions dialect_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
19 changes: 8 additions & 11 deletions dialects/mssql/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,25 +168,22 @@ func (s mssql) CurrentDatabase() (name string) {
return
}

func parseInt(value interface{}) (int64, error) {
return strconv.ParseInt(fmt.Sprint(value), 0, 0)
}

func (mssql) LimitAndOffsetSQL(limit, offset interface{}) (sql string, err error) {
parseInt := func(value interface{}) (int64, error) {
return strconv.ParseInt(fmt.Sprint(value), 0, 0)
}
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"
Expand Down

0 comments on commit 11e2819

Please sign in to comment.