package dbxm import ( "context" "fmt" "slices" "strings" "gitea.auvem.com/go-toolkit/dbx" "gitea.auvem.com/go-toolkit/dbx/internal/dbxshared" "github.com/fatih/color" "github.com/go-jet/jet/v2/mysql" ) func init() { 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{} func (_mysqlLogger) InitLogger() { mysql.SetQueryLogger(func(ctx context.Context, queryInfo mysql.QueryInfo) { _, args := queryInfo.Statement.Sql() dbx.ModuleDB().Logger().Debug( "Executed SQL query", "args", args, "duration", queryInfo.Duration, "rows", queryInfo.RowsProcessed, "err", queryInfo.Err, ) lines := strings.Split(queryInfo.Statement.DebugSql(), "\n") for i, line := range lines { fmt.Printf("%s\t%s\n", color.CyanString(fmt.Sprintf("%03d", i)), line) } }) }