Documentation ¶
Overview ¶
Package orm provide ORM for MySQL/PostgreSQL/sqlite Simple Usage
package main import ( "fmt" "github.com/Sonek-HoangBui/beego/v2/client/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 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 RegisterModelWithSuffix(suffix 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 SetNameStrategy(s string)
- 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) AndNotCond(cond *Condition) *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
- func (c *Condition) OrNotCond(cond *Condition) *Condition
- func (c Condition) Raw(expr string, sql string) *Condition
- type DB
- func (d *DB) Begin() (*sql.Tx, error)
- func (d *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
- func (d *DB) Exec(query string, args ...interface{}) (sql.Result, error)
- func (d *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (d *DB) Prepare(query string) (*sql.Stmt, error)
- func (d *DB) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
- func (d *DB) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (d *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (d *DB) QueryRow(query string, args ...interface{}) *sql.Row
- func (d *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
- type DateField
- type DateTimeField
- type Driver
- type DriverType
- type Fielder
- type FloatField
- type Inserter
- type IntegerField
- type JSONField
- type JsonbField
- 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) ForUpdate() 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
- type TiDBQueryBuilder
- func (qb *TiDBQueryBuilder) And(cond string) QueryBuilder
- func (qb *TiDBQueryBuilder) Asc() QueryBuilder
- func (qb *TiDBQueryBuilder) Delete(tables ...string) QueryBuilder
- func (qb *TiDBQueryBuilder) Desc() QueryBuilder
- func (qb *TiDBQueryBuilder) ForUpdate() QueryBuilder
- func (qb *TiDBQueryBuilder) From(tables ...string) QueryBuilder
- func (qb *TiDBQueryBuilder) GroupBy(fields ...string) QueryBuilder
- func (qb *TiDBQueryBuilder) Having(cond string) QueryBuilder
- func (qb *TiDBQueryBuilder) In(vals ...string) QueryBuilder
- func (qb *TiDBQueryBuilder) InnerJoin(table string) QueryBuilder
- func (qb *TiDBQueryBuilder) InsertInto(table string, fields ...string) QueryBuilder
- func (qb *TiDBQueryBuilder) LeftJoin(table string) QueryBuilder
- func (qb *TiDBQueryBuilder) Limit(limit int) QueryBuilder
- func (qb *TiDBQueryBuilder) Offset(offset int) QueryBuilder
- func (qb *TiDBQueryBuilder) On(cond string) QueryBuilder
- func (qb *TiDBQueryBuilder) Or(cond string) QueryBuilder
- func (qb *TiDBQueryBuilder) OrderBy(fields ...string) QueryBuilder
- func (qb *TiDBQueryBuilder) RightJoin(table string) QueryBuilder
- func (qb *TiDBQueryBuilder) Select(fields ...string) QueryBuilder
- func (qb *TiDBQueryBuilder) Set(kv ...string) QueryBuilder
- func (qb *TiDBQueryBuilder) String() string
- func (qb *TiDBQueryBuilder) Subquery(sub string, alias string) string
- func (qb *TiDBQueryBuilder) Update(tables ...string) QueryBuilder
- func (qb *TiDBQueryBuilder) Values(vals ...string) QueryBuilder
- func (qb *TiDBQueryBuilder) Where(cond string) QueryBuilder
- type TimeField
Constants ¶
const ( DRMySQL = DriverType(orm.DRMySQL) DRSqlite = DriverType(orm.DRSqlite) // sqlite DROracle = DriverType(orm.DROracle) // oracle DRPostgres = DriverType(orm.DRPostgres) // pgsql DRTiDB = DriverType(orm.DRTiDB) // TiDB )
Enum the Database driver
const ( TypeBooleanField = orm.TypeBooleanField TypeVarCharField = orm.TypeVarCharField TypeCharField = orm.TypeCharField TypeTextField = orm.TypeTextField TypeTimeField = orm.TypeTimeField TypeDateField = orm.TypeDateField TypeDateTimeField = orm.TypeDateTimeField TypeBitField = orm.TypeBitField TypeSmallIntegerField = orm.TypeSmallIntegerField TypeIntegerField = orm.TypeIntegerField TypeBigIntegerField = orm.TypeBigIntegerField TypePositiveBitField = orm.TypePositiveBitField TypePositiveSmallIntegerField = orm.TypePositiveSmallIntegerField TypePositiveIntegerField = orm.TypePositiveIntegerField TypePositiveBigIntegerField = orm.TypePositiveBigIntegerField TypeFloatField = orm.TypeFloatField TypeDecimalField = orm.TypeDecimalField TypeJSONField = orm.TypeJSONField TypeJsonbField = orm.TypeJsonbField RelForeignKey = orm.RelForeignKey RelOneToOne = orm.RelOneToOne RelManyToMany = orm.RelManyToMany RelReverseOne = orm.RelReverseOne RelReverseMany = orm.RelReverseMany )
Define the Type enum
const ( IsIntegerField = orm.IsIntegerField IsPositiveIntegerField = orm.IsPositiveIntegerField IsRelField = orm.IsRelField IsFieldType = orm.IsFieldType )
Define some logic enum
const ( ColAdd = orm.ColAdd ColMinus = orm.ColMinus ColMultiply = orm.ColMultiply ColExcept = orm.ColExcept ColBitAnd = orm.ColBitAnd ColBitRShift = orm.ColBitRShift ColBitLShift = orm.ColBitLShift ColBitXOR = orm.ColBitXOR ColBitOr = orm.ColBitOr )
define Col operations
const CommaSpace = orm.CommaSpace
CommaSpace is the separation
const (
DebugQueries = iota
)
DebugQueries define the debug
const (
ExprSep = "__"
)
ExprSep define the expression separation
Variables ¶
var ( Debug = orm.Debug DebugLog = orm.DebugLog DefaultRowsLimit = orm.DefaultRowsLimit DefaultRelsDepth = orm.DefaultRelsDepth DefaultTimeLoc = orm.DefaultTimeLoc 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") )
Define common vars
var ( // ErrMissPK missing pk error ErrMissPK = orm.ErrMissPK )
var LogFunc = orm.LogFunc
costomer log func
var (
SnakeAcronymNameStrategy = "snakeStringWithAcronym"
)
Functions ¶
func AddAliasWthDB ¶
AddAliasWthDB add a aliasName for the drivename
func BootStrap ¶
func BootStrap()
BootStrap bootstrap models. make all model parsed and can not add more models
func GetDB ¶
GetDB Get *sql.DB from registered database by db alias name. Use "default" as alias name if you not set.
func RegisterDataBase ¶
RegisterDataBase Setting the database connect params. Use the database driver self dataSource args.
func RegisterDriver ¶
func RegisterDriver(driverName string, typ DriverType) error
RegisterDriver 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{})
RegisterModelWithPrefix register models with a prefix
func RegisterModelWithSuffix ¶
func RegisterModelWithSuffix(suffix string, models ...interface{})
RegisterModelWithSuffix register models with a suffix
func ResetModelCache ¶
func ResetModelCache()
ResetModelCache Clean model cache. Then you can re-RegisterModel. Common use this api for test case.
func RunCommand ¶
func RunCommand()
RunCommand listen for orm command and then run it if command arguments passed.
func SetDataBaseTZ ¶
SetDataBaseTZ Change the database default used timezone
func SetMaxIdleConns ¶
SetMaxIdleConns Change the max idle conns for *sql.DB, use specify database alias name
func SetMaxOpenConns ¶
SetMaxOpenConns Change the max open conns for *sql.DB, use specify database alias name
Types ¶
type BigIntegerField ¶
type BigIntegerField orm.BigIntegerField
BigIntegerField -9223372036854775808 to 9223372036854775807.
func (*BigIntegerField) FieldType ¶
func (e *BigIntegerField) FieldType() int
FieldType return enum type
func (*BigIntegerField) RawValue ¶
func (e *BigIntegerField) RawValue() interface{}
RawValue return BigIntegerField value
func (*BigIntegerField) SetRaw ¶
func (e *BigIntegerField) SetRaw(value interface{}) error
SetRaw convert interface int64/string to int64
func (*BigIntegerField) String ¶
func (e *BigIntegerField) String() string
String convert BigIntegerField to string
type BooleanField ¶
type BooleanField orm.BooleanField
BooleanField A true/false field.
func (*BooleanField) FieldType ¶
func (e *BooleanField) FieldType() int
FieldType return BooleanField the type
func (*BooleanField) RawValue ¶
func (e *BooleanField) RawValue() interface{}
RawValue return the current value
func (*BooleanField) SetRaw ¶
func (e *BooleanField) SetRaw(value interface{}) error
SetRaw set the interface to bool
func (*BooleanField) String ¶
func (e *BooleanField) String() string
String format the Bool to string
type CharField ¶
CharField A string field required values tag: size The size is enforced at the database level and in models’s validation. eg: `orm:"size(120)"`
func (*CharField) RawValue ¶
func (e *CharField) RawValue() interface{}
RawValue return the CharField value
type Condition ¶
Condition struct. work for WHERE conditions.
func (*Condition) AndNotCond ¶
AndNotCond combine a AND NOT condition to current condition
type DB ¶
func (*DB) ExecContext ¶
func (*DB) PrepareContext ¶
func (*DB) QueryContext ¶
type DateField ¶
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 ¶
type DateTimeField orm.DateTimeField
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
FieldType return the enum TypeDateTimeField
func (*DateTimeField) RawValue ¶
func (e *DateTimeField) RawValue() interface{}
RawValue return the datetime value
func (*DateTimeField) Set ¶
func (e *DateTimeField) Set(d time.Time)
Set set the time.Time to datetime
func (*DateTimeField) SetRaw ¶
func (e *DateTimeField) SetRaw(value interface{}) error
SetRaw convert the string or time.Time to DateTimeField
func (*DateTimeField) String ¶
func (e *DateTimeField) String() string
String return the time's String
func (DateTimeField) Value ¶
func (e DateTimeField) Value() time.Time
Value return the datetime value
type FloatField ¶
type FloatField orm.FloatField
FloatField A floating-point number represented in go by a float32 value.
func (*FloatField) RawValue ¶
func (e *FloatField) RawValue() interface{}
RawValue return the FloatField value
func (*FloatField) SetRaw ¶
func (e *FloatField) SetRaw(value interface{}) error
SetRaw converter interface Float64 float32 or string to FloatField
type IntegerField ¶
type IntegerField orm.IntegerField
IntegerField -2147483648 to 2147483647
func (*IntegerField) FieldType ¶
func (e *IntegerField) FieldType() int
FieldType return the enum type
func (*IntegerField) RawValue ¶
func (e *IntegerField) RawValue() interface{}
RawValue return IntegerField value
func (*IntegerField) SetRaw ¶
func (e *IntegerField) SetRaw(value interface{}) error
SetRaw convert interface int32/string to int32
type JSONField ¶
JSONField postgres json field.
func (*JSONField) RawValue ¶
func (j *JSONField) RawValue() interface{}
RawValue return JSONField value
type JsonbField ¶
type JsonbField orm.JsonbField
JsonbField postgres json field.
func (*JsonbField) RawValue ¶
func (j *JsonbField) RawValue() interface{}
RawValue return JsonbField value
func (*JsonbField) SetRaw ¶
func (j *JsonbField) SetRaw(value interface{}) error
SetRaw convert interface string to string
func (*JsonbField) String ¶
func (j *JsonbField) String() string
String convert JsonbField to string
type MySQLQueryBuilder ¶
type MySQLQueryBuilder orm.MySQLQueryBuilder
MySQLQueryBuilder is the SQL build
func (*MySQLQueryBuilder) And ¶
func (qb *MySQLQueryBuilder) And(cond string) QueryBuilder
And join the and cond
func (*MySQLQueryBuilder) Delete ¶
func (qb *MySQLQueryBuilder) Delete(tables ...string) QueryBuilder
Delete join the Delete tables
func (*MySQLQueryBuilder) Desc ¶
func (qb *MySQLQueryBuilder) Desc() QueryBuilder
Desc join the desc
func (*MySQLQueryBuilder) ForUpdate ¶
func (qb *MySQLQueryBuilder) ForUpdate() QueryBuilder
ForUpdate add the FOR UPDATE clause
func (*MySQLQueryBuilder) From ¶
func (qb *MySQLQueryBuilder) From(tables ...string) QueryBuilder
From join the tables
func (*MySQLQueryBuilder) GroupBy ¶
func (qb *MySQLQueryBuilder) GroupBy(fields ...string) QueryBuilder
GroupBy join the Group by fields
func (*MySQLQueryBuilder) Having ¶
func (qb *MySQLQueryBuilder) Having(cond string) QueryBuilder
Having join the Having cond
func (*MySQLQueryBuilder) In ¶
func (qb *MySQLQueryBuilder) In(vals ...string) QueryBuilder
In join the IN (vals)
func (*MySQLQueryBuilder) InnerJoin ¶
func (qb *MySQLQueryBuilder) InnerJoin(table string) QueryBuilder
InnerJoin INNER JOIN the table
func (*MySQLQueryBuilder) InsertInto ¶
func (qb *MySQLQueryBuilder) InsertInto(table string, fields ...string) QueryBuilder
InsertInto join the insert SQL
func (*MySQLQueryBuilder) LeftJoin ¶
func (qb *MySQLQueryBuilder) LeftJoin(table string) QueryBuilder
LeftJoin LEFT JOIN the table
func (*MySQLQueryBuilder) Limit ¶
func (qb *MySQLQueryBuilder) Limit(limit int) QueryBuilder
Limit join the limit num
func (*MySQLQueryBuilder) Offset ¶
func (qb *MySQLQueryBuilder) Offset(offset int) QueryBuilder
Offset join the offset num
func (*MySQLQueryBuilder) On ¶
func (qb *MySQLQueryBuilder) On(cond string) QueryBuilder
On join with on cond
func (*MySQLQueryBuilder) Or ¶
func (qb *MySQLQueryBuilder) Or(cond string) QueryBuilder
Or join the or cond
func (*MySQLQueryBuilder) OrderBy ¶
func (qb *MySQLQueryBuilder) OrderBy(fields ...string) QueryBuilder
OrderBy join the Order by fields
func (*MySQLQueryBuilder) RightJoin ¶
func (qb *MySQLQueryBuilder) RightJoin(table string) QueryBuilder
RightJoin RIGHT JOIN the table
func (*MySQLQueryBuilder) Select ¶
func (qb *MySQLQueryBuilder) Select(fields ...string) QueryBuilder
Select will join the fields
func (*MySQLQueryBuilder) Set ¶
func (qb *MySQLQueryBuilder) Set(kv ...string) QueryBuilder
Set join the set kv
func (*MySQLQueryBuilder) String ¶
func (qb *MySQLQueryBuilder) String() string
String join all Tokens
func (*MySQLQueryBuilder) Subquery ¶
func (qb *MySQLQueryBuilder) Subquery(sub string, alias string) string
Subquery join the sub as alias
func (*MySQLQueryBuilder) Update ¶
func (qb *MySQLQueryBuilder) Update(tables ...string) QueryBuilder
Update join the update table
func (*MySQLQueryBuilder) Values ¶
func (qb *MySQLQueryBuilder) Values(vals ...string) QueryBuilder
Values join the Values(vals)
func (*MySQLQueryBuilder) Where ¶
func (qb *MySQLQueryBuilder) Where(cond string) QueryBuilder
Where join the Where cond
type Ormer ¶
type Ormer interface { // read data to model // for example: // this will find User by Id field // u = &User{Id: user.Id} // err = Ormer.Read(u) // this will find User by UserName field // u = &User{UserName: "astaxie", Password: "pass"} // err = Ormer.Read(u, "UserName") Read(md interface{}, cols ...string) error // Like Read(), but with "FOR UPDATE" clause, useful in transaction. // Some databases are not support this feature. ReadForUpdate(md interface{}, cols ...string) error // Try to read a row from the database, or insert one if it doesn't exist ReadOrCreate(md interface{}, col1 string, cols ...string) (bool, int64, error) // insert model data to database // for example: // user := new(User) // id, err = Ormer.Insert(user) // user must be a pointer and Insert will set user's pk field Insert(interface{}) (int64, error) // mysql:InsertOrUpdate(model) or InsertOrUpdate(model,"colu=colu+value") // if colu type is integer : can use(+-*/), string : convert(colu,"value") // postgres: InsertOrUpdate(model,"conflictColumnName") or InsertOrUpdate(model,"conflictColumnName","colu=colu+value") // if colu type is integer : can use(+-*/), string : colu || "value" InsertOrUpdate(md interface{}, colConflitAndArgs ...string) (int64, error) // insert some models to database InsertMulti(bulk int, mds interface{}) (int64, error) // update model to database. // cols set the columns those want to update. // find model by Id(pk) field and update columns specified by fields, if cols is null then update all columns // for example: // user := User{Id: 2} // user.Langs = append(user.Langs, "zh-CN", "en-US") // user.Extra.Name = "beego" // user.Extra.Data = "orm" // num, err = Ormer.Update(&user, "Langs", "Extra") Update(md interface{}, cols ...string) (int64, error) // delete model in database Delete(md interface{}, cols ...string) (int64, error) // load related models to md model. // args are limit, offset int and order string. // // example: // Ormer.LoadRelated(post,"Tags") // for _,tag := range post.Tags{...} // args[0] bool true useDefaultRelsDepth ; false depth 0 // args[0] int loadRelationDepth // args[1] int limit default limit 1000 // args[2] int offset default offset 0 // args[3] string order for example : "-Id" // make sure the relation is defined in model struct tags. LoadRelated(md interface{}, name string, args ...interface{}) (int64, error) // create a models to models queryer // for example: // post := Post{Id: 4} // m2m := Ormer.QueryM2M(&post, "Tags") QueryM2M(md interface{}, name string) QueryM2Mer // return a QuerySeter for table operations. // table name can be string or struct. // e.g. QueryTable("user"), QueryTable(&user{}) or QueryTable((*User)(nil)), QueryTable(ptrStructOrTableName interface{}) QuerySeter // switch to another registered database driver by given name. Using(name string) error // begin transaction // for example: // o := NewOrm() // err := o.Begin() // ... // err = o.Rollback() Begin() error // begin transaction with provided context and option // the provided context is used until the transaction is committed or rolled back. // if the context is canceled, the transaction will be rolled back. // the provided TxOptions is optional and may be nil if defaults should be used. // if a non-default isolation level is used that the driver doesn't support, an error will be returned. // for example: // o := NewOrm() // err := o.BeginTx(context.Background(), &sql.TxOptions{Isolation: sql.LevelRepeatableRead}) // ... // err = o.Rollback() BeginTx(ctx context.Context, opts *sql.TxOptions) error // commit transaction Commit() error // rollback transaction Rollback() error // return a raw query seter for raw sql string. // for example: // ormer.Raw("UPDATE `user` SET `user_name` = ? WHERE `user_name` = ?", "slene", "testing").Exec() // // update user testing's name to slene Raw(query string, args ...interface{}) RawSeter Driver() Driver DBStats() *sql.DBStats }
Ormer define the orm interface
type PositiveBigIntegerField ¶
type PositiveBigIntegerField orm.PositiveBigIntegerField
PositiveBigIntegerField 0 to 18446744073709551615
func (*PositiveBigIntegerField) FieldType ¶
func (e *PositiveBigIntegerField) FieldType() int
FieldType return enum type
func (*PositiveBigIntegerField) RawValue ¶
func (e *PositiveBigIntegerField) RawValue() interface{}
RawValue return PositiveBigIntegerField value
func (*PositiveBigIntegerField) Set ¶
func (e *PositiveBigIntegerField) Set(d uint64)
Set PositiveBigIntegerField value
func (*PositiveBigIntegerField) SetRaw ¶
func (e *PositiveBigIntegerField) SetRaw(value interface{}) error
SetRaw convert interface uint64/string to Uint64
func (*PositiveBigIntegerField) String ¶
func (e *PositiveBigIntegerField) String() string
String convert PositiveBigIntegerField to string
func (PositiveBigIntegerField) Value ¶
func (e PositiveBigIntegerField) Value() uint64
Value return uint64
type PositiveIntegerField ¶
type PositiveIntegerField orm.PositiveIntegerField
PositiveIntegerField 0 to 4294967295
func (*PositiveIntegerField) FieldType ¶
func (e *PositiveIntegerField) FieldType() int
FieldType return enum type
func (*PositiveIntegerField) RawValue ¶
func (e *PositiveIntegerField) RawValue() interface{}
RawValue return the PositiveIntegerField Value
func (*PositiveIntegerField) Set ¶
func (e *PositiveIntegerField) Set(d uint32)
Set the PositiveIntegerField value
func (*PositiveIntegerField) SetRaw ¶
func (e *PositiveIntegerField) SetRaw(value interface{}) error
SetRaw convert interface uint32/string to Uint32
func (*PositiveIntegerField) String ¶
func (e *PositiveIntegerField) String() string
String convert PositiveIntegerField to string
func (PositiveIntegerField) Value ¶
func (e PositiveIntegerField) Value() uint32
Value return PositiveIntegerField value. Uint32
type PositiveSmallIntegerField ¶
type PositiveSmallIntegerField orm.PositiveSmallIntegerField
PositiveSmallIntegerField 0 to 65535
func (*PositiveSmallIntegerField) FieldType ¶
func (e *PositiveSmallIntegerField) FieldType() int
FieldType return enum type
func (*PositiveSmallIntegerField) RawValue ¶
func (e *PositiveSmallIntegerField) RawValue() interface{}
RawValue returns PositiveSmallIntegerField value
func (*PositiveSmallIntegerField) Set ¶
func (e *PositiveSmallIntegerField) Set(d uint16)
Set PositiveSmallIntegerField value
func (*PositiveSmallIntegerField) SetRaw ¶
func (e *PositiveSmallIntegerField) SetRaw(value interface{}) error
SetRaw convert Interface uint16/string to uint16
func (*PositiveSmallIntegerField) String ¶
func (e *PositiveSmallIntegerField) String() string
String convert uint16 to string
func (PositiveSmallIntegerField) Value ¶
func (e PositiveSmallIntegerField) Value() uint16
Value return uint16
type QueryBuilder ¶
type QueryBuilder orm.QueryBuilder
QueryBuilder is the Query builder interface
func NewQueryBuilder ¶
func NewQueryBuilder(driver string) (qb QueryBuilder, err error)
NewQueryBuilder return the QueryBuilder
type QueryM2Mer ¶
type QueryM2Mer orm.QueryM2Mer
QueryM2Mer model to model query struct all operations are on the m2m table only, will not affect the origin model table
type RawSeter ¶
RawSeter raw query seter create From Ormer.Raw for example:
sql := fmt.Sprintf("SELECT %sid%s,%sname%s FROM %suser%s WHERE id = ?",Q,Q,Q,Q,Q,Q) rs := Ormer.Raw(sql, 1)
type SmallIntegerField ¶
type SmallIntegerField orm.SmallIntegerField
SmallIntegerField -32768 to 32767
func (*SmallIntegerField) FieldType ¶
func (e *SmallIntegerField) FieldType() int
FieldType return enum type SmallIntegerField
func (*SmallIntegerField) RawValue ¶
func (e *SmallIntegerField) RawValue() interface{}
RawValue return smallint value
func (*SmallIntegerField) Set ¶
func (e *SmallIntegerField) Set(d int16)
Set the SmallIntegerField value
func (*SmallIntegerField) SetRaw ¶
func (e *SmallIntegerField) SetRaw(value interface{}) error
SetRaw convert interface int16/string to int16
func (*SmallIntegerField) String ¶
func (e *SmallIntegerField) String() string
String convert smallint to string
type TextField ¶
TextField A large text field.
func (*TextField) RawValue ¶
func (e *TextField) RawValue() interface{}
RawValue return TextField value
type TiDBQueryBuilder ¶
type TiDBQueryBuilder orm.TiDBQueryBuilder
TiDBQueryBuilder is the SQL build
func (*TiDBQueryBuilder) And ¶
func (qb *TiDBQueryBuilder) And(cond string) QueryBuilder
And join the and cond
func (*TiDBQueryBuilder) Delete ¶
func (qb *TiDBQueryBuilder) Delete(tables ...string) QueryBuilder
Delete join the Delete tables
func (*TiDBQueryBuilder) ForUpdate ¶
func (qb *TiDBQueryBuilder) ForUpdate() QueryBuilder
ForUpdate add the FOR UPDATE clause
func (*TiDBQueryBuilder) From ¶
func (qb *TiDBQueryBuilder) From(tables ...string) QueryBuilder
From join the tables
func (*TiDBQueryBuilder) GroupBy ¶
func (qb *TiDBQueryBuilder) GroupBy(fields ...string) QueryBuilder
GroupBy join the Group by fields
func (*TiDBQueryBuilder) Having ¶
func (qb *TiDBQueryBuilder) Having(cond string) QueryBuilder
Having join the Having cond
func (*TiDBQueryBuilder) In ¶
func (qb *TiDBQueryBuilder) In(vals ...string) QueryBuilder
In join the IN (vals)
func (*TiDBQueryBuilder) InnerJoin ¶
func (qb *TiDBQueryBuilder) InnerJoin(table string) QueryBuilder
InnerJoin INNER JOIN the table
func (*TiDBQueryBuilder) InsertInto ¶
func (qb *TiDBQueryBuilder) InsertInto(table string, fields ...string) QueryBuilder
InsertInto join the insert SQL
func (*TiDBQueryBuilder) LeftJoin ¶
func (qb *TiDBQueryBuilder) LeftJoin(table string) QueryBuilder
LeftJoin LEFT JOIN the table
func (*TiDBQueryBuilder) Limit ¶
func (qb *TiDBQueryBuilder) Limit(limit int) QueryBuilder
Limit join the limit num
func (*TiDBQueryBuilder) Offset ¶
func (qb *TiDBQueryBuilder) Offset(offset int) QueryBuilder
Offset join the offset num
func (*TiDBQueryBuilder) On ¶
func (qb *TiDBQueryBuilder) On(cond string) QueryBuilder
On join with on cond
func (*TiDBQueryBuilder) Or ¶
func (qb *TiDBQueryBuilder) Or(cond string) QueryBuilder
Or join the or cond
func (*TiDBQueryBuilder) OrderBy ¶
func (qb *TiDBQueryBuilder) OrderBy(fields ...string) QueryBuilder
OrderBy join the Order by fields
func (*TiDBQueryBuilder) RightJoin ¶
func (qb *TiDBQueryBuilder) RightJoin(table string) QueryBuilder
RightJoin RIGHT JOIN the table
func (*TiDBQueryBuilder) Select ¶
func (qb *TiDBQueryBuilder) Select(fields ...string) QueryBuilder
Select will join the fields
func (*TiDBQueryBuilder) Set ¶
func (qb *TiDBQueryBuilder) Set(kv ...string) QueryBuilder
Set join the set kv
func (*TiDBQueryBuilder) String ¶
func (qb *TiDBQueryBuilder) String() string
String join all Tokens
func (*TiDBQueryBuilder) Subquery ¶
func (qb *TiDBQueryBuilder) Subquery(sub string, alias string) string
Subquery join the sub as alias
func (*TiDBQueryBuilder) Update ¶
func (qb *TiDBQueryBuilder) Update(tables ...string) QueryBuilder
Update join the update table
func (*TiDBQueryBuilder) Values ¶
func (qb *TiDBQueryBuilder) Values(vals ...string) QueryBuilder
Values join the Values(vals)
func (*TiDBQueryBuilder) Where ¶
func (qb *TiDBQueryBuilder) Where(cond string) QueryBuilder
Where join the Where cond
type TimeField ¶
TimeField A time, represented in go by a time.Time instance. only time values like 10:00:00 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"`