Documentation ¶
Overview ¶
Simple Usage
package main import ( "fmt" "github.com/astaxie/beego/orm" _ "github.com/go-sql-driver/mysql" // import your used driver ) // Model Struct type User struct { Id int `orm:"auto"` Name string `orm:"size(100)"` } func init() { orm.RegisterDataBase("default", "mysql", "root:root@/my_db?charset=utf8", 30) } func main() { o := orm.NewOrm() user := User{Name: "slene"} // insert id, err := o.Insert(&user) // update user.Name = "astaxie" num, err := o.Update(&user) // read one u := User{Id: user.Id} err = o.Read(&u) // delete num, err = o.Delete(&u) }
more docs: http://beego.me/docs/mvc/model/overview.md
Index ¶
- Constants
- Variables
- func AddAliasWthDB(aliasName, driverName string, db *sql.DB) error
- func BootStrap()
- func ColValue(opt operator, value interface{}) interface{}
- func GetDB(aliasNames ...string) (*sql.DB, error)
- func RegisterDataBase(aliasName, driverName, dataSource string, params ...int) error
- func RegisterDriver(driverName string, typ DriverType) error
- func RegisterModel(models ...interface{})
- func RegisterModelWithPrefix(prefix string, models ...interface{})
- func ResetModelCache()
- func RunCommand()
- func RunSyncdb(name string, force bool, verbose bool) error
- func SetDataBaseTZ(aliasName string, tz *time.Location) error
- func SetMaxIdleConns(aliasName string, maxIdleConns int)
- func SetMaxOpenConns(aliasName string, maxOpenConns int)
- func ToInt64(value interface{}) (d int64)
- func ToStr(value interface{}, args ...int) (s string)
- type BigIntegerField
- type BooleanField
- type CharField
- type Condition
- func (c Condition) And(expr string, args ...interface{}) *Condition
- func (c *Condition) AndCond(cond *Condition) *Condition
- func (c Condition) AndNot(expr string, args ...interface{}) *Condition
- func (c *Condition) IsEmpty() bool
- func (c Condition) Or(expr string, args ...interface{}) *Condition
- func (c *Condition) OrCond(cond *Condition) *Condition
- func (c Condition) OrNot(expr string, args ...interface{}) *Condition
- type DateField
- type DateTimeField
- type Driver
- type DriverType
- type Fielder
- type FloatField
- type Inserter
- type IntegerField
- type Log
- type MySQLQueryBuilder
- func (qb *MySQLQueryBuilder) And(cond string) QueryBuilder
- func (qb *MySQLQueryBuilder) Asc() QueryBuilder
- func (qb *MySQLQueryBuilder) Delete(tables ...string) QueryBuilder
- func (qb *MySQLQueryBuilder) Desc() QueryBuilder
- func (qb *MySQLQueryBuilder) From(tables ...string) QueryBuilder
- func (qb *MySQLQueryBuilder) GroupBy(fields ...string) QueryBuilder
- func (qb *MySQLQueryBuilder) Having(cond string) QueryBuilder
- func (qb *MySQLQueryBuilder) In(vals ...string) QueryBuilder
- func (qb *MySQLQueryBuilder) InnerJoin(table string) QueryBuilder
- func (qb *MySQLQueryBuilder) InsertInto(table string, fields ...string) QueryBuilder
- func (qb *MySQLQueryBuilder) LeftJoin(table string) QueryBuilder
- func (qb *MySQLQueryBuilder) Limit(limit int) QueryBuilder
- func (qb *MySQLQueryBuilder) Offset(offset int) QueryBuilder
- func (qb *MySQLQueryBuilder) On(cond string) QueryBuilder
- func (qb *MySQLQueryBuilder) Or(cond string) QueryBuilder
- func (qb *MySQLQueryBuilder) OrderBy(fields ...string) QueryBuilder
- func (qb *MySQLQueryBuilder) RightJoin(table string) QueryBuilder
- func (qb *MySQLQueryBuilder) Select(fields ...string) QueryBuilder
- func (qb *MySQLQueryBuilder) Set(kv ...string) QueryBuilder
- func (qb *MySQLQueryBuilder) String() string
- func (qb *MySQLQueryBuilder) Subquery(sub string, alias string) string
- func (qb *MySQLQueryBuilder) Update(tables ...string) QueryBuilder
- func (qb *MySQLQueryBuilder) Values(vals ...string) QueryBuilder
- func (qb *MySQLQueryBuilder) Where(cond string) QueryBuilder
- type Ormer
- type Params
- type ParamsList
- type PositiveBigIntegerField
- func (e *PositiveBigIntegerField) FieldType() int
- func (e *PositiveBigIntegerField) RawValue() interface{}
- func (e *PositiveBigIntegerField) Set(d uint64)
- func (e *PositiveBigIntegerField) SetRaw(value interface{}) error
- func (e *PositiveBigIntegerField) String() string
- func (e PositiveBigIntegerField) Value() uint64
- type PositiveIntegerField
- func (e *PositiveIntegerField) FieldType() int
- func (e *PositiveIntegerField) RawValue() interface{}
- func (e *PositiveIntegerField) Set(d uint32)
- func (e *PositiveIntegerField) SetRaw(value interface{}) error
- func (e *PositiveIntegerField) String() string
- func (e PositiveIntegerField) Value() uint32
- type PositiveSmallIntegerField
- func (e *PositiveSmallIntegerField) FieldType() int
- func (e *PositiveSmallIntegerField) RawValue() interface{}
- func (e *PositiveSmallIntegerField) Set(d uint16)
- func (e *PositiveSmallIntegerField) SetRaw(value interface{}) error
- func (e *PositiveSmallIntegerField) String() string
- func (e PositiveSmallIntegerField) Value() uint16
- type QueryBuilder
- type QueryM2Mer
- type QuerySeter
- type RawPreparer
- type RawSeter
- type SmallIntegerField
- type StrTo
- func (f StrTo) Bool() (bool, error)
- func (f *StrTo) Clear()
- func (f StrTo) Exist() bool
- func (f StrTo) Float32() (float32, error)
- func (f StrTo) Float64() (float64, error)
- func (f StrTo) Int() (int, error)
- func (f StrTo) Int16() (int16, error)
- func (f StrTo) Int32() (int32, error)
- func (f StrTo) Int64() (int64, error)
- func (f StrTo) Int8() (int8, error)
- func (f *StrTo) Set(v string)
- func (f StrTo) String() string
- func (f StrTo) Uint() (uint, error)
- func (f StrTo) Uint16() (uint16, error)
- func (f StrTo) Uint32() (uint32, error)
- func (f StrTo) Uint64() (uint64, error)
- func (f StrTo) Uint8() (uint8, error)
- type TextField
Constants ¶
const ( // bool TypeBooleanField = 1 << iota // string TypeCharField // string TypeTextField // time.Time TypeDateField // time.Time TypeDateTimeField // int8 TypeBitField // int16 TypeSmallIntegerField // int32 TypeIntegerField // int64 TypeBigIntegerField // uint8 TypePositiveBitField // uint16 TypePositiveSmallIntegerField // uint32 TypePositiveIntegerField // uint64 TypePositiveBigIntegerField // float64 TypeFloatField // float64 TypeDecimalField RelForeignKey RelOneToOne RelManyToMany RelReverseOne RelReverseMany )
const ( IsIntegerField = ^-TypePositiveBigIntegerField >> 4 << 5 IsPostiveIntegerField = ^-TypePositiveBigIntegerField >> 8 << 9 IsRelField = ^-RelReverseMany >> 14 << 15 IsFieldType = ^-RelReverseMany<<1 + 1 )
const ( Col_Add operator = iota Col_Minus Col_Multiply Col_Except )
const COMMA_SPACE = ", "
const (
Debug_Queries = iota
)
const (
ExprSep = "__"
)
Variables ¶
var ( // DebugLevel = Debug_Queries Debug = false DebugLog = NewLog(os.Stderr) DefaultRowsLimit = 1000 DefaultRelsDepth = 2 DefaultTimeLoc = time.Local ErrTxHasBegan = errors.New("<Ormer.Begin> transaction already begin") ErrTxDone = errors.New("<Ormer.Commit/Rollback> transaction not begin") ErrMultiRows = errors.New("<QuerySeter> return multi rows") ErrNoRows = errors.New("<QuerySeter> no row found") ErrStmtClosed = errors.New("<QuerySeter> stmt already closed") ErrArgs = errors.New("<Ormer> args error may be empty") ErrNotImplement = errors.New("have not implement") )
var (
ErrMissPK = errors.New("missed pk value") // missing pk error
)
Functions ¶
func ColValue ¶
func ColValue(opt operator, value interface{}) interface{}
ColValue do the field raw changes. e.g Nums = Nums + 10. usage:
Params{ "Nums": ColValue(Col_Add, 10), }
func GetDB ¶
Get *sql.DB from registered database by db alias name. Use "default" as alias name if you not set.
func RegisterDataBase ¶
Setting the database connect params. Use the database driver self dataSource args.
func RegisterDriver ¶
func RegisterDriver(driverName string, typ DriverType) error
Register a database driver use specify driver name, this can be definition the driver is which database type.
func RegisterModelWithPrefix ¶
func RegisterModelWithPrefix(prefix string, models ...interface{})
register models with a prefix
func ResetModelCache ¶
func ResetModelCache()
Clean model cache. Then you can re-RegisterModel. Common use this api for test case.
func RunCommand ¶
func RunCommand()
listen for orm command and then run it if command arguments passed.
func RunSyncdb ¶
run syncdb command line. name means table's alias name. default is "default". force means run next sql if the current is error. verbose means show all info when running command or not.
func SetDataBaseTZ ¶
Change the database default used timezone
func SetMaxIdleConns ¶
Change the max idle conns for *sql.DB, use specify database alias name
func SetMaxOpenConns ¶
Change the max open conns for *sql.DB, use specify database alias name
Types ¶
type BigIntegerField ¶
type BigIntegerField int64
-9223372036854775808 to 9223372036854775807.
func (*BigIntegerField) FieldType ¶
func (e *BigIntegerField) FieldType() int
func (*BigIntegerField) RawValue ¶
func (e *BigIntegerField) RawValue() interface{}
func (*BigIntegerField) Set ¶
func (e *BigIntegerField) Set(d int64)
func (*BigIntegerField) SetRaw ¶
func (e *BigIntegerField) SetRaw(value interface{}) error
func (*BigIntegerField) String ¶
func (e *BigIntegerField) String() string
func (BigIntegerField) Value ¶
func (e BigIntegerField) Value() int64
type BooleanField ¶
type BooleanField bool
A true/false field.
func (*BooleanField) FieldType ¶
func (e *BooleanField) FieldType() int
func (*BooleanField) RawValue ¶
func (e *BooleanField) RawValue() interface{}
func (*BooleanField) Set ¶
func (e *BooleanField) Set(d bool)
func (*BooleanField) SetRaw ¶
func (e *BooleanField) SetRaw(value interface{}) error
func (*BooleanField) String ¶
func (e *BooleanField) String() string
func (BooleanField) Value ¶
func (e BooleanField) Value() bool
type CharField ¶
type CharField string
A string field required values tag: size The size is enforced at the database level and in models’s validation. eg: `orm:"size(120)"`
type Condition ¶
type Condition struct {
// contains filtered or unexported fields
}
condition struct. work for WHERE conditions.
type DateField ¶
A date, represented in go by a time.Time instance. only date values like 2006-01-02 Has a few extra, optional attr tag:
auto_now: Automatically set the field to now every time the object is saved. Useful for “last-modified” timestamps. Note that the current date is always used; it’s not just a default value that you can override.
auto_now_add: Automatically set the field to now when the object is first created. Useful for creation of timestamps. Note that the current date is always used; it’s not just a default value that you can override.
eg: `orm:"auto_now"` or `orm:"auto_now_add"`
type DateTimeField ¶
A date, represented in go by a time.Time instance. datetime values like 2006-01-02 15:04:05 Takes the same extra arguments as DateField.
func (*DateTimeField) FieldType ¶
func (e *DateTimeField) FieldType() int
func (*DateTimeField) RawValue ¶
func (e *DateTimeField) RawValue() interface{}
func (*DateTimeField) Set ¶
func (e *DateTimeField) Set(d time.Time)
func (*DateTimeField) SetRaw ¶
func (e *DateTimeField) SetRaw(value interface{}) error
func (*DateTimeField) String ¶
func (e *DateTimeField) String() string
func (DateTimeField) Value ¶
func (e DateTimeField) Value() time.Time
type DriverType ¶
type DriverType int
database driver constant int.
const ( DR_MySQL DriverType // mysql DR_Sqlite // sqlite DR_Oracle // oracle DR_Postgres // pgsql )
type Fielder ¶
type Fielder interface { String() string FieldType() int SetRaw(interface{}) error RawValue() interface{} }
field info
type FloatField ¶
type FloatField float64
A floating-point number represented in go by a float32 value.
func (*FloatField) FieldType ¶
func (e *FloatField) FieldType() int
func (*FloatField) RawValue ¶
func (e *FloatField) RawValue() interface{}
func (*FloatField) Set ¶
func (e *FloatField) Set(d float64)
func (*FloatField) SetRaw ¶
func (e *FloatField) SetRaw(value interface{}) error
func (*FloatField) String ¶
func (e *FloatField) String() string
func (FloatField) Value ¶
func (e FloatField) Value() float64
type IntegerField ¶
type IntegerField int32
-2147483648 to 2147483647
func (*IntegerField) FieldType ¶
func (e *IntegerField) FieldType() int
func (*IntegerField) RawValue ¶
func (e *IntegerField) RawValue() interface{}
func (*IntegerField) Set ¶
func (e *IntegerField) Set(d int32)
func (*IntegerField) SetRaw ¶
func (e *IntegerField) SetRaw(value interface{}) error
func (*IntegerField) String ¶
func (e *IntegerField) String() string
func (IntegerField) Value ¶
func (e IntegerField) Value() int32
type MySQLQueryBuilder ¶ added in v1.4.2
type MySQLQueryBuilder struct {
Tokens []string
}
func (*MySQLQueryBuilder) And ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) And(cond string) QueryBuilder
func (*MySQLQueryBuilder) Asc ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) Asc() QueryBuilder
func (*MySQLQueryBuilder) Delete ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) Delete(tables ...string) QueryBuilder
func (*MySQLQueryBuilder) Desc ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) Desc() QueryBuilder
func (*MySQLQueryBuilder) From ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) From(tables ...string) QueryBuilder
func (*MySQLQueryBuilder) GroupBy ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) GroupBy(fields ...string) QueryBuilder
func (*MySQLQueryBuilder) Having ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) Having(cond string) QueryBuilder
func (*MySQLQueryBuilder) In ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) In(vals ...string) QueryBuilder
func (*MySQLQueryBuilder) InnerJoin ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) InnerJoin(table string) QueryBuilder
func (*MySQLQueryBuilder) InsertInto ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) InsertInto(table string, fields ...string) QueryBuilder
func (*MySQLQueryBuilder) LeftJoin ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) LeftJoin(table string) QueryBuilder
func (*MySQLQueryBuilder) Limit ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) Limit(limit int) QueryBuilder
func (*MySQLQueryBuilder) Offset ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) Offset(offset int) QueryBuilder
func (*MySQLQueryBuilder) On ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) On(cond string) QueryBuilder
func (*MySQLQueryBuilder) Or ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) Or(cond string) QueryBuilder
func (*MySQLQueryBuilder) OrderBy ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) OrderBy(fields ...string) QueryBuilder
func (*MySQLQueryBuilder) RightJoin ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) RightJoin(table string) QueryBuilder
func (*MySQLQueryBuilder) Select ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) Select(fields ...string) QueryBuilder
func (*MySQLQueryBuilder) Set ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) Set(kv ...string) QueryBuilder
func (*MySQLQueryBuilder) String ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) String() string
func (*MySQLQueryBuilder) Subquery ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) Subquery(sub string, alias string) string
func (*MySQLQueryBuilder) Update ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) Update(tables ...string) QueryBuilder
func (*MySQLQueryBuilder) Values ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) Values(vals ...string) QueryBuilder
func (*MySQLQueryBuilder) Where ¶ added in v1.4.2
func (qb *MySQLQueryBuilder) Where(cond string) QueryBuilder
type Ormer ¶
type Ormer interface { Read(interface{}, ...string) error ReadOrCreate(interface{}, string, ...string) (bool, int64, error) Insert(interface{}) (int64, error) InsertMulti(int, interface{}) (int64, error) Update(interface{}, ...string) (int64, error) Delete(interface{}) (int64, error) LoadRelated(interface{}, string, ...interface{}) (int64, error) QueryM2M(interface{}, string) QueryM2Mer QueryTable(interface{}) QuerySeter Using(string) error Begin() error Commit() error Rollback() error Raw(string, ...interface{}) RawSeter Driver() Driver }
orm struct
type ParamsList ¶
type ParamsList []interface{}
type PositiveBigIntegerField ¶
type PositiveBigIntegerField uint64
0 to 18446744073709551615
func (*PositiveBigIntegerField) FieldType ¶
func (e *PositiveBigIntegerField) FieldType() int
func (*PositiveBigIntegerField) RawValue ¶
func (e *PositiveBigIntegerField) RawValue() interface{}
func (*PositiveBigIntegerField) Set ¶
func (e *PositiveBigIntegerField) Set(d uint64)
func (*PositiveBigIntegerField) SetRaw ¶
func (e *PositiveBigIntegerField) SetRaw(value interface{}) error
func (*PositiveBigIntegerField) String ¶
func (e *PositiveBigIntegerField) String() string
func (PositiveBigIntegerField) Value ¶
func (e PositiveBigIntegerField) Value() uint64
type PositiveIntegerField ¶
type PositiveIntegerField uint32
0 to 4294967295
func (*PositiveIntegerField) FieldType ¶
func (e *PositiveIntegerField) FieldType() int
func (*PositiveIntegerField) RawValue ¶
func (e *PositiveIntegerField) RawValue() interface{}
func (*PositiveIntegerField) Set ¶
func (e *PositiveIntegerField) Set(d uint32)
func (*PositiveIntegerField) SetRaw ¶
func (e *PositiveIntegerField) SetRaw(value interface{}) error
func (*PositiveIntegerField) String ¶
func (e *PositiveIntegerField) String() string
func (PositiveIntegerField) Value ¶
func (e PositiveIntegerField) Value() uint32
type PositiveSmallIntegerField ¶
type PositiveSmallIntegerField uint16
0 to 65535
func (*PositiveSmallIntegerField) FieldType ¶
func (e *PositiveSmallIntegerField) FieldType() int
func (*PositiveSmallIntegerField) RawValue ¶
func (e *PositiveSmallIntegerField) RawValue() interface{}
func (*PositiveSmallIntegerField) Set ¶
func (e *PositiveSmallIntegerField) Set(d uint16)
func (*PositiveSmallIntegerField) SetRaw ¶
func (e *PositiveSmallIntegerField) SetRaw(value interface{}) error
func (*PositiveSmallIntegerField) String ¶
func (e *PositiveSmallIntegerField) String() string
func (PositiveSmallIntegerField) Value ¶
func (e PositiveSmallIntegerField) Value() uint16
type QueryBuilder ¶ added in v1.4.2
type QueryBuilder interface { Select(fields ...string) QueryBuilder From(tables ...string) QueryBuilder InnerJoin(table string) QueryBuilder LeftJoin(table string) QueryBuilder RightJoin(table string) QueryBuilder On(cond string) QueryBuilder Where(cond string) QueryBuilder And(cond string) QueryBuilder Or(cond string) QueryBuilder In(vals ...string) QueryBuilder OrderBy(fields ...string) QueryBuilder Asc() QueryBuilder Desc() QueryBuilder Limit(limit int) QueryBuilder Offset(offset int) QueryBuilder GroupBy(fields ...string) QueryBuilder Having(cond string) QueryBuilder Update(tables ...string) QueryBuilder Set(kv ...string) QueryBuilder Delete(tables ...string) QueryBuilder InsertInto(table string, fields ...string) QueryBuilder Values(vals ...string) QueryBuilder Subquery(sub string, alias string) string String() string }
func NewQueryBuilder ¶ added in v1.4.2
func NewQueryBuilder(driver string) (qb QueryBuilder, err error)
type QueryM2Mer ¶
type QueryM2Mer interface { Add(...interface{}) (int64, error) Remove(...interface{}) (int64, error) Exist(interface{}) bool Clear() (int64, error) Count() (int64, error) }
model to model query struct
type QuerySeter ¶
type QuerySeter interface { Filter(string, ...interface{}) QuerySeter Exclude(string, ...interface{}) QuerySeter SetCond(*Condition) QuerySeter Limit(interface{}, ...interface{}) QuerySeter Offset(interface{}) QuerySeter OrderBy(...string) QuerySeter RelatedSel(...interface{}) QuerySeter Count() (int64, error) Exist() bool Update(Params) (int64, error) Delete() (int64, error) PrepareInsert() (Inserter, error) All(interface{}, ...string) (int64, error) One(interface{}, ...string) error Values(*[]Params, ...string) (int64, error) ValuesList(*[]ParamsList, ...string) (int64, error) ValuesFlat(*ParamsList, string) (int64, error) RowsToMap(*Params, string, string) (int64, error) RowsToStruct(interface{}, string, string) (int64, error) }
query seter
type RawPreparer ¶
raw query statement
type RawSeter ¶
type RawSeter interface { Exec() (sql.Result, error) QueryRow(...interface{}) error QueryRows(...interface{}) (int64, error) SetArgs(...interface{}) RawSeter Values(*[]Params, ...string) (int64, error) ValuesList(*[]ParamsList, ...string) (int64, error) ValuesFlat(*ParamsList, ...string) (int64, error) RowsToMap(*Params, string, string) (int64, error) RowsToStruct(interface{}, string, string) (int64, error) Prepare() (RawPreparer, error) }
raw query seter
type SmallIntegerField ¶
type SmallIntegerField int16
-32768 to 32767
func (*SmallIntegerField) FieldType ¶
func (e *SmallIntegerField) FieldType() int
func (*SmallIntegerField) RawValue ¶
func (e *SmallIntegerField) RawValue() interface{}
func (*SmallIntegerField) Set ¶
func (e *SmallIntegerField) Set(d int16)
func (*SmallIntegerField) SetRaw ¶
func (e *SmallIntegerField) SetRaw(value interface{}) error
func (*SmallIntegerField) String ¶
func (e *SmallIntegerField) String() string
func (SmallIntegerField) Value ¶
func (e SmallIntegerField) Value() int16
Source Files ¶
- cmd.go
- cmd_utils.go
- db.go
- db_alias.go
- db_mysql.go
- db_oracle.go
- db_postgres.go
- db_sqlite.go
- db_tables.go
- db_utils.go
- models.go
- models_boot.go
- models_fields.go
- models_info_f.go
- models_info_m.go
- models_utils.go
- orm.go
- orm_conds.go
- orm_log.go
- orm_object.go
- orm_querym2m.go
- orm_queryset.go
- orm_raw.go
- qb.go
- qb_mysql.go
- types.go
- utils.go