Skip to content

Commit

Permalink
mysql only accept offset with limit together
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jul 3, 2017
1 parent eae7f6b commit d395b35
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions dialect_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"unicode/utf8"
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion join_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit d395b35

Please sign in to comment.