database

package
v1.2.19 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2024 License: MIT Imports: 10 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidCondition  = errors.New("invalid condition")
	ErrInvalidSingleData = errors.New("invalid single data")
)

Functions

func Column added in v1.2.18

func Column(tableName, columnName string) string

func ColumnAlias added in v1.2.18

func ColumnAlias(tableName, columnName, alias string) string

func EmptySlice added in v1.2.18

func EmptySlice(s any) bool

func FromMap added in v1.2.18

func FromMap(m any) bool

func FromSlice added in v1.2.18

func FromSlice(s any) bool

func JoinSubQueryAlias added in v1.2.18

func JoinSubQueryAlias(query *gorm.DB, alias string) string

func NewExecuteSqlError added in v1.2.10

func NewExecuteSqlError(sql string, errorOccurred error) error

func NewTemplateFormatError added in v1.2.10

func NewTemplateFormatError(template []rune) error

func Receivable added in v1.2.18

func Receivable(v any) bool

Types

type BaseDatabaseImplement

type BaseDatabaseImplement struct {
	Db      *gorm.DB
	Logger  logger.Logger
	Timeout time.Duration
	// contains filtered or unexported fields
}

func (*BaseDatabaseImplement) Count

func (s *BaseDatabaseImplement) Count(table, query string, args ...any) (count int64, err error)

func (*BaseDatabaseImplement) CountWithCtx

func (s *BaseDatabaseImplement) CountWithCtx(ctx context.Context, table, query string, args ...any) (count int64, err error)

func (*BaseDatabaseImplement) DeleteAll

func (s *BaseDatabaseImplement) DeleteAll(query string, args ...any) error

func (*BaseDatabaseImplement) DeleteAllWithCtx

func (s *BaseDatabaseImplement) DeleteAllWithCtx(ctx context.Context, query string, args ...any) error

func (*BaseDatabaseImplement) DeleteOne

func (s *BaseDatabaseImplement) DeleteOne(query string, args ...any) error

func (*BaseDatabaseImplement) DeleteOneWithCtx

func (s *BaseDatabaseImplement) DeleteOneWithCtx(ctx context.Context, query string, args ...any) error

func (*BaseDatabaseImplement) ExecRaw

func (s *BaseDatabaseImplement) ExecRaw(sql string, args ...any) error

func (*BaseDatabaseImplement) ExecRawWithCtx

func (s *BaseDatabaseImplement) ExecRawWithCtx(ctx context.Context, sql string, args ...any) error

func (*BaseDatabaseImplement) ExtMethods added in v1.2.7

func (s *BaseDatabaseImplement) ExtMethods() ExtMethods

func (*BaseDatabaseImplement) GetAll

func (s *BaseDatabaseImplement) GetAll(receiver any, query string, args ...any) error

func (*BaseDatabaseImplement) GetAllWithCtx

func (s *BaseDatabaseImplement) GetAllWithCtx(ctx context.Context, receiver any, query string, args ...any) error

func (*BaseDatabaseImplement) GetOne

func (s *BaseDatabaseImplement) GetOne(receiver any, query string, args ...any) error

func (*BaseDatabaseImplement) GetOneWithCtx

func (s *BaseDatabaseImplement) GetOneWithCtx(ctx context.Context, receiver any, query string, args ...any) error

func (*BaseDatabaseImplement) GetPage

func (s *BaseDatabaseImplement) GetPage(receiver any, offset, limit int, query string, args ...any) error

func (*BaseDatabaseImplement) Has

func (s *BaseDatabaseImplement) Has(table, query string, args ...any) (exist bool, err error)

func (*BaseDatabaseImplement) HasWithCtx

func (s *BaseDatabaseImplement) HasWithCtx(ctx context.Context, table, query string, args ...any) (exist bool, err error)

func (*BaseDatabaseImplement) Init

func (s *BaseDatabaseImplement) Init(_ Options) error

func (*BaseDatabaseImplement) InsertAll

func (s *BaseDatabaseImplement) InsertAll(data any) error

func (*BaseDatabaseImplement) InsertAllWithCtx

func (s *BaseDatabaseImplement) InsertAllWithCtx(ctx context.Context, data any) error

func (*BaseDatabaseImplement) InsertOne

func (s *BaseDatabaseImplement) InsertOne(data any) error

func (*BaseDatabaseImplement) InsertOneWithCtx

func (s *BaseDatabaseImplement) InsertOneWithCtx(ctx context.Context, data any) error

func (*BaseDatabaseImplement) Migrate

func (s *BaseDatabaseImplement) Migrate(models ...any) error

func (*BaseDatabaseImplement) ParseDatabaseOptions

func (s *BaseDatabaseImplement) ParseDatabaseOptions(db *sql.DB, opts Options)

func (*BaseDatabaseImplement) PickAll

func (s *BaseDatabaseImplement) PickAll(receiver any, length int, query string, args ...any) error

func (*BaseDatabaseImplement) PickAllWithCtx

func (s *BaseDatabaseImplement) PickAllWithCtx(ctx context.Context, receiver any, length int, query string, args ...any) error

func (*BaseDatabaseImplement) PickOne

func (s *BaseDatabaseImplement) PickOne(receiver any, query string, args ...any) error

func (*BaseDatabaseImplement) PickOneWithCtx

func (s *BaseDatabaseImplement) PickOneWithCtx(ctx context.Context, receiver any, query string, args ...any) error

func (*BaseDatabaseImplement) QueryRaw

func (s *BaseDatabaseImplement) QueryRaw(receiver any, sql string, args ...any) error

func (*BaseDatabaseImplement) QueryRawWithCtx

func (s *BaseDatabaseImplement) QueryRawWithCtx(ctx context.Context, receiver any, sql string, args ...any) error

func (*BaseDatabaseImplement) SetDriverName added in v1.2.7

func (s *BaseDatabaseImplement) SetDriverName(name string)

func (*BaseDatabaseImplement) SetLogger added in v1.2.10

func (s *BaseDatabaseImplement) SetLogger(logger logger.Logger)

func (*BaseDatabaseImplement) SetRandCommand

func (s *BaseDatabaseImplement) SetRandCommand(command string)

func (*BaseDatabaseImplement) UpdateAll

func (s *BaseDatabaseImplement) UpdateAll(data any, query string, args ...any) error

func (*BaseDatabaseImplement) UpdateAllWithCtx

func (s *BaseDatabaseImplement) UpdateAllWithCtx(ctx context.Context, data any, query string, args ...any) error

func (*BaseDatabaseImplement) UpdateOne

func (s *BaseDatabaseImplement) UpdateOne(data any, query string, args ...any) error

func (*BaseDatabaseImplement) UpdateOneWithCtx

func (s *BaseDatabaseImplement) UpdateOneWithCtx(ctx context.Context, data any, query string, args ...any) error

type BaseDatabaseImplementV2 added in v1.2.18

type BaseDatabaseImplementV2 struct {
	Db *gorm.DB
}

func (*BaseDatabaseImplementV2) CreateDataOnDuplicateKeyUpdate added in v1.2.18

func (v2 *BaseDatabaseImplementV2) CreateDataOnDuplicateKeyUpdate(ctx context.Context, data any, indexKeys, updateFields []string) error

func (*BaseDatabaseImplementV2) CreateSingleDataIfNotExist added in v1.2.18

func (v2 *BaseDatabaseImplementV2) CreateSingleDataIfNotExist(ctx context.Context, data any) (created bool, err error)

func (*BaseDatabaseImplementV2) ExecuteRawSql added in v1.2.18

func (v2 *BaseDatabaseImplementV2) ExecuteRawSql(ctx context.Context, sql string) error

func (*BaseDatabaseImplementV2) ExecuteRawSqlQuery added in v1.2.18

func (v2 *BaseDatabaseImplementV2) ExecuteRawSqlQuery(ctx context.Context, receiver any, sql string) error

func (*BaseDatabaseImplementV2) ExecuteRawSqlTemplate added in v1.2.18

func (v2 *BaseDatabaseImplementV2) ExecuteRawSqlTemplate(ctx context.Context, sql string, template RawSqlTemplate) error

func (*BaseDatabaseImplementV2) ExecuteRawSqlTemplateQuery added in v1.2.18

func (v2 *BaseDatabaseImplementV2) ExecuteRawSqlTemplateQuery(ctx context.Context, receiver any, sql string, template RawSqlTemplate) error

func (*BaseDatabaseImplementV2) GetDataByCustomCondition added in v1.2.18

func (v2 *BaseDatabaseImplementV2) GetDataByCustomCondition(ctx context.Context, receiver, condition any, needFields ...string) error

func (*BaseDatabaseImplementV2) GetDataBySingleCondition added in v1.2.18

func (v2 *BaseDatabaseImplementV2) GetDataBySingleCondition(ctx context.Context, receiver any, column string, condition any, needFields ...string) error

func (*BaseDatabaseImplementV2) GetGormCore added in v1.2.18

func (v2 *BaseDatabaseImplementV2) GetGormCore(ctx context.Context) *gorm.DB

func (*BaseDatabaseImplementV2) ListDataWithPage added in v1.2.18

func (v2 *BaseDatabaseImplementV2) ListDataWithPage(ctx context.Context, receiver any, filter any, order string, desc bool, offset, limit int, needFields ...string) error

func (*BaseDatabaseImplementV2) UpdateDataByCustomCondition added in v1.2.18

func (v2 *BaseDatabaseImplementV2) UpdateDataByCustomCondition(ctx context.Context, updates, condition any) error

func (*BaseDatabaseImplementV2) UpdateDataBySingleCondition added in v1.2.18

func (v2 *BaseDatabaseImplementV2) UpdateDataBySingleCondition(ctx context.Context, updates any, column string, condition any) error

type BaseExtMethodGroup added in v1.2.7

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

func (*BaseExtMethodGroup) DriverName added in v1.2.7

func (b *BaseExtMethodGroup) DriverName() string

func (*BaseExtMethodGroup) Exec added in v1.2.7

func (b *BaseExtMethodGroup) Exec(cmd func(db *gorm.DB) *gorm.DB) error

func (*BaseExtMethodGroup) ExecCtx added in v1.2.7

func (b *BaseExtMethodGroup) ExecCtx(ctx context.Context, cmd func(db *gorm.DB) *gorm.DB) error

func (*BaseExtMethodGroup) GetGorm added in v1.2.7

func (b *BaseExtMethodGroup) GetGorm() *gorm.DB

func (*BaseExtMethodGroup) Transaction added in v1.2.10

func (b *BaseExtMethodGroup) Transaction(cmd func(tx *gorm.DB) error) error

func (*BaseExtMethodGroup) TransactionCtx added in v1.2.10

func (b *BaseExtMethodGroup) TransactionCtx(ctx context.Context, cmd func(tx *gorm.DB) error) error

type DBLogger added in v1.2.19

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

func NewDBLogger added in v1.2.19

func NewDBLogger(log logger.Logger) *DBLogger

func (*DBLogger) Error added in v1.2.19

func (dl *DBLogger) Error(ctx context.Context, s string, i ...interface{})

func (*DBLogger) Info added in v1.2.19

func (dl *DBLogger) Info(ctx context.Context, s string, i ...interface{})

func (*DBLogger) LogMode added in v1.2.19

func (dl *DBLogger) LogMode(level glog.LogLevel) glog.Interface

func (*DBLogger) Trace added in v1.2.19

func (dl *DBLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error)

func (*DBLogger) Warn added in v1.2.19

func (dl *DBLogger) Warn(ctx context.Context, s string, i ...interface{})

type Database deprecated

type Database interface {
	Init(options Options) error
	Migrate(models ...any) error
	Has(table string, query string, args ...any) (exist bool, err error)
	Count(table string, query string, args ...any) (count int64, err error)
	GetOne(receiver any, query string, args ...any) error
	GetAll(receiver any, query string, args ...any) error
	GetPage(receiver any, offset, limit int, query string, args ...any) error
	PickOne(receiver any, query string, args ...any) error
	PickAll(receiver any, length int, query string, args ...any) error
	InsertOne(data any) error
	InsertAll(data any) error
	UpdateOne(data any, query string, args ...any) error
	UpdateAll(data any, query string, args ...any) error
	DeleteOne(query string, args ...any) error
	DeleteAll(query string, args ...any) error
	ExecRaw(sql string, args ...any) error
	QueryRaw(receiver any, sql string, args ...any) error
	HasWithCtx(ctx context.Context, table string, query string, args ...any) (exist bool, err error)
	CountWithCtx(ctx context.Context, table string, query string, args ...any) (count int64, err error)
	GetOneWithCtx(ctx context.Context, receiver any, query string, args ...any) error
	GetAllWithCtx(ctx context.Context, receiver any, query string, args ...any) error
	PickOneWithCtx(ctx context.Context, receiver any, query string, args ...any) error
	PickAllWithCtx(ctx context.Context, receiver any, length int, query string, args ...any) error
	InsertOneWithCtx(ctx context.Context, data any) error
	InsertAllWithCtx(ctx context.Context, data any) error
	UpdateOneWithCtx(ctx context.Context, data any, query string, args ...any) error
	UpdateAllWithCtx(ctx context.Context, data any, query string, args ...any) error
	DeleteOneWithCtx(ctx context.Context, query string, args ...any) error
	DeleteAllWithCtx(ctx context.Context, query string, args ...any) error
	ExecRawWithCtx(ctx context.Context, sql string, args ...any) error
	QueryRawWithCtx(ctx context.Context, receiver any, sql string, args ...any) error
	ExtMethods() ExtMethods
	SetLogger(logger logger.Logger)
}

