gdb

package
v1.11.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 16, 2020 License: MIT Imports: 26 Imported by: 165

Documentation

Overview

Package gdb provides ORM features for popular relationship databases.

Index

Constants

View Source
const (
	ORM_TAG_FOR_STRUCT  = "orm"
	ORM_TAG_FOR_UNIQUE  = "unique"
	ORM_TAG_FOR_PRIMARY = "primary"
)
View Source
const (
	OPTION_OMITEMPTY = 1 << iota
	OPTION_ALLOWEMPTY
)
View Source
const (
	DEFAULT_GROUP_NAME = "default" // 默认配置名称
)

Variables

This section is empty.

Functions

func AddConfigNode

func AddConfigNode(group string, node ConfigNode)

按照配置分组添加一台数据库服务器配置

func AddDefaultConfigGroup

func AddDefaultConfigGroup(nodes ConfigGroup)

添加默认链接的数据库服务器集群配置

func AddDefaultConfigNode

func AddDefaultConfigNode(node ConfigNode)

添加默认链接的一台数据库服务器配置

func GetDefaultGroup

func GetDefaultGroup() string

获取默认链接的数据库链接配置项(默认是 default)

func GetOrmMappingOfStruct

func GetOrmMappingOfStruct(pointer interface{}) map[string]string

获得orm标签与属性的映射关系

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)

设置当前应用的数据库配置信息,进行全局数据库配置覆盖操作

func SetConfigGroup

func SetConfigGroup(group string, nodes ConfigGroup)

按照配置分组设置数据库服务器集群配置

func SetDefaultGroup

func SetDefaultGroup(name string)

设置默认链接的数据库链接配置项(默认是 default)

Types

type Config

type Config map[string]ConfigGroup

数据库分组配置

type ConfigGroup

type ConfigGroup []ConfigNode

数据库集群配置

func GetConfig

func GetConfig(group string) ConfigGroup

添加一台数据库服务器配置

type ConfigNode

type ConfigNode struct {
	Host             string        // 地址
	Port             string        // 端口
	User             string        // 账号
	Pass             string        // 密码
	Name             string        // 数据库名称
	Type             string        // 数据库类型:mysql, sqlite, mssql, pgsql, oracle
	Role             string        // (可选,默认为master)数据库的角色,用于主从操作分离,至少需要有一个master,参数值:master, slave
	Debug            bool          // (可选)开启调试模式
	Prefix           string        // (可选)表名前缀
	Weight           int           // (可选)用于负载均衡的权重计算,当集群中只有一个节点时,权重没有任何意义
	Charset          string        // (可选,默认为 utf8)编码,默认为 utf8
	LinkInfo         string        // (可选)自定义链接信息,当该字段被设置值时,以上链接字段(Host,Port,User,Pass,Name)将失效(该字段是一个扩展功能)
	MaxIdleConnCount int           // (可选)连接池最大限制的连接数
	MaxOpenConnCount int           // (可选)连接池最大打开的连接数
	MaxConnLifetime  time.Duration // (可选)连接对象可重复使用的时间长度
}

数据库单项配置

func (*ConfigNode) String

func (node *ConfigNode) String() 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)
	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.

func Instance

func Instance(name ...string) (db DB, err error)

Instance returns an instance for DB operations. The parameter <name> specifies the configuration group name, which is DEFAULT_GROUP_NAME in default.

func New

func New(name ...string) (db DB, err error)

New creates and returns an ORM object with global configurations. The parameter <name> specifies the configuration group name, which is DEFAULT_GROUP_NAME in default.

type List

type List = []Map

关联数组列表(索引从0开始的数组),绑定多条记录(使用别名)

type Map

type Map = map[string]interface{}

关联数组,绑定一条数据表记录(使用别名)

type Model

type Model struct {
	// contains filtered or unexported fields
}

Model is the DAO for ORM.

func (*Model) All

func (m *Model) All(where ...interface{}) (Result, error)

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) And

func (m *Model) And(where interface{}, args ...interface{}) *Model

And adds "AND" condition to the where statement.

func (*Model) As added in v1.11.2

func (m *Model) As(as string) *Model

As sets an alias name for current table.

func (*Model) Batch

func (m *Model) Batch(batch int) *Model

Batch sets the batch operation number for the model.

func (*Model) Cache

func (m *Model) Cache(duration time.Duration, name ...string) *Model

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) Chunk

func (m *Model) Chunk(limit int, callback func(result Result, err error) bool)

Chunk iterates the table with given size and callback function.

func (*Model) Clone

func (m *Model) Clone() *Model

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

func (m *Model) Count(where ...interface{}) (int, error)

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) DB added in v1.10.1

func (m *Model) DB(db DB) *Model

