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:
Elijah Duffy
2026-01-28 11:57:50 -08:00
parent 20b5dee18a
commit 14686a83cb
4 changed files with 69 additions and 148 deletions

View File

@@ -3,7 +3,6 @@ package dbxm
import ( import (
"context" "context"
"fmt" "fmt"
"slices"
"strings" "strings"
"gitea.auvem.com/go-toolkit/dbx" "gitea.auvem.com/go-toolkit/dbx"
@@ -16,61 +15,6 @@ func init() {
dbxshared.RegisterLogger(dbx.DialectMySQL, &_mysqlLogger{}) dbxshared.RegisterLogger(dbx.DialectMySQL, &_mysqlLogger{})
} }
// SoftDelete sets the deleted_at column to the current time
func SoftDelete(sqlo dbx.Executable, tbl mysql.Table, conds mysql.BoolExpression) (int64, error) {
stmt := tbl.UPDATE().WHERE(conds)
query := stmt.DebugSql()
lines := strings.Split(query, "\n")
for i, line := range lines {
fmt.Println(i, line)
}
lines = slices.Insert(lines, 2, "SET deleted_at = NOW()")
query = strings.Join(lines, "\n")
res, err := sqlo.Exec(query)
if err != nil {
return 0, err
}
return res.RowsAffected()
}
// NormalCols processes a list of columns and strips out any that implement any of
// mysql.ColumnTimestamp, mysql.ColumnTime, or mysql.ColumnDate
func NormalCols(cols ...mysql.Column) mysql.ColumnList {
res := make(mysql.ColumnList, 0)
for _, col := range cols {
_, ok := col.(mysql.ColumnTimestamp)
if !ok {
_, ok = col.(mysql.ColumnTime)
}
if !ok {
_, ok = col.(mysql.ColumnDate)
}
if !ok {
res = append(res, col)
}
}
return res
}
// ContainsCol checks if a column list contains a specific column.
func ContainsCol(cols mysql.ColumnList, col mysql.Column) bool {
return slices.Contains(cols, col)
}
// 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 {
return dbxshared.DestName(destTypeStruct, path...)
}
type _mysqlLogger struct{} type _mysqlLogger struct{}
func (_mysqlLogger) InitLogger() { func (_mysqlLogger) InitLogger() {

View File

@@ -3,7 +3,6 @@ package dbxp
import ( import (
"context" "context"
"fmt" "fmt"
"slices"
"strings" "strings"
"gitea.auvem.com/go-toolkit/dbx" "gitea.auvem.com/go-toolkit/dbx"
@@ -16,61 +15,6 @@ func init() {
dbxshared.RegisterLogger(dbx.DialectPostgres, &_postgresLogger{}) dbxshared.RegisterLogger(dbx.DialectPostgres, &_postgresLogger{})
} }
// SoftDelete sets the deleted_at column to the current time
func SoftDelete(sqlo dbx.Executable, tbl postgres.Table, conds postgres.BoolExpression) (int64, error) {
stmt := tbl.UPDATE().WHERE(conds)
query := stmt.DebugSql()
lines := strings.Split(query, "\n")
for i, line := range lines {
fmt.Println(i, line)
}
lines = slices.Insert(lines, 2, "SET deleted_at = NOW()")
query = strings.Join(lines, "\n")
res, err := sqlo.Exec(query)
if err != nil {
return 0, err
}
return res.RowsAffected()
}
// NormalCols processes a list of columns and strips out any that implement any of
// postgres.ColumnTimestamp, postgres.ColumnTime, or postgres.ColumnDate
func NormalCols(cols ...postgres.Column) postgres.ColumnList {
res := make(postgres.ColumnList, 0)
for _, col := range cols {
_, ok := col.(postgres.ColumnTimestamp)
if !ok {
_, ok = col.(postgres.ColumnTime)
}
if !ok {
_, ok = col.(postgres.ColumnDate)
}
if !ok {
res = append(res, col)
}
}
return res
}
// ContainsCol checks if a column list contains a specific column.
func ContainsCol(cols postgres.ColumnList, col postgres.Column) bool {
return slices.Contains(cols, col)
}
// 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 {
return dbxshared.DestName(destTypeStruct, path...)
}
type _postgresLogger struct{} type _postgresLogger struct{}
func (_postgresLogger) InitLogger() { func (_postgresLogger) InitLogger() {

View File

@@ -1,36 +0,0 @@
package dbxshared
import (
"reflect"
"strings"
)
// 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 {
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
}

View File

@@ -6,10 +6,30 @@ import (
"strings" "strings"
"time" "time"
"gitea.auvem.com/go-toolkit/dbx/internal/dbxshared"
"github.com/go-jet/jet/v2/mysql" "github.com/go-jet/jet/v2/mysql"
"github.com/go-jet/jet/v2/postgres"
"golang.org/x/exp/constraints" "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 // 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 // statement. It replaces all spaces with % and adds % to the beginning and
// end of the string. // end of the string.
@@ -24,6 +44,55 @@ func StringToFilter(str string) string {
return str 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 // 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). // 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 { func ExprValues[T any](values []T, f func(T) mysql.Expression) []mysql.Expression {