Help us shape the future of Go! Take the 2026 Go Developer Survey

trysql

package module
v0.0.0-...-bc11111 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2022 License: MIT Imports: 11 Imported by: 0

README

测试 db_config.json

{
  "mysql": {
    "user": "wms_test",
    "password": "",
    "url": "ip:port",
    "database": "wms_dev"
  },
  "postgresql": {
    "user": "",
    "password": "",
    "url": "ip:port",
    "database": "aitrack_dev"
  }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DbSession

type DbSession interface {
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	Exec(query string, args ...interface{}) (sql.Result, error)
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
	Prepare(query string) (*sql.Stmt, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
	QueryRow(query string, args ...interface{}) *sql.Row
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)

	// Rollback a transaction
	Rollback() error
	// Commit a transaction
	Commit() error
	// InTx commits a transaction if no errors, otherwise rollback
	// txFunc is the operations wrapped in a transaction
	InTx(txFunc func() error) error
}

DbSession (SQL Go database connection) is a wrapper for SQL database handler ( can be *sql.DB or *sql.Tx) It should be able to work with all SQL data that follows SQL standard.

func NewTxSession

func NewTxSession(sdb *sql.DB, tx bool) DbSession

NewTxSession 创建 DbSession ,tx 为 true 时, 开启事务

func NewTxSessionContext

func NewTxSessionContext(sdb *sql.DB, tx bool, ctx context.Context, opts *sql.TxOptions) DbSession

NewTxSessionContext 创建 DbSession ,tx 为 true 时, 开启事务

The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to BeginTx is canceled.

The provided TxOptions is optional and may be nil if defaults should be used. If a non-default isolation level is used that the driver doesn't support, an error will be returned.

type DbType

type DbType uint8
const (
	Mysql DbType = iota
	Postgresql
)

type DefaultSqlSessionFactory

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

func (*DefaultSqlSessionFactory) Do

func (ssf *DefaultSqlSessionFactory) Do(sqlHandler SqlHandler) error

func (*DefaultSqlSessionFactory) DoContext

func (ssf *DefaultSqlSessionFactory) DoContext(ctx context.Context, sqlHandler SqlHandler) error

func (*DefaultSqlSessionFactory) DoInTx

func (ssf *DefaultSqlSessionFactory) DoInTx(sqlHandler SqlHandler) error

func (*DefaultSqlSessionFactory) DoInTxContext

func (ssf *DefaultSqlSessionFactory) DoInTxContext(ctx context.Context, sqlHandler SqlHandler) error

func (*DefaultSqlSessionFactory) DoInTxTimeoutContext

func (ssf *DefaultSqlSessionFactory) DoInTxTimeoutContext(timeout time.Duration, ctx context.Context, sqlHandler SqlHandler) error

func (*DefaultSqlSessionFactory) DoTimeoutContext

func (ssf *DefaultSqlSessionFactory) DoTimeoutContext(timeout time.Duration, ctx context.Context, sqlHandler SqlHandler) error

func (*DefaultSqlSessionFactory) NewSqlSession

func (ssf *DefaultSqlSessionFactory) NewSqlSession() SqlSession

func (*DefaultSqlSessionFactory) NewTimeoutContext

func (ssf *DefaultSqlSessionFactory) NewTimeoutContext(ctx context.Context, duration ...time.Duration) (context.Context, context.CancelFunc)

func (*DefaultSqlSessionFactory) NewTxDbSession

func (ssf *DefaultSqlSessionFactory) NewTxDbSession() DbSession

func (*DefaultSqlSessionFactory) NewTxDbSessionContext

func (ssf *DefaultSqlSessionFactory) NewTxDbSessionContext(ctx context.Context, opts *sql.TxOptions) DbSession

func (*DefaultSqlSessionFactory) NewTxSqlSession

func (ssf *DefaultSqlSessionFactory) NewTxSqlSession(dbSession DbSession) SqlSession

type MySqlSession

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

func (*MySqlSession) AddParam

func (sb *MySqlSession) AddParam(param string, value any) SqlSession

func (*MySqlSession) AddParamSelective

func (sb *MySqlSession) AddParamSelective(param string, value any) SqlSession

func (*MySqlSession) And

func (sb *MySqlSession) And() SqlSession

func (*MySqlSession) Append

func (sb *MySqlSession) Append(sql SqlSession) SqlSession

func (*MySqlSession) AppendRaw

func (sb *MySqlSession) AppendRaw(sql string, args ...any) SqlSession

func (*MySqlSession) AsList

func (sb *MySqlSession) AsList(dest any) error

func (*MySqlSession) AsListContext

func (sb *MySqlSession) AsListContext(ctx context.Context, dest any) error

func (*MySqlSession) AsMap

func (sb *MySqlSession) AsMap() (map[string]any, error)

func (*MySqlSession) AsMapContext

func (sb *MySqlSession) AsMapContext(ctx context.Context) (map[string]any, error)

func (*MySqlSession) AsMapList

func (sb *MySqlSession) AsMapList() ([]map[string]any, error)

func (*MySqlSession) AsMapListContext

func (sb *MySqlSession) AsMapListContext(ctx context.Context) ([]map[string]any, error)

func (*MySqlSession) AsPrimitive

func (sb *MySqlSession) AsPrimitive(dest any) error

func (*MySqlSession) AsPrimitiveContext

func (sb *MySqlSession) AsPrimitiveContext(ctx context.Context, dest any) error

func (*MySqlSession) AsPrimitiveList

func (sb *MySqlSession) AsPrimitiveList(dest any) error

func (*MySqlSession) AsPrimitiveListContext

func (sb *MySqlSession) AsPrimitiveListContext(ctx context.Context, dest any) error

func (*MySqlSession) AsSingle

func (sb *MySqlSession) AsSingle(dest any) error

func (*MySqlSession) AsSingleContext

func (sb *MySqlSession) AsSingleContext(ctx context.Context, dest any) error

func (MySqlSession) Commit

func (bss MySqlSession) Commit() error

func (*MySqlSession) DeleteFrom

func (sb *MySqlSession) DeleteFrom(table string) SqlSession

func (*MySqlSession) Done

func (sb *MySqlSession) Done() error

func (*MySqlSession) DoneContext

func (sb *MySqlSession) DoneContext(ctx context.Context) error

func (*MySqlSession) DoneInsertId

func (sb *MySqlSession) DoneInsertId(column string) (int64, error)

func (*MySqlSession) DoneInsertIdContext

func (sb *MySqlSession) DoneInsertIdContext(ctx context.Context, _ string) (int64, error)

func (*MySqlSession) DoneRowsAffected

func (sb *MySqlSession) DoneRowsAffected() (int64, error)

func (*MySqlSession) DoneRowsAffectedContext

func (sb *MySqlSession) DoneRowsAffectedContext(ctx context.Context) (int64, error)

func (MySqlSession) Exec

func (bss MySqlSession) Exec(query string, args ...any) (sql.Result, error)

func (MySqlSession) ExecContext

func (bss MySqlSession) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)

