mssql

package
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InjectCreatePool

func InjectCreatePool(fun CreateConnectFunc)

InjectCreatePool 注入连接池的创建过程 connect 连接MsSql配置 fun 回调函数返回MsSql连接配置

func TransSql

func TransSql(sql string, whereParams ...interface{}) string

TransSql 转原生 sql

Types

type Aliaser

type Aliaser interface {
	Join(tableName string) Joiner
	LeftJoin(tableName string) Joiner
	RightJoin(tableName string) Joiner
	Union(model Modeler) Unioner
	Where(field string, value interface{}) Wherer
	WhereRaw(where string) Wherer
	WhereSymbol(field, symbol string, value interface{}) Wherer
	WhereIn(field string, value interface{}) Wherer
	WhereNotIn(field string, value interface{}) Wherer
	GroupBy(field ...string) GroupByer
	Count() int64
	Get() lib.SqlRows
	Find() lib.SqlRow
	OrderBy(orderBy string) OrderByer
	Limit(offset, count int) Limiter
	Pager(page, pageSize int) (lib.SqlRows, lib.SqlRow)
	Update(data lib.SqlIn) int64
}

Aliaser 另外取名接口

type CreateConnectFunc

type CreateConnectFunc func(*DB) []MsSqlConnect

CreateConnectFunc 创建连接

type DB

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

DB 数据库和连接

func Connect

func Connect(connectName, databaseName string) *DB

Connect 连接到数据库

func GetDB

func GetDB() *DB

GetDB 获得数据库对象

func NewDB

func NewDB() *DB

NewDB 新建数据库连接包括连接池

func (*DB) BeginTransaction

func (db *DB) BeginTransaction()

BeginTransaction 开启事务

func (*DB) Commit

func (db *DB) Commit()

Commit 提交事务

func (*DB) Desc

func (db *DB) Desc(tableName string) lib.Columns

Desc 查询表结构

func (*DB) Exec

func (db *DB) Exec(sql string) int64

Exec 执行原生sql

func (*DB) IsExist

func (db *DB) IsExist(tableName string) bool

IsExist 检测表是否存在

func (*DB) Query

func (db *DB) Query(sql string, whereParams ...interface{}) lib.SqlRows

Query

func (*DB) Rollback

func (db *DB) Rollback()

Rollback 事务回滚

func (*DB) SetDB

func (db *DB) SetDB(connectName, databaseName string) DBer

SetDB 设置连接名和数据库名称

func (*DB) SetPool

func (db *DB) SetPool(pool MsSqlPooler) DBPool

SetPool 设置连接池

func (*DB) ShowDatabases

func (db *DB) ShowDatabases() []string

ShowDatabases 显示所有数据库

func (*DB) ShowTables

func (db *DB) ShowTables() []string

ShowTables 显示数据库的所有表名称

type DBConfig

type DBConfig struct {
	Name         string `json:"name"`
	DatabaseName string `json:"databaseName"`
	UserName     string `json:"userName"`
	Password     string `json:"password"`
	Host         string `json:"host"`
	Port         string `json:"port"`
	Charset      string `json:"charset"`
	MaxOpenConns int    `json:"maxOpenConns"`
	MaxIdleConns int    `json:"maxIdleConns"`
	MaxLifetime  int    `json:"maxLifetime"`
	Prefix       string `json:"prefix"`
	Suffix       string `json:"suffix"`
}

DBConfig 从库配置数据

type DBPool

type DBPool interface {
	SetDB(connectName, databaseName string) DBer
}

DBPool 连接池接口

type DBer

type DBer interface {
	BeginTransaction()
	Commit()
	Rollback()
	Exec(sql string) int64
	Query(sql string, whereParams ...interface{}) lib.SqlRows
	Desc(tableName string) lib.Columns
	IsExist(tableName string) bool
	ShowTables() []string
	ShowDatabases() []string
}

DBer 数据库接口

type Fielder

