Skip to content

Commit 4900acf

Browse files
committed
Squashed commit of the following:
commit 8da6da7 Author: Erik Unger <[email protected]> Date: Fri Sep 29 14:23:22 2023 +0200 added db.IsTransaction commit 7a8869d Author: Erik Unger <[email protected]> Date: Fri Sep 29 14:08:45 2023 +0200 added db.InsertStruct commit faedda1 Author: Erik Unger <[email protected]> Date: Fri Sep 29 12:50:12 2023 +0200 ErrColumnsWithoutStructFields commit 56fab7e Author: Erik Unger <[email protected]> Date: Fri Sep 29 12:33:57 2023 +0200 TestReflectStructColumnPointers commit 4a88cad Author: Erik Unger <[email protected]> Date: Wed Mar 15 21:41:59 2023 +0100 new impl func interfaces commit c71b6c9 Author: Erik Unger <[email protected]> Date: Tue Mar 14 21:22:49 2023 +0100 sub modules commit d6e094b Author: Erik Unger <[email protected]> Date: Sat Mar 11 09:52:22 2023 +0100 use Execer and Querier for impl funcs commit 5c3a217 Author: Erik Unger <[email protected]> Date: Fri Mar 10 22:50:34 2023 +0100 pqconn uses impl.NewGenericConnection commit b38a83a Author: Erik Unger <[email protected]> Date: Fri Mar 10 21:59:28 2023 +0100 begin v1 refactoring
1 parent be31368 commit 4900acf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1402
-645
lines changed

