Skip to content

Commit f98b9fe

Browse files
committed
Move kbuilder outside from the internal/ dir but hide it with a build flag
1 parent a06a19a commit f98b9fe

File tree

13 files changed

+187
-32
lines changed

13 files changed

+187
-32
lines changed

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
args=
22
path=./...
33

4+
BUILD_TAGS=-tags ksql_enable_kbuilder_experiment
45
GOBIN=$(shell go env GOPATH)/bin
56

67
TIME=5s
@@ -13,7 +14,7 @@ pre-download-all-images:
1314
docker pull mariadb:10.8
1415

1516
test: setup go-mod-tidy
16-
$(GOBIN)/richgo test $(path) $(args)
17+
$(GOBIN)/richgo test $(BUILD_TAGS) $(path) $(args)
1718
@( cd benchmarks ; $(GOBIN)/richgo test $(path) $(args) )
1819
@( cd examples ; $(GOBIN)/richgo test $(path) $(args) )
1920
@( cd adapters/kpgx ; $(GOBIN)/richgo test $(path) $(args) -timeout=60s )
@@ -34,8 +35,8 @@ readme: benchmark.tmp readme.template.md
3435
go run scripts/build-readme-from-template.go readme.template.md
3536

3637
lint: setup go-mod-tidy
37-
@$(GOBIN)/staticcheck $(path) $(args)
38-
@go vet $(path) $(args)
38+
@$(GOBIN)/staticcheck $(BUILD_TAGS) $(path) $(args)
39+
@go vet $(BUILD_TAGS) $(path) $(args)
3940
@make --no-print-directory -C benchmarks lint
4041
@echo "StaticCheck & Go Vet found no problems on your code!"
4142

codecov.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ coverage:
22
ignore:
33
- "internal/**/*"
44
- "examples/**/*"
5-
- "internal/kbuilder/*"
5+
- "kbuilder/*"
66
- "kstructs/*"
77
- "test_adapters.go"
88
- "internal_mocks.go"

internal/kbuilder/README.md

Lines changed: 0 additions & 14 deletions
This file was deleted.
File renamed without changes.

kbuilder/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Welcome to the Keep It Simple Query Builder
2+
3+
This is the Keep It Simple query builder created to work
4+
either in conjunction or separated from the KSQL package.
5+
6+
This package was started after KSQL and while the KSQL is already
7+
in a usable state I still don't recommend using this one since this
8+
being actively implemented and might change without further warning.
9+
10+
## Enabling kbuilder
11+
12+
kbuilder is only available using a specific build tag, so if you want
13+
to experiment with this tool you will need to import KSQL normally, and when
14+
you compile your project you will need to add the tag `ksql_enable_kbuilder_experiment`,
15+
e.g.:
16+
17+
18+
```
19+
go run -tags ksql_enable_kbuilder_experiment [path to your entrypoint]
20+
go test -tags ksql_enable_kbuilder_experiment [path to your entrypoint]
21+
go build -tags ksql_enable_kbuilder_experiment [path to your entrypoint]
22+
```
23+
24+
Not enabling this flag explicitly will cause it to panic, as I
25+
don't want anyone using it without knowing it is still experimental.
26+
27+
## TODO List
28+
29+
- Add support to Update and Delete operations
30+
- Improve support to JOINs by adding the `tablename` tag to the structs
31+
- Add error check for when the Select, Insert and Update attrs are all empty

internal/kbuilder/insert.go renamed to kbuilder/insert.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build ksql_enable_kbuilder_experiment
2+
13
package kbuilder
24

35
import (

kbuilder/insert_fallback.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//go:build !ksql_enable_kbuilder_experiment
2+
3+
package kbuilder
4+
5+
import (
6+
"github.com/vingarcia/ksql/sqldialect"
7+
)
8+
9+
// This is the fallback file for kbuilder package when the ksql_enable_kbuilder_experiment
10+
// build tag is not enabled, check the file named `insert.go` to see the actual implementation.
11+
12+
// Insert is the struct template for building INSERT queries
13+
type Insert struct {
14+
// Into expects a table name, e.g. "users"
15+
Into string
16+
17+
// Data expected either a single record annotated with `ksql` tags
18+
// or a list of records annotated likewise.
19+
Data interface{}
20+
}
21+
22+
// Build is a utility function for finding the dialect based on the driver and
23+
// then calling BuildQuery(dialect)
24+
func (i Insert) Build(driver string) (sqlQuery string, params []interface{}, _ error) {
25+
panic("kbuilder is an experimental package and needs to be explicitly enabled see github.com/vingarcia/ksql/tree/master/kbuilder/README.md")
26+
}
27+
28+
// BuildQuery implements the queryBuilder interface
29+
func (i Insert) BuildQuery(dialect sqldialect.Provider) (sqlQuery string, params []interface{}, _ error) {
30+
panic("kbuilder is an experimental package and needs to be explicitly enabled see github.com/vingarcia/ksql/tree/master/kbuilder/README.md")
31+
}

internal/kbuilder/insert_test.go renamed to kbuilder/insert_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package kbuilder_test
33
import (
44
"testing"
55

6-
"github.com/vingarcia/ksql/internal/kbuilder"
76
tt "github.com/vingarcia/ksql/internal/testtools"
7+
"github.com/vingarcia/ksql/kbuilder"
88
)
99

1010
func TestInsertQuery(t *testing.T) {
File renamed without changes.

internal/kbuilder/query.go renamed to kbuilder/query.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build ksql_enable_kbuilder_experiment
2+
13
package kbuilder
24

35
import (
@@ -131,14 +133,14 @@ func (w WhereQueries) Where(cond string, params ...interface{}) WhereQueries {
131133
}
132134

133135
// WhereIf conditionally adds a new boolean expression to the WhereQueries helper.
134-
func (w WhereQueries) WhereIf(cond string, param interface{}) WhereQueries {
135-
if param == nil || reflect.ValueOf(param).IsNil() {
136+
func (w WhereQueries) WhereIf(ifCond bool, cond string, params ...interface{}) WhereQueries {
137+
if !ifCond {
136138
return w
137139
}
138140

139141
return append(w, WhereQuery{
140142
cond: cond,
141-
params: []interface{}{param},
143+
params: params,
142144
})
143145
}
144146

@@ -152,14 +154,14 @@ func Where(cond string, params ...interface{}) WhereQueries {
152154
}
153155

154156
// WhereIf conditionally adds a new boolean expression to the WhereQueries helper
155-
func WhereIf(cond string, param interface{}) WhereQueries {
156-
if param == nil || reflect.ValueOf(param).IsNil() {
157+
func WhereIf(ifCond bool, cond string, params ...interface{}) WhereQueries {
158+
if !ifCond {
157159
return WhereQueries{}
158160
}
159161

160162
return WhereQueries{{
161163
cond: cond,
162-
params: []interface{}{param},
164+
params: params,
163165
}}
164166
}
165167

0 commit comments

Comments
 (0)