session

package
v0.0.0-...-7550b6a Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: MIT-0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTableNoPK                         = errors.New("worm: table no primary key")
	ErrTablePKValuesZero                 = errors.New("worm: values of primary keys are zero")
	ErrMissingWhere                      = errors.New("worm: requires WHERE clause, use .WHERE() first")
	ErrInsertValuesEmpty                 = errors.New("worm: insert values are empty")
	ErrUpdateValuesNil                   = errors.New("worm: update values are nil")
	ErrUpdateMapEmpty                    = errors.New("worm: update map is empty")
	ErrSelectValueNil                    = errors.New("worm: select values are nil")
	ErrSelectValuesNotPointer            = errors.New("worm: select values must be pointer, eg: *Struct, *[]Struct, *[]*Struct")
	ErrSelectDestinationValueInvalid     = errors.New("worm: select destination value is invalid")
	ErrSelectDestinationValueNotSettable = errors.New("worm: select destination value is not settable")
	ErrSelectDestinationSliceNotSettable = errors.New("worm: select destination slice is not settable")
)
View Source
var (
	ErrModelNotSpecified       = errors.New("worm: data model is not be specified")
	ErrModelNoExportableFields = errors.New("worm: data model has no exportable fields")
)

Functions

This section is empty.

Types

type BaseDatabase

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

BaseDatabase 定义数据库操作的基本接口

type HookAfterDelete

type HookAfterDelete interface {
	CallAfterDelete(sess *Session) error
}

HookAfterDelete 在删除数据之后调用

type HookAfterInsert

type HookAfterInsert interface {
	CallAfterInsert(sess *Session) error
}

HookAfterInsert 在插入数据之后调用

type HookAfterSelect

type HookAfterSelect interface {
	CallAfterSelect(sess *Session) error
}

HookAfterSelect 在查询数据之后调用

type HookAfterUpdate

type HookAfterUpdate interface {
	CallAfterUpdate(sess *Session) error
}

HookAfterUpdate 在更新数据之后调用

type HookBeforeDelete

type HookBeforeDelete interface {
	CallBeforeDelete(sess *Session) error
}

HookBeforeDelete 在删除数据之前调用

type HookBeforeInsert

type HookBeforeInsert interface {
	CallBeforeInsert(sess *Session) error
}

HookBeforeInsert 在插入数据之前调用

type HookBeforeSelect

type HookBeforeSelect interface {
	CallBeforeSelect(sess *Session) error
}

HookBeforeSelect 在查询数据之前调用

type HookBeforeUpdate

type HookBeforeUpdate interface {
	CallBeforeUpdate(sess *Session) error
}

HookBeforeUpdate 在更新数据之前调用

type Session

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

Session 数据库的连接会话

func New

func New(id uint64, dbname string, db *sql.DB, dial dialect.Dialect, stmts *sync.Map) *Session

New 新建一个连接会话(默认查询超时时间10s)

func (*Session) Begin

func (own *Session) Begin() (err error)

Begin 开始事务

func (*Session) COUNT

func (own *Session) COUNT(column ...string) (int64, result.Result, error)

COUNT 根据条件统计数据

func (*Session) CallMethod

func (own *Session) CallMethod(method string, value any) error

CallMethod 调用钩子函数

func (*Session) Close

func (own *Session) Close() error

Close 关闭会话

func (*Session) Commit

func (own *Session) Commit() (err error)

Commit 提交事务

func (*Session) CreateTable

func (own *Session) CreateTable(metadata ...dialect.TableMetadata) error

CreateTable 创建数据表

func (*Session) DELETE

func (own *Session) DELETE(value ...any) (result.Result, error)

DELETE 删除数据

  • value: 支持格式 map[[string]]any、struct、*struct
  • 注意:若使用 struct 或 *struct 参数时,若没有 WHERE 条件,则主动使用主键作为 WHERE 条件,若主键均为零值,则返回错误

func (*Session) DUPLICATE

func (own *Session) DUPLICATE(columns ...string) *Session

DUPLICATE 执行 INSERT 语句时,主键或唯一索引冲突时的更新操作

func (*Session) DeleteTable

func (own *Session) DeleteTable() error

DeleteTable 删除数据表

func (*Session) Exec

func (own *Session) Exec(sql string, args ...any) (sql.Result, error)

Exec 执行 SQL 语句

func (*Session) ExecContext

func (own *Session) ExecContext(ctx context.Context, sql string, args ...any) (sql.Result, error)

ExecContext 执行带上下文的 SQL 语句

func (*Session) ExistsTable

func (own *Session) ExistsTable() bool

ExistsTable 检查数据表是否存在

func (*Session) FROM

func (own *Session) FROM(table string) *Session

FROM 写入 FROM 子句

func (*Session) GROUP

func (own *Session) GROUP(columns ...string) *Session

