-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.go
More file actions
46 lines (36 loc) · 1.5 KB
/
builder.go
File metadata and controls
46 lines (36 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Package querybm provides a simple SQL query builder and mapper that works well with models generated by sqlc.
package querybm
import "github.com/tecowl/querybm/statement"
// Builder defines the interface for components that can build SQL statement parts.
type Builder interface {
// Build modifies the given statement by adding SQL components to it.
Build(st *statement.Statement)
}
// Condition is an alias for Builder used specifically for WHERE clause conditions.
type Condition = Builder
// BuildFunc is a function type that takes a statement and modifies it.
// It is used to create a Builder from a function.
// This allows for more flexible and functional-style building of SQL statements.
type BuildFunc = func(st *statement.Statement)
type buildFuncWrapper struct {
fn BuildFunc
}
var _ Builder = (*buildFuncWrapper)(nil)
// NewBuilder creates a new Builder from a BuildFunc.
// This allows you to define how the SQL statement should be built using a function.
// This is useful for cases where you want to encapsulate the building logic in a function rather than a struct.
func NewBuilder(fn BuildFunc) Builder { // nolint:ireturn
return &buildFuncWrapper{fn: fn}
}
// Build implements Builder.
func (b *buildFuncWrapper) Build(st *statement.Statement) {
b.fn(st)
}
// BuildFuncs is a slice of BuildFunc that implements the Builder interface.
type BuildFuncs []BuildFunc
// Build implements Builder for BuildFuncs.
func (s BuildFuncs) Build(st *statement.Statement) {
for _, fn := range s {
fn(st)
}
}