type Fielder interface {
	Alias(name string) Aliaser
	Join(tableName string) Joiner
	LeftJoin(tableName string) Joiner
	RightJoin(tableName string) Joiner
	Union(model Modeler) Unioner
	Where(field string, value interface{}) Wherer
	WhereRaw(where string) Wherer
	WhereSymbol(field, symbol string, value interface{}) Wherer
	WhereIn(field string, value interface{}) Wherer
	WhereNotIn(field string, value interface{}) Wherer
	GroupBy(field ...string) GroupByer
	Count() int64
	Get() lib.SqlRows
	Find() lib.SqlRow
	OrderBy(orderBy string) OrderByer
	Limit(offset, count int) Limiter
	Pager(page, pageSize int) (lib.SqlRows, lib.SqlRow)
}

Fielder 字段暴露出去的接口

type GroupByer

type GroupByer interface {
	Having(field string, value interface{}) Havinger
	HavingRaw(having string) Havinger
	HavingSymbol(field, symbol string, value interface{}) Havinger
	HavingIn(field string, value interface{}) Havinger
	HavingNotIn(field string, value interface{}) Havinger
	Get() lib.SqlRows
	Find() lib.SqlRow
	Count() int64
	OrderBy(orderBy string) OrderByer
	Limit(offset, count int) Limiter
	Union(model Modeler) Unioner
	Pager(page, pageSize int) (lib.SqlRows, lib.SqlRow)
}

GroupByer 分组接口

type Havinger

type Havinger interface {
	AndHaving(field string, value interface{}) Havinger
	AndHavingRaw(having string) Havinger
	AndHavingSymbol(field, symbol string, value interface{}) Havinger
	AndHavingIn(field string, value interface{}) Havinger
	AndHavingNotIn(field string, value interface{}) Havinger
	OrHaving(field string, value interface{}) Havinger
	OrHavingRaw(having string) Havinger
	OrHavingSymbol(field, symbol string, value interface{}) Havinger
	OrHavingIn(field string, value interface{}) Havinger
	OrHavingNotIn(field string, value interface{}) Havinger
	Get() lib.SqlRows
	Find() lib.SqlRow
	Count() int64
	OrderBy(orderBy string) OrderByer
	Limit(offset, count int) Limiter
	Union(model Modeler) Unioner
	Pager(page, pageSize int) (lib.SqlRows, lib.SqlRow)
}

Havinger having条件

type Joiner

type Joiner interface {
	On(field string, value interface{}) Oner
	OnRaw(on string) Oner
	OnSymbol(field, symbol string, value interface{}) Oner
	OnIn(field string, value interface{}) Oner
	OnNotIn(field string, value interface{}) Oner
}

Joiner 连接接口

type Limit

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

type Limiter

type Limiter interface {
	Get() lib.SqlRows
	Find() lib.SqlRow
}

Limiter

type Model

type Model struct {
	PriKey string

	SoftDelete bool
	DeleteName string
	// contains filtered or unexported fields
}

Model 模型

func (*Model) Alias

func (m *Model) Alias(name string) Aliaser

Alias 命名

func (*Model) AndHaving

func (m *Model) AndHaving(field string, value interface{}) Havinger

AndHaving having条件

func (*Model) AndHavingIn

func (m *Model) AndHavingIn(field string, value interface{}) Havinger

AndHavingIn having条件

func (*Model) AndHavingNotIn

func (m *Model) AndHavingNotIn(field string, value interface{}) Havinger

AndHavingNotIn having条件

func (*Model) AndHavingRaw

func (m *Model) AndHavingRaw(having string) Havinger

AndHavingRaw having条件

func (*Model) AndHavingSymbol

func (m *Model) AndHavingSymbol(field, symbol string, value interface{}) Havinger

AndHavingSymbol having条件

func (*Model) AndOn

func (m *Model) AndOn(field string, value interface{}) Oner

AndOn Join 条件on

func (*Model) AndOnIn

func (m *Model) AndOnIn(field string, value interface{}) Oner

AndOnIn Join 条件on

func (*Model) AndOnNotIn

func (m *Model) AndOnNotIn(field string, value interface{}) Oner

AndOnNotIn Join 条件on

func (*Model) AndOnRaw

func (m *Model) AndOnRaw(on string) Oner

AndOnRaw Join 条件on

func (*Model) AndOnSymbol

func (m *Model) AndOnSymbol(field, symbol string, value interface{}) Oner

