Skip to content

Commit

Permalink
u
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed May 30, 2023
1 parent 922fb04 commit b9fb700
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
10 changes: 3 additions & 7 deletions persistence/sql/batch/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type (
}
)

func buildInsertQueryArgs[T any](ctx context.Context, dialect string, mapper *reflectx.Mapper, quoter quoter, models []*T) (insertQueryArgs, error) {
func buildInsertQueryArgs[T any](ctx context.Context, dialect string, mapper *reflectx.Mapper, quoter quoter, models []*T) insertQueryArgs {
var (
v T
model = pop.NewModel(v, ctx)
Expand Down Expand Up @@ -106,7 +106,7 @@ func buildInsertQueryArgs[T any](ctx context.Context, dialect string, mapper *re
ColumnsDecl: strings.Join(quotedColumns, ", "),
Columns: columns,
Placeholders: strings.Join(placeholders, ",\n"),
}, nil
}
}

func buildInsertQueryValues[T any](dialect string, mapper *reflectx.Mapper, columns []string, models []*T, nowFunc func() time.Time) (values []any, err error) {
Expand Down Expand Up @@ -184,11 +184,7 @@ func Create[T any](ctx context.Context, p *TracerConnection, models []*T) (err e
return errors.Errorf("store is not a quoter: %T", conn.Store)
}

queryArgs, err := buildInsertQueryArgs(ctx, conn.Dialect.Name(), conn.TX.Mapper, quoter, models)
if err != nil {
return err
}

queryArgs := buildInsertQueryArgs(ctx, conn.Dialect.Name(), conn.TX.Mapper, quoter, models)
values, err := buildInsertQueryValues(conn.Dialect.Name(), conn.TX.Mapper, queryArgs.Columns, models, func() time.Time { return time.Now().UTC().Truncate(time.Microsecond) })
if err != nil {
return err
Expand Down
15 changes: 5 additions & 10 deletions persistence/sql/batch/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ func Test_buildInsertQueryArgs(t *testing.T) {
t.Run("case=testModel", func(t *testing.T) {
models := makeModels[testModel]()
mapper := reflectx.NewMapper("db")
args, err := buildInsertQueryArgs(ctx, "other", mapper, testQuoter{}, models)
require.NoError(t, err)
args := buildInsertQueryArgs(ctx, "other", mapper, testQuoter{}, models)
snapshotx.SnapshotT(t, args)

query := fmt.Sprintf("INSERT INTO %s (%s) VALUES\n%s", args.TableName, args.ColumnsDecl, args.Placeholders)
Expand All @@ -75,24 +74,21 @@ func Test_buildInsertQueryArgs(t *testing.T) {
t.Run("case=Identities", func(t *testing.T) {
models := makeModels[identity.Identity]()
mapper := reflectx.NewMapper("db")
args, err := buildInsertQueryArgs(ctx, "other", mapper, testQuoter{}, models)
require.NoError(t, err)
args := buildInsertQueryArgs(ctx, "other", mapper, testQuoter{}, models)
snapshotx.SnapshotT(t, args)
})

t.Run("case=RecoveryAddress", func(t *testing.T) {
models := makeModels[identity.RecoveryAddress]()
mapper := reflectx.NewMapper("db")
args, err := buildInsertQueryArgs(ctx, "other", mapper, testQuoter{}, models)
require.NoError(t, err)
args := buildInsertQueryArgs(ctx, "other", mapper, testQuoter{}, models)
snapshotx.SnapshotT(t, args)
})

t.Run("case=RecoveryAddress", func(t *testing.T) {
models := makeModels[identity.RecoveryAddress]()
mapper := reflectx.NewMapper("db")
args, err := buildInsertQueryArgs(ctx, "other", mapper, testQuoter{}, models)
require.NoError(t, err)
args := buildInsertQueryArgs(ctx, "other", mapper, testQuoter{}, models)
snapshotx.SnapshotT(t, args)
})

Expand All @@ -104,8 +100,7 @@ func Test_buildInsertQueryArgs(t *testing.T) {
}
}
mapper := reflectx.NewMapper("db")
args, err := buildInsertQueryArgs(ctx, "cockroach", mapper, testQuoter{}, models)
require.NoError(t, err)
args := buildInsertQueryArgs(ctx, "cockroach", mapper, testQuoter{}, models)
snapshotx.SnapshotT(t, args)
})
}
Expand Down

0 comments on commit b9fb700

Please sign in to comment.