func (*MySqlSession) From

func (sb *MySqlSession) From(tables ...string) SqlSession

func (*MySqlSession) GroupBy

func (sb *MySqlSession) GroupBy(columns ...string) SqlSession

func (*MySqlSession) Having

func (sb *MySqlSession) Having(condition string, value any) SqlSession

func (MySqlSession) In

func (bss MySqlSession) In(column string, args []any)

func (MySqlSession) InTx

func (bss MySqlSession) InTx(txFunc func() error) error

func (*MySqlSession) InnerJoin

func (sb *MySqlSession) InnerJoin(joins ...string) SqlSession

func (*MySqlSession) InnerJoinSelective

func (sb *MySqlSession) InnerJoinSelective(join string, condition any) SqlSession

func (*MySqlSession) InsertInto

func (sb *MySqlSession) InsertInto(table string) SqlSession

func (*MySqlSession) IntoColumns

func (sb *MySqlSession) IntoColumns(columns ...string) SqlSession

func (*MySqlSession) IntoMultiValues

func (sb *MySqlSession) IntoMultiValues(values [][]any) SqlSession

func (*MySqlSession) IntoValues

func (sb *MySqlSession) IntoValues(values ...any) SqlSession

func (*MySqlSession) Join

func (sb *MySqlSession) Join(joins ...string) SqlSession