AndOnSymbol Join 条件on

func (*Model) AndWhere

func (m *Model) AndWhere(field string, value interface{}) Wherer

AndWhere where条件

func (*Model) AndWhereIn

func (m *Model) AndWhereIn(field string, value interface{}) Wherer

AndWhereIn where条件

func (*Model) AndWhereNotIn

func (m *Model) AndWhereNotIn(field string, value interface{}) Wherer

AndWhereNotIn where条件

func (*Model) AndWhereRaw

func (m *Model) AndWhereRaw(where string) Wherer

AndWhereRaw where条件

func (*Model) AndWhereSymbol

func (m *Model) AndWhereSymbol(field, symbol string, value interface{}) Wherer

AndWhereSymbol where条件

func (*Model) Count

func (m *Model) Count() int64

Count 统计数量

func (*Model) CountByClear

func (m *Model) CountByClear(isClear bool) int64

CountByClear 统计数量

func (*Model) Create

func (m *Model) Create(row lib.SqlIn) int64

Create 插入数据

func (*Model) DB

func (m *Model) DB() DBer

DBer 获得数据库接口

func (*Model) Delete

func (m *Model) Delete() int64

Delete 删除

func (*Model) Drop

func (m *Model) Drop() int64

Drop 删除表

func (*Model) Fields

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

Fields 查询字段

func (*Model) Find

func (m *Model) Find() lib.SqlRow

Find 获得单条数据

func (*Model) Get

func (m *Model) Get() lib.SqlRows

Get 获得列表

func (*Model) GetByClear

func (m *Model) GetByClear(isClear bool) lib.SqlRows

GetByClear 获得列表

func (*Model) GetLastSql

func (m *Model) GetLastSql() string

GetLastSql 获得最后执行的sql

func (*Model) GetModelSql

func (m *Model) GetModelSql() (string, []interface{})

GetModelSql 模型sql

func (*Model) GetSqlInfo

func (m *Model) GetSqlInfo() (string, []interface{})

GetSqlInfo 获得最后执行的sql

func (*Model) GroupBy

func (m *Model) GroupBy(field ...string) GroupByer

GroupBy 分组

func (*Model) Having

func (m *Model) Having(field string, value interface{}) Havinger

Having having条件

func (*Model) HavingIn

func (m *Model) HavingIn(field string, value interface{}) Havinger

HavingIn having条件

func (*Model) HavingNotIn

func (m *Model) HavingNotIn(field string, value interface{}) Havinger

HavingNotIn having条件

func (*Model) HavingRaw

func (m *Model) HavingRaw(having string) Havinger

HavingRaw having条件

func (*Model) HavingSymbol

func (m *Model) HavingSymbol(field, symbol string, value interface{}) Havinger

HavingSymbol having条件

func (*Model) Init

func (m *Model) Init(dbName, tableName string) Modeler

Init 初始化

func (*Model) InitByConnectName

func (m *Model) InitByConnectName(connectName, dbName, tableName string) Modeler

Init 初始化

func (*Model) Insert

func (m *Model) Insert(row lib.SqlIn) int64

Insert 插入操作

func (*Model) Join

func (m *Model) Join(tableName string) Joiner

Join 连接查询

func (*Model) LeftJoin

func (m *Model) LeftJoin(tableName string) Joiner

LeftJoin 连接查询

func (*Model) Limit

func (m *Model) Limit(offset, count int) Limiter

Limit 排序

func (*Model) On

func (m *Model) On(field string, value interface{}) Oner

On Join 条件on

func (*Model) OnIn

func (m *Model) OnIn(field string, value interface{}) Oner

OnIn Join 条件on

func (*Model) OnNotIn

func (m *Model) OnNotIn(field string, value interface{}) Oner

OnNotIn Join 条件on

func (*Model) OnRaw

func (m *Model) OnRaw(on string) Oner

OnRaw Join 条件on

func (*Model) OnSymbol

func (m *Model) OnSymbol(field, symbol string, value interface{}) Oner

OnSymbol Join 条件on

func (*Model) OrHaving

func (m *Model) OrHaving(field string, value interface{}) Havinger

OrHaving having条件

func (*Model) OrHavingIn

