Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 81e724f72b | |||
| fa4efb38f7 |
+1
-1
@@ -16,7 +16,7 @@
|
||||
- `Query`, `MustQuery`, and `UpdateOne` helpers (+ Context variants).
|
||||
- `WithTxValue` for transactional functions that return a value.
|
||||
- `QueryCount`, `BuildQueryCountFn`, and `CountResult` for pagination counts.
|
||||
- `StringKSUIDExpr`, `BinaryKSUIDExpr`, `ExprPtrs`, and flexible `Parse*Any` KSUID parsers.
|
||||
- `BinaryKSUID.BinExpr`, `ExprStringKSUIDs`, and flexible `Parse*Any` KSUID parsers.
|
||||
- `MarshalUint64` and `UnmarshalUint64` for JS-safe GraphQL uint64 scalars.
|
||||
- Context-aware variants for all query and mutation helpers.
|
||||
- `Delete`, `DeleteAffected`, `WithTx`, `ContainsCol`, and `CurrentDialect()`.
|
||||
|
||||
@@ -45,7 +45,7 @@ For Postgres, use `dbx.DialectPostgres` and blank-import `dbxp` instead of `dbxm
|
||||
| Query | `Fetch`, `MustFetch`, `FetchOne`, `MustFetchOne`, `Query`, `MustQuery` (+ `*Context` variants) |
|
||||
| Mutations | `Insert`, `InsertReturning`, `Update`, `UpdateAffected`, `UpdateOne`, `UpdateReturning`, `Delete`, `DeleteAffected` (+ `*Context` variants) |
|
||||
| Transactions | `WithTx`, `WithTxValue` |
|
||||
| Columns | `NormalCols`, `ContainsCol`, `ExprValues`, `ExprPtrs`, `ExprStringers`, `StringKSUIDExpr`, `BinaryKSUIDExpr`, `QueryCount`, `BuildQueryCountFn` |
|
||||
| Columns | `NormalCols`, `ContainsCol`, `ExprValues`, `ExprStringers`, `ExprStringKSUIDs`, `QueryCount`, `BuildQueryCountFn` |
|
||||
| Partial update | `ApplyPtr`, `ApplyComplexPtr`, `ApplyInterfacePtr`, `ApplyVal` |
|
||||
| Pointers | `Ptr`, `Val`, `NowPtr`, `TrimPtr`, `TrimPtrToNil`, `IsZero` |
|
||||
| Types | `StringKSUID`, `BinaryKSUID`, `JSONB` |
|
||||
|
||||
@@ -16,8 +16,7 @@
|
||||
// # Jet column utilities
|
||||
//
|
||||
// Dialect-neutral type aliases ([Column], [ColumnList]) and helpers for column
|
||||
// lists ([NormalCols], [ContainsCol]) and expression building ([ExprValues],
|
||||
// [ExprPtrs], [StringKSUIDExpr], [BinaryKSUIDExpr]).
|
||||
// lists ([NormalCols], [ContainsCol]) and expression building ([ExprValues]).
|
||||
// [QueryCount] and [BuildQueryCountFn] support pagination total counts.
|
||||
//
|
||||
// Partial-update helpers ([ApplyPtr], [ApplyVal]) track changed fields for
|
||||
@@ -27,8 +26,7 @@
|
||||
//
|
||||
// [StringKSUID] and [BinaryKSUID] wrap segmentio/ksuid with storage-specific
|
||||
// SQL encoding. Both implement [ApplyInterface] via Equal and IsZero for use
|
||||
// with [ApplyInterfacePtr]. Expr returns a Jet expression via [StringKSUIDExpr]
|
||||
// or [BinaryKSUIDExpr]. Both share identical GraphQL string scalar transit.
|
||||
// with [ApplyInterfacePtr]. Both share identical GraphQL string scalar transit.
|
||||
// Choose StringKSUID for text columns (VARCHAR, TEXT); choose BinaryKSUID for
|
||||
// binary columns (BINARY(20), BYTEA). [ParseStringKSUIDAny] and
|
||||
// [ParseBinaryKSUIDAny] accept flexible input for API boundaries.
|
||||
|
||||
+6
-26
@@ -16,19 +16,6 @@ func ExprValues[T any](values []T, f func(T) Expression) []Expression {
|
||||
return expressions
|
||||
}
|
||||
|
||||
// ExprPtrs converts a list of pointers to Expression values, skipping nil
|
||||
// entries. Function f receives the dereferenced value.
|
||||
func ExprPtrs[T any](values []*T, f func(T) Expression) []Expression {
|
||||
expressions := make([]Expression, 0, len(values))
|
||||
for _, v := range values {
|
||||
if v == nil {
|
||||
continue
|
||||
}
|
||||
expressions = append(expressions, f(*v))
|
||||
}
|
||||
return expressions
|
||||
}
|
||||
|
||||
// ExprStringers converts a list of fmt.Stringers to a list of Expression values.
|
||||
func ExprStringers(values []fmt.Stringer) []Expression {
|
||||
expressions := make([]Expression, len(values))
|
||||
@@ -38,18 +25,11 @@ func ExprStringers(values []fmt.Stringer) []Expression {
|
||||
return expressions
|
||||
}
|
||||
|
||||
// StringKSUIDExpr returns a Jet expression for a string-encoded KSUID column.
|
||||
func StringKSUIDExpr(id StringKSUID) Expression {
|
||||
if id.IsNil() {
|
||||
return mysql.StringExp(mysql.NULL)
|
||||
// ExprStringKSUIDs converts StringKSUID pointers to Jet string expressions.
|
||||
func ExprStringKSUIDs(ids []*StringKSUID) []Expression {
|
||||
expressions := make([]Expression, len(ids))
|
||||
for i, id := range ids {
|
||||
expressions[i] = mysql.String(id.String())
|
||||
}
|
||||
return mysql.String(id.String())
|
||||
}
|
||||
|
||||
// BinaryKSUIDExpr returns a Jet expression for a binary-encoded KSUID column.
|
||||
func BinaryKSUIDExpr(id BinaryKSUID) Expression {
|
||||
if id.IsNil() {
|
||||
return mysql.StringExp(mysql.NULL)
|
||||
}
|
||||
return mysql.String(string(id.Bytes()))
|
||||
return expressions
|
||||
}
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
package dbx
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestStringKSUIDExpr(t *testing.T) {
|
||||
id := NewStringKSUID()
|
||||
expr := StringKSUIDExpr(id)
|
||||
require.NotNil(t, expr)
|
||||
assert.Equal(t, expr, id.Expr())
|
||||
|
||||
nilExpr := StringKSUIDExpr(NilStringKSUID)
|
||||
require.NotNil(t, nilExpr)
|
||||
assert.Equal(t, nilExpr, NilStringKSUID.Expr())
|
||||
}
|
||||
|
||||
func TestBinaryKSUIDExpr(t *testing.T) {
|
||||
id := NewBinaryKSUID()
|
||||
expr := BinaryKSUIDExpr(id)
|
||||
require.NotNil(t, expr)
|
||||
assert.Equal(t, expr, id.Expr())
|
||||
|
||||
nilExpr := BinaryKSUIDExpr(NilBinaryKSUID)
|
||||
require.NotNil(t, nilExpr)
|
||||
assert.Equal(t, nilExpr, NilBinaryKSUID.Expr())
|
||||
}
|
||||
|
||||
func TestExprPtrs_StringKSUID(t *testing.T) {
|
||||
a := NewStringKSUID()
|
||||
b := NewStringKSUID()
|
||||
exprs := ExprPtrs([]*StringKSUID{&a, nil, &b}, StringKSUIDExpr)
|
||||
require.Len(t, exprs, 2)
|
||||
}
|
||||
|
||||
func TestExprPtrs_BinaryKSUID(t *testing.T) {
|
||||
a := NewBinaryKSUID()
|
||||
exprs := ExprPtrs([]*BinaryKSUID{&a, nil}, BinaryKSUIDExpr)
|
||||
require.Len(t, exprs, 1)
|
||||
assert.NotNil(t, exprs[0])
|
||||
}
|
||||
|
||||
func TestExprValues_StringKSUID(t *testing.T) {
|
||||
a := NewStringKSUID()
|
||||
b := NewStringKSUID()
|
||||
exprs := ExprValues([]*StringKSUID{&a, &b}, func(id *StringKSUID) Expression {
|
||||
return id.Expr()
|
||||
})
|
||||
require.Len(t, exprs, 2)
|
||||
}
|
||||
|
||||
func TestExprValues_BinaryKSUID(t *testing.T) {
|
||||
a := NewBinaryKSUID()
|
||||
exprs := ExprValues([]BinaryKSUID{a}, BinaryKSUIDExpr)
|
||||
require.Len(t, exprs, 1)
|
||||
assert.NotNil(t, exprs[0])
|
||||
}
|
||||
+9
-5
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/go-jet/jet/v2/mysql"
|
||||
"github.com/segmentio/ksuid"
|
||||
)
|
||||
|
||||
@@ -43,16 +44,19 @@ func ParseBinaryKSUID(b []byte) (BinaryKSUID, error) {
|
||||
return BinaryKSUID{KSUID: id}, nil
|
||||
}
|
||||
|
||||
// BinExpr returns a Jet string expression for binary-encoded IN clauses.
|
||||
func (b BinaryKSUID) BinExpr() mysql.StringExpression {
|
||||
if b.IsNil() {
|
||||
return mysql.StringExp(mysql.NULL)
|
||||
}
|
||||
return mysql.String(string(b.Bytes()))
|
||||
}
|
||||
|
||||
// AsStringKSUID returns a StringKSUID view of the same identifier.
|
||||
func (b BinaryKSUID) AsStringKSUID() StringKSUID {
|
||||
return StringKSUID{KSUID: b.KSUID}
|
||||
}
|
||||
|
||||
// Expr returns a Jet expression for this BinaryKSUID. See [BinaryKSUIDExpr].
|
||||
func (b BinaryKSUID) Expr() Expression {
|
||||
return BinaryKSUIDExpr(b)
|
||||
}
|
||||
|
||||
// Scan implements sql.Scanner for binary-backed KSUID columns.
|
||||
func (b *BinaryKSUID) Scan(src any) error {
|
||||
switch v := src.(type) {
|
||||
|
||||
@@ -52,3 +52,19 @@ func TestParseStringKSUIDAny_InvalidType(t *testing.T) {
|
||||
_, err := ParseStringKSUIDAny(123)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestBinaryKSUID_BinExpr(t *testing.T) {
|
||||
id := NewBinaryKSUID()
|
||||
expr := id.BinExpr()
|
||||
require.NotNil(t, expr)
|
||||
|
||||
nilExpr := NilBinaryKSUID.BinExpr()
|
||||
require.NotNil(t, nilExpr)
|
||||
}
|
||||
|
||||
func TestExprStringKSUIDs(t *testing.T) {
|
||||
a := NewStringKSUID()
|
||||
b := NewStringKSUID()
|
||||
exprs := ExprStringKSUIDs([]*StringKSUID{&a, &b})
|
||||
require.Len(t, exprs, 2)
|
||||
}
|
||||
|
||||
@@ -48,11 +48,6 @@ func (s StringKSUID) AsBinaryKSUID() BinaryKSUID {
|
||||
return BinaryKSUID{KSUID: s.KSUID}
|
||||
}
|
||||
|
||||
// Expr returns a Jet expression for this StringKSUID. See [StringKSUIDExpr].
|
||||
func (s StringKSUID) Expr() Expression {
|
||||
return StringKSUIDExpr(s)
|
||||
}
|
||||
|
||||
// Scan implements sql.Scanner for string-backed KSUID columns.
|
||||
func (s *StringKSUID) Scan(src any) error {
|
||||
switch v := src.(type) {
|
||||
|
||||
Reference in New Issue
Block a user