DB sets/changes the db object for current operation.

func (*Model) Data

func (m *Model) Data(data ...interface{}) *Model

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

func (m *Model) Delete(where ...interface{}) (result sql.Result, err error)

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

func (m *Model) Fields(fields string) *Model

Fields sets the operation fields of the model, multiple fields joined using char ','.

func (*Model) FieldsEx added in v1.10.0

func (m *Model) FieldsEx(fields string) *Model

FieldsEx sets the excluded operation fields of the model, multiple fields joined using char ','.

func (*Model) Filter

func (m *Model) Filter() *Model

Filter marks filtering the fields which does not exist in the fields of the operated table.

func (*Model) FindAll added in v1.10.1

func (m *Model) FindAll(where ...interface{}) (Result, error)

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

func (m *Model) FindCount(where ...interface{}) (int, error)

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

func (m *Model) FindOne(where ...interface{}) (Record, error)

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

func (m *Model) FindScan(pointer interface{}, where ...interface{}) error

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

func (m *Model) FindValue(fieldsAndWhere ...interface{}) (Value, error)

FindValue retrieves and returns single field value by Model.WherePri and Model.Value. Also see Model.WherePri and Model.Value.

func (*Model) ForPage

func (m *Model) ForPage(page, limit int) *Model

ForPage is alias of Model.Page. See Model.Page. Deprecated.

func (*Model) Group added in v1.10.1

func (m *Model) Group(groupBy string) *Model

Group sets the "GROUP BY" statement for the model.

func (*Model) GroupBy

func (m *Model) GroupBy(groupBy string) *Model

GroupBy is alias of Model.Group. See Model.Group. Deprecated.

func (*Model) InnerJoin

func (m *Model) InnerJoin(table string, on string) *Model

InnerJoin does "INNER JOIN ... ON ..." statement on the model.

func (*Model) Insert

func (m *Model) Insert(data ...interface{}) (result sql.Result, err error)

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) LeftJoin

func (m *Model) LeftJoin(table string, on string) *Model

LeftJoin does "LEFT JOIN ... ON ..." statement on the model.

func (*Model) Limit

func (m *Model) Limit(limit ...int) *Model

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) Master

func (m *Model) Master() *Model

Master marks the following operation on master node.

func (*Model) Offset

func (m *Model) Offset(offset int) *Model

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

func (m *Model) OmitEmpty() *Model

OmitEmpty sets OPTION_OMITEMPTY option for the model, which automatically filers the data and where attributes for empty values.

func (*Model) One

func (m *Model) One(where ...interface{}) (Record, error)

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) Option added in v1.9.7

func (m *Model) Option(option int) *Model

Option sets the extra operation option for the model.

func (*Model) OptionOmitEmpty added in v1.9.10

func (m *Model) OptionOmitEmpty() *Model

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) Or

func (m *Model) Or(where interface{}, args ...interface{}) *Model

Or adds "OR" condition to the where statement.

func (*Model) Order added in v1.10.1

func (m *Model) Order(orderBy string) *Model

Order sets the "ORDER BY" statement for the model.

func (*Model) OrderBy

func (m *Model) OrderBy(orderBy string) *Model

OrderBy is alias of Model.Order. See Model.Order. Deprecated.

func (*Model) Page added in v1.10.1

func (m *Model) Page(page, limit int) *Model

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

func (m *Model) Replace(data ...interface{}) (result sql.Result, err error)

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) RightJoin

func (m *Model) RightJoin(table string, on string) *Model

RightJoin does "RIGHT JOIN ... ON ..." statement on the model.

func (*Model) Safe

func (m *Model) Safe(safe ...bool) *Model

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

func (m *Model) Save(data ...interface{}) (result sql.Result, err error)

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

func (m *Model) Scan(pointer interface{}, where ...interface{}) error

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) Schema added in v1.11.1

func (m *Model) Schema(schema string) *Model

Schema sets the schema for current operation.

func (*Model) Select

func (m *Model) Select(where ...interface{}) (Result, error)

Select is alias of Model.All. See Model.All. Deprecated.

func (*Model) Slave

func (m *Model) Slave() *Model

Slave marks the following operation on slave node. Note that it makes sense only if there's any slave node configured.

func (*Model) Struct

func (m *Model) Struct(pointer interface{}, where ...interface{}) error

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

func (m *Model) Structs(pointer interface{}, where ...interface{}) error

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) TX added in v1.10.1

func (m *Model) TX(tx *TX) *Model

TX sets/changes the transaction for current operation.

func (*Model) Update

func (m *Model) Update(dataAndWhere ...interface{}) (result sql.Result, err error)

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

func (m *Model) Value(fieldsAndWhere ...interface{}) (Value, error)

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

