Skip to content

Commit 9cc5f10

Browse files
#91: Change interface{} to any, version 1.17 -> 1.18 + test for TestNewConnectionFromDB (#92)
* #91: Change interface{} to any, version 1.17 -> 1.18 + test for TestNewConnectionFromDB * #91: Change go version to 1.18 in github Actions
1 parent 191a8df commit 9cc5f10

File tree

5 files changed

+56
-49
lines changed

5 files changed

+56
-49
lines changed

.github/workflows/go.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ jobs:
1818
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
1919

2020
steps:
21-
- name: Set up Go 1.14
21+
- name: Set up Go 1.18
2222
uses: actions/setup-go@v1
2323
with:
24-
go-version: 1.14
24+
go-version: 1.18
2525
id: go
2626
- name: Check out code into the Go module directory
2727
uses: actions/checkout@v1

builder.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const (
3535

3636
// inner type to build qualified sql
3737
type builder struct {
38-
whereBindings []map[string]interface{}
38+
whereBindings []map[string]any
3939
startBindingsAt int
4040
where string
4141
table string
@@ -90,7 +90,7 @@ func (r *DB) reset() {
9090
r.Builder.table = ""
9191
r.Builder.columns = []string{"*"}
9292
r.Builder.where = ""
93-
r.Builder.whereBindings = make([]map[string]interface{}, 0)
93+
r.Builder.whereBindings = make([]map[string]any, 0)
9494
r.Builder.groupBy = ""
9595
r.Builder.having = ""
9696
r.Builder.orderBy = make([]map[string]string, 0)
@@ -140,7 +140,7 @@ func (r *DB) GroupBy(expr string) *DB {
140140
}
141141

142142
// Having similar to Where but used with GroupBy to apply over the grouped results
143-
func (r *DB) Having(operand, operator string, val interface{}) *DB {
143+
func (r *DB) Having(operand, operator string, val any) *DB {
144144
r.Builder.having = operand + " " + operator + " " + convertToStr(val)
145145
return r
146146
}
@@ -237,61 +237,61 @@ func (r *DB) buildJoin(joinType, table, on string) *DB {
237237
}
238238

239239
// Where accepts left operand-operator-right operand to apply them to where clause
240-
func (r *DB) Where(operand, operator string, val interface{}) *DB {
240+
func (r *DB) Where(operand, operator string, val any) *DB {
241241
return r.buildWhere("", operand, operator, val)
242242
}
243243

244244
// AndWhere accepts left operand-operator-right operand to apply them to where clause
245245
// with AND logical operator
246-
func (r *DB) AndWhere(operand, operator string, val interface{}) *DB {
246+
func (r *DB) AndWhere(operand, operator string, val any) *DB {
247247
return r.buildWhere("AND", operand, operator, val)
248248
}
249249

250250
// OrWhere accepts left operand-operator-right operand to apply them to where clause
251251
// with OR logical operator
252-
func (r *DB) OrWhere(operand, operator string, val interface{}) *DB {
252+
func (r *DB) OrWhere(operand, operator string, val any) *DB {
253253
return r.buildWhere("OR", operand, operator, val)
254254
}
255255

256-
func (r *DB) buildWhere(prefix, operand, operator string, val interface{}) *DB {
256+
func (r *DB) buildWhere(prefix, operand, operator string, val any) *DB {
257257
if prefix != "" {
258258
prefix = " " + prefix + " "
259259
}
260-
r.Builder.whereBindings = append(r.Builder.whereBindings, map[string]interface{}{prefix + operand + " " + operator: val})
260+
r.Builder.whereBindings = append(r.Builder.whereBindings, map[string]any{prefix + operand + " " + operator: val})
261261
return r
262262
}
263263

264264
// WhereBetween sets the clause BETWEEN 2 values
265-
func (r *DB) WhereBetween(col string, val1, val2 interface{}) *DB {
265+
func (r *DB) WhereBetween(col string, val1, val2 any) *DB {
266266
return r.buildWhere("", col, sqlOperatorBetween, convertToStr(val1)+and+convertToStr(val2))
267267
}
268268

269269
// OrWhereBetween sets the clause OR BETWEEN 2 values
270-
func (r *DB) OrWhereBetween(col string, val1, val2 interface{}) *DB {
270+
func (r *DB) OrWhereBetween(col string, val1, val2 any) *DB {
271271
return r.buildWhere(sqlOperatorOr, col, sqlOperatorBetween, convertToStr(val1)+and+convertToStr(val2))
272272
}
273273

274274
// AndWhereBetween sets the clause AND BETWEEN 2 values
275-
func (r *DB) AndWhereBetween(col string, val1, val2 interface{}) *DB {
275+
func (r *DB) AndWhereBetween(col string, val1, val2 any) *DB {
276276
return r.buildWhere(sqlOperatorAnd, col, sqlOperatorBetween, convertToStr(val1)+and+convertToStr(val2))
277277
}
278278

279279
// WhereNotBetween sets the clause NOT BETWEEN 2 values
280-
func (r *DB) WhereNotBetween(col string, val1, val2 interface{}) *DB {
280+
func (r *DB) WhereNotBetween(col string, val1, val2 any) *DB {
281281
return r.buildWhere("", col, sqlOperatorNotBetween, convertToStr(val1)+and+convertToStr(val2))
282282
}
283283

284284
// OrWhereNotBetween sets the clause OR BETWEEN 2 values
285-
func (r *DB) OrWhereNotBetween(col string, val1, val2 interface{}) *DB {
285+
func (r *DB) OrWhereNotBetween(col string, val1, val2 any) *DB {
286286
return r.buildWhere(sqlOperatorOr, col, sqlOperatorNotBetween, convertToStr(val1)+and+convertToStr(val2))
287287
}
288288

289289
// AndWhereNotBetween sets the clause AND BETWEEN 2 values
290-
func (r *DB) AndWhereNotBetween(col string, val1, val2 interface{}) *DB {
290+
func (r *DB) AndWhereNotBetween(col string, val1, val2 any) *DB {
291291
return r.buildWhere(sqlOperatorAnd, col, sqlOperatorNotBetween, convertToStr(val1)+and+convertToStr(val2))
292292
}
293293

294-
func convertToStr(val interface{}) string {
294+
func convertToStr(val any) string {
295295
switch v := val.(type) {
296296
case string:
297297
return "'" + v + "'"
@@ -363,7 +363,7 @@ func (r *DB) Rename(from, to string) (sql.Result, error) {
363363
}
364364

365365
// WhereIn appends IN (val1, val2, val3...) stmt to WHERE clause
366-
func (r *DB) WhereIn(field string, in interface{}) *DB {
366+
func (r *DB) WhereIn(field string, in any) *DB {
367367
ins, err := interfaceToSlice(in)
368368
if err != nil {
369369
return nil
@@ -373,7 +373,7 @@ func (r *DB) WhereIn(field string, in interface{}) *DB {
373373
}
374374

375375
// WhereNotIn appends NOT IN (val1, val2, val3...) stmt to WHERE clause
376-
func (r *DB) WhereNotIn(field string, in interface{}) *DB {
376+
func (r *DB) WhereNotIn(field string, in any) *DB {
377377
ins, err := interfaceToSlice(in)
378378
if err != nil {
379379
return nil
@@ -383,7 +383,7 @@ func (r *DB) WhereNotIn(field string, in interface{}) *DB {
383383
}
384384

385385
// OrWhereIn appends OR IN (val1, val2, val3...) stmt to WHERE clause
386-
func (r *DB) OrWhereIn(field string, in interface{}) *DB {
386+
func (r *DB) OrWhereIn(field string, in any) *DB {
387387
ins, err := interfaceToSlice(in)
388388
if err != nil {
389389
return nil
@@ -393,7 +393,7 @@ func (r *DB) OrWhereIn(field string, in interface{}) *DB {
393393
}
394394

395395
// OrWhereNotIn appends OR NOT IN (val1, val2, val3...) stmt to WHERE clause
396-
func (r *DB) OrWhereNotIn(field string, in interface{}) *DB {
396+
func (r *DB) OrWhereNotIn(field string, in any) *DB {
397397
ins, err := interfaceToSlice(in)
398398
if err != nil {
399399
return nil
@@ -403,7 +403,7 @@ func (r *DB) OrWhereNotIn(field string, in interface{}) *DB {
403403
}
404404

405405
// AndWhereIn appends OR IN (val1, val2, val3...) stmt to WHERE clause
406-
func (r *DB) AndWhereIn(field string, in interface{}) *DB {
406+
func (r *DB) AndWhereIn(field string, in any) *DB {
407407
ins, err := interfaceToSlice(in)
408408
if err != nil {
409409
return nil
@@ -414,7 +414,7 @@ func (r *DB) AndWhereIn(field string, in interface{}) *DB {
414414
}
415415

416416
// AndWhereNotIn appends OR NOT IN (val1, val2, val3...) stmt to WHERE clause
417-
func (r *DB) AndWhereNotIn(field string, in interface{}) *DB {
417+
func (r *DB) AndWhereNotIn(field string, in any) *DB {
418418
ins, err := interfaceToSlice(in)
419419
if err != nil {
420420
return nil
@@ -454,7 +454,7 @@ func (r *DB) AndWhereNotNull(field string) *DB {
454454
}
455455

456456
// prepares slice for Where bindings, IN/NOT IN etc
457-
func prepareSlice(in []interface{}) (out []string) {
457+
func prepareSlice(in []any) (out []string) {
458458
for _, value := range in {
459459
switch v := value.(type) {
460460
case string:

builder_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package buildsqlx
22

33
import (
4+
"database/sql"
45
"errors"
56
"fmt"
67
"os"
78
"testing"
89

910
_ "github.com/lib/pq"
1011
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
1113
)
1214

1315
const (
@@ -46,6 +48,11 @@ func TestMain(m *testing.M) {
4648
os.Exit(m.Run())
4749
}
4850

51+
func TestNewConnectionFromDB(t *testing.T) {
52+
conn := NewConnectionFromDb(&sql.DB{})
53+
require.Equal(t, conn.db, &sql.DB{})
54+
}
55+
4956
func TestSelectAndLimit(t *testing.T) {
5057
_, err := db.Truncate(TestTable)
5158
assert.NoError(t, err)

factory.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var (
2020
)
2121

2222
// Get builds all sql statements chained before and executes query collecting data to the slice
23-
func (r *DB) Get() ([]map[string]interface{}, error) {
23+
func (r *DB) Get() ([]map[string]any, error) {
2424
builder := r.Builder
2525
if builder.table == "" {
2626
return nil, errTableCallBeforeOp
@@ -51,14 +51,14 @@ func (r *DB) Get() ([]map[string]interface{}, error) {
5151

5252
columns, _ := rows.Columns()
5353
count := len(columns)
54-
values := make([]interface{}, count)
55-
valuePtrs := make([]interface{}, count)
54+
values := make([]any, count)
55+
valuePtrs := make([]any, count)
5656

5757
// collecting data from struct with fields
58-
var res []map[string]interface{}
58+
var res []map[string]any
5959

6060
for rows.Next() {
61-
collect := make(map[string]interface{}, count)
61+
collect := make(map[string]any, count)
6262

6363
for i := range columns {
6464
valuePtrs[i] = &values[i]
@@ -87,8 +87,8 @@ func (r *DB) Get() ([]map[string]interface{}, error) {
8787
return res, nil
8888
}
8989

90-
func prepareValues(values []map[string]interface{}) []interface{} {
91-
var vls []interface{}
90+
func prepareValues(values []map[string]any) []any {
91+
var vls []any
9292
for _, v := range values {
9393
_, vals, _ := prepareBindings(v)
9494
vls = append(vls, vals...)
@@ -143,14 +143,14 @@ func (r *builder) buildClauses() string {
143143
}
144144

145145
// composes WHERE clause string for particular query stmt
146-
func composeWhere(whereBindings []map[string]interface{}, startedAt int) string {
146+
func composeWhere(whereBindings []map[string]any, startedAt int) string {
147147
where := " WHERE "
148148
i := startedAt
149149
for _, m := range whereBindings {
150150
for k, v := range m {
151151
// operand >= $i
152152
switch vi := v.(type) {
153-
case []interface{}:
153+
case []any:
154154
placeholders := make([]string, 0, len(vi))
155155
for range vi {
156156
placeholders = append(placeholders, "$"+strconv.Itoa(i))
@@ -192,7 +192,7 @@ func composeOrderBy(orderBy []map[string]string, orderByRaw *string) string {
192192
}
193193

194194
// Insert inserts one row with param bindings
195-
func (r *DB) Insert(data map[string]interface{}) error {
195+
func (r *DB) Insert(data map[string]any) error {
196196
builder := r.Builder
197197
if builder.table == "" {
198198
return errTableCallBeforeOp
@@ -212,7 +212,7 @@ func (r *DB) Insert(data map[string]interface{}) error {
212212
}
213213

214214
// InsertGetId inserts one row with param bindings and returning id
215-
func (r *DB) InsertGetId(data map[string]interface{}) (uint64, error) {
215+
func (r *DB) InsertGetId(data map[string]any) (uint64, error) {
216216
builder := r.Builder
217217
if builder.table == "" {
218218
return 0, errTableCallBeforeOp
@@ -232,8 +232,8 @@ func (r *DB) InsertGetId(data map[string]interface{}) (uint64, error) {
232232
return id, nil
233233
}
234234

235-
func prepareValue(value interface{}) []interface{} {
236-
values := []interface{}{}
235+
func prepareValue(value any) []any {
236+
var values []any
237237
switch v := value.(type) {
238238
case string:
239239
//if where { // todo: left comments for further exploration, probably incorrect behaviour for pg driver
@@ -249,7 +249,7 @@ func prepareValue(value interface{}) []interface{} {
249249
values = append(values, strconv.FormatInt(v, 10))
250250
case uint64:
251251
values = append(values, strconv.FormatUint(v, 10))
252-
case []interface{}:
252+
case []any:
253253
for _, vi := range v {
254254
values = append(values, prepareValue(vi)...)
255255
}
@@ -261,7 +261,7 @@ func prepareValue(value interface{}) []interface{} {
261261
}
262262

263263
// prepareBindings prepares slices to split in favor of INSERT sql statement
264-
func prepareBindings(data map[string]interface{}) (columns []string, values []interface{}, bindings []string) {
264+
func prepareBindings(data map[string]any) (columns []string, values []any, bindings []string) {
265265
i := 1
266266
for column, value := range data {
267267
if strings.Contains(column, sqlOperatorIs) || strings.Contains(column, sqlOperatorBetween) {
@@ -284,7 +284,7 @@ func prepareBindings(data map[string]interface{}) (columns []string, values []in
284284
}
285285

286286
// InsertBatch inserts multiple rows based on transaction
287-
func (r *DB) InsertBatch(data []map[string]interface{}) error {
287+
func (r *DB) InsertBatch(data []map[string]any) error {
288288
builder := r.Builder
289289
if builder.table == "" {
290290
return errTableCallBeforeOp
@@ -328,13 +328,13 @@ func (r *DB) InsertBatch(data []map[string]interface{}) error {
328328
}
329329

330330
// prepareInsertBatch prepares slices to split in favor of INSERT sql statement
331-
func prepareInsertBatch(data []map[string]interface{}) (columns []string, values [][]interface{}) {
332-
values = make([][]interface{}, len(data))
331+
func prepareInsertBatch(data []map[string]any) (columns []string, values [][]any) {
332+
values = make([][]any, len(data))
333333
colToIdx := make(map[string]int)
334334

335335
i := 0
336336
for k, v := range data {
337-
values[k] = make([]interface{}, len(v))
337+
values[k] = make([]any, len(v))
338338

339339
for column, value := range v {
340340
if k == 0 {
@@ -364,7 +364,7 @@ func prepareInsertBatch(data []map[string]interface{}) (columns []string, values
364364

365365
// Update builds an UPDATE sql stmt with corresponding where/from clauses if stated
366366
// returning affected rows
367-
func (r *DB) Update(data map[string]interface{}) (int64, error) {
367+
func (r *DB) Update(data map[string]any) (int64, error) {
368368
builder := r.Builder
369369
if builder.table == "" {
370370
return 0, errTableCallBeforeOp
@@ -414,7 +414,7 @@ func (r *DB) Delete() (int64, error) {
414414
}
415415

416416
// Replace inserts data if conflicting row hasn't been found, else it will update an existing one
417-
func (r *DB) Replace(data map[string]interface{}, conflict string) (int64, error) {
417+
func (r *DB) Replace(data map[string]any, conflict string) (int64, error) {
418418
builder := r.Builder
419419
if builder.table == "" {
420420
return 0, errTableCallBeforeOp
@@ -436,7 +436,7 @@ func (r *DB) Replace(data map[string]interface{}, conflict string) (int64, error
436436

437437
// InTransaction executes fn passed as an argument in transaction mode
438438
// if there are no results returned - txn will be rolled back, otherwise committed and returned
439-
func (r *DB) InTransaction(fn func() (interface{}, error)) error {
439+
func (r *DB) InTransaction(fn func() (any, error)) error {
440440
txn, err := r.Sql().Begin()
441441
if err != nil {
442442
return err
@@ -465,11 +465,11 @@ func (r *DB) InTransaction(fn func() (interface{}, error)) error {
465465
if v > 0 {
466466
isOk = true
467467
}
468-
case []map[string]interface{}:
468+
case []map[string]any:
469469
if len(v) > 0 {
470470
isOk = true
471471
}
472-
case map[string]interface{}:
472+
case map[string]any:
473473
if len(v) > 0 {
474474
isOk = true
475475
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/arthurkushman/buildsqlx
22

3-
go 1.17
3+
go 1.18
44

55
require (
66
github.com/lib/pq v1.2.0

0 commit comments

Comments
 (0)