func (*MySqlSession) LeftOuterJoin

func (sb *MySqlSession) LeftOuterJoin(joins ...string) SqlSession

func (*MySqlSession) Limit

func (sb *MySqlSession) Limit(limit int) SqlSession

func (*MySqlSession) LogSql

func (sb *MySqlSession) LogSql(logSql bool) SqlSession

func (*MySqlSession) New

func (sb *MySqlSession) New() SqlSession

func (MySqlSession) NotIn

func (bss MySqlSession) NotIn(column string, args []any)

func (*MySqlSession) Offset

func (sb *MySqlSession) Offset(offset int) SqlSession

func (*MySqlSession) Or

func (sb *MySqlSession) Or() SqlSession

func (*MySqlSession) OrderBy

func (sb *MySqlSession) OrderBy(columns ...string) SqlSession

func (*MySqlSession) OuterJoin

func (sb *MySqlSession) OuterJoin(joins ...string) SqlSession

func (MySqlSession) Prepare

func (bss MySqlSession) Prepare(query string) (*sql.Stmt, error)

func (MySqlSession) PrepareContext

func (bss MySqlSession) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

func (MySqlSession) Query

func (bss MySqlSession) Query(query string, args ...any) (*sql.Rows, error)

func (MySqlSession) QueryContext

func (bss MySqlSession) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

func (MySqlSession) QueryRow

func (bss MySqlSession) QueryRow(query string, args ...any) *sql.Row

func (MySqlSession) QueryRowContext

func (bss MySqlSession) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

func (*MySqlSession) Reset

func (sb *MySqlSession) Reset() SqlSession

func (*MySqlSession) RightOuterJoin

func (sb *MySqlSession) RightOuterJoin(joins ...string) SqlSession

func (MySqlSession) Rollback

func (bss MySqlSession) Rollback() error

func (*MySqlSession) Select

func (sb *MySqlSession) Select(columns ...string) SqlSession

func (*MySqlSession) Set

func (sb *MySqlSession) Set(column string, value any) SqlSession

func (*MySqlSession) SetSelective

func (sb *MySqlSession) SetSelective(column string, value any) SqlSession

func (*MySqlSession) Update

func (sb *MySqlSession) Update(table string) SqlSession

func (*MySqlSession) Values

func (sb *MySqlSession) Values(column string, value any) SqlSession

func (*MySqlSession) ValuesSelective

func (sb *MySqlSession) ValuesSelective(column string, value any) SqlSession

func (*MySqlSession) Where

func (sb *MySqlSession) Where(condition string, args ...any) SqlSession

func (*MySqlSession) WhereIn

func (sb *MySqlSession) WhereIn(column string, args []any) SqlSession

func (*MySqlSession) WhereInInt64

func (sb *MySqlSession) WhereInInt64(column string, args []int64) SqlSession

func (*MySqlSession) WhereNotIn

func (sb *MySqlSession) WhereNotIn(column string, args []any) SqlSession

func (*MySqlSession) WhereNotInInt64

func (sb *MySqlSession) WhereNotInInt64(column string, args []int64) SqlSession

func (*MySqlSession) WhereSelective

func (sb *MySqlSession) WhereSelective(condition string, arg any) SqlSession

type NonTxDbSession

type NonTxDbSession struct {
	DB *sql.DB
}

NonTxDbSession is the concrete implementation of DbSession by using *sql.DB

func (*NonTxDbSession) Commit

func (tx *NonTxDbSession) Commit() error

Commit NOP

func (*NonTxDbSession) Exec

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

func (*NonTxDbSession) ExecContext

func (tx *NonTxDbSession) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*NonTxDbSession) InTx

func (tx *NonTxDbSession) InTx(_ func() error) error

InTx NOP

func (*NonTxDbSession) Prepare

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

func (*NonTxDbSession) PrepareContext

