From 11e2819f44a6b6e2b21119a9eaf451244abd808b Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 5 Dec 2019 23:13:54 +0800 Subject: [PATCH] Extract parseInt --- dialect_common.go | 12 ++++-------- dialects/mssql/mssql.go | 19 ++++++++----------- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/dialect_common.go b/dialect_common.go index 950c1986..d549510c 100644 --- a/dialect_common.go +++ b/dialect_common.go @@ -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) } } diff --git a/dialects/mssql/mssql.go b/dialects/mssql/mssql.go index 43acb379..cb2714e0 100644 --- a/dialects/mssql/mssql.go +++ b/dialects/mssql/mssql.go @@ -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"