func (m *Model) OrHavingIn(field string, value interface{}) Havinger

OrHavingIn having条件

func (*Model) OrHavingNotIn

func (m *Model) OrHavingNotIn(field string, value interface{}) Havinger

OrHavingNotIn having条件

func (*Model) OrHavingRaw

func (m *Model) OrHavingRaw(having string) Havinger

OrHavingRaw having条件

func (*Model) OrHavingSymbol

func (m *Model) OrHavingSymbol(field, symbol string, value interface{}) Havinger

OrHavingSymbol having条件

func (*Model) OrOn

func (m *Model) OrOn(field string, value interface{}) Oner

OrOn Join 条件on

func (*Model) OrOnIn

func (m *Model) OrOnIn(field string, value interface{}) Oner

OrOnIn Join 条件on

func (*Model) OrOnNotIn

func (m *Model) OrOnNotIn(field string, value interface{}) Oner

OrOnNotIn Join 条件on

func (*Model) OrOnRaw

func (m *Model) OrOnRaw(on string) Oner

OrOnRaw Join 条件on

func (*Model) OrOnSymbol

func (m *Model) OrOnSymbol(field, symbol string, value interface{}) Oner

OrOnSymbol Join 条件on

func (*Model) OrWhere

func (m *Model) OrWhere(field string, value interface{}) Wherer

OrWhere where条件

func (*Model) OrWhereIn

func (m *Model) OrWhereIn(field string, value interface{}) Wherer

OrWhereIn where条件

func (*Model) OrWhereNotIn

func (m *Model) OrWhereNotIn(field string, value interface{}) Wherer

OrWhereNotIn where条件

func (*Model) OrWhereRaw

func (m *Model) OrWhereRaw(where string) Wherer

OrWhereRaw where条件

func (*Model) OrWhereSymbol

func (m *Model) OrWhereSymbol(field, symbol string, value interface{}) Wherer

OrWhereSymbol where条件

func (*Model) OrderBy

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

OrderBy 排序

func (*Model) Pager

func (m *Model) Pager(page, pageSize int) (lib.SqlRows, lib.SqlRow)

Pager 分页查询

func (*Model) RightJoin

func (m *Model) RightJoin(tableName string) Joiner

RightJoin 连接查询

func (*Model) Truncate

func (m *Model) Truncate() int64

Truncate 清空表

func (*Model) Union

func (m *Model) Union(model Modeler) Unioner

Union 联合查询

func (*Model) Update

func (m *Model) Update(data lib.SqlIn) int64

Update 更新操作

func (*Model) Where

func (m *Model) Where(field string, value interface{}) Wherer

Where where条件

func (*Model) WhereIn

func (m *Model) WhereIn(field string, value interface{}) Wherer

WhereIn where条件

func (*Model) WhereNotIn

func (m *Model) WhereNotIn(field string, value interface{}) Wherer

WhereNotIn where条件

func (*Model) WhereRaw

func (m *Model) WhereRaw(where string) Wherer

WhereRaw where条件

func (*Model) WhereSymbol

func (m *Model) WhereSymbol(field, symbol string, value interface{}) Wherer

WhereSymbol where条件

type Modeler

type Modeler interface {
	Fields(fields ...string) Fielder
	Count() int64
	GetLastSql() string
	GetSqlInfo() (string, []interface{})
	GetModelSql() (string, []interface{})
	Alias(name string) Aliaser
	Join(tableName string) Joiner
	LeftJoin(tableName string) Joiner
	RightJoin(tableName string) Joiner
	Union(model Modeler) Unioner
	Where(field string, value interface{}) Wherer
	WhereRaw(where string) Wherer
	WhereSymbol(field, symbol string, value interface{}) Wherer
	WhereIn(field string, value interface{}) Wherer
	WhereNotIn(field string, value interface{}) Wherer
	GroupBy(field ...string) GroupByer
	Get() lib.SqlRows
	Find() lib.SqlRow
	OrderBy(orderBy string) OrderByer
	Limit(offset, count int) Limiter
	Pager(page, pageSize int) (lib.SqlRows, lib.SqlRow)
	Update(data lib.SqlIn) int64
	Insert(row lib.SqlIn) int64
	Create(row lib.SqlIn) int64
	Drop() int64
	Truncate() int64
	Delete() int64
	DB() DBer
}

