move utilities out of db-specific packages
DestName & NormalCols now live in root dbx package utilizing union interfaces. SoftDelete helper has been dropped.
This commit is contained in:
69
utility.go
69
utility.go
@@ -6,10 +6,30 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.auvem.com/go-toolkit/dbx/internal/dbxshared"
|
||||
"github.com/go-jet/jet/v2/mysql"
|
||||
"github.com/go-jet/jet/v2/postgres"
|
||||
"golang.org/x/exp/constraints"
|
||||
)
|
||||
|
||||
// Column is a union type for mysql.Column and postgres.Column
|
||||
type Column interface {
|
||||
mysql.Column
|
||||
postgres.Column
|
||||
}
|
||||
|
||||
// ColumnList is a union type for mysql.ColumnList and postgres.ColumnList
|
||||
type ColumnList interface {
|
||||
mysql.ColumnList
|
||||
postgres.ColumnList
|
||||
}
|
||||
|
||||
// BoolExpression is a union type for mysql.BoolExpression and postgres.BoolExpression
|
||||
type BoolExpression interface {
|
||||
mysql.BoolExpression
|
||||
postgres.BoolExpression
|
||||
}
|
||||
|
||||
// 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.
|
||||
@@ -24,6 +44,55 @@ func StringToFilter(str string) string {
|
||||
return str
|
||||
}
|
||||
|
||||
// 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.Pointer {
|
||||
v = v.Elem()
|
||||
}
|
||||
|
||||
destIdent := v.Type().String()
|
||||
destIdent = destIdent[strings.LastIndex(destIdent, ".")+1:]
|
||||
|
||||
for i, p := range path {
|
||||
if v.Kind() != reflect.Struct {
|
||||
dbxshared.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() {
|
||||
dbxshared.DBModule.Logger().Error("DestName: field does not exist", "path", destIdent+"."+strings.Join(path[:i+1], "."))
|
||||
return ""
|
||||
}
|
||||
|
||||
destIdent += "." + p
|
||||
}
|
||||
|
||||
return destIdent
|
||||
}
|
||||
|
||||
// NormalCols processes a list of columns and strips out any that implement any of
|
||||
// ColumnTimestamp, ColumnTime, or ColumnDate.
|
||||
func NormalCols[CL ColumnList](cols ...Column) CL {
|
||||
res := make(CL, 0)
|
||||
|
||||
for _, col := range cols {
|
||||
switch col.(type) {
|
||||
case mysql.ColumnTimestamp, // = postgres.ColumnTimestamp
|
||||
mysql.ColumnTime, // = postgres.ColumnTime
|
||||
mysql.ColumnDate: // = postgres.ColumnDate
|
||||
// skip time/date/timestamp columns
|
||||
default:
|
||||
res = append(res, col)
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
// ExprValues converts a list of values to a list of mysql.Expression values using
|
||||
// function f to transform the values (mysql.String for strings, mysql.Uint64, etc).
|
||||
func ExprValues[T any](values []T, f func(T) mysql.Expression) []mysql.Expression {
|
||||
|
||||
Reference in New Issue
Block a user