func (tx *NonTxDbSession) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

func (*NonTxDbSession) Query

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

func (*NonTxDbSession) QueryContext

func (tx *NonTxDbSession) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

func (*NonTxDbSession) QueryRow

func (tx *NonTxDbSession) QueryRow(query string, args ...interface{}) *sql.Row

func (*NonTxDbSession) QueryRowContext

func (tx *NonTxDbSession) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

func (*NonTxDbSession) Rollback

func (tx *NonTxDbSession) Rollback() error

Rollback NOP

type PostgreSqlSession

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

func (*PostgreSqlSession) AddParam

func (sb *PostgreSqlSession) AddParam(param string, value any) SqlSession

func (*PostgreSqlSession) AddParamSelective

func (sb *PostgreSqlSession) AddParamSelective(param string, value any) SqlSession

func (*PostgreSqlSession) And

func (sb *PostgreSqlSession) And() SqlSession

func (*PostgreSqlSession) Append

func (sb *PostgreSqlSession) Append(sql SqlSession) SqlSession

func (*PostgreSqlSession) AppendRaw

func (sb *PostgreSqlSession) AppendRaw(sql string, args ...any) SqlSession

func (*PostgreSqlSession) AsList

func (sb *PostgreSqlSession) AsList(dest any) error

func (*PostgreSqlSession) AsListContext

func (sb *PostgreSqlSession) AsListContext(ctx context.Context, dest any) error

func (*PostgreSqlSession) AsMap

func (sb *PostgreSqlSession) AsMap() (map[string]any, error)

func (*PostgreSqlSession) AsMapContext

func (sb *PostgreSqlSession) AsMapContext(ctx context.Context) (map[string]any, error)

func (*PostgreSqlSession) AsMapList

func (sb *PostgreSqlSession) AsMapList() ([]map[string]any, error)

func (*PostgreSqlSession) AsMapListContext

func (sb *PostgreSqlSession) AsMapListContext(ctx context.Context) ([]map[string]any, error)

func (*PostgreSqlSession) AsPrimitive

func (sb *PostgreSqlSession) AsPrimitive(dest any) error

func (*PostgreSqlSession) AsPrimitiveContext

func (sb *PostgreSqlSession) AsPrimitiveContext(ctx context.Context, dest any) error

func (*PostgreSqlSession) AsPrimitiveList

func (sb *PostgreSqlSession) AsPrimitiveList(dest any) error

func (*PostgreSqlSession) AsPrimitiveListContext

func (sb *PostgreSqlSession) AsPrimitiveListContext(ctx context.Context, dest any) error

func (*PostgreSqlSession) AsSingle

func (sb *PostgreSqlSession) AsSingle(dest any) error

func (*PostgreSqlSession) AsSingleContext

func (sb *PostgreSqlSession) AsSingleContext(ctx context.Context, dest any) error

func (PostgreSqlSession) Commit

func (bss PostgreSqlSession) Commit() error

func (*PostgreSqlSession) DeleteFrom

func (sb *PostgreSqlSession) DeleteFrom(table string) SqlSession

func (*PostgreSqlSession) Done

func (sb *PostgreSqlSession) Done() error

func (*PostgreSqlSession) DoneContext

func (sb *PostgreSqlSession) DoneContext(ctx context.Context) error

func (*PostgreSqlSession) DoneInsertId

func (sb *PostgreSqlSession) DoneInsertId(column string) (int64, error)

func (*PostgreSqlSession) DoneInsertIdContext

func (sb *PostgreSqlSession) DoneInsertIdContext(ctx context.Context, column string) (int64, error)

func (*PostgreSqlSession) DoneRowsAffected

func (sb *PostgreSqlSession) DoneRowsAffected() (int64, error)

func (*PostgreSqlSession) DoneRowsAffectedContext

func (sb *PostgreSqlSession) DoneRowsAffectedContext(ctx context.Context) (int64, error)

func (PostgreSqlSession) Exec

func (bss PostgreSqlSession) Exec(query string, args ...any) (sql.Result, error)

