From 7a23685e0bebd31bb46f28d5c8637269bfefaf19 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 4 Nov 2013 20:32:46 +0800 Subject: [PATCH] Test with mysql --- README.md | 5 ++++- chain.go | 1 - do.go | 20 ++++++++++++++------ gorm_test.go | 12 +++++++++--- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 972fecd1..0180babb 100644 --- a/README.md +++ b/README.md @@ -59,8 +59,11 @@ type Address struct { // TableName: `addresses` ```go import "github.com/jinzhu/gorm" import _ "github.com/lib/pq" +// import _ "github.com/go-sql-driver/mysql" db, err := Open("postgres", "user=gorm dbname=gorm sslmode=disable") +// db, err = Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True") + // Set the maximum idle database connections db.SetPool(100) @@ -617,7 +620,7 @@ db.Where("email = ?", "x@example.org").Attrs(User{FromIp: "111.111.111.111"}).Fi * Auto Migration * SQL Log * SQL Query with goroutines -* Only tested with postgres, confirm works with other database adaptors +* Only tested with postgres and mysql, confirm works with other database adaptors # Author diff --git a/chain.go b/chain.go index 1b05e564..65296ce7 100644 --- a/chain.go +++ b/chain.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "regexp" - "strconv" ) diff --git a/do.go b/do.go index cd96a729..3e0a215e 100644 --- a/do.go +++ b/do.go @@ -7,9 +7,8 @@ import ( "reflect" "regexp" "strconv" - "time" - "strings" + "time" ) type Do struct { @@ -69,7 +68,11 @@ func (s *Do) setModel(value interface{}) *Do { func (s *Do) addToVars(value interface{}) string { s.sqlVars = append(s.sqlVars, value) - return fmt.Sprintf("$%d", len(s.sqlVars)) + if s.driver == "postgres" { + return fmt.Sprintf("$%d", len(s.sqlVars)) + } else { + return "?" + } } func (s *Do) exec(sql ...string) { @@ -159,9 +162,10 @@ func (s *Do) create() (i int64) { } else { var err error s.sqlResult, err = s.db.Exec(s.sql, s.sqlVars...) - s.err(err) - id, err = s.sqlResult.LastInsertId() - s.err(err) + if s.err(err) == nil { + id, err = s.sqlResult.LastInsertId() + s.err(err) + } } if !s.hasError() { @@ -413,7 +417,11 @@ func (s *Do) pluck(column string, value interface{}) { case []uint8: if dest_type.String() == "string" { dest = string(dest.([]uint8)) + } else if dest_type.String() == "int64" { + dest, _ = strconv.Atoi(string(dest.([]uint8))) + dest = int64(dest.(int)) } + dest_out.Set(reflect.Append(dest_out, reflect.ValueOf(dest))) default: dest_out.Set(reflect.Append(dest_out, reflect.ValueOf(dest))) diff --git a/gorm_test.go b/gorm_test.go index c772ce9b..c1204da6 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -3,10 +3,10 @@ package gorm import ( "errors" "fmt" + _ "github.com/go-sql-driver/mysql" _ "github.com/lib/pq" "reflect" "strconv" - "testing" "time" ) @@ -64,9 +64,16 @@ var ( func init() { var err error db, err = Open("postgres", "user=gorm dbname=gorm sslmode=disable") + + // CREATE USER 'gorm'@'localhost' IDENTIFIED BY 'gorm'; + // CREATE DATABASE 'gorm'; + // GRANT ALL ON gorm.* TO 'gorm'@'localhost'; + db, err = Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True") + if err != nil { panic(fmt.Sprintf("No error should happen when connect database, but got %+v", err)) } + db.SetPool(10) // db.DebugMode = true @@ -474,8 +481,7 @@ func TestLimit(t *testing.T) { func TestOffset(t *testing.T) { var users1, users2, users3, users4 []User - db.Order("age desc").Find(&users1).Offset(3).Find(&users2).Offset(5).Find(&users3).Offset(-1).Find(&users4) - + db.Limit(100).Order("age desc").Find(&users1).Offset(3).Find(&users2).Offset(5).Find(&users3).Offset(-1).Find(&users4) if !((len(users1) == len(users4)) && (len(users1)-len(users2) == 3) && (len(users1)-len(users3) == 5)) { t.Errorf("Offset should works perfectly") }