Test with mysql

This commit is contained in:
Jinzhu 2013-11-04 20:32:46 +08:00
parent e78c10690b
commit 7a23685e0b
4 changed files with 27 additions and 11 deletions

View File

@ -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

View File

@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"regexp"
"strconv"
)

20
do.go
View File

@ -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)))

View File

@ -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")
}