gormstarter

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2025 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RawGormDB

func RawGormDB(dbType ...DBType) *gorm.DB

RawGormDB 获取 gorm.DB原始能力,如果多数据库类型初始化后,不指定DBType默认返回最先加载的数据库类型

func RawMysqlGormDB added in v0.1.11

func RawMysqlGormDB() *gorm.DB

RawMysqlGormDB 获取 mysql 数据库类型的 gorm.DB

func RawPostgresGormDB added in v0.1.11

func RawPostgresGormDB() *gorm.DB

RawPostgresGormDB 获取 postgres 数据库类型的 gorm.DB

Types

type BaseMapper

type BaseMapper[M IBaseModel] struct {
	Tx *gorm.DB
	// contains filtered or unexported fields
}

func (BaseMapper[T]) CountByCond added in v0.1.10

func (b BaseMapper[T]) CountByCond(condition *T) (int64, error)

func (BaseMapper[T]) CountByCondMap added in v0.1.10

func (b BaseMapper[T]) CountByCondMap(condition map[string]any) (int64, error)

func (BaseMapper[T]) CountByGorm added in v0.1.10

func (b BaseMapper[T]) CountByGorm(raw func(*gorm.DB)) (int64, error)

func (BaseMapper[T]) CountByWhere added in v0.1.10

func (b BaseMapper[T]) CountByWhere(rawWhereSql string, args ...interface{}) (int64, error)

func (BaseMapper[T]) DeleteByCond added in v0.1.8

func (b BaseMapper[T]) DeleteByCond(condition *T) (int64, error)

func (BaseMapper[T]) DeleteById added in v0.1.3

func (b BaseMapper[T]) DeleteById(id ...any) (int64, error)

func (BaseMapper[T]) DeleteByWhere added in v0.1.3

func (b BaseMapper[T]) DeleteByWhere(rawWhereSql string, args ...interface{}) (int64, error)

func (BaseMapper[T]) Gorm added in v0.1.10

func (b BaseMapper[T]) Gorm() *gorm.DB

func (BaseMapper[T]) Save

func (b BaseMapper[T]) Save(entity *T, excludeColumns ...string) (int64, error)

func (BaseMapper[T]) SaveBatch

func (b BaseMapper[T]) SaveBatch(entities *[]*T, excludeColumns ...string) (int64, error)

func (BaseMapper[T]) SaveOrUpdateByPrimaryKey added in v0.1.3

func (b BaseMapper[T]) SaveOrUpdateByPrimaryKey(entity *T, excludeColumns ...string) (int64, error)

func (BaseMapper[T]) SaveWithoutZeroField added in v0.1.10

func (b BaseMapper[T]) SaveWithoutZeroField(entity *T) (int64, error)

func (BaseMapper[T]) SelectByCond added in v0.1.8

func (b BaseMapper[T]) SelectByCond(condition *T, orderBy string, result *[]*T, specifyColumns ...string) (int64, error)

func (BaseMapper[T]) SelectByCondMap added in v0.1.8

func (b BaseMapper[T]) SelectByCondMap(condition map[string]any, orderBy string, result *[]*T, specifyColumns ...string) (int64, error)

func (BaseMapper[T]) SelectByGorm added in v0.1.10

func (b BaseMapper[T]) SelectByGorm(result *[]*T, rawDb func(*gorm.DB)) (int64, error)

func (BaseMapper[T]) SelectById added in v0.1.3

func (b BaseMapper[T]) SelectById(id any, result *T) (int64, error)

func (BaseMapper[T]) SelectByIds added in v0.1.7

func (b BaseMapper[T]) SelectByIds(id []interface{}, result *[]*T) (int64, error)

func (BaseMapper[T]) SelectByWhere added in v0.1.3

func (b BaseMapper[T]) SelectByWhere(rawWhereSql, orderBy string, result *[]*T, args ...interface{}) (int64, error)

func (BaseMapper[T]) SelectOneByCond added in v0.1.8

func (b BaseMapper[T]) SelectOneByCond(condition *T, result *T, specifyColumns ...string) (int64, error)

func (BaseMapper[T]) SelectOneByCondMap added in v0.1.8

