refactor: use dialect-neutral Jet type aliases
Replace mysql/postgres intersection interfaces with mysql type aliases per go-jet/jet#555 and generalize Apply/Expr signatures. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -3,7 +3,6 @@ package dbx
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
"github.com/go-jet/jet/v2/mysql"
|
||||
"golang.org/x/exp/constraints"
|
||||
)
|
||||
|
||||
@@ -19,8 +18,8 @@ type ApplyInterface[T any] interface {
|
||||
func ApplyPtr[T constraints.Float | constraints.Integer | string | bool](
|
||||
existing *T,
|
||||
newVal *T,
|
||||
updatedColumns *mysql.ColumnList,
|
||||
targetColumn mysql.Column,
|
||||
updatedColumns *ColumnList,
|
||||
targetColumn Column,
|
||||
) *T {
|
||||
if newVal == nil {
|
||||
return existing
|
||||
@@ -46,8 +45,8 @@ func ApplyComplexPtr[
|
||||
](
|
||||
existing *Existing,
|
||||
newVal *New,
|
||||
updatedColumns *mysql.ColumnList,
|
||||
targetColumn mysql.Column,
|
||||
updatedColumns *ColumnList,
|
||||
targetColumn Column,
|
||||
) *Existing {
|
||||
if newVal == nil {
|
||||
return existing
|
||||
@@ -73,8 +72,8 @@ func ApplyComplexPtr[
|
||||
func ApplyInterfacePtr[T ApplyInterface[T]](
|
||||
existing *T,
|
||||
newVal *T,
|
||||
updatedColumns *mysql.ColumnList,
|
||||
targetColumn mysql.Column,
|
||||
updatedColumns *ColumnList,
|
||||
targetColumn Column,
|
||||
) *T {
|
||||
if newVal == nil {
|
||||
return existing
|
||||
@@ -98,8 +97,8 @@ func ApplyInterfacePtr[T ApplyInterface[T]](
|
||||
func ApplyVal[T constraints.Float | constraints.Integer | string | bool](
|
||||
existing T,
|
||||
newVal *T,
|
||||
updatedColumns *mysql.ColumnList,
|
||||
targetColumn mysql.Column,
|
||||
updatedColumns *ColumnList,
|
||||
targetColumn Column,
|
||||
) T {
|
||||
if newVal == nil {
|
||||
return existing
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
//
|
||||
// # Jet column utilities
|
||||
//
|
||||
// Dialect-neutral union types ([Column], [ColumnList]) and helpers for column
|
||||
// Dialect-neutral type aliases ([Column], [ColumnList]) and helpers for column
|
||||
// lists ([NormalCols]) and expression building ([ExprValues]).
|
||||
//
|
||||
// Partial-update helpers ([ApplyPtr], [ApplyVal]) track changed fields for
|
||||
|
||||
+5
-5
@@ -4,14 +4,14 @@ import "github.com/go-jet/jet/v2/mysql"
|
||||
|
||||
// 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)
|
||||
func NormalCols(cols ...Column) ColumnList {
|
||||
res := make(ColumnList, 0)
|
||||
|
||||
for _, col := range cols {
|
||||
switch col.(type) {
|
||||
case mysql.ColumnTimestamp, // = postgres.ColumnTimestamp
|
||||
mysql.ColumnTime, // = postgres.ColumnTime
|
||||
mysql.ColumnDate: // = postgres.ColumnDate
|
||||
case mysql.ColumnTimestamp,
|
||||
mysql.ColumnTime,
|
||||
mysql.ColumnDate:
|
||||
default:
|
||||
res = append(res, col)
|
||||
}
|
||||
|
||||
+6
-6
@@ -6,19 +6,19 @@ import (
|
||||
"github.com/go-jet/jet/v2/mysql"
|
||||
)
|
||||
|
||||
// ExprValues converts a list of values to a list of mysql.Expression values using
|
||||
// ExprValues converts a list of values to a list of 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 {
|
||||
expressions := make([]mysql.Expression, len(values))
|
||||
func ExprValues[T any](values []T, f func(T) Expression) []Expression {
|
||||
expressions := make([]Expression, len(values))
|
||||
for i, v := range values {
|
||||
expressions[i] = f(v)
|
||||
}
|
||||
return expressions
|
||||
}
|
||||
|
||||
// ExprStringers converts a list of fmt.Stringers to a list of mysql.Expression values.
|
||||
func ExprStringers(values []fmt.Stringer) []mysql.Expression {
|
||||
expressions := make([]mysql.Expression, len(values))
|
||||
// ExprStringers converts a list of fmt.Stringers to a list of Expression values.
|
||||
func ExprStringers(values []fmt.Stringer) []Expression {
|
||||
expressions := make([]Expression, len(values))
|
||||
for i, v := range values {
|
||||
expressions[i] = mysql.String(v.String())
|
||||
}
|
||||
|
||||
+10
-19
@@ -1,24 +1,15 @@
|
||||
package dbx
|
||||
|
||||
import (
|
||||
"github.com/go-jet/jet/v2/mysql"
|
||||
"github.com/go-jet/jet/v2/postgres"
|
||||
)
|
||||
import "github.com/go-jet/jet/v2/mysql"
|
||||
|
||||
// Column is a union type for mysql.Column and postgres.Column
|
||||
type Column interface {
|
||||
mysql.Column
|
||||
postgres.Column
|
||||
}
|
||||
// Column is a dialect-neutral alias for Jet column expressions.
|
||||
type Column = mysql.Column
|
||||
|
||||
// ColumnList is a union type for mysql.ColumnList and postgres.ColumnList
|
||||
type ColumnList interface {
|
||||
mysql.ColumnList
|
||||
postgres.ColumnList
|
||||
}
|
||||
// ColumnList is a dialect-neutral alias for Jet column lists.
|
||||
type ColumnList = mysql.ColumnList
|
||||
|
||||
// BoolExpression is a union type for mysql.BoolExpression and postgres.BoolExpression
|
||||
type BoolExpression interface {
|
||||
mysql.BoolExpression
|
||||
postgres.BoolExpression
|
||||
}
|
||||
// Expression is a dialect-neutral alias for Jet SQL expressions.
|
||||
type Expression = mysql.Expression
|
||||
|
||||
// BoolExpression is a dialect-neutral alias for Jet boolean expressions.
|
||||
type BoolExpression = mysql.BoolExpression
|
||||
|
||||
Reference in New Issue
Block a user