func (PostgreSqlSession) ExecContext

func (bss PostgreSqlSession) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)

func (*PostgreSqlSession) From

func (sb *PostgreSqlSession) From(tables ...string) SqlSession

func (*PostgreSqlSession) GroupBy

func (sb *PostgreSqlSession) GroupBy(columns ...string) SqlSession

func (*PostgreSqlSession) Having

func (sb *PostgreSqlSession) Having(condition string, value any) SqlSession

func (PostgreSqlSession) In

func (bss PostgreSqlSession) In(column string, args []any)

func (*PostgreSqlSession) InTx

func (sb *PostgreSqlSession) InTx(txFunc func() error) error

func (*PostgreSqlSession) InnerJoin

func (sb *PostgreSqlSession) InnerJoin(joins ...string) SqlSession

func (*PostgreSqlSession) InnerJoinSelective

func (sb *PostgreSqlSession) InnerJoinSelective(join string, condition any) SqlSession

func (*PostgreSqlSession) InsertInto

func (sb *PostgreSqlSession) InsertInto(table string) SqlSession

func (*PostgreSqlSession) IntoColumns

func (sb *PostgreSqlSession) IntoColumns(columns ...string) SqlSession

func (*PostgreSqlSession) IntoMultiValues

func (sb *PostgreSqlSession) IntoMultiValues(values [][]any) SqlSession

func (*PostgreSqlSession) IntoValues

func (sb *PostgreSqlSession) IntoValues(values ...any) SqlSession

func (*PostgreSqlSession) Join

func (sb *PostgreSqlSession) Join(joins ...string) SqlSession

func (*PostgreSqlSession) LeftOuterJoin

func (sb *PostgreSqlSession) LeftOuterJoin(joins ...string) SqlSession

func (*PostgreSqlSession) Limit

func (sb *PostgreSqlSession) Limit(limit int) SqlSession

func (*PostgreSqlSession) LogSql

func (sb *PostgreSqlSession) LogSql(logSql bool) SqlSession

func (*PostgreSqlSession) New

func (sb *PostgreSqlSession) New() SqlSession

func (PostgreSqlSession) NotIn

func (bss PostgreSqlSession) NotIn(column string, args []any)

func (*PostgreSqlSession) Offset

func (sb *PostgreSqlSession) Offset(offset int) SqlSession

func (*PostgreSqlSession) Or

func (sb *PostgreSqlSession) Or() SqlSession

func (*PostgreSqlSession) OrderBy

func (sb *PostgreSqlSession) OrderBy(columns ...string) SqlSession

func (*PostgreSqlSession) OuterJoin

func (sb *PostgreSqlSession) OuterJoin(joins ...string) SqlSession

func (PostgreSqlSession) Prepare

func (bss PostgreSqlSession) Prepare(query string) (*sql.Stmt, error)

func (PostgreSqlSession) PrepareContext

func (bss PostgreSqlSession) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

func (PostgreSqlSession) Query

func (bss PostgreSqlSession) Query(query string, args ...any) (*sql.Rows, error)

func (PostgreSqlSession) QueryContext

func (bss PostgreSqlSession) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

func (PostgreSqlSession) QueryRow

func (bss PostgreSqlSession) QueryRow(query string, args ...any) *sql.Row

func (PostgreSqlSession) QueryRowContext

func (bss PostgreSqlSession) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

func (*PostgreSqlSession) Reset

func (sb *PostgreSqlSession) Reset() SqlSession

func (*PostgreSqlSession) RightOuterJoin

func (sb *PostgreSqlSession) RightOuterJoin(joins ...string) SqlSession

func (PostgreSqlSession) Rollback

func (bss PostgreSqlSession) Rollback() error

func (*PostgreSqlSession) Select

func (sb *PostgreSqlSession) Select(columns ...string) SqlSession

func (*PostgreSqlSession) Set

func (sb *PostgreSqlSession) Set(column string, value any) SqlSession

func (*PostgreSqlSession) SetSelective

func (sb *PostgreSqlSession) SetSelective(column string, value any) SqlSession

func (*PostgreSqlSession) Update

