2013-08-07 11:11:44 +00:00
|
|
|
package orm
|
|
|
|
|
|
|
|
import (
|
2014-02-28 03:53:35 +00:00
|
|
|
"database/sql"
|
2013-11-26 13:34:40 +00:00
|
|
|
"encoding/json"
|
2013-08-07 11:11:44 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2013-11-26 10:32:12 +00:00
|
|
|
"strings"
|
2013-08-07 11:11:44 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
2013-08-10 16:15:26 +00:00
|
|
|
_ "github.com/lib/pq"
|
2013-08-07 11:11:44 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
2013-11-26 13:34:40 +00:00
|
|
|
// A slice string field.
|
2013-11-26 10:32:12 +00:00
|
|
|
type SliceStringField []string
|
|
|
|
|
|
|
|
func (e SliceStringField) Value() []string {
|
|
|
|
return []string(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *SliceStringField) Set(d []string) {
|
|
|
|
*e = SliceStringField(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *SliceStringField) Add(v string) {
|
|
|
|
*e = append(*e, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *SliceStringField) String() string {
|
|
|
|
return strings.Join(e.Value(), ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *SliceStringField) FieldType() int {
|
|
|
|
return TypeCharField
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *SliceStringField) SetRaw(value interface{}) error {
|
|
|
|
switch d := value.(type) {
|
|
|
|
case []string:
|
|
|
|
e.Set(d)
|
|
|
|
case string:
|
|
|
|
if len(d) > 0 {
|
|
|
|
parts := strings.Split(d, ",")
|
|
|
|
v := make([]string, 0, len(parts))
|
|
|
|
for _, p := range parts {
|
|
|
|
v = append(v, strings.TrimSpace(p))
|
|
|
|
}
|
|
|
|
e.Set(v)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("<SliceStringField.SetRaw> unknown value `%v`", value)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *SliceStringField) RawValue() interface{} {
|
|
|
|
return e.String()
|
|
|
|
}
|
|
|
|
|
2013-11-26 13:34:40 +00:00
|
|
|
var _ Fielder = new(SliceStringField)
|
|
|
|
|
|
|
|
// A json field.
|
|
|
|
type JsonField struct {
|
|
|
|
Name string
|
|
|
|
Data string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *JsonField) String() string {
|
|
|
|
data, _ := json.Marshal(e)
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *JsonField) FieldType() int {
|
|
|
|
return TypeTextField
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *JsonField) SetRaw(value interface{}) error {
|
|
|
|
switch d := value.(type) {
|
|
|
|
case string:
|
|
|
|
return json.Unmarshal([]byte(d), e)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("<JsonField.SetRaw> unknown value `%v`", value)
|
|
|
|
}
|
2013-11-26 10:32:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-26 13:34:40 +00:00
|
|
|
func (e *JsonField) RawValue() interface{} {
|
|
|
|
return e.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Fielder = new(JsonField)
|
|
|
|
|
2013-08-13 09:16:12 +00:00
|
|
|
type Data struct {
|
2013-08-16 13:57:39 +00:00
|
|
|
Id int
|
2013-08-13 09:16:12 +00:00
|
|
|
Boolean bool
|
2013-08-16 14:24:10 +00:00
|
|
|
Char string `orm:"size(50)"`
|
2013-08-16 13:57:39 +00:00
|
|
|
Text string `orm:"type(text)"`
|
2013-08-13 09:16:12 +00:00
|
|
|
Date time.Time `orm:"type(date)"`
|
2013-09-09 14:33:41 +00:00
|
|
|
DateTime time.Time `orm:"column(datetime)"`
|
2013-08-13 09:16:12 +00:00
|
|
|
Byte byte
|
|
|
|
Rune rune
|
|
|
|
Int int
|
|
|
|
Int8 int8
|
|
|
|
Int16 int16
|
|
|
|
Int32 int32
|
|
|
|
Int64 int64
|
|
|
|
Uint uint
|
|
|
|
Uint8 uint8
|
|
|
|
Uint16 uint16
|
|
|
|
Uint32 uint32
|
|
|
|
Uint64 uint64
|
|
|
|
Float32 float32
|
|
|
|
Float64 float64
|
|
|
|
Decimal float64 `orm:"digits(8);decimals(4)"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type DataNull struct {
|
2014-02-28 03:53:35 +00:00
|
|
|
Id int
|
|
|
|
Boolean bool `orm:"null"`
|
|
|
|
Char string `orm:"null;size(50)"`
|
|
|
|
Text string `orm:"null;type(text)"`
|
|
|
|
Date time.Time `orm:"null;type(date)"`
|
|
|
|
DateTime time.Time `orm:"null;column(datetime)""`
|
|
|
|
Byte byte `orm:"null"`
|
|
|
|
Rune rune `orm:"null"`
|
|
|
|
Int int `orm:"null"`
|
|
|
|
Int8 int8 `orm:"null"`
|
|
|
|
Int16 int16 `orm:"null"`
|
|
|
|
Int32 int32 `orm:"null"`
|
|
|
|
Int64 int64 `orm:"null"`
|
|
|
|
Uint uint `orm:"null"`
|
|
|
|
Uint8 uint8 `orm:"null"`
|
|
|
|
Uint16 uint16 `orm:"null"`
|
|
|
|
Uint32 uint32 `orm:"null"`
|
|
|
|
Uint64 uint64 `orm:"null"`
|
|
|
|
Float32 float32 `orm:"null"`
|
|
|
|
Float64 float64 `orm:"null"`
|
|
|
|
Decimal float64 `orm:"digits(8);decimals(4);null"`
|
|
|
|
NullString sql.NullString `orm:"null"`
|
|
|
|
NullBool sql.NullBool `orm:"null"`
|
|
|
|
NullFloat64 sql.NullFloat64 `orm:"null"`
|
|
|
|
NullInt64 sql.NullInt64 `orm:"null"`
|
2013-08-13 09:16:12 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 15:31:47 +00:00
|
|
|
type String string
|
|
|
|
type Boolean bool
|
|
|
|
type Byte byte
|
|
|
|
type Rune rune
|
|
|
|
type Int int
|
|
|
|
type Int8 int8
|
|
|
|
type Int16 int16
|
|
|
|
type Int32 int32
|
|
|
|
type Int64 int64
|
|
|
|
type Uint uint
|
|
|
|
type Uint8 uint8
|
|
|
|
type Uint16 uint16
|
|
|
|
type Uint32 uint32
|
|
|
|
type Uint64 uint64
|
|
|
|
type Float32 float64
|
|
|
|
type Float64 float64
|
|
|
|
|
|
|
|
type DataCustom struct {
|
|
|
|
Id int
|
|
|
|
Boolean Boolean
|
|
|
|
Char string `orm:"size(50)"`
|
|
|
|
Text string `orm:"type(text)"`
|
|
|
|
Byte Byte
|
|
|
|
Rune Rune
|
|
|
|
Int Int
|
|
|
|
Int8 Int8
|
|
|
|
Int16 Int16
|
|
|
|
Int32 Int32
|
|
|
|
Int64 Int64
|
|
|
|
Uint Uint
|
|
|
|
Uint8 Uint8
|
|
|
|
Uint16 Uint16
|
|
|
|
Uint32 Uint32
|
|
|
|
Uint64 Uint64
|
|
|
|
Float32 Float32
|
|
|
|
Float64 Float64
|
|
|
|
Decimal Float64 `orm:"digits(8);decimals(4)"`
|
|
|
|
}
|
|
|
|
|
2013-08-22 10:35:26 +00:00
|
|
|
// only for mysql
|
|
|
|
type UserBig struct {
|
2013-08-30 04:32:05 +00:00
|
|
|
Id uint64
|
|
|
|
Name string
|
2013-08-22 10:35:26 +00:00
|
|
|
}
|
|
|
|
|
2013-08-07 11:11:44 +00:00
|
|
|
type User struct {
|
2013-08-16 13:57:39 +00:00
|
|
|
Id int
|
2013-08-09 12:14:18 +00:00
|
|
|
UserName string `orm:"size(30);unique"`
|
|
|
|
Email string `orm:"size(100)"`
|
|
|
|
Password string `orm:"size(100)"`
|
2014-03-19 01:46:09 +00:00
|
|
|
Status int16 `orm:"column(Status)"`
|
2013-08-09 12:14:18 +00:00
|
|
|
IsStaff bool
|
|
|
|
IsActive bool `orm:"default(1)"`
|
|
|
|
Created time.Time `orm:"auto_now_add;type(date)"`
|
|
|
|
Updated time.Time `orm:"auto_now"`
|
|
|
|
Profile *Profile `orm:"null;rel(one);on_delete(set_null)"`
|
|
|
|
Posts []*Post `orm:"reverse(many)" json:"-"`
|
|
|
|
ShouldSkip string `orm:"-"`
|
2013-10-09 12:28:54 +00:00
|
|
|
Nums int
|
2013-11-26 10:32:12 +00:00
|
|
|
Langs SliceStringField `orm:"size(100)"`
|
2013-11-26 13:34:40 +00:00
|
|
|
Extra JsonField `orm:"type(text)"`
|
2013-08-07 11:11:44 +00:00
|
|
|
}
|
|
|
|
|
2013-08-22 13:19:58 +00:00
|
|
|
func (u *User) TableIndex() [][]string {
|
|
|
|
return [][]string{
|
|
|
|
[]string{"Id", "UserName"},
|
2013-08-25 05:50:50 +00:00
|
|
|
[]string{"Id", "Created"},
|
2013-08-22 13:19:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) TableUnique() [][]string {
|
|
|
|
return [][]string{
|
|
|
|
[]string{"UserName", "Email"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-07 11:11:44 +00:00
|
|
|
func NewUser() *User {
|
|
|
|
obj := new(User)
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
|
|
|
type Profile struct {
|
2013-10-14 14:31:35 +00:00
|
|
|
Id int
|
|
|
|
Age int16
|
|
|
|
Money float64
|
|
|
|
User *User `orm:"reverse(one)" json:"-"`
|
|
|
|
BestPost *Post `orm:"rel(one);null"`
|
2013-08-07 11:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Profile) TableName() string {
|
|
|
|
return "user_profile"
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProfile() *Profile {
|
|
|
|
obj := new(Profile)
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
|
|
|
type Post struct {
|
2013-08-16 13:57:39 +00:00
|
|
|
Id int
|
|
|
|
User *User `orm:"rel(fk)"`
|
2013-08-07 11:11:44 +00:00
|
|
|
Title string `orm:"size(60)"`
|
2013-08-16 14:24:10 +00:00
|
|
|
Content string `orm:"type(text)"`
|
2013-08-07 11:11:44 +00:00
|
|
|
Created time.Time `orm:"auto_now_add"`
|
|
|
|
Updated time.Time `orm:"auto_now"`
|
2013-11-24 11:29:48 +00:00
|
|
|
Tags []*Tag `orm:"rel(m2m);rel_through(github.com/astaxie/beego/orm.PostTags)"`
|
2013-08-07 11:11:44 +00:00
|
|
|
}
|
|
|
|
|
2013-08-25 05:50:50 +00:00
|
|
|
func (u *Post) TableIndex() [][]string {
|
|
|
|
return [][]string{
|
|
|
|
[]string{"Id", "Created"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-07 11:11:44 +00:00
|
|
|
func NewPost() *Post {
|
|
|
|
obj := new(Post)
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
|
|
|
type Tag struct {
|
2013-10-14 14:31:35 +00:00
|
|
|
Id int
|
|
|
|
Name string `orm:"size(30)"`
|
|
|
|
BestPost *Post `orm:"rel(one);null"`
|
|
|
|
Posts []*Post `orm:"reverse(many)" json:"-"`
|
2013-08-07 11:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTag() *Tag {
|
|
|
|
obj := new(Tag)
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
2013-11-24 11:29:48 +00:00
|
|
|
type PostTags struct {
|
|
|
|
Id int
|
|
|
|
Post *Post `orm:"rel(fk)"`
|
|
|
|
Tag *Tag `orm:"rel(fk)"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *PostTags) TableName() string {
|
|
|
|
return "prefix_post_tags"
|
|
|
|
}
|
|
|
|
|
2013-08-07 11:11:44 +00:00
|
|
|
type Comment struct {
|
2013-08-16 13:57:39 +00:00
|
|
|
Id int
|
2013-11-06 14:05:10 +00:00
|
|
|
Post *Post `orm:"rel(fk);column(post)"`
|
2013-08-16 14:24:10 +00:00
|
|
|
Content string `orm:"type(text)"`
|
2013-08-07 11:11:44 +00:00
|
|
|
Parent *Comment `orm:"null;rel(fk)"`
|
|
|
|
Created time.Time `orm:"auto_now_add"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewComment() *Comment {
|
|
|
|
obj := new(Comment)
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
|
|
|
var DBARGS = struct {
|
|
|
|
Driver string
|
|
|
|
Source string
|
2013-08-09 05:20:19 +00:00
|
|
|
Debug string
|
2013-08-07 11:11:44 +00:00
|
|
|
}{
|
|
|
|
os.Getenv("ORM_DRIVER"),
|
|
|
|
os.Getenv("ORM_SOURCE"),
|
2013-08-09 05:20:19 +00:00
|
|
|
os.Getenv("ORM_DEBUG"),
|
2013-08-07 11:11:44 +00:00
|
|
|
}
|
|
|
|
|
2013-08-10 16:15:26 +00:00
|
|
|
var (
|
|
|
|
IsMysql = DBARGS.Driver == "mysql"
|
|
|
|
IsSqlite = DBARGS.Driver == "sqlite3"
|
|
|
|
IsPostgres = DBARGS.Driver == "postgres"
|
|
|
|
)
|
|
|
|
|
2013-09-09 14:33:41 +00:00
|
|
|
var (
|
|
|
|
dORM Ormer
|
|
|
|
dDbBaser dbBaser
|
|
|
|
)
|
2013-08-07 11:11:44 +00:00
|
|
|
|
|
|
|
func init() {
|
2013-08-09 05:20:19 +00:00
|
|
|
Debug, _ = StrTo(DBARGS.Debug).Bool()
|
|
|
|
|
2013-08-07 11:11:44 +00:00
|
|
|
if DBARGS.Driver == "" || DBARGS.Source == "" {
|
|
|
|
fmt.Println(`need driver and source!
|
|
|
|
|
|
|
|
Default DB Drivers.
|
|
|
|
|
|
|
|
driver: url
|
|
|
|
mysql: https://github.com/go-sql-driver/mysql
|
|
|
|
sqlite3: https://github.com/mattn/go-sqlite3
|
2013-08-10 16:15:26 +00:00
|
|
|
postgres: https://github.com/lib/pq
|
2013-08-07 11:11:44 +00:00
|
|
|
|
2013-08-19 14:37:39 +00:00
|
|
|
usage:
|
2013-08-07 11:11:44 +00:00
|
|
|
|
2013-08-19 14:37:39 +00:00
|
|
|
go get -u github.com/astaxie/beego/orm
|
|
|
|
go get -u github.com/go-sql-driver/mysql
|
|
|
|
go get -u github.com/mattn/go-sqlite3
|
|
|
|
go get -u github.com/lib/pq
|
2013-08-07 11:11:44 +00:00
|
|
|
|
2013-08-19 14:37:39 +00:00
|
|
|
#### MySQL
|
|
|
|
mysql -u root -e 'create database orm_test;'
|
|
|
|
export ORM_DRIVER=mysql
|
|
|
|
export ORM_SOURCE="root:@/orm_test?charset=utf8"
|
|
|
|
go test -v github.com/astaxie/beego/orm
|
2013-08-07 11:11:44 +00:00
|
|
|
|
|
|
|
|
2013-08-19 14:37:39 +00:00
|
|
|
#### Sqlite3
|
|
|
|
export ORM_DRIVER=sqlite3
|
2014-02-28 03:53:35 +00:00
|
|
|
export ORM_SOURCE='file:memory_test?mode=memory'
|
2013-08-19 14:37:39 +00:00
|
|
|
go test -v github.com/astaxie/beego/orm
|
2013-08-10 16:15:26 +00:00
|
|
|
|
2013-08-19 14:37:39 +00:00
|
|
|
|
|
|
|
#### PostgreSQL
|
|
|
|
psql -c 'create database orm_test;' -U postgres
|
|
|
|
export ORM_DRIVER=postgres
|
|
|
|
export ORM_SOURCE="user=postgres dbname=orm_test sslmode=disable"
|
|
|
|
go test -v github.com/astaxie/beego/orm
|
|
|
|
`)
|
|
|
|
os.Exit(2)
|
2013-08-07 11:11:44 +00:00
|
|
|
}
|
2013-08-19 14:37:39 +00:00
|
|
|
|
|
|
|
RegisterDataBase("default", DBARGS.Driver, DBARGS.Source, 20)
|
2013-09-16 01:48:04 +00:00
|
|
|
|
|
|
|
alias := getDbAlias("default")
|
|
|
|
if alias.Driver == DR_MySQL {
|
|
|
|
alias.Engine = "INNODB"
|
|
|
|
}
|
|
|
|
|
2013-08-07 11:11:44 +00:00
|
|
|
}
|