feat: update templates

This commit is contained in:
Rogee
2024-12-21 00:17:48 +08:00
parent 94553081c9
commit 58450dd63d
9 changed files with 44 additions and 245 deletions

View File

@@ -0,0 +1,40 @@
package db
import (
"context"
"database/sql"
"fmt"
"{{.ModuleName}}/pkg/consts"
"github.com/go-jet/jet/v2/qrm"
)
func FromContext(ctx context.Context, db *sql.DB) qrm.DB {
if tx, ok := ctx.Value(consts.CtxKeyTx).(*sql.Tx); ok {
return tx
}
return db
}
func TruncateAllTables(ctx context.Context, db *sql.DB, tableName ...string) error {
for _, name := range tableName {
sql := fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY", name)
if _, err := db.ExecContext(ctx, sql); err != nil {
return err
}
}
return nil
}
func WrapLike(v string) string {
return "%" + v + "%"
}
func WrapLikeLeft(v string) string {
return "%" + v
}
func WrapLikeRight(v string) string {
return "%" + v
}