Database is the interface that wraps the basic database operations. The implementation of this interface should be thread-safe.

Deprecated: Use DatabaseV2 instead.

type DatabaseV2 added in v1.2.18

type DatabaseV2 interface {
	// GetGormCore retrieves the core *gorm.DB instance with the provided context.
	//
	// Parameters:
	//	ctx (context.Context): The context for the database operation.
	//
	// Returns:
	//	*gorm.DB: The GORM database instance with the provided context.
	GetGormCore(ctx context.Context) *gorm.DB

	// GetDataBySingleCondition retrieves data from the database based on a single column condition.
	// The result is stored in the receiver.
	//
	// Parameters:
	//	ctx (context.Context): The context for the database operation.
	//	receiver (any): The destination where the query result will be stored.
	//	column (string): The column name to apply the condition.
	//	condition (any): The condition value for the specified column.
	//	needFields (...string): Optional fields to select in the query.
	//
	// Returns:
	//	error: An error if the operation fails, otherwise nil.
	GetDataBySingleCondition(ctx context.Context, receiver any, column string, condition any, needFields ...string) error

	// GetDataByCustomCondition retrieves data from the database based on a custom condition.
	// The result is stored in the receiver.
	//
	// Parameters:
	//	ctx (context.Context): The context for the database operation.
	//	receiver (any): The destination where the query result will be stored.
	//	condition (any): The custom condition for the query.
	//	needFields (...string): Optional fields to select in the query.
	//
	// Returns:
	//	error: An error if the operation fails, otherwise nil.
	GetDataByCustomCondition(ctx context.Context, receiver, condition any, needFields ...string) error

	// ListDataWithPage retrieves a paginated list of data from the database based on the provided filter and ordering.
	// The result is stored in the receiver.
	//
	// Parameters:
	//	ctx (context.Context): The context for the database operation.
	//	receiver (any): The destination where the query result will be stored.
	//	filter (any): The filter condition for the query.
	//	order (string): The column name to order by.
	//	desc (bool): Whether to order in descending order.
	//	offset (int): The offset for pagination.
	//	limit (int): The limit for pagination.
	//	needFields (...string): Optional fields to select in the query.
	//
	// Returns:
	//	error: An error if the operation fails, otherwise nil.
	ListDataWithPage(ctx context.Context, receiver any, filter any, order string, desc bool, offset, limit int, needFields ...string) error

	// CreateSingleDataIfNotExist creates a single data record in the database if it does not already exist.
	//
	// Parameters:
	//	ctx (context.Context): The context for the database operation.
	//	data (any): The data record to be created.
	//
	// Returns:
	//	created (bool): True if the data record was created, false if it already exists.
	//	err (error): An error if the operation fails, otherwise nil.
	CreateSingleDataIfNotExist(ctx context.Context, data any) (created bool, err error)

	// CreateDataOnDuplicateKeyUpdate creates a data record in the database or updates it if a duplicate key is found.
	//
	// Parameters:
	//	ctx (context.Context): The context for the database operation.
	//	data (any): The data record to be created or updated.
	//	indexKeys ([]string): The index keys to check for duplication.
	//	updateFields ([]string): The fields to update if a duplicate key is found.
	//
	// Returns:
	//	error: An error if the operation fails, otherwise nil.
	CreateDataOnDuplicateKeyUpdate(ctx context.Context, data any, indexKeys, updateFields []string) error

	// UpdateDataBySingleCondition updates data in the database based on a single column condition.
	//
	// Parameters:
	//	ctx (context.Context): The context for the database operation.
	//	updates (any): The data to be updated.
	//	column (string): The column name to apply the condition.
	//	condition (any): The condition value for the specified column.
	//
	// Returns:
	//	error: An error if the operation fails, otherwise nil.
	UpdateDataBySingleCondition(ctx context.Context, updates any, column string, condition any) error

	// UpdateDataByCustomCondition updates data in the database based on a custom condition.
	//
	// Parameters:
	//	ctx (context.Context): The context for the database operation.
	//	updates (any): The data to be updated.
	//	condition (any): The custom condition for the query.
	//
	// Returns:
	//	error: An error if the operation fails, otherwise nil.
	UpdateDataByCustomCondition(ctx context.Context, updates, condition any) error

	// ExecuteRawSqlTemplateQuery executes a raw SQL template query with the provided context.
	//
	// Parameters:
	//	ctx (context.Context): The context for the database operation.
	//	receiver (any): The destination where the query result will be stored.
	//  sql (string): The raw SQL template to execute.
	//	template (RawSqlTemplate): The raw SQL template value to execute.
	//
	// Returns:
	//	error: An error if the operation fails, otherwise nil.
	ExecuteRawSqlTemplateQuery(ctx context.Context, receiver any, sql string, template RawSqlTemplate) error

	// ExecuteRawSqlTemplate executes a raw SQL template with the provided context.
	//
	// Parameters:
	//	ctx (context.Context): The context for the database operation.
	//  sql (string): The raw SQL template to execute.
	//	template (RawSqlTemplate): The raw SQL template value to execute.
	//
	// Returns:
	//	error: An error if the operation fails, otherwise nil.
	ExecuteRawSqlTemplate(ctx context.Context, sql string, template RawSqlTemplate) error

	// ExecuteRawSqlQuery executes a raw SQL query with the provided context.
	//
	// Parameters:
	//	ctx (context.Context): The context for the database operation.
	//	receiver (any): The destination where the query result will be stored.
	//  sql (string): The raw SQL to execute.
	//
	// Returns:
	//	error: An error if the operation fails, otherwise nil.
	ExecuteRawSqlQuery(ctx context.Context, receiver any, sql string) error

	// ExecuteRawSql executes a raw SQL with the provided context.
	//
	// Parameters:
	//	ctx (context.Context): The context for the database operation.
	//  sql (string): The raw SQL to execute.
	//
	// Returns:
	//	error: An error if the operation fails, otherwise nil.
	ExecuteRawSql(ctx context.Context, sql string) error
}

