sql

package
v0.0.0-...-e1367df Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

*通过数据库生成 代码*

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildSelect

func BuildSelect(source []string, entity interface{}) string

获取select字段名列表(source为空值时,取entity中的键名作为字段名)

func NewMssqlDriver

func NewMssqlDriver(dataSourceName string) (*xorm.Engine, error)

func NewPostgresDriver

func NewPostgresDriver(dataSourceName string) (*xorm.Engine, error)

Types

type BatchSqlReq

type BatchSqlReq struct {
	Mode  int // 0为Exec(Sql和Args), 1为InsertOne(Bean), 2为Update(Bean和Condi)
	Sql   string
	Args  []interface{}
	Bean  interface{}
	Condi interface{}
}

批量SQL请求参数列表

type DbContext

type DbContext struct {
	TableConfs    []SplitTableConf `mapstructure:"splitTables"`
	Name          string           `mapstructure:"name"`
	Driver        string           `mapstructure:"driver"`
	ConnectString string           `mapstructure:"str"`
	// contains filtered or unexported fields
}

数据库上下文

func GetContext

func GetContext(dbKey string) (*DbContext, error)

* 获取DB操作对象 * 参数dbKey:对应为config.toml中dbs.db的name值

func GetDefaultContext

func GetDefaultContext() *DbContext

* 获取默认DB操作对象 * 对应为config.toml中第一个有效的dbs.db配置

func NewContext

func NewContext(driver string, connectString string) (*DbContext, error)

* 构建DB操作对象 * * param driver 数据库引擎(mysql, mssql, postgres) * param connectString 连接字符串(标准连接URL) * return DB操作对象

func (DbContext) BatchExec

func (dtx DbContext) BatchExec(req []BatchSqlReq) (int64, error)

* 批量执行SQL * * param BatchSqlReq SQL对象,支持单个对象插入、自定义SQL语句、单个对象更新等 * * return 入库成功数量

func (DbContext) Close

func (dtx DbContext) Close()

关闭连接

func (DbContext) Delete

func (dtx DbContext) Delete(fullTableName string, entity interface{}) (int64, error)

* 删除数据 * * param fullTableName 表名,传空值时自动获取表名 * param entity 删除条件,支持map或struct类型 * * return 删除成功数量

func (DbContext) Engine

func (dtx DbContext) Engine() *xorm.Engine

返回客户端引擎

func (DbContext) Exec

func (dtx DbContext) Exec(sql string, args ...interface{}) (int64, error)

* 执行自定义SQL * * param sql SQL语句 * param args SQL参数 * * return 执行成功数量

func (DbContext) Get

func (dtx DbContext) Get(entity interface{}, id ...interface{}) error

* 根据ID查询数据记录 * * param entity 返回数据对象, 数据结构示例如下 * type Entity struct { * ID int64 `xorm:"ID"` //标签xorm定义表字段名称 * } * param id ID值列表 * * return 是否异常

func (DbContext) GetAllTableName

func (dtx DbContext) GetAllTableName(entity interface{}) ([]string, error)

* 根据分表配置规则获取所有表名 * * param entity 数据对象, 数据结构示例如下 * type Entity struct { * ID int64 `xorm:"ID"` //标签xorm定义表字段名称 * } * * return 返回表名列表

func (DbContext) GetTableIdx

func (dtx DbContext) GetTableIdx(tbname string, field string, value interface{}) (int32, error)

* 根据分表配置规则获取分表序号 * * param tbname 表标识名称,对应为config.toml中dbs.db的name值 * param field 分表字段名 * param value 分表字段值 * * return 返回分表序号

func (DbContext) GetTableName

func (dtx DbContext) GetTableName(entity interface{}) (string, error)

* 根据分表配置规则获取分表后的表名 * * param entity 数据对象, 数据结构示例如下 * type Entity struct { * ID int64 `xorm:"ID"` //标签xorm定义表字段名称 * } * * return 返回表名

func (DbContext) ImportData

func (dtx DbContext) ImportData(isIdentityInsert bool, batchNum int, entities ...interface{}) (int64, error)

* 批量导入数据 * * param isIdentityInsert 是否包含插入自增类型字段值 * param batchNum 单次批量入库数量 * param entities 入库数据,单个数据结构示例如下 * type Entity struct { * ID int64 `xorm:"ID"` //标签xorm定义表字段名称 * } * * return 入库成功数量

func (DbContext) Inserts

func (dtx DbContext) Inserts(entities ...interface{}) (int64, error)

* 插入数据,支持批量和单个 * * param entities 入库数据,单个数据结构示例如下 * type Entity struct { * ID int64 `xorm:"ID"` //标签xorm定义表字段名称 * } * * return 入库成功数量

func (DbContext) Query

func (dtx DbContext) Query(rowsSlicePtr interface{}, sql string, args ...interface{}) error

* 自定义SQL查询 * * param rowsSlicePtr 返回记录行数据对象, 支持[]struct, []map[string]interface{}类型,struct结构示例如下 * type Entity struct { * ID int64 `xorm:"ID"` //标签xorm定义表字段名称 * } * param sql SQL语句 * param args SQL参数 * * return 入库成功数量

func (DbContext) Update

func (dtx DbContext) Update(fullTableName string, entity interface{}, condi ...interface{}) (int64, error)

* 更新数据 * * param fullTableName 表名,传空值时自动获取表名 * param entity 入库数据,单个数据结构示例如下 * type Entity struct { * ID int64 `xorm:"ID"` //标签xorm定义表字段名称 * } * param condi 更新条件,支持map或struct类型 * * return 入库成功数量

type Policy

type Policy struct {
	Column string `mapstructure:"column"`
	Count  int    `mapstructure:"count"`
}

type SoftDelete

type SoftDelete interface {
	Delete()
}

type SplitTableConf

type SplitTableConf struct {
	TableName string   `mapstructure:"tableName"`
	Policies  []Policy `mapstructure:"policy"`
}

type Where

type Where struct {
	Sql  strings.Builder
	Args []interface{}
}

Where builder

func NewWhere

func NewWhere() *Where

func (*Where) AppendIf

func (w *Where) AppendIf(cond bool, whereStr string, args ...interface{}) *Where

append SQL短句(不支持In 数组)

func (Where) Build

func (w Where) Build() (string, []interface{})

func (*Where) ExprIf

func (w *Where) ExprIf(cond bool, col string, symbol string, args ...interface{}) *Where

append 单表达式查询(不支持In 数组)

func (*Where) InIf

func (w *Where) InIf(cond bool, col string, args ...interface{}) *Where

append in查询 数组

Jump to

Keyboard shortcuts

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