func (m *Model) Where(where interface{}, args ...interface{}) *Model

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

func (m *Model) WherePri(where interface{}, args ...interface{}) *Model

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

type Record map[string]Value

返回数据表记录Map

func (Record) GMap added in v1.9.7

func (r Record) GMap() *gmap.StrAnyMap

GMap converts <r> to a gmap.

func (Record) IsEmpty added in v1.10.0

func (r Record) IsEmpty() bool

IsEmpty checks and returns whether <r> is empty.

func (Record) Json added in v1.9.7

func (r Record) Json() string

Json converts <r> to JSON format content.

func (Record) Map added in v1.9.7

func (r Record) Map() Map

Map converts <r> to map[string]interface{}.

func (Record) Struct added in v1.9.7

func (r Record) Struct(pointer interface{}) error

Struct converts <r> to a struct. Note that the parameter <pointer> should be type of *struct/**struct.

func (Record) ToJson

func (r Record) ToJson() string

Deprecated.

func (Record) ToMap

func (r Record) ToMap() Map

Deprecated.

func (Record) ToStruct

func (r Record) ToStruct(pointer interface{}) error

Deprecated.

func (Record) ToXml

func (r Record) ToXml(rootTag ...string) string

Deprecated.

func (Record) Xml added in v1.9.7

func (r Record) Xml(rootTag ...string) string

Xml converts <r> to XML format content.

type Result

type Result []Record

返回数据表记录List

func (Result) IsEmpty added in v1.10.0

func (r Result) IsEmpty() bool

IsEmpty checks and returns whether <r> is empty.

func (Result) Json added in v1.9.7

func (r Result) Json() string

Json converts <r> to JSON format content.

func (Result) List added in v1.9.7

func (r Result) List() List

List converts <r> to a List.

func (Result) MapKeyInt added in v1.9.7

func (r Result) MapKeyInt(key string) map[int]Map

MapKeyInt converts <r> to a map[int]Map of which key is specified by <key>.

func (Result) MapKeyStr added in v1.9.7

func (r Result) MapKeyStr(key string) map[string]Map

MapKeyStr converts <r> to a map[string]Map of which key is specified by <key>.

func (Result) MapKeyUint added in v1.9.7

func (r Result) MapKeyUint(key string) map[uint]Map

MapKeyUint converts <r> to a map[uint]Map of which key is specified by <key>.

func (Result) RecordKeyInt added in v1.9.7

func (r Result) RecordKeyInt(key string) map[int]Record

RecordKeyInt converts <r> to a map[int]Record of which key is specified by <key>.

func (Result) RecordKeyStr added in v1.9.7

func (r Result) RecordKeyStr(key string) map[string]Record

RecordKeyInt converts <r> to a map[int]Record of which key is specified by <key>.

func (Result) RecordKeyUint added in v1.9.7

func (r Result) RecordKeyUint(key string) map[uint]Record

RecordKeyUint converts <r> to a map[uint]Record of which key is specified by <key>.

func (Result) Structs added in v1.9.7

func (r Result) Structs(pointer interface{}) (err error)

Structs converts <r> to struct slice. Note that the parameter <pointer> should be type of *[]struct/*[]*struct.

func (Result) ToIntMap

func (r Result) ToIntMap(key string) map[int]Map

Deprecated.

func (Result) ToIntRecord

func (r Result) ToIntRecord(key string) map[int]Record

Deprecated.

func (Result) ToJson

func (r Result) ToJson() string

Deprecated.

func (Result) ToList

func (r Result) ToList() List

Deprecated.

func (Result) ToStringMap

func (r Result) ToStringMap(key string) map[string]Map

Deprecated.

func (Result) ToStringRecord

func (r Result) ToStringRecord(key string) map[string]Record

Deprecated.

func (Result) ToStructs

func (r Result) ToStructs(pointer interface{}) (err error)

Deprecated.

func (Result) ToUintMap

func (r Result) ToUintMap(key string) map[uint]Map

Deprecated.

func (Result) ToUintRecord

func (r Result) ToUintRecord(key string) map[uint]Record

Deprecated.

func (Result) ToXml

func (r Result) ToXml(rootTag ...string) string

Deprecated.

func (Result) Xml added in v1.9.7

func (r Result) Xml(rootTag ...string) string

Xml converts <r> to XML format content.

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.

func (*Schema) Model added in v1.11.2

func (s *Schema) Model(table string) *Model

Model is alias of dbBase.Table. See dbBase.Table.

func (*Schema) Table added in v1.11.2

func (s *Schema) Table(table string) *Model

Table creates and returns a new ORM model. The parameter <tables> can be more than one table names, like : "user", "user u", "user, user_detail", "user u, user_detail ud"

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
}

数据库事务对象

func (*TX) BatchInsert

func (tx *TX) BatchInsert(table string, list interface{}, batch ...int) (sql.Result, error)

CURD操作:批量数据指定批次量写入

func (*TX) BatchReplace

func (tx *TX) BatchReplace(table string, list interface{}, batch ...int) (sql.Result, error)

CURD操作:批量数据指定批次量写入, 如果数据存在(主键或者唯一索引),那么删除后重新写入一条

func (*TX) BatchSave

func (tx *TX) BatchSave(table string, list interface{}, batch ...int) (sql.Result, error)

CURD操作:批量数据指定批次量写入, 如果数据存在(主键或者唯一索引),那么更新,否则写入一条新数据

func (*TX) Commit

func (tx *TX) Commit() error

事务操作,提交

func (*TX) Delete

func (tx *TX) Delete(table string, condition interface{}, args ...interface{}) (sql.Result, error)

CURD操作:删除数据

func (*TX) Exec

func (tx *TX) Exec(query string, args ...interface{}) (sql.Result, error)

(事务)执行一条sql,并返回执行情况,主要用于非查询操作

func (*TX) From

func (tx *TX) From(table string) *Model

From is alias of tx.Table. See tx.Table. Deprecated.

func (*TX) GetAll

func (tx *TX) GetAll(query string, args ...interface{}) (Result, error)

数据库查询,获取查询结果集,以列表结构返回

func (*TX) GetCount

func (tx *TX) GetCount(query string, args ...interface{}) (int, error)

数据库查询,获取查询数量

func (*TX) GetOne

func (tx *TX) GetOne(query string, args ...interface{}) (Record, error)

数据库查询,获取查询结果记录,以关联数组结构返回

func (*TX) GetScan

func (tx *TX) GetScan(objPointer interface{}, query string, args ...interface{}) error

将结果转换为指定的struct/*struct/[]struct/[]*struct, 参数应该为指针类型,否则返回失败。 该方法自动识别参数类型,调用Struct/Structs方法。

func (*TX) GetStruct

func (tx *TX) GetStruct(obj interface{}, query string, args ...interface{}) error

数据库查询,获取查询结果记录,自动映射数据到给定的struct对象中

func (*TX) GetStructs

func (tx *TX) GetStructs(objPointerSlice interface{}, query string, args ...interface{}) error

数据库查询,查询多条记录,并自动转换为指定的slice对象, 如: []struct/[]*struct。

func (*TX) GetValue

func (tx *TX) GetValue(query string, args ...interface{}) (Value, error)

数据库查询,获取查询字段值

func (*TX) Insert

func (tx *TX) Insert(table string, data interface{}, batch ...int) (sql.Result, error)

CURD操作:单条数据写入, 仅仅执行写入操作,如果存在冲突的主键或者唯一索引,那么报错返回

func (*TX) Model added in v1.11.0

func (tx *TX) Model(table string) *Model

Model is alias of tx.Table. See tx.Table.

func (*TX) Prepare

func (tx *TX) Prepare(query string) (*sql.Stmt, error)

sql预处理,执行完成后调用返回值sql.Stmt.Exec完成sql操作

func (*TX) Query

func (tx *TX) Query(query string, args ...interface{}) (rows *sql.Rows, err error)

(事务)数据库sql查询操作,主要执行查询

func (*TX) Replace

func (tx *TX) Replace(table string, data interface{}, batch ...int) (sql.Result, error)

CURD操作:单条数据写入, 如果数据存在(主键或者唯一索引),那么删除后重新写入一条

func (*TX) Rollback

func (tx *TX) Rollback() error

事务操作,回滚

func (*TX) Save

func (tx *TX) Save(table string, data interface{}, batch ...int) (sql.Result, error)

CURD操作:单条数据写入, 如果数据存在(主键或者唯一索引),那么更新,否则写入一条新数据

func (*TX) Schema added in v1.11.2

func (tx *TX) Schema(schema string) *Schema

Schema creates and returns a initialization model from schema, from which it can then create a Model.

func (*TX) Table

func (tx *TX) Table(table string) *Model

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)

CURD操作:数据更新,统一采用sql预处理, data参数支持字符串或者关联数组类型,内部会自行做判断处理.

type TableField

type TableField struct {
	Index   int         // 用于字段排序(因为map类型是无序的)
	Name    string      // 字段名称
	Type    string      // 字段类型
	Null    bool        // 是否可为null
	Key     string      // 索引信息
	Default interface{} // 默认值
	Extra   string      // 其他信息
	Comment string      // 字段描述
}

表字段结构信息

type Value

type Value = *gvar.Var

返回数据表记录值

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL