split mysql specific code into separate package
This commit is contained in:
56
dbx.go
56
dbx.go
@@ -2,40 +2,14 @@ package dbx
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"gitea.auvem.com/go-toolkit/app"
|
||||
"github.com/go-jet/jet/v2/qrm"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
// ModuleDBName is the name of the database module.
|
||||
const ModuleDBName = "database"
|
||||
|
||||
var (
|
||||
// ErrNoRows is returned when a query returns no rows.
|
||||
ErrNoRows = qrm.ErrNoRows
|
||||
|
||||
// sqlDB stores the current SQL database handle.
|
||||
sqlDB *sql.DB
|
||||
|
||||
// config stores the database connection configuration.
|
||||
config DBConfig
|
||||
|
||||
// dbModule is the singleton module instance for the database connection.
|
||||
dbModule *app.Module
|
||||
)
|
||||
|
||||
// SQLO returns the current SQL database handle.
|
||||
func SQLO() *sql.DB {
|
||||
dbModule.RequireLoaded("dbx.SQLO requires database module") // ensure the module is loaded before accessing the database
|
||||
if sqlDB == nil {
|
||||
panic("SQL database not initialized")
|
||||
}
|
||||
return sqlDB
|
||||
}
|
||||
|
||||
// Queryable interface is an SQL driver object that can execute SQL statements
|
||||
// for Jet.
|
||||
type Queryable interface {
|
||||
@@ -57,36 +31,6 @@ type ExecutableTx interface {
|
||||
Begin() (*sql.Tx, error)
|
||||
}
|
||||
|
||||
// DestName returns the name of the type passed as `destTypeStruct` as a string,
|
||||
// normalized for compatibility with the Jet QRM.
|
||||
func DestName(destTypeStruct any, path ...string) string {
|
||||
v := reflect.ValueOf(destTypeStruct)
|
||||
for v.Kind() == reflect.Ptr {
|
||||
v = v.Elem()
|
||||
}
|
||||
|
||||
destIdent := v.Type().String()
|
||||
destIdent = destIdent[strings.LastIndex(destIdent, ".")+1:]
|
||||
|
||||
for i, p := range path {
|
||||
if v.Kind() != reflect.Struct {
|
||||
dbModule.Logger().Error("DestName: path parent is not a struct", "path", destIdent+"."+strings.Join(path[:i+1], "."))
|
||||
return ""
|
||||
}
|
||||
|
||||
v = v.FieldByName(p)
|
||||
|
||||
if !v.IsValid() {
|
||||
dbModule.Logger().Error("DestName: field does not exist", "path", destIdent+"."+strings.Join(path[:i+1], "."))
|
||||
return ""
|
||||
}
|
||||
|
||||
destIdent += "." + p
|
||||
}
|
||||
|
||||
return destIdent
|
||||
}
|
||||
|
||||
// StringToFilter processes a string to be used as a filter in an SQL LIKE
|
||||
// statement. It replaces all spaces with % and adds % to the beginning and
|
||||
// end of the string.
|
||||
|
||||
Reference in New Issue
Block a user