func (sb *PostgreSqlSession) Update(table string) SqlSession

func (*PostgreSqlSession) Values

func (sb *PostgreSqlSession) Values(column string, value any) SqlSession

func (*PostgreSqlSession) ValuesSelective

func (sb *PostgreSqlSession) ValuesSelective(column string, value any) SqlSession

func (*PostgreSqlSession) Where

func (sb *PostgreSqlSession) Where(condition string, args ...any) SqlSession

func (*PostgreSqlSession) WhereIn

func (sb *PostgreSqlSession) WhereIn(column string, args []any) SqlSession

func (*PostgreSqlSession) WhereInInt64

func (sb *PostgreSqlSession) WhereInInt64(column string, args []int64) SqlSession

func (*PostgreSqlSession) WhereNotIn

func (sb *PostgreSqlSession) WhereNotIn(column string, args []any) SqlSession

func (*PostgreSqlSession) WhereNotInInt64

func (sb *PostgreSqlSession) WhereNotInInt64(column string, args []int64) SqlSession

func (*PostgreSqlSession) WhereSelective

func (sb *PostgreSqlSession) WhereSelective(condition string, arg any) SqlSession

type SqlHandler

type SqlHandler func(ctx context.Context, sqlSession SqlSession) error

SqlHandler sql 执行函数 , ctx 是 Timeout Context

type SqlSession

