Skip to content

Commit

Permalink
GORM support for MSSQL, passes all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Trost committed Sep 16, 2014
1 parent facd84d commit f79e1a2
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 18 deletions.
4 changes: 4 additions & 0 deletions common_dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func (s *commonDialect) SupportLastInsertId() bool {
return true
}

func (s *commonDialect) HasTop() bool {
return false
}

func (d *commonDialect) SqlTag(value reflect.Value, size int) string {
switch value.Kind() {
case reflect.Bool:
Expand Down
1 change: 1 addition & 0 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var timeType = reflect.TypeOf(time.Time{})
type Dialect interface {
BinVar(i int) string
SupportLastInsertId() bool
HasTop() bool
SqlTag(value reflect.Value, size int) string
PrimaryKeyTag(value reflect.Value, size int) string
ReturningStr(key string) string
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ func Open(dialect string, drivesources ...string) (DB, error) {
source = drivesources[1]
}

db = DB{dialect: NewDialect(dialect), tagIdentifier: "sql", logger: defaultLogger, callback: DefaultCallback, source: source, values: map[string]interface{}{}}
db = DB{dialect: NewDialect(dialect), tagIdentifier: "sql",
logger: defaultLogger, callback: DefaultCallback, source: source,
values: map[string]interface{}{}}
db.db, err = sql.Open(driver, source)
db.parent = &db
}
Expand Down
32 changes: 20 additions & 12 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@ var (
func init() {
var err error
switch os.Getenv("GORM_DIALECT") {
case "mysql":
// CREATE USER 'gorm'@'localhost' IDENTIFIED BY 'gorm';
// CREATE DATABASE gorm;
// GRANT ALL ON gorm.* TO 'gorm'@'localhost';
fmt.Println("testing mysql...")
DB, err = gorm.Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True")
case "postgres":
fmt.Println("testing postgres...")
DB, err = gorm.Open("postgres", "user=gorm DB.ame=gorm sslmode=disable")
default:
fmt.Println("testing sqlite3...")
DB, err = gorm.Open("sqlite3", "/tmp/gorm.db")
case "mysql":
// CREATE USER 'gorm'@'localhost' IDENTIFIED BY 'gorm';
// CREATE DATABASE gorm;
// GRANT ALL ON gorm.* TO 'gorm'@'localhost';
fmt.Println("testing mysql...")
DB, err = gorm.Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True")
case "postgres":
fmt.Println("testing postgres...")
DB, err = gorm.Open("postgres", "user=gorm DB.ame=gorm sslmode=disable")
case "mssql":
fmt.Println("testing mssql...")
DB, err = gorm.Open("mssql", "server=SERVER_HERE;database=DB_HERE;user id=USER_HERE;password=PW_HERE;port=1433")
default:
fmt.Println("testing sqlite3...")
DB, err = gorm.Open("sqlite3", "/tmp/gorm.db")
}

// DB.SetLogger(Logger{log.New(os.Stdout, "\r\n", 0)})
Expand Down Expand Up @@ -445,6 +448,11 @@ func TestTimeWithZone(t *testing.T) {
for index, vtime := range times {
name := "time_with_zone_" + strconv.Itoa(index)
user := User{Name: name, Birthday: vtime}

//mssql does not support time zones
if dialect := os.Getenv("GORM_DIALECT"); dialect == "mssql" {
user.Birthday = vtime.UTC()
}
DB.Save(&user)
if user.Birthday.UTC().Format(format) != "2013-02-18 17:51:49 +0000" {
t.Errorf("User's birthday should not be changed after save")
Expand Down
4 changes: 4 additions & 0 deletions mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func (s *mysql) SupportLastInsertId() bool {
return true
}

func (s *mysql) HasTop() bool {
return false
}

func (d *mysql) SqlTag(value reflect.Value, size int) string {
switch value.Kind() {
case reflect.Bool:
Expand Down
4 changes: 4 additions & 0 deletions postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func (s *postgres) SupportLastInsertId() bool {
return false
}

func (s *postgres) HasTop() bool {
return false
}

func (d *postgres) SqlTag(value reflect.Value, size int) string {
switch value.Kind() {
case reflect.Bool:
Expand Down
5 changes: 2 additions & 3 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,9 @@ func TestOrderAndPluck(t *testing.T) {
}

var ages1, ages2 []int64
scopedb.Order("age desc").Pluck("age", &ages1).Order("age").Pluck("age", &ages2)
scopedb.Order("age desc").Pluck("age", &ages1).Pluck("age", &ages2)
if !reflect.DeepEqual(ages1, ages2) {
t.Errorf("The first order is the primary order")
}
t.Errorf("The first order is the primary order") }

var ages3, ages4 []int64
scopedb.Model(&User{}).Order("age desc").Pluck("age", &ages3).Order("age", true).Pluck("age", &ages4)
Expand Down
34 changes: 32 additions & 2 deletions scope_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,25 @@ func (s *Scope) orderSql() string {
}

func (s *Scope) limitSql() string {
if len(s.Search.Limit) == 0 {
if !s.Dialect().HasTop() {
if len(s.Search.Limit) == 0 {
return ""
} else {
return " LIMIT " + s.Search.Limit
}
} else{
return ""
}
}

func (s *Scope) topSql() string{
if s.Dialect().HasTop() && len(s.Search.Offset) == 0 {
if len(s.Search.Limit) == 0 {
return ""
} else{
return " TOP(" + s.Search.Limit + ")"
}
} else{
return ""
} else {
return " LIMIT " + s.Search.Limit
Expand All @@ -207,7 +225,15 @@ func (s *Scope) offsetSql() string {
if len(s.Search.Offset) == 0 {
return ""
} else {
return " OFFSET " + s.Search.Offset
if s.Dialect().HasTop(){
sql := " OFFSET " + s.Search.Offset + " ROW "
if len(s.Search.Limit) > 0{
sql += "FETCH NEXT " + s.Search.Limit + " ROWS ONLY"
}
return sql
}else{
return " OFFSET " + s.Search.Offset
}
}
}

Expand Down Expand Up @@ -235,7 +261,11 @@ func (scope *Scope) prepareQuerySql() {
if scope.Search.Raw {
scope.Raw(strings.TrimLeft(scope.CombinedConditionSql(), "WHERE "))
} else {
<<<<<<< HEAD
scope.Raw(fmt.Sprintf("SELECT %v FROM %v %v", scope.selectSql(), scope.QuotedTableName(), scope.CombinedConditionSql()))
=======
scope.Raw(fmt.Sprintf("SELECT %v %v FROM %v %v", scope.topSql(), scope.selectSql(), scope.QuotedTableName(), scope.CombinedConditionSql()))
>>>>>>> 15a20a4... GORM support for MSSQL, passes all tests
}
return
}
Expand Down
4 changes: 4 additions & 0 deletions sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func (s *sqlite3) SupportLastInsertId() bool {
return true
}

func (s *sqlite3) HasTop() bool {
return false
}

func (s *sqlite3) SqlTag(value reflect.Value, size int) string {
switch value.Kind() {
case reflect.Bool:
Expand Down

0 comments on commit f79e1a2

Please sign in to comment.