2014-07-29 06:59:13 +04:00
|
|
|
package gorm_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"database/sql/driver"
|
|
|
|
"errors"
|
2015-03-12 12:47:31 +03:00
|
|
|
"fmt"
|
2014-07-29 06:59:13 +04:00
|
|
|
|
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Id int64
|
|
|
|
Age int64
|
|
|
|
UserNum Num
|
|
|
|
Name string `sql:"size:255"`
|
|
|
|
Birthday time.Time // Time
|
|
|
|
CreatedAt time.Time // CreatedAt: Time of record is created, will be insert automatically
|
|
|
|
UpdatedAt time.Time // UpdatedAt: Time of record is updated, will be updated automatically
|
|
|
|
Emails []Email // Embedded structs
|
|
|
|
BillingAddress Address // Embedded struct
|
2015-02-18 08:37:27 +03:00
|
|
|
BillingAddressID sql.NullInt64 // Embedded struct's foreign key
|
2014-07-29 06:59:13 +04:00
|
|
|
ShippingAddress Address // Embedded struct
|
|
|
|
ShippingAddressId int64 // Embedded struct's foreign key
|
|
|
|
CreditCard CreditCard
|
|
|
|
Latitude float64
|
2014-07-29 13:28:10 +04:00
|
|
|
Languages []Language `gorm:"many2many:user_languages;"`
|
2015-02-18 08:37:27 +03:00
|
|
|
CompanyID int64
|
2014-08-30 19:08:58 +04:00
|
|
|
Company Company
|
2014-07-29 06:59:13 +04:00
|
|
|
Role
|
|
|
|
PasswordHash []byte
|
2014-07-29 12:25:38 +04:00
|
|
|
IgnoreMe int64 `sql:"-"`
|
|
|
|
IgnoreStringSlice []string `sql:"-"`
|
|
|
|
Ignored struct{ Name string } `sql:"-"`
|
2014-08-15 07:14:33 +04:00
|
|
|
IgnoredPointer *User `sql:"-"`
|
2014-07-29 06:59:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type CreditCard struct {
|
2015-02-18 05:19:34 +03:00
|
|
|
ID int8
|
2014-07-29 06:59:13 +04:00
|
|
|
Number string
|
|
|
|
UserId sql.NullInt64
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
DeletedAt time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type Email struct {
|
|
|
|
Id int16
|
|
|
|
UserId int
|
2014-07-29 08:02:03 +04:00
|
|
|
Email string `sql:"type:varchar(100);"`
|
2014-07-29 06:59:13 +04:00
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type Address struct {
|
2015-02-18 05:19:34 +03:00
|
|
|
ID int
|
2014-07-29 06:59:13 +04:00
|
|
|
Address1 string
|
|
|
|
Address2 string
|
|
|
|
Post string
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
DeletedAt time.Time
|
|
|
|
}
|
|
|
|
|
2014-07-29 13:28:10 +04:00
|
|
|
type Language struct {
|
2015-02-28 14:09:45 +03:00
|
|
|
Id int
|
|
|
|
Name string
|
|
|
|
Users []User `gorm:"many2many:user_languages;"`
|
2014-07-29 13:28:10 +04:00
|
|
|
}
|
|
|
|
|
2014-07-29 06:59:13 +04:00
|
|
|
type Product struct {
|
|
|
|
Id int64
|
|
|
|
Code string
|
|
|
|
Price int64
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
AfterFindCallTimes int64
|
|
|
|
BeforeCreateCallTimes int64
|
|
|
|
AfterCreateCallTimes int64
|
|
|
|
BeforeUpdateCallTimes int64
|
|
|
|
AfterUpdateCallTimes int64
|
|
|
|
BeforeSaveCallTimes int64
|
|
|
|
AfterSaveCallTimes int64
|
|
|
|
BeforeDeleteCallTimes int64
|
|
|
|
AfterDeleteCallTimes int64
|
|
|
|
}
|
|
|
|
|
2014-07-29 12:25:38 +04:00
|
|
|
type Company struct {
|
2014-08-14 17:45:42 +04:00
|
|
|
Id int64
|
|
|
|
Name string
|
|
|
|
Owner *User `sql:"-"`
|
2014-07-29 12:25:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Role struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (role *Role) Scan(value interface{}) error {
|
2014-09-16 19:32:35 +04:00
|
|
|
if b, ok := value.([]uint8); ok {
|
|
|
|
role.Name = string(b)
|
|
|
|
} else {
|
|
|
|
role.Name = value.(string)
|
|
|
|
}
|
2014-07-29 12:25:38 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (role Role) Value() (driver.Value, error) {
|
|
|
|
return role.Name, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (role Role) IsAdmin() bool {
|
|
|
|
return role.Name == "admin"
|
|
|
|
}
|
|
|
|
|
|
|
|
type Num int64
|
|
|
|
|
|
|
|
func (i *Num) Scan(src interface{}) error {
|
|
|
|
switch s := src.(type) {
|
|
|
|
case []byte:
|
|
|
|
case int64:
|
|
|
|
*i = Num(s)
|
|
|
|
default:
|
|
|
|
return errors.New("Cannot scan NamedInt from " + reflect.ValueOf(src).String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-29 06:59:13 +04:00
|
|
|
type Animal struct {
|
2014-11-19 06:44:57 +03:00
|
|
|
Counter uint64 `gorm:"primary_key:yes"`
|
|
|
|
Name string `sql:"DEFAULT:'galeone'"`
|
|
|
|
From string //test reserved sql keyword as field name
|
|
|
|
Age time.Time `sql:"DEFAULT:current_timestamp"`
|
|
|
|
unexported string // unexported value
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
2014-07-29 06:59:13 +04:00
|
|
|
}
|
|
|
|
|
2014-12-08 13:33:30 +03:00
|
|
|
type JoinTable struct {
|
|
|
|
From uint64
|
|
|
|
To uint64
|
|
|
|
Time time.Time `sql:"default: null"`
|
|
|
|
}
|
|
|
|
|
2014-07-29 06:59:13 +04:00
|
|
|
type Post struct {
|
|
|
|
Id int64
|
|
|
|
CategoryId sql.NullInt64
|
|
|
|
MainCategoryId int64
|
|
|
|
Title string
|
|
|
|
Body string
|
2015-02-17 18:36:23 +03:00
|
|
|
Comments []*Comment
|
2014-07-29 06:59:13 +04:00
|
|
|
Category Category
|
|
|
|
MainCategory Category
|
|
|
|
}
|
|
|
|
|
2014-07-29 12:25:38 +04:00
|
|
|
type Category struct {
|
|
|
|
Id int64
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2014-07-29 06:59:13 +04:00
|
|
|
type Comment struct {
|
|
|
|
Id int64
|
|
|
|
PostId int64
|
|
|
|
Content string
|
2015-02-18 08:16:32 +03:00
|
|
|
Post Post
|
2014-07-29 06:59:13 +04:00
|
|
|
}
|
|
|
|
|
2014-07-29 12:25:38 +04:00
|
|
|
// Scanner
|
|
|
|
type NullValue struct {
|
|
|
|
Id int64
|
2016-01-04 13:40:06 +03:00
|
|
|
Name sql.NullString `sql:"not null"`
|
|
|
|
Gender *sql.NullString `sql:"not null"`
|
2014-07-29 12:25:38 +04:00
|
|
|
Age sql.NullInt64
|
|
|
|
Male sql.NullBool
|
|
|
|
Height sql.NullFloat64
|
|
|
|
AddedAt NullTime
|
2014-07-29 06:59:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type NullTime struct {
|
|
|
|
Time time.Time
|
|
|
|
Valid bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nt *NullTime) Scan(value interface{}) error {
|
|
|
|
if value == nil {
|
|
|
|
nt.Valid = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
nt.Time, nt.Valid = value.(time.Time), true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nt NullTime) Value() (driver.Value, error) {
|
|
|
|
if !nt.Valid {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nt.Time, nil
|
|
|
|
}
|
2015-03-12 12:47:31 +03:00
|
|
|
|
|
|
|
func getPreparedUser(name string, role string) *User {
|
|
|
|
var company Company
|
|
|
|
DB.Where(Company{Name: role}).FirstOrCreate(&company)
|
|
|
|
|
|
|
|
return &User{
|
|
|
|
Name: name,
|
|
|
|
Age: 20,
|
|
|
|
Role: Role{role},
|
|
|
|
BillingAddress: Address{Address1: fmt.Sprintf("Billing Address %v", name)},
|
|
|
|
ShippingAddress: Address{Address1: fmt.Sprintf("Shipping Address %v", name)},
|
|
|
|
CreditCard: CreditCard{Number: fmt.Sprintf("123456%v", name)},
|
|
|
|
Emails: []Email{
|
|
|
|
{Email: fmt.Sprintf("user_%v@example1.com", name)}, {Email: fmt.Sprintf("user_%v@example2.com", name)},
|
|
|
|
},
|
|
|
|
Company: company,
|
|
|
|
Languages: []Language{
|
|
|
|
{Name: fmt.Sprintf("lang_1_%v", name)},
|
|
|
|
{Name: fmt.Sprintf("lang_2_%v", name)},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|