Documentation ¶
Index ¶
- Constants
- Variables
- func AddAlias(aliasName, driverName string) error
- func BootStrap()
- func ColValue(opt operator, value interface{}) interface{}
- func HasDefaultDataBase() bool
- func RegisterDataBase(aliasName, driverName, dataSource string, force bool, params ...int) (err 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 ReleaseDataBase(aliasName string)
- func ResetModelCache()
- func SetDataBaseTZ(aliasName string, tz *time.Location) error
- 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() (err error)
- func (d *DB) Commit() (err 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) (st *sql.Stmt, err 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
- func (d *DB) Rollback() (err error)
- type DateField
- type DateTimeField
- type DriverType
- type Fielder
- type FloatField
- type Index
- type IndexViewer
- type IntegerField
- type JSONField
- type JsonbField
- type Log
- type OperatorUpdate
- 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 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 TimeField
Constants ¶
const ( TypeBooleanField = 1 << iota TypeVarCharField TypeCharField TypeTextField TypeTimeField TypeDateField TypeDateTimeField TypeBitField TypeSmallIntegerField TypeIntegerField TypeBigIntegerField TypePositiveBitField TypePositiveSmallIntegerField TypePositiveIntegerField TypePositiveBigIntegerField TypeFloatField TypeDecimalField TypeJSONField TypeJsonbField RelForeignKey RelOneToOne RelManyToMany RelReverseOne RelReverseMany MongoStruct TypeInterface TypeSlice TypeUnknow )
Define the Type enum
const ( IsIntegerField = ^-TypePositiveBigIntegerField >> 6 << 7 IsPositiveIntegerField = ^-TypePositiveBigIntegerField >> 10 << 11 IsRelField = ^-RelReverseMany >> 18 << 19 IsFieldType = ^-TypeUnknow<<1 + 1 )
Define some logic enum
const ( ColAdd operator = iota ColMinus ColMultiply ColExcept )
define Col operations
const (
ExprSep = "__"
)
ExprSep define the expression separation
Variables ¶
var ( ErrNoDriver = errors.New("db driver has not registered") ErrDBConnection = errors.New("db connections error") )
var ( Debug = false DebugLog = NewLog(os.Stdout) DefaultRowsLimit = -1 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> err no rows") ErrStmtClosed = errors.New("<QuerySeter> stmt already closed") ErrArgs = errors.New("<Ormer> args error may be empty") ErrNotImplement = errors.New("have not implement") ErrHaveNoPK = errors.New("<Ormer> the PK value should not be nil") ErrNoDocuments = mongo.ErrNoDocuments )
var ( MongoNameStrategy = "noSnakeString" SnakeAcronymNameStrategy = "snakeStringWithAcronym" )
var (
ErrMissPK = errors.New("missed pk value")
)
var (
ErrNoIndexKey = errors.New("have not a index key")
)
var LogFunc func(query map[string]interface{})
costomer log func
Functions ¶
func BootStrap ¶
func BootStrap()
BootStrap bootstrap models. make all model parsed and can not add more models
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 HasDefaultDataBase ¶
func HasDefaultDataBase() bool
func RegisterDataBase ¶
func RegisterDataBase(aliasName, driverName, dataSource string, force bool, params ...int) (err error)
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 ReleaseDataBase ¶
func ReleaseDataBase(aliasName string)
func ResetModelCache ¶
func ResetModelCache()
ResetModelCache Clean model cache. Then you can re-RegisterModel. Common use this api for test case.
func SetDataBaseTZ ¶
SetDataBaseTZ Change the database default used timezone
Types ¶
type BigIntegerField ¶
type BigIntegerField int64
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 bool
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 ¶
type CharField string
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 ¶
type Condition struct {
// contains filtered or unexported fields
}
Condition struct. work for WHERE conditions.
func (*Condition) AndNotCond ¶
AndNotCond combine a AND NOT condition to current condition
type DB ¶
type DB struct { *sync.RWMutex DbType DriverType MDB *mongo.Database Session mongo.Session DB *sql.DB TX interface{} // contains filtered or unexported fields }
func (*DB) ExecContext ¶
func (*DB) PrepareContext ¶
func (*DB) QueryContext ¶
func (*DB) QueryRowContext ¶
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 ¶
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 DriverType ¶
type DriverType int
DriverType database driver constant int.
const ( DRMongo DriverType // MongoDB DRClickHouse // ClickHouse )
Enum the Database driver
type Fielder ¶
type Fielder interface { String() string FieldType() int SetRaw(interface{}) error RawValue() interface{} }
Fielder define field info
type FloatField ¶
type FloatField float64
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 Index ¶
type Index struct { Keys []string options.IndexOptions }
type IndexViewer ¶
type IntegerField ¶
type IntegerField int32
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 ¶
type JSONField string
JSONField postgres json field.
func (*JSONField) RawValue ¶
func (j *JSONField) RawValue() interface{}
RawValue return JSONField value
type JsonbField ¶
type JsonbField string
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 OperatorUpdate ¶
type OperatorUpdate string
var ( MgoSet OperatorUpdate = "$set" MgoUnSet OperatorUpdate = "$unset" MgoInc OperatorUpdate = "$inc" MgoPush OperatorUpdate = "$push" MgoPushAll OperatorUpdate = "$pushAll" MgoAddToSet OperatorUpdate = "$addToSet" MgoPop OperatorUpdate = "$pop" MgoPull OperatorUpdate = "$pull" MgoPullAll OperatorUpdate = "$pullAll" MgoRename OperatorUpdate = "$rename" MgoSetOnInsert OperatorUpdate = "$setOnInsert" )
var (
OpDefault OperatorUpdate = "$set"
)
type Ormer ¶
type Ormer interface { Read(interface{}, ...string) error ReadOrCreate(interface{}, string, ...string) (bool, interface{}, error) Insert(interface{}) (interface{}, error) InsertMulti(int, interface{}) (interface{}, error) // InsertOrUpdate(md interface{}, colConflitAndArgs ...string) (int64, error) Update(interface{}, ...string) (interface{}, error) Delete(interface{}, ...string) (interface{}, error) QueryTable(interface{}) QuerySeter Begin() error Commit() error Rollback() error Using(string) error RawDB() interface{} Raw(query string, args ...interface{}) RawSeter }
Ormer define the orm interface
type PositiveBigIntegerField ¶
type PositiveBigIntegerField uint64
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 uint32
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 uint16
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 QuerySeter ¶
type QuerySeter interface { Filter(string, ...interface{}) QuerySeter FilterRaw(string, string) QuerySeter Exclude(string, ...interface{}) QuerySeter SetCond(*Condition) QuerySeter GetCond() *Condition Limit(limit interface{}, args ...interface{}) QuerySeter Offset(interface{}) QuerySeter GroupBy(...string) QuerySeter OrderBy(...string) QuerySeter RelatedSel(...interface{}) QuerySeter ForUpdate() QuerySeter Count() (int64, error) Exist() bool Update(OperatorUpdate, Params) (int64, error) Delete() (int64, error) All(interface{}, ...string) error One(interface{}, ...string) error Distinct(string) ([]interface{}, error) Values(*[]Params, ...string) (int64, error) ValuesList(*[]ParamsList, ...string) (int64, error) ValuesFlat(*ParamsList, string) (int64, error) RowsToMap(result *Params, keyCol, valueCol string) (int64, error) RowsToStruct(ptrStruct interface{}, keyCol, valueCol string) (int64, error) IndexView() IndexViewer }
type RawPreparer ¶
RawPreparer raw query statement
type RawSeter ¶
type RawSeter interface { //execute sql and get result Exec() (sql.Result, error) //query data and map to container //for example: // var name string // var id int // rs.QueryRow(&id,&name) // id==2 name=="slene" QueryRow(containers ...interface{}) error // query data rows and map to container // var ids []int // var names []int // query = fmt.Sprintf("SELECT 'id','name' FROM %suser%s", Q, Q) // num, err = dORM.Raw(query).QueryRows(&ids,&names) // ids=>{1,2},names=>{"nobody","slene"} QueryRows(containers ...interface{}) (int64, error) SetArgs(...interface{}) RawSeter // query data to []map[string]interface // see QuerySeter's Values Values(container *[]Params, cols ...string) (int64, error) // query data to [][]interface // see QuerySeter's ValuesList ValuesList(container *[]ParamsList, cols ...string) (int64, error) // query data to []interface // see QuerySeter's ValuesFlat ValuesFlat(container *ParamsList, cols ...string) (int64, error) // query all rows into map[string]interface with specify key and value column name. // keyCol = "name", valueCol = "value" // table data // name | value // total | 100 // found | 200 // to map[string]interface{}{ // "total": 100, // "found": 200, // } RowsToMap(result *Params, keyCol, valueCol string) (int64, error) // query all rows into struct with specify key and value column name. // keyCol = "name", valueCol = "value" // table data // name | value // total | 100 // found | 200 // to struct { // Total int // Found int // } RowsToStruct(ptrStruct interface{}, keyCol, valueCol string) (int64, error) // return prepared raw statement for used in times. // for example: // pre, err := dORM.Raw("INSERT INTO tag (name) VALUES (?)").Prepare() // r, err := pre.Exec("name1") // INSERT INTO tag (name) VALUES (`name1`) Prepare() (RawPreparer, error) }
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 int16
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 ¶
type TextField string
TextField A large text field.
func (*TextField) RawValue ¶
func (e *TextField) RawValue() interface{}
RawValue return TextField value
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"`