func (b BaseMapper[T]) SelectOneByCondMap(condition map[string]any, result *T, specifyColumns ...string) (int64, error)

func (BaseMapper[T]) SelectOneByGorm added in v0.1.10

func (b BaseMapper[T]) SelectOneByGorm(result *T, rawDb func(*gorm.DB)) (int64, error)

func (BaseMapper[T]) SelectOneByWhere added in v0.1.3

func (b BaseMapper[T]) SelectOneByWhere(rawWhereSql string, result *T, args ...interface{}) (int64, error)

func (BaseMapper[T]) SelectPageByCond added in v0.1.8

func (b BaseMapper[T]) SelectPageByCond(condition *T, orderBy string, pageNumber, pageSize int, result *[]*T, specifyColumns ...string) (total int64, err error)

func (BaseMapper[T]) SelectPageByCondMap added in v0.1.8

func (b BaseMapper[T]) SelectPageByCondMap(condition map[string]any, orderBy string, pageNumber, pageSize int, result *[]*T, specifyColumns ...string) (total int64, err error)

func (BaseMapper[T]) SelectPageByWhere added in v0.1.3

func (b BaseMapper[T]) SelectPageByWhere(rawWhereSql, orderBy string, pageNumber, pageSize int, result *[]*T, args ...interface{}) (total int64, err error)

func (BaseMapper[T]) UpdateByCond added in v0.1.8

func (b BaseMapper[T]) UpdateByCond(updated, condition *T, updateColumns ...string) (int64, error)

func (BaseMapper[T]) UpdateByCondMap added in v0.1.8

func (b BaseMapper[T]) UpdateByCondMap(updated, condition map[string]any) (int64, error)

func (BaseMapper[T]) UpdateByCondWithZeroField added in v0.1.8

func (b BaseMapper[T]) UpdateByCondWithZeroField(updated, condition *T, allowZeroFiledColumns []string) (int64, error)

func (BaseMapper[T]) UpdateById added in v0.1.3

func (b BaseMapper[T]) UpdateById(updated *T, updateColumns ...string) (int64, error)

func (BaseMapper[T]) UpdateByIdUseMap added in v0.1.8

func (b BaseMapper[T]) UpdateByIdUseMap(updated map[string]any, id any) (int64, error)

func (BaseMapper[T]) UpdateByIdWithoutZeroField added in v0.1.8

func (b BaseMapper[T]) UpdateByIdWithoutZeroField(updated *T, allowZeroFiledColumns ...string) (int64, error)

func (BaseMapper[T]) UpdateByWhere added in v0.1.3

func (b BaseMapper[T]) UpdateByWhere(updated *T, rawWhereSql string, args ...interface{}) (int64, error)

type BaseModel

type BaseModel[IdType any] struct {
	ID IdType `gorm:"<-:false,primaryKey" json:"id"`
}

type DBType

type DBType string
const (
	DBTypeMySQL    DBType = "mysql"
	DBTypePostgres DBType = "postgres"
)

type GormConfig added in v0.1.6

type GormConfig struct {
	Username string
	Password string
	Host     string
	Port     uint
	Database string

	Charset string // default charset : utf8mb4
	DBType  DBType // 数据库类型 不指定时默认为 mysql

	TimeUTC       bool // true: create/update UTC time; false LOCAL time
	DryRun        bool // create sql not exec
	UseDefaultLog bool

	// MYSQL 配置
	MySQLUrlParam string // more Param such as `allowNativePasswords=false&checkConnLiveness=false`  https://github.com/go-sql-driver/mysql?tab=readme-ov-file#dsn-data-source-name

	// Postgres 配置
	PostgresTimezone  string
	PostgresEnableSSl bool

	InitFunc func(instance *gorm.DB)
}

type GormStarter

type GormStarter struct {
	// Config 配置
	Config GormConfig
	// 懒加载函数,用于在实际执行时动态获取配置 该权重高于Config的直接配置
	LazyConfig func() GormConfig

	GormSetting *parent.Setting
	// contains filtered or unexported fields
}

func (*GormStarter) Setting

func (g *GormStarter) Setting() *parent.Setting

