Skip to content

Commit 25f80f1

Browse files
committed
Revise the functional options for statements
1 parent 17a77cf commit 25f80f1

File tree

5 files changed

+39
-78
lines changed

5 files changed

+39
-78
lines changed

database/delete.go

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ type DeleteStatement interface {
3030
Table() string
3131

3232
Where() string
33-
34-
apply(do *deleteOptions)
3533
}
3634

3735
// NewDeleteStatement returns a new deleteStatement for the given entity.
@@ -72,28 +70,21 @@ func (d *deleteStatement) Where() string {
7270
return d.where
7371
}
7472

75-
func (d *deleteStatement) apply(opts *deleteOptions) {
76-
opts.stmt = d
77-
}
78-
79-
// DeleteOption is the interface for functional options for DeleteStatement.
80-
type DeleteOption interface {
81-
// apply applies the option to the given deleteOptions.
82-
apply(*deleteOptions)
83-
}
84-
85-
// DeleteOptionFunc is a function type that implements the DeleteOption interface.
86-
type DeleteOptionFunc func(opts *deleteOptions)
73+
// DeleteOption is a functional option for DeleteStreamed().
74+
type DeleteOption func(opts *deleteOptions)
8775

88-
func (f DeleteOptionFunc) apply(opts *deleteOptions) {
89-
f(opts)
76+
// WithDeleteStatement sets the DELETE statement to be used for deleting entities.
77+
func WithDeleteStatement(stmt DeleteStatement) DeleteOption {
78+
return func(opts *deleteOptions) {
79+
opts.stmt = stmt
80+
}
9081
}
9182

9283
// WithOnDelete sets the callbacks for a successful DELETE operation.
9384
func WithOnDelete(onDelete ...OnSuccess[any]) DeleteOption {
94-
return DeleteOptionFunc(func(opts *deleteOptions) {
85+
return func(opts *deleteOptions) {
9586
opts.onDelete = onDelete
96-
})
87+
}
9788
}
9889

9990
// deleteOptions stores the options for DeleteStreamed.
@@ -112,7 +103,7 @@ func DeleteStreamed(
112103
) error {
113104
opts := &deleteOptions{}
114105
for _, option := range options {
115-
option.apply(opts)
106+
option(opts)
116107
}
117108

118109
first, forward, err := com.CopyFirst(ctx, entities)

database/insert.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ type InsertStatement interface {
2626

2727
// ExcludedColumns returns the columns to be excluded from the INSERT statement.
2828
ExcludedColumns() []string
29-
30-
// apply implements the InsertOption interface and applies itself to the given options.
31-
apply(opts *insertOptions)
3229
}
3330

3431
// NewInsertStatement returns a new insertStatement for the given entity.
@@ -80,10 +77,6 @@ func (i *insertStatement) ExcludedColumns() []string {
8077
return i.excludedColumns
8178
}
8279

83-
func (i *insertStatement) apply(opts *insertOptions) {
84-
opts.stmt = i
85-
}
86-
8780
// InsertSelectStatement is the interface for building INSERT SELECT statements.
8881
type InsertSelectStatement interface {
8982
// Into sets the table name for the INSERT SELECT statement.
@@ -176,24 +169,21 @@ func (i *insertSelectStatement) Select() SelectStatement {
176169
return i.selectStmt
177170
}
178171

179-
// InsertOption is the interface for functional options for InsertStreamed.
180-
type InsertOption interface {
181-
// apply applies the option to the given insertOptions.
182-
apply(opts *insertOptions)
183-
}
172+
// InsertOption is a functional option for InsertStreamed().
173+
type InsertOption func(opts *insertOptions)
184174

185-
// InsertOptionFunc is a function type that implements the InsertOption interface.
186-
type InsertOptionFunc func(opts *insertOptions)
187-
188-
func (f InsertOptionFunc) apply(opts *insertOptions) {
189-
f(opts)
175+
// WithInsertStatement sets the INSERT statement to be used for inserting entities.
176+
func WithInsertStatement(stmt InsertStatement) InsertOption {
177+
return func(opts *insertOptions) {
178+
opts.stmt = stmt
179+
}
190180
}
191181

192182
// WithOnInsert sets the onInsert callbacks for a successful INSERT statement.
193183
func WithOnInsert(onInsert ...OnSuccess[any]) InsertOption {
194-
return InsertOptionFunc(func(opts *insertOptions) {
184+
return func(opts *insertOptions) {
195185
opts.onInsert = onInsert
196-
})
186+
}
197187
}
198188

199189
// insertOptions stores the options for InsertStreamed.

database/update.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ type UpdateStatement interface {
3232

3333
// Where returns the where clause for the UPDATE statement.
3434
Where() string
35-
36-
// apply implements the UpdateOption interface and applies itself to the given options.
37-
apply(opts *updateOptions)
3835
}
3936

4037
// NewUpdateStatement returns a new updateStatement for the given entity.
@@ -97,28 +94,21 @@ func (u *updateStatement) Where() string {
9794
return u.where
9895
}
9996

100-
func (u *updateStatement) apply(opts *updateOptions) {
101-
opts.stmt = u
102-
}
103-
104-
// UpdateOption is the interface for functional options for UpdateStatement.
105-
type UpdateOption interface {
106-
// apply applies the option to the given updateOptions.
107-
apply(opts *updateOptions)
108-
}
97+
// UpdateOption is a functional option for UpdateStreamed().
98+
type UpdateOption func(opts *updateOptions)
10999

110-
// UpdateOptionFunc is a function type that implements the UpdateOption interface.
111-
type UpdateOptionFunc func(opts *updateOptions)
112-
113-
func (f UpdateOptionFunc) apply(opts *updateOptions) {
114-
f(opts)
100+
// WithUpdateStatement sets the UPDATE statement to be used for updating entities.
101+
func WithUpdateStatement(stmt UpdateStatement) UpdateOption {
102+
return func(opts *updateOptions) {
103+
opts.stmt = stmt
104+
}
115105
}
116106

117107
// WithOnUpdate sets the callback functions to be called after a successful UPDATE.
118108
func WithOnUpdate(onUpdate ...OnSuccess[any]) UpdateOption {
119-
return UpdateOptionFunc(func(opts *updateOptions) {
109+
return func(opts *updateOptions) {
120110
opts.onUpdate = onUpdate
121-
})
111+
}
122112
}
123113

124114
// updateOptions stores the options for UpdateStreamed.

database/upsert.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ type UpsertStatement interface {
3535

3636
// ExcludedColumns returns the columns to be excluded from the UPSERT statement.
3737
ExcludedColumns() []string
38-
39-
// apply implements the UpsertOption interface and applies itself to the given options.
40-
apply(opts *upsertOptions)
4138
}
4239

4340
// NewUpsertStatement returns a new upsertStatement for the given entity.
@@ -89,28 +86,21 @@ func (u *upsertStatement) ExcludedColumns() []string {
8986
return u.excludedColumns
9087
}
9188

92-
func (u *upsertStatement) apply(opts *upsertOptions) {
93-
opts.stmt = u
94-
}
95-
96-
// UpsertOption is the interface for functional options for UpsertStreamed.
97-
type UpsertOption interface {
98-
// apply applies the option to the given upsertOptions.
99-
apply(opts *upsertOptions)
100-
}
101-
102-
// UpsertOptionFunc is a function type that implements the UpsertOption interface.
103-
type UpsertOptionFunc func(opts *upsertOptions)
89+
// UpsertOption is a functional option for UpsertStreamed().
90+
type UpsertOption func(opts *upsertOptions)
10491

105-
func (f UpsertOptionFunc) apply(opts *upsertOptions) {
106-
f(opts)
92+
// WithUpsertStatement sets the UPSERT statement to be used for upserting entities.
93+
func WithUpsertStatement(stmt UpsertStatement) UpsertOption {
94+
return func(opts *upsertOptions) {
95+
opts.stmt = stmt
96+
}
10797
}
10898

10999
// WithOnUpsert sets the callback functions to be called after a successful UPSERT.
110100
func WithOnUpsert(onUpsert ...OnSuccess[any]) UpsertOption {
111-
return UpsertOptionFunc(func(opts *upsertOptions) {
101+
return func(opts *upsertOptions) {
112102
opts.onUpsert = onUpsert
113-
})
103+
}
114104
}
115105

116106
// upsertOptions stores the options for UpsertStreamed.
@@ -136,7 +126,7 @@ func UpsertStreamed[T any, V EntityConstraint[T]](
136126
)
137127

138128
for _, option := range options {
139-
option.apply(opts)
129+
option(opts)
140130
}
141131

142132
if opts.stmt != nil {

database/upsert_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func TestUpsertStreamed(t *testing.T) {
164164

165165
go func() {
166166
if tst.Data.Statement != nil {
167-
upsertError = UpsertStreamed(ctx, db, entities, tst.Data.Statement)
167+
upsertError = UpsertStreamed(ctx, db, entities, WithUpsertStatement(tst.Data.Statement))
168168
} else {
169169
upsertError = UpsertStreamed(ctx, db, entities)
170170
}

0 commit comments

Comments
 (0)