DatabaseV2 is the interface that wraps the basic database operations. The implementation of this interface should be thread-safe.

type ExecuteSqlError added in v1.2.10

type ExecuteSqlError struct {
	Sql           string `json:"sql"`
	ErrorOccurred error  `json:"error"`
}

func (*ExecuteSqlError) Error added in v1.2.10

func (err *ExecuteSqlError) Error() string

type ExtMethods added in v1.2.7

type ExtMethods interface {
	DriverName() string
	GetGorm() *gorm.DB
	Exec(cmd func(db *gorm.DB) *gorm.DB) error
	ExecCtx(ctx context.Context, cmd func(db *gorm.DB) *gorm.DB) error
	Transaction(cmd func(tx *gorm.DB) error) error
	TransactionCtx(ctx context.Context, cmd func(tx *gorm.DB) error) error
}

type Extended added in v1.2.7

type Extended interface {
	Database
	ExtensionName() string
}

type Extension added in v1.2.7

type Extension[extended Extended] interface {
	InitializeExtension(base Database) extended
}

type Options

type Options struct {
	DataSource string
	MaxIdle    int
	MaxOpen    int
	MaxLife    time.Duration
	Timeout    time.Duration
	Logger     logger.Logger
}

type RawSqlTemplate added in v1.2.18

type RawSqlTemplate interface {
	ParseTemplate(tmpl string) (result string)
}

type TemplateFormatError added in v1.2.10

type TemplateFormatError struct {
	Template string `json:"template"`
}

func (*TemplateFormatError) Error added in v1.2.10

func (err *TemplateFormatError) Error() string

Directories

Path Synopsis
extension
orm

Jump to

Keyboard shortcuts

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