Documentation
¶
Overview ¶
Package gdb provides ORM features for popular relationship databases.
Index ¶
- Constants
- func AddConfigNode(group string, node ConfigNode)
- func AddDefaultConfigGroup(nodes ConfigGroup)
- func AddDefaultConfigNode(node ConfigNode)
- func GetDefaultGroup() string
- func GetPrimaryKey(pointer interface{}) string
- func GetPrimaryKeyCondition(primary string, where ...interface{}) (newWhereCondition []interface{})
- func GetWhereConditionOfStruct(pointer interface{}) (where string, args []interface{})
- func SetConfig(config Config)
- func SetConfigGroup(group string, nodes ConfigGroup)
- func SetDefaultGroup(name string)
- type Config
- type ConfigGroup
- type ConfigNode
- type DB
- type List
- type Map
- type Model
- func (m *Model) All(where ...interface{}) (Result, error)
- func (m *Model) And(where interface{}, args ...interface{}) *Model
- func (m *Model) As(as string) *Model
- func (m *Model) Batch(batch int) *Model
- func (m *Model) Cache(duration time.Duration, name ...string) *Model
- func (m *Model) Chunk(limit int, callback func(result Result, err error) bool)
- func (m *Model) Clone() *Model
- func (m *Model) Count(where ...interface{}) (int, error)
- func (m *Model) DB(db DB) *Model
- func (m *Model) Data(data ...interface{}) *Model
- func (m *Model) Delete(where ...interface{}) (result sql.Result, err error)
- func (m *Model) Fields(fields string) *Model
- func (m *Model) FieldsEx(fields string) *Model
- func (m *Model) FieldsExStr(fields string, prefix ...string) string
- func (m *Model) FieldsStr(prefix ...string) string
- func (m *Model) Filter() *Model
- func (m *Model) FindAll(where ...interface{}) (Result, error)
- func (m *Model) FindCount(where ...interface{}) (int, error)
- func (m *Model) FindOne(where ...interface{}) (Record, error)
- func (m *Model) FindScan(pointer interface{}, where ...interface{}) error
- func (m *Model) FindValue(fieldsAndWhere ...interface{}) (Value, error)
- func (m *Model) ForPage(page, limit int) *Model
- func (m *Model) Group(groupBy string) *Model
- func (m *Model) GroupBy(groupBy string) *Model
- func (m *Model) InnerJoin(table string, on string) *Model
- func (m *Model) Insert(data ...interface{}) (result sql.Result, err error)
- func (m *Model) InsertIgnore(data ...interface{}) (result sql.Result, err error)
- func (m *Model) LeftJoin(table string, on string) *Model
- func (m *Model) Limit(limit ...int) *Model
- func (m *Model) Master() *Model
- func (m *Model) Offset(offset int) *Model
- func (m *Model) OmitEmpty() *Model
- func (m *Model) One(where ...interface{}) (Record, error)
- func (m *Model) Option(option int) *Model
- func (m *Model) OptionOmitEmpty() *Model
- func (m *Model) Or(where interface{}, args ...interface{}) *Model
- func (m *Model) Order(orderBy string) *Model
- func (m *Model) OrderBy(orderBy string) *Model
- func (m *Model) Page(page, limit int) *Model
- func (m *Model) Replace(data ...interface{}) (result sql.Result, err error)
- func (m *Model) RightJoin(table string, on string) *Model
- func (m *Model) Safe(safe ...bool) *Model
- func (m *Model) Save(data ...interface{}) (result sql.Result, err error)
- func (m *Model) Scan(pointer interface{}, where ...interface{}) error
- func (m *Model) Schema(schema string) *Model
- func (m *Model) Select(where ...interface{}) (Result, error)
- func (m *Model) Slave() *Model
- func (m *Model) Struct(pointer interface{}, where ...interface{}) error
- func (m *Model) Structs(pointer interface{}, where ...interface{}) error
- func (m *Model) TX(tx *TX) *Model
- func (m *Model) Update(dataAndWhere ...interface{}) (result sql.Result, err error)
- func (m *Model) Value(fieldsAndWhere ...interface{}) (Value, error)
- func (m *Model) Where(where interface{}, args ...interface{}) *Model
- func (m *Model) WherePri(where interface{}, args ...interface{}) *Model
- type Record
- func (r Record) GMap() *gmap.StrAnyMap
- func (r Record) IsEmpty() bool
- func (r Record) Json() string
- func (r Record) Map() Map
- func (r Record) Struct(pointer interface{}) error
- func (r Record) ToJson() string
- func (r Record) ToMap() Map
- func (r Record) ToStruct(pointer interface{}) error
- func (r Record) ToXml(rootTag ...string) string
- func (r Record) Xml(rootTag ...string) string
- type Result
- func (r Result) IsEmpty() bool
- func (r Result) Json() string
- func (r Result) List() List
- func (r Result) MapKeyInt(key string) map[int]Map
- func (r Result) MapKeyStr(key string) map[string]Map
- func (r Result) MapKeyUint(key string) map[uint]Map
- func (r Result) RecordKeyInt(key string) map[int]Record
- func (r Result) RecordKeyStr(key string) map[string]Record
- func (r Result) RecordKeyUint(key string) map[uint]Record
- func (r Result) Structs(pointer interface{}) (err error)
- func (r Result) ToIntMap(key string) map[int]Map
- func (r Result) ToIntRecord(key string) map[int]Record
- func (r Result) ToJson() string
- func (r Result) ToList() List
- func (r Result) ToStringMap(key string) map[string]Map
- func (r Result) ToStringRecord(key string) map[string]Record
- func (r Result) ToStructs(pointer interface{}) (err error)
- func (r Result) ToUintMap(key string) map[uint]Map
- func (r Result) ToUintRecord(key string) map[uint]Record
- func (r Result) ToXml(rootTag ...string) string
- func (r Result) Xml(rootTag ...string) string
- type Schema
- type Sql
- type TX
- func (tx *TX) BatchInsert(table string, list interface{}, batch ...int) (sql.Result, error)
- func (tx *TX) BatchInsertIgnore(table string, list interface{}, batch ...int) (sql.Result, error)
- func (tx *TX) BatchReplace(table string, list interface{}, batch ...int) (sql.Result, error)
- func (tx *TX) BatchSave(table string, list interface{}, batch ...int) (sql.Result, error)
- func (tx *TX) Commit() error
- func (tx *TX) Delete(table string, condition interface{}, args ...interface{}) (sql.Result, error)
- func (tx *TX) Exec(query string, args ...interface{}) (sql.Result, error)
- func (tx *TX) From(table string) *Model
- func (tx *TX) GetAll(query string, args ...interface{}) (Result, error)
- func (tx *TX) GetCount(query string, args ...interface{}) (int, error)
- func (tx *TX) GetOne(query string, args ...interface{}) (Record, error)
- func (tx *TX) GetScan(objPointer interface{}, query string, args ...interface{}) error
- func (tx *TX) GetStruct(obj interface{}, query string, args ...interface{}) error
- func (tx *TX) GetStructs(objPointerSlice interface{}, query string, args ...interface{}) error
- func (tx *TX) GetValue(query string, args ...interface{}) (Value, error)
- func (tx *TX) Insert(table string, data interface{}, batch ...int) (sql.Result, error)
- func (tx *TX) InsertIgnore(table string, data interface{}, batch ...int) (sql.Result, error)
- func (tx *TX) Model(table string) *Model
- func (tx *TX) Prepare(query string) (*sql.Stmt, error)
- func (tx *TX) Query(query string, args ...interface{}) (rows *sql.Rows, err error)
- func (tx *TX) Replace(table string, data interface{}, batch ...int) (sql.Result, error)
- func (tx *TX) Rollback() error
- func (tx *TX) Save(table string, data interface{}, batch ...int) (sql.Result, error)
- func (tx *TX) Schema(schema string) *Schema
- func (tx *TX) Table(table string) *Model
- func (tx *TX) Update(table string, data interface{}, condition interface{}, args ...interface{}) (sql.Result, error)
- type TableField
- type Value
Constants ¶
const ( ORM_TAG_FOR_STRUCT = "orm" ORM_TAG_FOR_UNIQUE = "unique" ORM_TAG_FOR_PRIMARY = "primary" )
const ( OPTION_OMITEMPTY = 1 << iota OPTION_ALLOWEMPTY )
const (
DEFAULT_GROUP_NAME = "default" // Default group name.
)
Variables ¶
This section is empty.
Functions ¶
func AddConfigNode ¶
func AddConfigNode(group string, node ConfigNode)
AddConfigNode adds one node configuration to configuration of given group.
func AddDefaultConfigGroup ¶
func AddDefaultConfigGroup(nodes ConfigGroup)
AddDefaultConfigGroup adds multiple node configurations to configuration of default group.
func AddDefaultConfigNode ¶
func AddDefaultConfigNode(node ConfigNode)
AddDefaultConfigNode adds one node configuration to configuration of default group.
func GetDefaultGroup ¶
func GetDefaultGroup() string
GetDefaultGroup returns the { name of default configuration.
func GetPrimaryKey ¶ added in v1.10.1
func GetPrimaryKey(pointer interface{}) string
GetPrimaryKey retrieves and returns primary key field name from given struct.
func GetPrimaryKeyCondition ¶ added in v1.10.1
func GetPrimaryKeyCondition(primary string, where ...interface{}) (newWhereCondition []interface{})
GetPrimaryKeyCondition returns a new where condition by primary field name. The optional parameter <where> is like follows: 123, []int{1, 2, 3}, "john", []string{"john", "smith"} g.Map{"id": g.Slice{1,2,3}}, g.Map{"id": 1, "name": "john"}, etc.
Note that it returns the given <where> parameter directly if there's the <primary> is empty.
func GetWhereConditionOfStruct ¶
func GetWhereConditionOfStruct(pointer interface{}) (where string, args []interface{})
GetWhereConditionOfStruct returns the where condition sql and arguments by given struct pointer. This function automatically retrieves primary or unique field and its attribute value as condition.
func SetConfig ¶
func SetConfig(config Config)
SetConfig sets the global configuration for package. It will overwrite the old configuration of package.
func SetConfigGroup ¶
func SetConfigGroup(group string, nodes ConfigGroup)
SetConfigGroup sets the configuration for given group.
func SetDefaultGroup ¶
func SetDefaultGroup(name string)
SetDefaultGroup sets the group name for default configuration.
Types ¶
type ConfigGroup ¶
type ConfigGroup []ConfigNode
ConfigGroup is a slice of configuration node for specified named group.
func GetConfig ¶
func GetConfig(group string) ConfigGroup
GetConfig retrieves and returns the configuration of given group.
type ConfigNode ¶
type ConfigNode struct {
Host string // Host of server, ip or domain like: 127.0.0.1, localhost
Port string // Port, it's commonly 3306.
User string // Authentication username.
Pass string // Authentication password.
Name string // Default used database name.
Type string // Database type: mysql, sqlite, mssql, pgsql, oracle.
Role string // (Optional, "master" in default) Node role, used for master-slave mode: master, slave.
Debug bool // (Optional) Debug mode enables debug information logging and output.
Prefix string // (Optional) Table prefix.
Weight int // (Optional) Weight for load balance calculating, it's useless if there's just one node.
Charset string // (Optional, "utf8mb4" in default) Custom charset when operating on database.
LinkInfo string // (Optional) Custom link information, when it is used, configuration Host/Port/User/Pass/Name are ignored.
MaxIdleConnCount int // (Optional) Max idle connection configuration for underlying connection pool.
MaxOpenConnCount int // (Optional) Max open connection configuration for underlying connection pool.
MaxConnLifetime time.Duration // (Optional) Max connection TTL configuration for underlying connection pool.
}
ConfigNode is configuration for one node.
func (*ConfigNode) String ¶
func (node *ConfigNode) String() string
String returns the node as string.
type DB ¶
type DB interface {
// Open creates a raw connection object for database with given node configuration.
// Note that it is not recommended using the this function manually.
Open(config *ConfigNode) (*sql.DB, error)
// Query APIs.
Query(query string, args ...interface{}) (*sql.Rows, error)
Exec(sql string, args ...interface{}) (sql.Result, error)
Prepare(sql string, execOnMaster ...bool) (*sql.Stmt, error)
// Query APIs for convenience purpose.
GetAll(query string, args ...interface{}) (Result, error)
GetOne(query string, args ...interface{}) (Record, error)
GetValue(query string, args ...interface{}) (Value, error)
GetCount(query string, args ...interface{}) (int, error)
GetStruct(objPointer interface{}, query string, args ...interface{}) error
GetStructs(objPointerSlice interface{}, query string, args ...interface{}) error
GetScan(objPointer interface{}, query string, args ...interface{}) error
// Master/Slave support.
Master() (*sql.DB, error)
Slave() (*sql.DB, error)
// Ping.
PingMaster() error
PingSlave() error
// Transaction.
Begin() (*TX, error)
Insert(table string, data interface{}, batch ...int) (sql.Result, error)
InsertIgnore(table string, data interface{}, batch ...int) (sql.Result, error)
Replace(table string, data interface{}, batch ...int) (sql.Result, error)
Save(table string, data interface{}, batch ...int) (sql.Result, error)
BatchInsert(table string, list interface{}, batch ...int) (sql.Result, error)
BatchReplace(table string, list interface{}, batch ...int) (sql.Result, error)
BatchSave(table string, list interface{}, batch ...int) (sql.Result, error)
Update(table string, data interface{}, condition interface{}, args ...interface{}) (sql.Result, error)
Delete(table string, condition interface{}, args ...interface{}) (sql.Result, error)
// Create model.
From(tables string) *Model
Table(tables string) *Model
Schema(schema string) *Schema
// Configuration methods.
SetDebug(debug bool)
SetSchema(schema string)
SetLogger(logger *glog.Logger)
GetLogger() *glog.Logger
SetMaxIdleConnCount(n int)
SetMaxOpenConnCount(n int)
SetMaxConnLifetime(d time.Duration)
Tables(schema ...string) (tables []string, err error)
TableFields(table string, schema ...string) (map[string]*TableField, error)
// contains filtered or unexported methods
}
DB is the interface for ORM operations.
type Map ¶
type Map = map[string]interface{}
Map is alias of map[string]interface{}, which is the most common usage map type.
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
Model is the DAO for ORM.
func (*Model) All ¶
All does "SELECT FROM ..." statement for the model. It retrieves the records from table and returns the result as slice type. It returns nil if there's no record retrieved with the given conditions from table.
The optional parameter <where> is the same as the parameter of Model.Where function, see Model.Where.
func (*Model) Cache ¶
Cache sets the cache feature for the model. It caches the result of the sql, which means if there's another same sql request, it just reads and returns the result from cache, it but not committed and executed into the database.
If the parameter <duration> < 0, which means it clear the cache with given <name>. If the parameter <duration> = 0, which means it never expires. If the parameter <duration> > 0, which means it expires after <duration>.
The optional parameter <name> is used to bind a name to the cache, which means you can later control the cache like changing the <duration> or clearing the cache with specified <name>.
Note that, the cache feature is disabled if the model is operating on a transaction.
func (*Model) Clone ¶
Clone creates and returns a new model which is a clone of current model. Note that it uses deep-copy for the clone.
func (*Model) Count ¶
Count does "SELECT COUNT(x) FROM ..." statement for the model. The optional parameter <where> is the same as the parameter of Model.Where function, see Model.Where.
func (*Model) Data ¶
Data sets the operation data for the model. The parameter <data> can be type of string/map/gmap/slice/struct/*struct, etc. Eg: Data("uid=10000") Data("uid", 10000) Data(g.Map{"uid": 10000, "name":"john"}) Data(g.Slice{g.Map{"uid": 10000, "name":"john"}, g.Map{"uid": 20000, "name":"smith"})
func (*Model) Delete ¶
Delete does "DELETE FROM ... " statement for the model. The optional parameter <where> is the same as the parameter of Model.Where function, see Model.Where.
func (*Model) Fields ¶
Fields sets the operation fields of the model, multiple fields joined using char ','.
func (*Model) FieldsEx ¶ added in v1.10.0
FieldsEx sets the excluded operation fields of the model, multiple fields joined using char ','.
func (*Model) FieldsExStr ¶ added in v1.11.5
FieldsExStr retrieves and returns fields which are not in parameter <fields> from the table, joined with char ','. The parameter <fields> specifies the fields that are excluded. The optional parameter <prefix> specifies the prefix for each field, eg: FieldsExStr("id", "u.").
func (*Model) FieldsStr ¶ added in v1.11.5
FieldsStr retrieves and returns all fields from the table, joined with char ','. The optional parameter <prefix> specifies the prefix for each field, eg: FieldsStr("u.").
func (*Model) Filter ¶
Filter marks filtering the fields which does not exist in the fields of the operated table.
func (*Model) FindAll ¶ added in v1.10.1
FindAll retrieves and returns Result by by Model.WherePri and Model.All. Also see Model.WherePri and Model.All.
func (*Model) FindCount ¶ added in v1.10.1
FindCount retrieves and returns the record number by Model.WherePri and Model.Count. Also see Model.WherePri and Model.Count.
func (*Model) FindOne ¶ added in v1.10.1
FindOne retrieves and returns a single Record by Model.WherePri and Model.One. Also see Model.WherePri and Model.One.
func (*Model) FindScan ¶ added in v1.11.0
FindScan retrieves and returns the record/records by Model.WherePri and Model.Scan. Also see Model.WherePri and Model.Scan.
func (*Model) FindValue ¶ added in v1.10.1
FindValue retrieves and returns single field value by Model.WherePri and Model.Value. Also see Model.WherePri and Model.Value.
func (*Model) Insert ¶
Insert does "INSERT INTO ..." statement for the model. The optional parameter <data> is the same as the parameter of Model.Data function, see Model.Data.
func (*Model) InsertIgnore ¶ added in v1.11.5
InsertIgnore does "INSERT IGNORE INTO ..." statement for the model. The optional parameter <data> is the same as the parameter of Model.Data function, see Model.Data.
func (*Model) Limit ¶
Limit sets the "LIMIT" statement for the model. The parameter <limit> can be either one or two number, if passed two number is passed, it then sets "LIMIT limit[0],limit[1]" statement for the model, or else it sets "LIMIT limit[0]" statement.
func (*Model) Offset ¶
Offset sets the "OFFSET" statement for the model. It only makes sense for some databases like SQLServer, PostgreSQL, etc.
func (*Model) OmitEmpty ¶ added in v1.10.1
OmitEmpty sets OPTION_OMITEMPTY option for the model, which automatically filers the data and where attributes for empty values.
func (*Model) One ¶
One retrieves one record from table and returns the result as map type. It returns nil if there's no record retrieved with the given conditions from table.
The optional parameter <where> is the same as the parameter of Model.Where function, see Model.Where.
func (*Model) OptionOmitEmpty ¶ added in v1.9.10
OptionOmitEmpty sets OPTION_OMITEMPTY option for the model, which automatically filers the data and where attributes for empty values. Deprecated, use OmitEmpty instead.
func (*Model) Page ¶ added in v1.10.1
Page sets the paging number for the model. The parameter <page> is started from 1 for paging. Note that, it differs that the Limit function start from 0 for "LIMIT" statement.
func (*Model) Replace ¶
Replace does "REPLACE INTO ..." statement for the model. The optional parameter <data> is the same as the parameter of Model.Data function, see Model.Data.
func (*Model) Safe ¶
Safe marks this model safe or unsafe. If safe is true, it clones and returns a new model object whenever the operation done, or else it changes the attribute of current model.
func (*Model) Save ¶
Save does "INSERT INTO ... ON DUPLICATE KEY UPDATE..." statement for the model. The optional parameter <data> is the same as the parameter of Model.Data function, see Model.Data.
It updates the record if there's primary or unique index in the saving data, or else it inserts a new record into the table.
func (*Model) Scan ¶
Scan automatically calls Struct or Structs function according to the type of parameter <pointer>. It calls function Struct if <pointer> is type of *struct/**struct. It calls function Structs if <pointer> is type of *[]struct/*[]*struct.
The optional parameter <where> is the same as the parameter of Model.Where function, see Model.Where.
Note that it returns sql.ErrNoRows if there's no record retrieved with the given conditions from table.
Eg: user := new(User) err := db.Table("user").Where("id", 1).Struct(user)
user := (*User)(nil) err := db.Table("user").Where("id", 1).Struct(&user)
users := ([]User)(nil) err := db.Table("user").Structs(&users)
users := ([]*User)(nil) err := db.Table("user").Structs(&users)
func (*Model) Slave ¶
Slave marks the following operation on slave node. Note that it makes sense only if there's any slave node configured.
func (*Model) Struct ¶
Struct retrieves one record from table and converts it into given struct. The parameter <pointer> should be type of *struct/**struct. If type **struct is given, it can create the struct internally during converting.
The optional parameter <where> is the same as the parameter of Model.Where function, see Model.Where.
Note that it returns sql.ErrNoRows if there's no record retrieved with the given conditions from table.
Eg: user := new(User) err := db.Table("user").Where("id", 1).Struct(user)
user := (*User)(nil) err := db.Table("user").Where("id", 1).Struct(&user)
func (*Model) Structs ¶
Structs retrieves records from table and converts them into given struct slice. The parameter <pointer> should be type of *[]struct/*[]*struct. It can create and fill the struct slice internally during converting.
The optional parameter <where> is the same as the parameter of Model.Where function, see Model.Where.
Note that it returns sql.ErrNoRows if there's no record retrieved with the given conditions from table.
Eg: users := ([]User)(nil) err := db.Table("user").Structs(&users)
users := ([]*User)(nil) err := db.Table("user").Structs(&users)
func (*Model) Update ¶
Update does "UPDATE ... " statement for the model.
If the optional parameter <dataAndWhere> is given, the dataAndWhere[0] is the updated data field, and dataAndWhere[1:] is treated as where condition fields. Also see Model.Data and Model.Where functions.
func (*Model) Value ¶
Value retrieves a specified record value from table and returns the result as interface type. It returns nil if there's no record found with the given conditions from table.
If the optional parameter <fieldsAndWhere> is given, the fieldsAndWhere[0] is the selected fields and fieldsAndWhere[1:] is treated as where condition fields. Also see Model.Fields and Model.Where functions.
func (*Model) Where ¶
Where sets the condition statement for the model. The parameter <where> can be type of string/map/gmap/slice/struct/*struct, etc. Note that, if it's called more than one times, multiple conditions will be joined into where statement using "AND". Eg: Where("uid=10000") Where("uid", 10000) Where("money>? AND name like ?", 99999, "vip_%") Where("uid", 1).Where("name", "john") Where("status IN (?)", g.Slice{1,2,3}) Where("age IN(?,?)", 18, 50) Where(User{ Id : 1, UserName : "john"})
func (*Model) WherePri ¶ added in v1.10.1
WherePri does the same logic as Model.Where except that if the parameter <where> is a single condition like int/string/float/slice, it treats the condition as the primary key value. That is, if primary key is "id" and given <where> parameter as "123", the WherePri function treats it as "id=123", but Model.Where treats it as string "123".
type Record ¶
Record is the row record of the table.
type Result ¶
type Result []Record
Result is the row record array.
func (Result) MapKeyInt ¶ added in v1.9.7
MapKeyInt converts <r> to a map[int]Map of which key is specified by <key>.
func (Result) MapKeyStr ¶ added in v1.9.7
MapKeyStr converts <r> to a map[string]Map of which key is specified by <key>.
func (Result) MapKeyUint ¶ added in v1.9.7
MapKeyUint converts <r> to a map[uint]Map of which key is specified by <key>.
func (Result) RecordKeyInt ¶ added in v1.9.7
RecordKeyInt converts <r> to a map[int]Record of which key is specified by <key>.
func (Result) RecordKeyStr ¶ added in v1.9.7
RecordKeyInt converts <r> to a map[int]Record of which key is specified by <key>.
func (Result) RecordKeyUint ¶ added in v1.9.7
RecordKeyUint converts <r> to a map[uint]Record of which key is specified by <key>.
func (Result) Structs ¶ added in v1.9.7
Structs converts <r> to struct slice. Note that the parameter <pointer> should be type of *[]struct/*[]*struct.
func (Result) ToStringRecord ¶
Deprecated.
type Schema ¶ added in v1.11.2
type Schema struct {
// contains filtered or unexported fields
}
Schema is a schema object from which it can then create a Model.
type Sql ¶
type Sql struct {
Sql string // SQL string(may contain reserved char '?').
Args []interface{} // Arguments for this sql.
Format string // Formatted sql which contains arguments in the sql.
Error error // Execution result.
Start int64 // Start execution timestamp in milliseconds.
End int64 // End execution timestamp in milliseconds.
}
Sql is the sql recording struct.
type TX ¶
type TX struct {
// contains filtered or unexported fields
}
TX is the struct for transaction management.
func (*TX) BatchInsert ¶
BatchInsert batch inserts data. The parameter <list> must be type of slice of map or struct.
func (*TX) BatchInsertIgnore ¶ added in v1.11.5
BatchInsert batch inserts data with ignore option. The parameter <list> must be type of slice of map or struct.
func (*TX) BatchReplace ¶
BatchReplace batch replaces data. The parameter <list> must be type of slice of map or struct.
func (*TX) BatchSave ¶
BatchSave batch replaces data. The parameter <list> must be type of slice of map or struct.
func (*TX) Delete ¶
Delete does "DELETE FROM ... " statement for the table.
The parameter <condition> can be type of string/map/gmap/slice/struct/*struct, etc. It is commonly used with parameter <args>. Eg: "uid=10000", "uid", 10000 "money>? AND name like ?", 99999, "vip_%" "status IN (?)", g.Slice{1,2,3} "age IN(?,?)", 18, 50 User{ Id : 1, UserName : "john"}
func (*TX) GetScan ¶
GetScan queries one or more records from database and converts them to given struct or struct array.
If parameter <pointer> is type of struct pointer, it calls GetStruct internally for the conversion. If parameter <pointer> is type of slice, it calls GetStructs internally for conversion.
func (*TX) GetStruct ¶
GetStruct queries one record from database and converts it to given struct. The parameter <pointer> should be a pointer to struct.
func (*TX) GetStructs ¶
GetStructs queries records from database and converts them to given struct. The parameter <pointer> should be type of struct slice: []struct/[]*struct.
func (*TX) GetValue ¶
GetValue queries and returns the field value from database. The sql should queries only one field from database, or else it returns only one field of the result.
func (*TX) Insert ¶
Insert does "INSERT INTO ..." statement for the table. If there's already one unique record of the data in the table, it returns error.
The parameter <data> can be type of map/gmap/struct/*struct/[]map/[]struct, etc. Eg: Data(g.Map{"uid": 10000, "name":"john"}) Data(g.Slice{g.Map{"uid": 10000, "name":"john"}, g.Map{"uid": 20000, "name":"smith"})
The parameter <batch> specifies the batch operation count when given data is slice.
func (*TX) InsertIgnore ¶ added in v1.11.5
InsertIgnore does "INSERT IGNORE INTO ..." statement for the table. If there's already one unique record of the data in the table, it ignores the inserting.
The parameter <data> can be type of map/gmap/struct/*struct/[]map/[]struct, etc. Eg: Data(g.Map{"uid": 10000, "name":"john"}) Data(g.Slice{g.Map{"uid": 10000, "name":"john"}, g.Map{"uid": 20000, "name":"smith"})
The parameter <batch> specifies the batch operation count when given data is slice.
func (*TX) Prepare ¶
Prepare creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement. The caller must call the statement's Close method when the statement is no longer needed.
func (*TX) Replace ¶
Replace does "REPLACE INTO ..." statement for the table. If there's already one unique record of the data in the table, it deletes the record and inserts a new one.
The parameter <data> can be type of map/gmap/struct/*struct/[]map/[]struct, etc. Eg: Data(g.Map{"uid": 10000, "name":"john"}) Data(g.Slice{g.Map{"uid": 10000, "name":"john"}, g.Map{"uid": 20000, "name":"smith"})
The parameter <data> can be type of map/gmap/struct/*struct/[]map/[]struct, etc. If given data is type of slice, it then does batch replacing, and the optional parameter <batch> specifies the batch operation count.
func (*TX) Save ¶
Save does "INSERT INTO ... ON DUPLICATE KEY UPDATE..." statement for the table. It updates the record if there's primary or unique index in the saving data, or else it inserts a new record into the table.
The parameter <data> can be type of map/gmap/struct/*struct/[]map/[]struct, etc. Eg: Data(g.Map{"uid": 10000, "name":"john"}) Data(g.Slice{g.Map{"uid": 10000, "name":"john"}, g.Map{"uid": 20000, "name":"smith"})
If given data is type of slice, it then does batch saving, and the optional parameter <batch> specifies the batch operation count.
func (*TX) Schema ¶ added in v1.11.2
Schema creates and returns a initialization model from schema, from which it can then create a Model.
func (*TX) Table ¶
Table acts like dbBase.Table except it operates on transaction. See dbBase.Table.
func (*TX) Update ¶
func (tx *TX) Update(table string, data interface{}, condition interface{}, args ...interface{}) (sql.Result, error)
Update does "UPDATE ... " statement for the table.
The parameter <data> can be type of string/map/gmap/struct/*struct, etc. Eg: "uid=10000", "uid", 10000, g.Map{"uid": 10000, "name":"john"}
The parameter <condition> can be type of string/map/gmap/slice/struct/*struct, etc. It is commonly used with parameter <args>. Eg: "uid=10000", "uid", 10000 "money>? AND name like ?", 99999, "vip_%" "status IN (?)", g.Slice{1,2,3} "age IN(?,?)", 18, 50 User{ Id : 1, UserName : "john"}
type TableField ¶
type TableField struct {
Index int // For ordering purpose as map is unordered.
Name string // Field name.
Type string // Field type.
Null bool // Field can be null or not.
Key string // The index information(empty if it's not a index).
Default interface{} // Default value for the field.
Extra string // Extra information.
Comment string // Comment.
}
TableField is the struct for table field.