type SqlSession interface {

	// Select  构建 Select 查询的列
	Select(columns ...string) SqlSession

	// From  构建 Select 的 From 子句
	From(tables ...string) SqlSession

	// Where  构建 Select, Update, Delete 的 Where 子句
	Where(condition string, args ...any) SqlSession

	// WhereSelective  当 arg 不为 零值时,构建 Select, Update, Delete 的 Where 子句
	WhereSelective(condition string, arg any) SqlSession

	// WhereIn  当 args 不空时,构建 Where 子句 IN 表达式
	WhereIn(column string, args []any) SqlSession

	// WhereNotIn 当 args 不空时, 构建 Where 子句 NotIn 表达式
	WhereNotIn(column string, args []any) SqlSession

	// WhereInInt64  当 args 不空时,构建 Where 子句 IN 表达式
	WhereInInt64(column string, args []int64) SqlSession

	// WhereNotInInt64 当 args 不空时, 构建 Where 子句 NotIn 表达式
	WhereNotInInt64(column string, args []int64) SqlSession

	// GroupBy  构建 GroupBy 子句
	GroupBy(columns ...string) SqlSession

	// Having  构建 Having 子句
	Having(condition string, value any) SqlSession

	// OrderBy  构建 OrderBy 子句
	OrderBy(columns ...string) SqlSession

	// InsertInto  构建 Insert 的 表
	InsertInto(table string) SqlSession

	// Values  构建 Insert 的 Value 子句
	Values(column string, value any) SqlSession

	// ValuesSelective  当 value 不为零值时,构建 Insert 的 Value 子句
	ValuesSelective(column string, value any) SqlSession

	// IntoColumns  构建 Insert 的 INTO 子句
	IntoColumns(columns ...string) SqlSession

	// IntoValues  构建 Insert 的 VALUES 子句
	IntoValues(values ...any) SqlSession

	// IntoMultiValues  批量构建 Insert 的 VALUES 子句,列: INSERT INTO A(ID,NAME) VALUES (1,'1'),(2,'2'),(3,'3')
	IntoMultiValues(values [][]any) SqlSession

	// Update 构建 Update 的表
	Update(table string) SqlSession

	// Set 构建 Update 的 Set 子句
	Set(column string, value any) SqlSession

	// SetSelective 当value 不为零值时, 构建 Update 的 Set 子句
	SetSelective(column string, value any) SqlSession

	// DeleteFrom 构建 Delete 的表
	DeleteFrom(table string) SqlSession

	// InnerJoin 构建 INNER JOIN 的表对象
	InnerJoin(joins ...string) SqlSession

	// InnerJoinSelective 当 condition 不为零值时, 构建 INNER JOIN 的表对象
	InnerJoinSelective(join string, condition any) SqlSession

	// LeftOuterJoin 构建 LEFT OUTER JOIN 的表对象
	LeftOuterJoin(joins ...string) SqlSession

	// RightOuterJoin 构建 LEFT OUTER JOIN 的表对象
	RightOuterJoin(joins ...string) SqlSession

	// OuterJoin 构建 OUTER JOIN 的表对象
	OuterJoin(joins ...string) SqlSession

	// Or 构建 Where 子句的 OR 表达式
	Or() SqlSession

	// And 构建 Where 子句的 AND 表达式
	And() SqlSession

	// Limit 构建 SELECT 的 LIMIT 子句
	Limit(limit int) SqlSession

	// Offset 构建 SELECT 的 Offset 子句
	Offset(offset int) SqlSession

	// AddParam 单独添加 SQL 动态参数值
	AddParam(param string, value any) SqlSession

	// AddParamSelective 单独添加 SQL 动态参数值, 仅当 value 不为零值时添加
	AddParamSelective(param string, value any) SqlSession

	// AppendRaw 在非 Append 方法自动构建的 SQL 之后追加 SQL
	AppendRaw(rawSql string, args ...any) SqlSession

	// Append 在非 Append 方法自动构建的 SQL 之后追加 SqlSession
	Append(sql SqlSession) SqlSession

	// DoneContext 执行 SQL
	DoneContext(ctx context.Context) error

	// Done 执行 SQL
	Done() error

	// DoneInsertIdContext 执行 INSERT SQL, 同时返回插入记录的 Id
	DoneInsertIdContext(ctx context.Context, column string) (int64, error)

	// DoneInsertId 执行 INSERT SQL, 同时返回插入记录的 Id
	DoneInsertId(column string) (int64, error)

	// DoneRowsAffectedContext 执行更新 SQL(Update, Delete), 同时返回操作的记录数
	DoneRowsAffectedContext(ctx context.Context) (int64, error)

	// DoneRowsAffected 执行更新 SQL(Update, Delete), 同时返回操作的记录数
	DoneRowsAffected() (int64, error)

	// AsSingleContext 执行 SQL,dest 是普通 struct 的引用指针
	AsSingleContext(ctx context.Context, dest any) error

	// AsSingle 执行 SQL,dest 是普通 struct 的引用指针
	AsSingle(dest any) error

	// AsListContext 执行 SQL,dest 是 slice 类型
	AsListContext(ctx context.Context, dest any) error

	// AsList 执行 SQL,dest 是 slice of struct 类型
	AsList(dest any) error

	// AsPrimitiveContext 执行 SQL,dest 是 primitive 类型
	AsPrimitiveContext(ctx context.Context, dest any) error

	// AsPrimitive 执行 SQL,dest 是 primitive 类型
	AsPrimitive(dest any) error

	// AsPrimitiveListContext 执行 SQL,dest 是 slice of primitive 类型
	AsPrimitiveListContext(ctx context.Context, dest any) error

	// AsPrimitiveList 执行 SQL,dest 是 slice of primitive 类型
	AsPrimitiveList(dest any) error

	// AsMapListContext 执行 SQL
	AsMapListContext(ctx context.Context) ([]map[string]any, error)

	// AsMapList 执行 SQL, 结果生成 Map 对象
	AsMapList() ([]map[string]any, error)

	// AsMapContext 执行 SQL
	AsMapContext(ctx context.Context) (map[string]any, error)

	// AsMap 执行 SQL, 结果生成 Map 对象
	AsMap() (map[string]any, error)

	// Reset 重置当前 SqlSession 以再次使用
	Reset() SqlSession

	// New 基于当前 SqlSession的 数据库连接,新建 一个 SqlSession ,
	New() SqlSession

	// LogSql 是否输出 Sql 信息,必须在 SQL 构建执行之前(Done***, As***)调用
	LogSql(logSql bool) SqlSession

	// DbSession SQL 最终代理到 该 DbSession 执行
	DbSession
}

SqlSession 用于构建 SQL,非线程安全

func NewMySqlSession