connection.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ type Connection interface {
3131
// StructFieldMapper used by methods of this Connection.
3232
StructFieldMapper() StructFieldMapper
3333

34+
// ValidateColumnName returns an error
35+
// if the passed name is not valid for a
36+
// column of the connection's database.
37+
ValidateColumnName(name string) error
38+
3439
// Ping returns an error if the database
3540
// does not answer on this connection
3641
// with an optional timeout.
@@ -45,11 +50,6 @@ type Connection interface {
4550
// to create this connection.
4651
Config() *Config
4752

48-
// ValidateColumnName returns an error
49-
// if the passed name is not valid for a
50-
// column of the connection's database.
51-
ValidateColumnName(name string) error
52-
5353
// Now returns the result of the SQL now()
5454
// function for the current connection.
5555
// Useful for getting the timestamp of a
@@ -59,6 +59,12 @@ type Connection interface {
5959
// Exec executes a query with optional args.
6060
Exec(query string, args ...any) error
6161

62+
// QueryRow queries a single row and returns a RowScanner for the results.
63+
QueryRow(query string, args ...any) RowScanner
64+
65+
// QueryRows queries multiple rows and returns a RowsScanner for the results.
66+
QueryRows(query string, args ...any) RowsScanner
67+
6268
// Insert a new row into table using the values.
6369
Insert(table string, values Values) error
6470

@@ -80,9 +86,6 @@ type Connection interface {
8086
// as new rows into table using the connection's
8187
// StructFieldMapper to map struct fields to column names.
8288
// Optional ColumnFilter can be passed to ignore mapped columns.
83-
//
84-
// TODO optimized version with single query if possible
85-
// split into multiple queries depending or maxArgs for query
8689
InsertStructs(table string, rowStructs any, ignoreColumns ...ColumnFilter) error
8790

8891
// InsertUniqueStruct inserts a new row into table using the connection's
@@ -120,12 +123,6 @@ type Connection interface {
120123
// If inserting conflicts on the primary key column(s), then an update is performed.
121124
UpsertStruct(table string, rowStruct any, ignoreColumns ...ColumnFilter) error
122125

123-
// QueryRow queries a single row and returns a RowScanner for the results.
124-
QueryRow(query string, args ...any) RowScanner
125-
126-
// QueryRows queries multiple rows and returns a RowsScanner for the results.
127-
QueryRows(query string, args ...any) RowsScanner
128-
129126
// IsTransaction returns if the connection is a transaction
130127
IsTransaction() bool
131128

errors.go

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"database/sql"
66
"errors"
7+
"fmt"
8+
"reflect"
79
"time"
810
)
911

@@ -57,6 +59,30 @@ const (
5759
ErrNullValueNotAllowed sentinelError = "null value not allowed"
5860
)
5961

62+
// ErrColumnsWithoutStructFields
63+
64+
type ErrColumnsWithoutStructFields struct {
65+
Columns []string
66+
Struct reflect.Value
67+
}
68+
69+
func (e ErrColumnsWithoutStructFields) Error() string {
70+
return fmt.Sprintf("columns %#v has no mapped struct field in %s", e.Columns, e.Struct.Type())
71+
}
72+
73+
// ErrStructFieldHasNoColumn
74+
75+
type ErrStructFieldHasNoColumn struct {
76+
StructField reflect.StructField
77+
Columns []string
78+
}
79+
80+
func (e ErrStructFieldHasNoColumn) Error() string {
81+
return fmt.Sprintf("struct field %s has no mapped column in %#v", e.StructField.Name, e.Columns)
82+
}
83+
84+
// ErrRaisedException
85+
6086
type ErrRaisedException struct {
6187
Message string
6288
}
@@ -65,6 +91,8 @@ func (e ErrRaisedException) Error() string {
6591
return "raised exception: " + e.Message
6692
}
6793

94+
// ErrIntegrityConstraintViolation
95+
6896
type ErrIntegrityConstraintViolation struct {
6997
Constraint string
7098
}
@@ -76,6 +104,8 @@ func (e ErrIntegrityConstraintViolation) Error() string {
76104
return "integrity constraint violation of constraint: " + e.Constraint
77105
}
78106

107+
// ErrRestrictViolation
108+
79109
type ErrRestrictViolation struct {
80110
Constraint string
81111
}
@@ -91,6 +121,8 @@ func (e ErrRestrictViolation) Unwrap() error {
91121
return ErrIntegrityConstraintViolation{Constraint: e.Constraint}
92122
}
93123

124+
// ErrNotNullViolation
125+
94126
type ErrNotNullViolation struct {
95127
Constraint string
96128
}
@@ -106,6 +138,8 @@ func (e ErrNotNullViolation) Unwrap() error {
106138
return ErrIntegrityConstraintViolation{Constraint: e.Constraint}
107139
}
108140

141+
// ErrForeignKeyViolation
142+
109143
type ErrForeignKeyViolation struct {
110144
Constraint string
111145
}
@@ -121,6 +155,8 @@ func (e ErrForeignKeyViolation) Unwrap() error {
121155
return ErrIntegrityConstraintViolation{Constraint: e.Constraint}
122156
}
123157

158+
// ErrUniqueViolation
159+
124160
type ErrUniqueViolation struct {
125161
Constraint string
126162
}
@@ -136,6 +172,8 @@ func (e ErrUniqueViolation) Unwrap() error {
136172
return ErrIntegrityConstraintViolation{Constraint: e.Constraint}
137173
}
138174

175+
// ErrCheckViolation
176+
139177
type ErrCheckViolation struct {
140178
Constraint string
141179
}
@@ -151,6 +189,8 @@ func (e ErrCheckViolation) Unwrap() error {
151189
return ErrIntegrityConstraintViolation{Constraint: e.Constraint}
152190
}
153191

192+
// ErrExclusionViolation
193+
154194
type ErrExclusionViolation struct {
155195
Constraint string
156196
}
@@ -188,14 +228,18 @@ func (e connectionWithError) WithContext(ctx context.Context) Connection {
188228
return connectionWithError{ctx: ctx, err: e.err}
189229
}
190230

191-
func (e connectionWithError) WithStructFieldMapper(namer StructFieldMapper) Connection {
231+
func (e connectionWithError) WithStructFieldMapper(StructFieldMapper) Connection {
192232
return e
193233
}
194234

195235
func (e connectionWithError) StructFieldMapper() StructFieldMapper {
196236
return DefaultStructFieldMapping
197237
}
198238

239+
func (e connectionWithError) ValidateColumnName(name string) error {
240+
return e.err
241+
}
242+
199243
func (e connectionWithError) Ping(time.Duration) error {
200244
return e.err
201245
}
@@ -208,10 +252,6 @@ func (e connectionWithError) Config() *Config {
208252
return &Config{Err: e.err}
209253
}
210254

211-
func (e connectionWithError) ValidateColumnName(name string) error {
212-
return e.err
213-
}
214-
215255
func (e connectionWithError) Now() (time.Time, error) {
216256
return time.Time{}, e.err
217257
}
@@ -220,6 +260,14 @@ func (e connectionWithError) Exec(query string, args ...any) error {
220260
return e.err
221261
}
222262

263+
func (e connectionWithError) QueryRow(query string, args ...any) RowScanner {
264+
return RowScannerWithError(e.err)
265+
}
266+
267+
func (e connectionWithError) QueryRows(query string, args ...any) RowsScanner {
268+
return RowsScannerWithError(e.err)
269+
}
270+
223271
func (e connectionWithError) Insert(table string, values Values) error {
224272
return e.err
225273
}
@@ -264,14 +312,6 @@ func (e connectionWithError) UpsertStruct(table string, rowStruct any, ignoreCol
264312
return e.err
265313
}
266314

267-
func (e connectionWithError) QueryRow(query string, args ...any) RowScanner {
268-
return RowScannerWithError(e.err)
269-
}
270-
271-
func (e connectionWithError) QueryRows(query string, args ...any) RowsScanner {
272-
return RowsScannerWithError(e.err)
273-
}
274-
275315
func (e connectionWithError) IsTransaction() bool {
276316
return false
277317
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.21
55
require (
66
github.com/domonda/go-errs v0.0.0-20230810132956-1b6272f9fc8f
77
github.com/domonda/go-types v0.0.0-20230829145420-30f9974e0bc7
8-
github.com/go-sql-driver/mysql v1.7.1
98
github.com/lib/pq v1.10.9
109
github.com/stretchr/testify v1.8.4
1110
)

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ github.com/domonda/go-pretty v0.0.0-20230810130018-8920f571470a h1:b3a6MwwMrHR9d
66
github.com/domonda/go-pretty v0.0.0-20230810130018-8920f571470a/go.mod h1:3QkM8UJdyJMeKZiIo7hYzSkQBpRS3k0gOHw4ysyEIB4=
77
github.com/domonda/go-types v0.0.0-20230829145420-30f9974e0bc7 h1:riEK9SQ1O0ADGI66P1Rz2zqB+g3qlREm/wF7DINj7RI=
88
github.com/domonda/go-types v0.0.0-20230829145420-30f9974e0bc7/go.mod h1:qMSeU/23ZUopt+1kY0pJ27iqNRtsY1jATQklyCyLRAU=
9-
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
10-
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
119
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
1210
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
1311
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=

go.work

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
go 1.21
2+
3+
use (
4+
.
5+
./mysqlconn
6+
./pqconn
7+
./pqconn/tests
8+
)

go.work.sum

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
github.com/cention-sany/utf7 v0.0.0-20170124080048-26cad61bd60a h1:MISbI8sU/PSK/ztvmWKFcI7UGb5/HQT7B+i3a2myKgI=
2+
github.com/cention-sany/utf7 v0.0.0-20170124080048-26cad61bd60a/go.mod h1:2GxOXOlEPAMFPfp014mK1SWq8G8BN8o7/dfYqJrVGn8=
3+
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
4+
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
5+
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
6+
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f h1:3BSP1Tbs2djlpprl7wCLuiqMaUh5SJkkzI2gDs+FgLs=
7+
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f/go.mod h1:Pcatq5tYkCW2Q6yrR2VRHlbHpZ/R4/7qyL1TCF7vl14=
8+
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
9+
github.com/jaytaylor/html2text v0.0.0-20211105163654-bc68cce691ba h1:QFQpJdgbON7I0jr2hYW7Bs+XV0qjc3d5tZoDnRFnqTg=
10+
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056/go.mod h1:CVKlgaMiht+LXvHG173ujK6JUhZXKb2u/BQtjPDIvyk=
11+
github.com/jhillyerd/enmime v0.10.1 h1:3VP8gFhK7R948YJBrna5bOgnTXEuPAoICo79kKkBKfA=
12+
github.com/jhillyerd/enmime v1.0.0/go.mod h1:EktNOa/V6ka9yCrfoB2uxgefp1lno6OVdszW0iQ5LnM=
13+
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
14+
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
15+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
16+
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
17+
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
18+
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
19+
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
20+
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
21+
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
22+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
23+
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
24+
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
25+
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf h1:pvbZ0lM0XWPBqUKqFU8cmavspvIl9nulOYwdy6IFRRo=
26+
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf/go.mod h1:RJID2RhlZKId02nZ62WenDCkgHFerpIOmW0iT7GKmXM=
27+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
28+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
29+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
30+
github.com/teamwork/test v0.0.0-20200108114543-02621bae84ad/go.mod h1:TIbx7tx6WHBjQeLRM4eWQZBL7kmBZ7/KI4x4v7Y5YmA=
31+
github.com/teamwork/tnef v0.0.0-20200108124832-7deabccfdb32 h1:j15wq0XPAY/HR/0+dtwUrIrF2ZTKbk7QIES2p4dAG+k=
32+
github.com/teamwork/tnef v0.0.0-20200108124832-7deabccfdb32/go.mod h1:v7dFaQrF/4+curx7UTH9rqTkHTgXqghfI3thANW150o=
33+
github.com/teamwork/utils v0.0.0-20220314153103-637fa45fa6cc/go.mod h1:3Fn0qxFeRNpvsg/9T1+btOOOKkd1qG2nPYKKcOmNpcs=
34+
github.com/ungerik/go-fs v0.0.0-20230206141012-abb864f815e3 h1:IEm9Je1L3HAIEwiXOuGgJuUPjXXHzf/1e704VyIbcGc=
35+
github.com/ungerik/go-fs v0.0.0-20230810132455-f7ff27f6fa2b h1:hZ/Tp1sn1oRwYqIZfjpfUp8N+5e3LGk32O8OAMh9VOk=
36+
github.com/ungerik/go-fs v0.0.0-20230810132455-f7ff27f6fa2b/go.mod h1:P8k1DG+Ox0KP4MFNTSPd8ojoDUwXjrWdGjsssF6vT/g=
37+
github.com/ungerik/go-reflection v0.0.0-20230810134712-a63435f6bc7e h1:BPksMeVdgSD8L4yXHYSY3HpdJ/5z2Ok5lF6PxHIVgEQ=
38+
github.com/ungerik/go-reflection v0.0.0-20230810134712-a63435f6bc7e/go.mod h1:1Q14POg/xa/P6/hWKfnUexqUhW1X6jgw+6gG7lOne1E=
39+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
40+
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
41+
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
42+
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
43+
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
44+
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
45+
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
46+
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
47+
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
48+
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
49+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
50+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
51+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
52+
gotest.tools/v3 v3.2.0/go.mod h1:Mcr9QNxkg0uMvy/YElmo4SpXgJKWgQvYrT7Kw5RzJ1A=
53+
mvdan.cc/xurls/v2 v2.4.0 h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc=
54+
mvdan.cc/xurls/v2 v2.5.0/go.mod h1:yQgaGQ1rFtJUzkmKiHYSSfuQxqfYmd//X6PxvholpeE=

0 commit comments

Comments
 (0)