Documentation
¶
Overview ¶
Package database Provides a unified API interface for relational and non-relational databases.
For relational databases, MySQL is integrated by default, and PostgreSQL, SQLite, Oracle, etc. will be integrated later. For non-relational databases, Redis and MongoDB are integrated by default, and will be integrated according to actual use later.
A simple integration example
type (
MysqlDB *sqlx.DB
MysqlOption func(MysqlDB)
)
func InitMysql(dsn string) MysqlDB {
if dsn == "" {
dsn = _default_dsn
}
ctx, cancle := context.WithTimeout(context.Background(), _conn_timeout)
defer cancle()
db, err := sqlx.ConnectContext(ctx, _driver_mysql, dsn)
if err != nil {
panic("init mysql database instance fail.")
}
return db
}
func SetupMysql(mdb MysqlDB, opts ...MysqlOption) {
for _, fn := range opts {
fn(mdb)
}
}
The most regrettable aspect is that it's impossible to bind all public APIs to each individual database type. This is a limitation of Go, and it's also why we need to pass the database instance. Since some public APIs don't always require generic types, we have to simplify the design here, but we've still achieved a highly templated code structure.
Index ¶
- func DeleteWithName(ctx context.Context, db *sqlx.DB, sqlStr string, obj any) error
- func DeleteWithPlace(ctx context.Context, db *sqlx.DB, sqlStr string, args ...any) error
- func GetRedisDB() *redis.Client
- func InitRedisDB(ro *redis.Options) *redis.Client
- func InsertWithName(ctx context.Context, db *sqlx.DB, sqlStr string, obj any) error
- func InsertWithPlace(ctx context.Context, db *sqlx.DB, sqlStr string, args ...any) error
- func QListWithName[R any](ctx context.Context, db *sqlx.DB, sqlStr string, obj any) ([]R, error)
- func QListWithPlace[R any](ctx context.Context, db *sqlx.DB, sqlStr string, args ...any) ([]R, error)
- func QueryWithName[R any](ctx context.Context, db *sqlx.DB, sqlStr string, obj any) (R, error)
- func QueryWithPlace[R any](ctx context.Context, db *sqlx.DB, sqlStr string, args ...any) (R, error)
- func SetupMysql(db MysqlDB, opts ...MysqlOption)
- func SetupSqlite(db SqliteDB, opts ...SqliteOption)
- func ToStmt(db *sqlx.DB, sqlStr string) (*sqlx.Stmt, error)
- func ToStmtContext(ctx context.Context, db *sqlx.DB, sqlStr string) (*sqlx.Stmt, error)
- func ToTx(db *sqlx.DB) (*sqlx.Tx, error)
- func ToTxContext(ctx context.Context, db *sqlx.DB, opts *sql.TxOptions) (*sqlx.Tx, error)
- func UpdateWithName(ctx context.Context, db *sqlx.DB, sqlStr string, obj any) error
- func UpdateWithPlace(ctx context.Context, db *sqlx.DB, sqlStr string, args ...any) error
- type MongoDB
- type MysqlDB
- type MysqlMeta
- type MysqlOption
- type NullBool
- type NullFloat64
- type NullInt16
- type NullInt32
- type NullInt64
- type NullString
- type NullTime
- type Page
- type SqliteDB
- type SqliteMeta
- type SqliteOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeleteWithName ¶
DeleteWithName return error occurred during the execution of the delete SQL with named parameters. The database instance needs to be explicitly specified.
func DeleteWithPlace ¶
DeleteWithPlace return error occurred during the execution of the delete SQL with placeholdler parameters. The database instance needs to be explicitly specified.
func GetRedisDB ¶
func InsertWithName ¶
InsertWithName return error occurred during the execution of the insert SQL with named parameters. The primary key that returns a successful insert may be modified later. The database instance needs to be explicitly specified.
func InsertWithPlace ¶
InsertWithPlace return error occurred during the execution of the insert SQL with placeholder parameters. The primary key that returns a successful insert may be modified later. The database instance needs to be explicitly specified.
func QListWithName ¶
QListWithName return the list of specify generic type and error occurred during the execution of the select SQL with named parameters. R should have the largest set of all fields that need to be retrieved and not be a pointer type. The database instance needs to be explicitly specified.
func QListWithPlace ¶
func QListWithPlace[R any](ctx context.Context, db *sqlx.DB, sqlStr string, args ...any) ([]R, error)
QListWithPlace return the list of specify generic type and error occurred during the execution of the select SQL with placeholdler parameters. R should have the largest set of all fields that need to be retrieved and not be a pointer type. The database instance needs to be explicitly specified.
func QueryWithName ¶
QueryWithName return the specify generic type and error occurred during the execution of the select SQL with named parameters. R should have the largest set of all fields that need to be retrieved and not be a pointer type. The database instance needs to be explicitly specified.
func QueryWithPlace ¶
QueryWithPlace return the specify generic type and error occurred during the execution of the select SQL with placeholdler parameters. R should have the largest set of all fields that need to be retrieved and not be a pointer type. The database instance needs to be explicitly specified.
func SetupMysql ¶
func SetupMysql(db MysqlDB, opts ...MysqlOption)
func SetupSqlite ¶
func SetupSqlite(db SqliteDB, opts ...SqliteOption)
func ToStmtContext ¶
func ToTxContext ¶
func UpdateWithName ¶
UpdateWithName return error occurred during the execution of the udpate SQL with named parameters. The database instance needs to be explicitly specified.
Types ¶
type MongoDB ¶
type MongoDB struct {
// contains filtered or unexported fields
}
MongoDB is just a layer of mongo encapsulation.
type MysqlMeta ¶
type MysqlMeta struct {
Id uint64 `db:"id"` // primary key
ExternId uuid.UUID `db:"extern_id"` // extern id as foreign key
CreatedAt time.Time `db:"created_at"` // created time
UpdatedAt time.Time `db:"updated_at"` // updated time
Deleted bool `db:"deleted"` // deleted or not
}
MysqlMeta is mysql attribute-independent metadata structure. It is generally used as an embedded structure.
type NullBool ¶
func NewNullBool ¶
type NullFloat64 ¶
type NullFloat64 struct{ sql.NullFloat64 }
func (NullFloat64) Float64Value ¶
func (nf NullFloat64) Float64Value() *float64
func (NullFloat64) NewNullFloat64 ¶
func (nf NullFloat64) NewNullFloat64(f *float64) NullFloat64
type NullString ¶
type NullString struct{ sql.NullString }
func NewNullString ¶
func NewNullString(s *string) NullString
func (NullString) StringValue ¶
func (ns NullString) StringValue() *string
type NullTime ¶
func NewNullTime ¶
type Page ¶
type Page[T any] struct { CurrentPage int `json:"current_page"` // current page to calculate offset PageSize int `json:"page_size"` // page size Total int `json:"total"` // total records obtained Items []T `json:"items"` // list of records }
Page is used for paginated queries.
func QPageWithName ¶
func QPageWithName[R any](ctx context.Context, db *sqlx.DB, sqlStr string, obj any) (Page[R], error)
QPageWithName return the page of specify generic type and error occurred during the execution of the select SQL with named parameters. R should have the largest set of all fields that need to be retrieved and not be a pointer type. The database instance needs to be explicitly specified.
func QPageWithPlace ¶
func QPageWithPlace[R any](ctx context.Context, db *sqlx.DB, sqlStr string, args ...any) (Page[R], error)
QPageWithPlace return the page of specify generic type and error occurred during the execution of the select SQL with placeholdler parameters. R should have the largest set of all fields that need to be retrieved and not be a pointer type. The database instance needs to be explicitly specified.
type SqliteMeta ¶
type SqliteMeta struct {
Id uint64 `db:"id"` // primary key
ExternId uuid.UUID `db:"extern_id"` // extern id as foreign key
CreatedAt time.Time `db:"created_at"` // created time
UpdatedAt time.Time `db:"updated_at"` // updated time
Deleted bool `db:"deleted"` // deleted or not
}
SqliteMeta is sqlite attribute-independent metadata structure. It is generally used as an embedded structure.
type SqliteOption ¶
type SqliteOption func(SqliteDB)