func NewMySqlSession(dbSession DbSession) SqlSession

func NewPostgreSqlSession

func NewPostgreSqlSession(dbSession DbSession) SqlSession

type SqlSessionFactory

type SqlSessionFactory interface {

	// NewSqlSession 新建一个 非事务 SqlSession
	NewSqlSession() SqlSession

	// NewTxSqlSession 新建一个 事务性 SqlSession, dbSession 提供事务支持
	NewTxSqlSession(dbSession DbSession) SqlSession

	// NewTxDbSession 新建一个 DbSession
	NewTxDbSession() DbSession

	// NewTxDbSessionContext 新建一个 DbSession
	//
	// The provided context is used until the transaction is committed or rolled back.
	// If the context is canceled, the sql package will roll back
	// the transaction. Tx.Commit will return an error if the context provided to
	// BeginTx is canceled.
	//
	// The provided TxOptions is optional and may be nil if defaults should be used.
	// If a non-default isolation level is used that the driver doesn't support,
	// an error will be returned.
	NewTxDbSessionContext(ctx context.Context, opts *sql.TxOptions) DbSession

	// NewTimeoutContext 新建一个 Timeout Context
	NewTimeoutContext(ctx context.Context, duration ...time.Duration) (context.Context, context.CancelFunc)

	// DoTimeoutContext 执行 非事务 Sql 查询,指定超时时间
	DoTimeoutContext(timeout time.Duration, ctx context.Context, sqlHandler SqlHandler) error

	// DoContext 执行 非事务 Sql 查询,使用默认超时
	DoContext(ctx context.Context, sqlHandler SqlHandler) error

	// Do 执行 非事务 Sql 查询, 使用默认超时
	Do(sqlHandler SqlHandler) error

	// DoInTxTimeoutContext 在一个事务中 执行 Sql 查询,指定超时时间
	DoInTxTimeoutContext(timeout time.Duration, ctx context.Context, sqlHandler SqlHandler) error

	// DoInTxContext 在一个事务中 执行 Sql 查询,使用默认超时
	DoInTxContext(ctx context.Context, sqlHandler SqlHandler) error

	// DoInTx 在一个事务中 执行 Sql 查询,使用默认超时
	DoInTx(sqlHandler SqlHandler) error
}

func NewSqlSessionFactory

func NewSqlSessionFactory(dbType DbType, db *sql.DB, sqlTimeout time.Duration, logSqlEnabled bool) SqlSessionFactory

NewSqlSessionFactory 新建一个 SqlSessionFactory,sqlTimeout 指定一个 SqlSession的 执行超时时间

func NewSqlSessionFactoryByDSN

func NewSqlSessionFactoryByDSN(dbType DbType, dsn string, maxActive, maxIdle int, connMaxLifetime, connMaxIdleTime, sqlTimeout time.Duration, logSqlEnabled bool) (SqlSessionFactory, error)

NewSqlSessionFactoryByDSN 初始化数据配置。 dsn 数据库连接字符串。 尝试根据指定的连接字符串创建数据库连接并且Ping,如果成功则返回nil,否则返回连接时发生的错误。

type TxDbSession

type TxDbSession struct {
	DB *sql.Tx
}

TxDbSession is the concrete implementation of DbSession by using *sql.Tx

func (*TxDbSession) Commit

func (tx *TxDbSession) Commit() error

func (*TxDbSession) Exec

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

func (*TxDbSession) ExecContext

func (tx *TxDbSession) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*TxDbSession) InTx

func (tx *TxDbSession) InTx(txFunc func() error) error

func (*TxDbSession) Prepare

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

func (*TxDbSession) PrepareContext

func (tx *TxDbSession) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

func (*TxDbSession) Query

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

func (*TxDbSession) QueryContext

func (tx *TxDbSession) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

func (*TxDbSession) QueryRow

func (tx *TxDbSession) QueryRow(query string, args ...interface{}) *sql.Row

func (*TxDbSession) QueryRowContext

func (tx *TxDbSession) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

func (*TxDbSession) Rollback

func (tx *TxDbSession) Rollback() error

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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