GROUP 写入 GROUP BY 子句

func (*Session) GetDatabase

func (own *Session) GetDatabase() BaseDatabase

GetDatabase 返回 *sql.DB 或 *sql.TX

func (*Session) GetID

func (own *Session) GetID() uint64

GetID 获取会话ID

func (*Session) GetSchema

func (own *Session) GetSchema() (*schema.Schema, error)

GetSchema 获取数据表

func (*Session) HAVING

func (own *Session) HAVING(condition where.Condition) *Session

HAVING 写入 HAVING 子句

func (*Session) INSERT

func (own *Session) INSERT(ignore bool, values ...any) (result.Result, error)

INSERT 插入单条或多条数据到数据表

  • ignore: 插入语句 INSERT IGNORE INTO ...
  • values: 支持格式 ...Struct 或 ...*Struct

func (*Session) LIMIT

func (own *Session) LIMIT(limit int) *Session

LIMIT 写入 LIMIT 子句

func (*Session) OFFSET

func (own *Session) OFFSET(offset int) *Session

OFFSET 写入 OFFSET 子句

func (*Session) ORDER

func (own *Session) ORDER(columns ...string) *Session

ORDER 写入 ORDER BY 子句

func (*Session) Query

func (own *Session) Query(sql string, args ...any) (*sql.Rows, error)

Query 执行 SQL 查询语句,返回多条结果

func (*Session) QueryContext

func (own *Session) QueryContext(ctx context.Context, sql string, args ...any) (*sql.Rows, error)

QueryContext 执行带上下文的 SQL 查询语句,返回多条结果

func (*Session) QueryRow

func (own *Session) QueryRow(sql string, args ...any) *sql.Row

QueryRow 执行 SQL 查询语句,返回单条结果

func (*Session) QueryRowContext

func (own *Session) QueryRowContext(ctx context.Context, sql string, args ...any) *sql.Row

QueryRowContext 执行带上下文的 SQL 查询语句,返回单条结果

func (*Session) Rollback

func (own *Session) Rollback() (err error)

Rollback 回滚事务

func (*Session) SELECT

func (own *Session) SELECT(values any) (res result.Result, err error)

SELECT 查询一条或多条数据

  • values: 必须是指针,支持 *Struct 或 *[]Struct 或 *[]*Struct

func (*Session) SetDebug

func (own *Session) SetDebug(color bool)

SetDebug 开启调试模式

func (*Session) SetSchema

func (own *Session) SetSchema(sch *schema.Schema)

SetSchema 设置数据表

func (*Session) SetTimeout

func (own *Session) SetTimeout(timeout time.Duration)

SetTimeout 设置执行 SQL 语句的超时时间

func (*Session) UPDATE

func (own *Session) UPDATE(values any) (result.Result, error)

UPDATE 更新数据

  • values: 支持格式 map[[string]]any、struct、*struct
  • 注意1:若传入结构体且没有手动设置 WHERE 子句,则内部使用主键自动构建 WHERE 子句,此时,数据模型必须定义主键字段,且模型实例中必须设置主键字段的值
  • 注意2:map[[string]]any 更新不支持钩子函数,也不自动构建 WHERE 子句,请自行调用 WHERE 方法

func (*Session) UseModel

func (own *Session) UseModel(model any) *Session

UseModel 使用指定数据模型

  • model: 必须使用指针格式 *Struct

func (*Session) WHERE

func (own *Session) WHERE(condition where.Condition) *Session

WHERE 写入 WHERE 子句

type Stmt

type Stmt struct {
	*sql.Stmt // 继承 *sql.Stmt
	// contains filtered or unexported fields
}

Stmt 包装 sql.Stmt,以适配本框架的 DEBUG 模式

func Prepare

func Prepare(db *sql.DB, stmts *sync.Map, key, query string) (*Stmt, error)

Prepare 预编译 SQL 语句

func (*Stmt) Exec

func (own *Stmt) Exec(id uint64, debug, debugColor bool, args ...any) (sql.Result, error)

func (*Stmt) ExecContext

func (own *Stmt) ExecContext(ctx context.Context, id uint64, debug, debugColor bool, args ...any) (sql.Result, error)

func (*Stmt) Query

func (own *Stmt) Query(id uint64, debug, debugColor bool, args ...any) (*sql.Rows, error)

func (*Stmt) QueryContext

func (own *Stmt) QueryContext(ctx context.Context, id uint64, debug, debugColor bool, args ...any) (*sql.Rows, error)

func (*Stmt) QueryRow

func (own *Stmt) QueryRow(id uint64, debug, debugColor bool, args ...any) *sql.Row

func (*Stmt) QueryRowContext

func (own *Stmt) QueryRowContext(ctx context.Context, id uint64, debug, debugColor bool, args ...any) *sql.Row

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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