diff --git a/dialect_mysql.go b/dialect_mysql.go index 271670b8..560e814a 100644 --- a/dialect_mysql.go +++ b/dialect_mysql.go @@ -5,6 +5,7 @@ import ( "fmt" "reflect" "regexp" + "strconv" "strings" "time" "unicode/utf8" @@ -126,6 +127,21 @@ func (s mysql) RemoveIndex(tableName string, indexName string) error { return err } +func (s mysql) LimitAndOffsetSQL(limit, offset interface{}) (sql string) { + if limit != nil { + if parsedLimit, err := strconv.ParseInt(fmt.Sprint(limit), 0, 0); err == nil && parsedLimit >= 0 { + sql += fmt.Sprintf(" LIMIT %d", parsedLimit) + } + + if offset != nil { + if parsedOffset, err := strconv.ParseInt(fmt.Sprint(offset), 0, 0); err == nil && parsedOffset >= 0 { + sql += fmt.Sprintf(" OFFSET %d", parsedOffset) + } + } + } + return +} + func (s mysql) HasForeignKey(tableName string, foreignKeyName string) bool { var count int s.db.QueryRow("SELECT count(*) FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA=? AND TABLE_NAME=? AND CONSTRAINT_NAME=? AND CONSTRAINT_TYPE='FOREIGN KEY'", s.CurrentDatabase(), tableName, foreignKeyName).Scan(&count) diff --git a/join_table_test.go b/join_table_test.go index dd2171e1..6d5f427d 100644 --- a/join_table_test.go +++ b/join_table_test.go @@ -106,7 +106,7 @@ func TestEmbeddedMany2ManyRelationship(t *testing.T) { t.Errorf("no error should return when delete embedded many2many relationship, but got %v", err) } - association := DB.Model(person).Debug().Association("Addresses") + association := DB.Model(person).Association("Addresses") if count := association.Count(); count != 1 || association.Error != nil { t.Errorf("Should found one address, but got %v, error is %v", count, association.Error) }