Modeler 模型接口

func NewModel

func NewModel(dbName, tableName string) Modeler

NewModel 新建一个模型

func NewModelByConnectName

func NewModelByConnectName(connectName, dbName, tableName string) Modeler

NewModelByConnectName 新建一个模型

type MsSqlConnect

type MsSqlConnect struct {
	Master DBConfig   `json:"master"`
	Slave  []DBConfig `json:"slave"`
}

MsSqlConnect mssql连接配置数据

func (MsSqlConnect) String

func (m MsSqlConnect) String() string

type MsSqlPool

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

MsSqlPool 全局连接池

func (*MsSqlPool) AddConnect

func (m *MsSqlPool) AddConnect(connect MsSqlConnect)

AddConnect 添加连接

func (*MsSqlPool) AddConnects

func (m *MsSqlPool) AddConnects(connects []MsSqlConnect)

AddConnects 添加多个连接

func (*MsSqlPool) BeginTx

func (m *MsSqlPool) BeginTx(connectName string)

BeginTx 开始事务

func (*MsSqlPool) Close

func (m *MsSqlPool) Close()

Close 关闭连接

func (*MsSqlPool) EndTx

func (m *MsSqlPool) EndTx(connectName string)

EndTx 获得事务连接

func (*MsSqlPool) GetDB

func (m *MsSqlPool) GetDB(connectName string) *sql.DB

GetDB 取出数据库连接

func (*MsSqlPool) GetIncludeReadDB

func (m *MsSqlPool) GetIncludeReadDB(connectName string) *sql.DB

GetIncludeReadDB 取出只读数据库连接

func (*MsSqlPool) GetTx

func (m *MsSqlPool) GetTx(connectName string) *sql.Tx

GetTx 获得事务连接

func (*MsSqlPool) Init

func (m *MsSqlPool) Init(connects []MsSqlConnect)

Init 初始化连接池

func (*MsSqlPool) IsTransaction

func (m *MsSqlPool) IsTransaction(connectName string) bool

IsTransaction 是否开启事务

func (*MsSqlPool) SetTx

func (m *MsSqlPool) SetTx(connectName string, tx *sql.Tx)

SetTx 设置事务连接

type MsSqlPooler

type MsSqlPooler interface {
	GetDB(connectName string) *sql.DB
	GetIncludeReadDB(connectName string) *sql.DB
	SetTx(connectName string, tx *sql.Tx)
	GetTx(connectName string) *sql.Tx
	BeginTx(connectName string)
	EndTx(connectName string)
	IsTransaction(connectName string) bool
	Close()
	Init(connect []MsSqlConnect)
	AddConnect(connect MsSqlConnect)
	AddConnects(connects []MsSqlConnect)
}

MysqlPooler 全局连接池接口

func NewPool

func NewPool(config MsSqlConnect) MsSqlPooler

NewPool 初始化数据库连接

func NewPools

func NewPools(configs []MsSqlConnect) MsSqlPooler

NewPools 初始化多数据库连接

type Oner

type Oner interface {
	OrOn(field string, value interface{}) Oner
	OrOnRaw(on string) Oner
	OrOnSymbol(field, symbol string, value interface{}) Oner
	OrOnIn(field string, value interface{}) Oner
	OrOnNotIn(field string, value interface{}) Oner
	AndOn(field string, value interface{}) Oner
	AndOnRaw(on string) Oner
	AndOnSymbol(field, symbol string, value interface{}) Oner
	AndOnIn(field string, value interface{}) Oner
	AndOnNotIn(field string, value interface{}) Oner
	Where(field string, value interface{}) Wherer
	WhereRaw(where string) Wherer
	WhereSymbol(field, symbol string, value interface{}) Wherer
	WhereIn(field string, value interface{}) Wherer
	WhereNotIn(field string, value interface{}) Wherer
	Join(tableName string) Joiner
	LeftJoin(tableName string) Joiner
	RightJoin(tableName string) Joiner
	GroupBy(field ...string) GroupByer
	Count() int64
	Get() lib.SqlRows
	Find() lib.SqlRow
	OrderBy(orderBy string) OrderByer
	Limit(offset, count int) Limiter
	Union(model Modeler) Unioner
	Pager(page, pageSize int) (lib.SqlRows, lib.SqlRow)
	Update(data lib.SqlIn) int64
}

