mirror of https://github.com/rocboss/paopao-ce
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
2.4 KiB
115 lines
2.4 KiB
// Copyright 2023 ROC. All rights reserved.
|
|
// Use of this source code is governed by a MIT style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package sakila
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"strings"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
"github.com/rocboss/paopao-ce/internal/conf"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var (
|
|
_db *sqlx.DB
|
|
)
|
|
|
|
type sqlxServant struct {
|
|
db *sqlx.DB
|
|
}
|
|
|
|
func (s *sqlxServant) with(handle func(tx *sqlx.Tx) error) error {
|
|
tx, err := s.db.Beginx()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
if err = handle(tx); err != nil {
|
|
return err
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
func (s *sqlxServant) withTx(ctx context.Context, opts *sql.TxOptions, handle func(*sqlx.Tx) error) error {
|
|
tx, err := s.db.BeginTxx(ctx, opts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
if err = handle(tx); err != nil {
|
|
return err
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
func (s *sqlxServant) in(query string, args ...any) (string, []any, error) {
|
|
q, params, err := sqlx.In(query, args...)
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
return s.db.Rebind(q), params, nil
|
|
}
|
|
|
|
func (s *sqlxServant) inExec(execer sqlx.Execer, query string, args ...any) (sql.Result, error) {
|
|
q, params, err := sqlx.In(query, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return execer.Exec(s.db.Rebind(q), params...)
|
|
}
|
|
|
|
func (s *sqlxServant) inSelect(queryer sqlx.Queryer, dest any, query string, args ...any) error {
|
|
q, params, err := sqlx.In(query, args...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return sqlx.Select(queryer, dest, s.db.Rebind(q), params...)
|
|
}
|
|
|
|
func (s *sqlxServant) inGet(queryer sqlx.Queryer, dest any, query string, args ...any) error {
|
|
q, params, err := sqlx.In(query, args...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return sqlx.Get(queryer, dest, s.db.Rebind(q), params...)
|
|
}
|
|
|
|
func newSqlxServant(db *sqlx.DB) *sqlxServant {
|
|
return &sqlxServant{
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
func r(query string) string {
|
|
return _db.Rebind(t(query))
|
|
}
|
|
|
|
func c(query string) *sqlx.Stmt {
|
|
stmt, err := _db.Preparex(_db.Rebind(t(query)))
|
|
if err != nil {
|
|
logrus.Fatalf("prepare query(%s) error: %s", query, err)
|
|
}
|
|
return stmt
|
|
}
|
|
|
|
func n(query string) *sqlx.NamedStmt {
|
|
stmt, err := _db.PrepareNamed(t(query))
|
|
if err != nil {
|
|
logrus.Fatalf("prepare named query(%s) error: %s", query, err)
|
|
}
|
|
return stmt
|
|
}
|
|
|
|
// t repace table prefix for query
|
|
func t(query string) string {
|
|
return strings.Replace(query, "@", conf.DatabaseSetting.TablePrefix, -1)
|
|
}
|
|
|
|
func initSqlxDB() {
|
|
_db = conf.MustSqlxDB()
|
|
}
|