func (*GormStarter) Start

func (g *GormStarter) Start() (interface{}, error)

func (*GormStarter) Stop

func (g *GormStarter) Stop(maxWaitTime time.Duration) (gracefully, stopped bool, err error)

type IBaseMapper added in v0.1.11

type IBaseMapper[B BaseMapper[T], T IBaseModel] interface {

	// Gorm Mapper对应的原生Gorm操作能力
	Gorm() *gorm.DB

	// SelectById 通过主键查询数据
	SelectById(id any, result *T) (int64, error)

	// SelectByIds 通过主键查询数据
	SelectByIds(id []interface{}, result *[]*T) (int64, error)

	// SelectOneByCond 通过条件查询 查询条件零值字段将被自动忽略
	// specifyColumns 需要指定只查询的数据库字段
	SelectOneByCond(condition *T, result *T, specifyColumns ...string) (int64, error)

	// SelectOneByCondMap 通过指定字段与值查询数据 解决查询条件零值问题
	// specifyColumns 需要指定只查询的数据库字段
	SelectOneByCondMap(condition map[string]any, result *T, specifyColumns ...string) (int64, error)

	// SelectOneByWhere 通过原始Where SQL查询 只需要输入SQL语句和参数 例如 where a = 1 则只需要rawWhereSql = "a = ?" args = 1
	SelectOneByWhere(rawWhereSql string, result *T, args ...interface{}) (int64, error)

	// SelectOneByGorm 通过原始Gorm查询单条数据 构建Gorm查询条件
	SelectOneByGorm(result *T, rawDb func(*gorm.DB)) (int64, error)

	// SelectByCond 通过条件查询 查询条件零值字段将被自动忽略
	// specifyColumns 需要指定只查询的数据库字段
	SelectByCond(condition *T, orderBy string, result *[]*T, specifyColumns ...string) (int64, error)

	// SelectByCondMap 通过指定字段与值查询数据 解决零值条件问题
	// specifyColumns 需要指定只查询的数据库字段
	SelectByCondMap(condition map[string]any, orderBy string, result *[]*T, specifyColumns ...string) (int64, error)

	// SelectByWhere 通过原始Where SQL查询 只需要输入SQL语句和参数 例如 where a = 1 则只需要rawWhereSql = "a = ?" args = 1
	SelectByWhere(rawWhereSql, orderBy string, result *[]*T, args ...interface{}) (int64, error)

	// SelectByGorm 通过原始Gorm查询数据
	SelectByGorm(result *[]*T, rawDb func(*gorm.DB)) (int64, error)

	// CountByCond 通过条件查询数据总数 查询条件零值字段将被自动忽略
	CountByCond(condition *T) (int64, error)

	// CountByCondMap 通过指定字段与值查询数据总数 解决零值条件问题
	CountByCondMap(condition map[string]any) (int64, error)

	// CountByWhere 通过原始SQL查询数据总数
	CountByWhere(rawWhereSql string, args ...interface{}) (int64, error)

	// CountByGorm 通过原始Gorm查询数据总数
	CountByGorm(rawDb func(*gorm.DB)) (int64, error)

	// SelectPageByCond 通过条件分页查询 零值字段将被自动忽略
	// specifyColumns 需要指定只查询的数据库字段 pageNumber 页码 1开始
	SelectPageByCond(condition *T, orderBy string, pageNumber, pageSize int, result *[]*T, specifyColumns ...string) (total int64, err error)

	// SelectPageByCondMap 通过指定字段与值查询数据分页查询 解决零值条件问题
	// specifyColumns 需要指定只查询的数据库字段 pageNumber 页码 1开始
	SelectPageByCondMap(condition map[string]any, orderBy string, pageNumber, pageSize int, result *[]*T, specifyColumns ...string) (total int64, err error)

	// SelectPageByWhere 通过原始SQL分页查询 rawWhereSql 例如 where a = 1 则只需要rawWhereSql = "a = ?" args = 1
	SelectPageByWhere(rawWhereSql, orderBy string, pageNumber, pageSize int, result *[]*T, args ...interface{}) (total int64, err error)

	// Save 保存数据 零值也将参与保存
	//	exclude 手动指定需要排除的字段名称 数据库字段/结构体字段名称
	Save(entity *T, excludeColumns ...string) (int64, error)

	// SaveWithoutZeroField 保存数据 零值将不会参与保存
	SaveWithoutZeroField(entity *T) (int64, error)

	// SaveBatch 批量新增 零值也将参与保存
	//	exclude 手动指定需要排除的字段名称 数据库字段/结构体字段
	SaveBatch(entities *[]*T, excludeColumns ...string) (int64, error)

	// SaveOrUpdateByPrimaryKey 保存/更新数据 零值也将参与保存
	// exclude 手动指定需要排除的字段名称 数据库字段/结构体字段 (如果触发的是update 创建时间可能会被错误的修改,可以通过excludeColumns来指定排除创建时间字段)
	// 仅根据主键冲突默认支持update 更多操作需要参阅 https://gorm.io/zh_CN/docs/create.html#upsert
	SaveOrUpdateByPrimaryKey(entity *T, excludeColumns ...string) (int64, error)

	// UpdateById 通过ID更新含零值字段
	// updateColumns 手动指定需要更新的列
	UpdateById(updated *T, updateColumns ...string) (int64, error)

	// UpdateByIdWithoutZeroField 通过ID更新非零值字段
	// allowZeroFiledColumns 额外指定需要更新零值字段
	UpdateByIdWithoutZeroField(updated *T, allowZeroFiledColumns ...string) (int64, error)

	// UpdateByIdUseMap 通过ID更新所有map中指定的列和值
	UpdateByIdUseMap(updated map[string]any, id any) (int64, error)

	// UpdateByCond 通过条件更新 条件:零值将自动忽略,更新:零值字段将被自动忽略
	// updateColumns 需要指定更新的数据库字段 更新指定字段(支持零值字段)
	UpdateByCond(updated, condition *T, updateColumns ...string) (int64, error)

	// UpdateByCondWithZeroField 通过条件更新,并指定可以更新的零值字段
	UpdateByCondWithZeroField(updated, condition *T, allowZeroFiledColumns []string) (int64, error)

	// UpdateByCondMap 通过Map类型条件更新
	UpdateByCondMap(updated, condition map[string]any) (int64, error)

	// UpdateByWhere 通过原始SQL查询条件,更新非零实体字段 Where SQL查询 只需要输入SQL语句和参数 例如 where a = 1 则只需要rawWhereSql = "a = ?" args = 1
	UpdateByWhere(updated *T, rawWhereSql string, args ...interface{}) (int64, error)

	// DeleteById 通过ID删除相关数据
	DeleteById(id ...any) (int64, error)

	// DeleteByCond 通过条件删除 零值字段将被自动忽略
	DeleteByCond(condition *T) (int64, error)

	// DeleteByWhere 通过原始SQL删除相关数据 Where SQL查询 只需要输入SQL语句和参数 例如 where a = 1 则只需要rawWhereSql = "a = ?" args = 1
	DeleteByWhere(rawWhereSql string, args ...interface{}) (int64, error)
}

type IBaseModel

type IBaseModel interface {
	TableName() string
}

type IBaseModelWithDBType added in v0.1.11

type IBaseModelWithDBType interface {
	TableName() string
	DBType() DBType
}

IBaseModelWithDBType 当gorm管理多个不同数据库类型时,需要实现此接口 以便指定该数据库类型 (初始化加载的第一个数据库类型不需要指定)

type Timestamp added in v0.1.9

type Timestamp json.Timestamp

Timestamp 时间戳处理 接收数据库的时间类型

func (Timestamp) MarshalJSON added in v0.1.9

func (t Timestamp) MarshalJSON() ([]byte, error)

func (*Timestamp) Scan added in v0.1.9

func (t *Timestamp) Scan(value interface{}) error

func (Timestamp) UnmarshalJSON added in v0.1.9

func (t Timestamp) UnmarshalJSON(data []byte) error

func (Timestamp) Value added in v0.1.9

func (t Timestamp) Value() (driver.Value, error)

Jump to

Keyboard shortcuts

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