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 // LimitAndOffsetSQL return generated SQL with Limit and Offset
func (s commonDialect) LimitAndOffsetSQL(limit, offset interface{}) (sql string, err error) { func (s commonDialect) LimitAndOffsetSQL(limit, offset interface{}) (sql string, err error) {
if limit != nil { if limit != nil {
parsedLimit, err := s.parseInt(limit) if parsedLimit, err := s.parseInt(limit); err != nil {
if err != nil {
return "", err return "", err
} } else if parsedLimit >= 0 {
if parsedLimit >= 0 {
sql += fmt.Sprintf(" LIMIT %d", parsedLimit) sql += fmt.Sprintf(" LIMIT %d", parsedLimit)
} }
} }
if offset != nil { if offset != nil {
parsedOffset, err := s.parseInt(offset) if parsedOffset, err := s.parseInt(offset); err != nil {
if err != nil {
return "", err return "", err
} } else if parsedOffset >= 0 {
if parsedOffset >= 0 {
sql += fmt.Sprintf(" OFFSET %d", parsedOffset) sql += fmt.Sprintf(" OFFSET %d", parsedOffset)
} }
} }

View File

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