Oner on连接条件

type OrderByer

type OrderByer interface {
	Get() lib.SqlRows
	Find() lib.SqlRow
	Limit(offset, count int) Limiter
	Pager(page, pageSize int) (lib.SqlRows, lib.SqlRow)
}

OrderByer 排序

type Table

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

Table 操作表

func (*Table) Delete

func (t *Table) Delete(onParams []interface{}, where string, whereParams ...interface{}) int64

Delete 删除表

func (*Table) Drop

func (t *Table) Drop() int64

Drop 删除表

func (*Table) GetDB

func (t *Table) GetDB() *DB

GetDB 获得数据库

func (*Table) GetLastSql

func (t *Table) GetLastSql() string

获得最后执行的sql

func (*Table) GetSqlInfo

func (t *Table) GetSqlInfo() (string, []interface{})

获得最后执行的sql

func (*Table) Insert

func (t *Table) Insert(row lib.SqlIn) int64

Insert 插入操作

func (*Table) Select

func (t *Table) Select(fields string, where string, whereParams ...interface{}) lib.SqlRows

Select 查询

func (*Table) SelectByHasWhere

func (t *Table) SelectByHasWhere(fields string, where string, hasWhere bool, page *Limit, whereParams ...interface{}) lib.SqlRows

SelectByHasWhere 查询

func (*Table) SetDB

func (t *Table) SetDB(db *DB)

SetDB 设置数据库

func (*Table) Truncate

func (t *Table) Truncate() int64

Truncate 清空表

func (*Table) Update

func (t *Table) Update(row lib.SqlIn, onParams []interface{}, where string, whereParams ...interface{}) int64

Update 更新操作

type Tabler

type Tabler interface {
	SetDB(db *DB)
	GetDB() *DB
	Insert(row lib.SqlIn) int64
	Drop() int64
	Truncate() int64
	Delete(onParams []interface{}, where string, whereParams ...interface{}) int64
	Update(row lib.SqlIn, onParams []interface{}, where string, whereParams ...interface{}) int64
	SelectByHasWhere(fields string, where string, hasWhere bool, page *Limit, whereParams ...interface{}) lib.SqlRows
	Select(fields string, where string, whereParams ...interface{}) lib.SqlRows
	GetLastSql() string
	GetSqlInfo() (string, []interface{})
}

Tabler 表操作接口

func NewTable

func NewTable(db *DB, tableName, firstTableName, pri string) Tabler

NewTable 初始化表

type Unioner

type Unioner interface {
	Count() int64
	Get() lib.SqlRows
	Find() lib.SqlRow
	OrderBy(orderBy string) OrderByer
	Limit(offset, count int) Limiter
	Pager(page, pageSize int) (lib.SqlRows, lib.SqlRow)
}

Unioner

type Wherer

type Wherer interface {
	AndWhere(field string, value interface{}) Wherer
	AndWhereRaw(where string) Wherer
	AndWhereSymbol(field, symbol string, value interface{}) Wherer
	AndWhereIn(field string, value interface{}) Wherer
	AndWhereNotIn(field string, value interface{}) Wherer
	OrWhere(field string, value interface{}) Wherer
	OrWhereRaw(where string) Wherer
	OrWhereSymbol(field, symbol string, value interface{}) Wherer
	OrWhereIn(field string, value interface{}) Wherer
	OrWhereNotIn(field string, value interface{}) Wherer
	GroupBy(field ...string) GroupByer
	Get() lib.SqlRows
	Find() lib.SqlRow
	Count() int64
	OrderBy(orderBy string) OrderByer
	Limit(offset, count int) Limiter
	Union(model Modeler) Unioner
	Pager(page, pageSize int) (lib.SqlRows, lib.SqlRow)
	Update(data lib.SqlIn) int64
	Delete() int64
}

Wherer 条件接口

Jump to

Keyboard shortcuts

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