This was an epically long change, and a terrible idea, but it compiles. This was essentially a cascade that came about because I can't blend jet and bob in the same transaction. In for a penny, I guess...
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
|
|
//"github.com/go-jet/jet/v2"
|
|
"github.com/go-jet/jet/v2/postgres"
|
|
)
|
|
|
|
type Updater[T postgres.Table, M any] struct {
|
|
Columns postgres.ColumnList
|
|
//Columns []jet.Column
|
|
Model M
|
|
Table T
|
|
|
|
buildWhere func(pk_values ...interface{}) postgres.BoolExpression
|
|
}
|
|
|
|
func (u Updater[T, M]) Execute(ctx context.Context, txn Ex, pk_values ...interface{}) error {
|
|
statement := u.Table.
|
|
UPDATE(u.Columns).
|
|
MODEL(u.Model).
|
|
WHERE(u.buildWhere(pk_values...))
|
|
return ExecuteNoneTx(ctx, txn, statement)
|
|
}
|
|
func (u Updater[T, M]) Has(c postgres.Column) bool {
|
|
for _, col := range u.Columns {
|
|
if col == c {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
func (u *Updater[T, M]) Set(c postgres.Column) {
|
|
u.Columns = append(u.Columns, c)
|
|
}
|
|
func (u *Updater[T, M]) Unset(c postgres.Column) {
|
|
var index = -1
|
|
for i, col := range u.Columns {
|
|
if col == c {
|
|
index = i
|
|
}
|
|
}
|
|
if index > -1 {
|
|
u.Columns[index] = u.Columns[len(u.Columns)-1]
|
|
u.Columns = u.Columns[:len(u.Columns)-1]
|
|
}
|
|
}
|
|
func NewUpdater[T postgres.Table, M any](
|
|
table T,
|
|
pk_columns ...postgres.ColumnInteger,
|
|
) Updater[T, M] {
|
|
return Updater[T, M]{
|
|
Columns: postgres.ColumnList{},
|
|
Table: table,
|
|
buildWhere: func(pk_values ...interface{}) postgres.BoolExpression {
|
|
conditions := make([]postgres.BoolExpression, len(pk_columns))
|
|
for i, col := range pk_columns {
|
|
conditions[i] = col.EQ(postgres.Int64(pk_values[i].(int64)))
|
|
}
|
|
return postgres.AND(conditions...)
|
|
},
|
|
}
|
|
}
|