Skip to content

Commit 1ac5af8

Browse files
author
José Carlos
authored
Merge pull request #330 from upper/issue-310
Don't ignore errors from (internal) FindTablePrimaryKeys
2 parents f5e098d + 6851793 commit 1ac5af8

File tree

5 files changed

+87
-29
lines changed

5 files changed

+87
-29
lines changed

Diff for: .travis.yml

+9-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@ addons:
2424
- mongodb-org-server
2525
- mongodb-org-shell
2626

27-
env: GOARCH=amd64 DB_HOST=127.0.0.1
27+
env:
28+
global:
29+
- MAKEFLAGS="-j4"
30+
- GOARCH=amd64
31+
- DB_HOST=127.0.0.1
32+
matrix:
33+
- TEST_CMD="make benchmark test-main"
34+
- TEST_CMD="make test-adapters"
2835

2936
install:
3037
- mkdir -p $GOPATH/src/upper.io
@@ -38,5 +45,4 @@ before_script:
3845
- mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
3946

4047
script:
41-
# - UPPERIO_DB_DEBUG=1 make test
42-
- make test
48+
- ${TEST_CMD}

Diff for: Makefile

+31-6
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,35 @@ DB_HOST ?= 127.0.0.1
44

55
export DB_HOST
66

7-
test:
8-
go test -v -benchtime=500ms -bench=. ./lib/... && \
9-
go test -v -benchtime=500ms -bench=. ./internal/... && \
10-
for ADAPTER in postgresql mysql sqlite ql mongo; do \
11-
$(MAKE) -C $$ADAPTER test || exit 1; \
12-
done && \
7+
benchmark-lib:
8+
go test -v -benchtime=500ms -bench=. ./lib/...
9+
10+
benchmark-internal:
11+
go test -v -benchtime=500ms -bench=. ./internal/...
12+
13+
benchmark: benchmark-lib benchmark-internal
14+
15+
test-lib:
16+
go test -v ./lib/...
17+
18+
test-internal:
19+
go test -v ./internal/...
20+
21+
test-libs: test-lib test-internal
22+
23+
test-adapters: test-adapter-postgresql test-adapter-mysql test-adapter-sqlite test-adapter-ql test-adapter-mongo
24+
25+
reset-db:
26+
$(MAKE) -C postgresql reset-db && \
27+
$(MAKE) -C mysql reset-db && \
28+
$(MAKE) -C sqlite reset-db && \
29+
$(MAKE) -C ql reset-db && \
30+
$(MAKE) -C mongo reset-db
31+
32+
test-main: reset-db
1333
go test -v
34+
35+
test: test-adapters test-libs test-main
36+
37+
test-adapter-%:
38+
$(MAKE) -C $* test || exit 1;

Diff for: internal/sqladapter/collection.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ type condsFilter interface {
3636

3737
// collection is the implementation of Collection.
3838
type collection struct {
39-
p PartialCollection
40-
pk []string
39+
p PartialCollection
40+
pk []string
41+
err error
4142
}
4243

4344
// NewBaseCollection returns a collection with basic methods.
4445
func NewBaseCollection(p PartialCollection) BaseCollection {
4546
c := &collection{p: p}
46-
c.pk, _ = c.p.Database().FindTablePrimaryKeys(c.p.Name())
47+
c.pk, c.err = c.p.Database().FindTablePrimaryKeys(c.p.Name())
4748
return c
4849
}
4950

@@ -66,11 +67,14 @@ func (c *collection) filterConds(conds ...interface{}) []interface{} {
6667

6768
// Find creates a result set with the given conditions.
6869
func (c *collection) Find(conds ...interface{}) db.Result {
69-
return NewResult(
70-
c.p.Database(),
71-
c.p.Name(),
72-
c.filterConds(conds...),
73-
)
70+
if c.err != nil {
71+
return &Result{err: c.err}
72+
}
73+
return &Result{
74+
b: c.p.Database(),
75+
table: c.p.Name(),
76+
conds: [][]interface{}{c.filterConds(conds...)},
77+
}
7478
}
7579

7680
// Exists returns true if the collection exists.

Diff for: internal/sqladapter/result.go

+20-10
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,6 @@ type Result struct {
4545
iterMu sync.Mutex
4646
}
4747

48-
// NewResult creates and Results a new Result set on the given table, this set
49-
// is limited by the given exql.Where conditions.
50-
func NewResult(b sqlbuilder.Builder, table string, conds []interface{}) *Result {
51-
return &Result{
52-
b: b,
53-
table: table,
54-
conds: [][]interface{}{conds},
55-
}
56-
}
57-
5848
func (r *Result) setErr(err error) error {
5949
if err == nil {
6050
return nil
@@ -128,12 +118,18 @@ func (r *Result) String() string {
128118

129119
// All dumps all Results into a pointer to an slice of structs or maps.
130120
func (r *Result) All(dst interface{}) error {
121+
if err := r.Err(); err != nil {
122+
return err
123+
}
131124
err := r.buildSelect().Iterator().All(dst)
132125
return r.setErr(err)
133126
}
134127

135128
// One fetches only one Result from the set.
136129
func (r *Result) One(dst interface{}) error {
130+
if err := r.Err(); err != nil {
131+
return err
132+
}
137133
err := r.buildSelect().Iterator().One(dst)
138134
return r.setErr(err)
139135
}
@@ -157,6 +153,10 @@ func (r *Result) Next(dst interface{}) bool {
157153

158154
// Delete deletes all matching items from the collection.
159155
func (r *Result) Delete() error {
156+
if err := r.Err(); err != nil {
157+
return err
158+
}
159+
160160
q := r.b.DeleteFrom(r.table).
161161
Limit(r.limit)
162162

@@ -170,6 +170,9 @@ func (r *Result) Delete() error {
170170

171171
// Close closes the Result set.
172172
func (r *Result) Close() error {
173+
if err := r.Err(); err != nil {
174+
return err
175+
}
173176
if r.iter != nil {
174177
return r.setErr(r.iter.Close())
175178
}
@@ -179,6 +182,9 @@ func (r *Result) Close() error {
179182
// Update updates matching items from the collection with values of the given
180183
// map or struct.
181184
func (r *Result) Update(values interface{}) error {
185+
if err := r.Err(); err != nil {
186+
return err
187+
}
182188
q := r.b.Update(r.table).
183189
Set(values).
184190
Limit(r.limit)
@@ -193,6 +199,10 @@ func (r *Result) Update(values interface{}) error {
193199

194200
// Count counts the elements on the set.
195201
func (r *Result) Count() (uint64, error) {
202+
if err := r.Err(); err != nil {
203+
return 0, err
204+
}
205+
196206
counter := struct {
197207
Count uint64 `db:"_t"`
198208
}{}

Diff for: postgresql/database.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package postgresql
2323

2424
import (
2525
"database/sql"
26+
"fmt"
2627
"strings"
2728
"sync"
2829

@@ -237,12 +238,22 @@ func (d *database) TableExists(name string) error {
237238
return db.ErrCollectionDoesNotExist
238239
}
239240

241+
// quotedTableName returns a valid regclass name for both regular tables and
242+
// for schemas.
243+
func quotedTableName(s string) string {
244+
chunks := strings.Split(s, ".")
245+
for i := range chunks {
246+
chunks[i] = fmt.Sprintf("%q", chunks[i])
247+
}
248+
return strings.Join(chunks, ".")
249+
}
250+
240251
// FindTablePrimaryKeys allows sqladapter find a table's primary keys.
241252
func (d *database) FindTablePrimaryKeys(tableName string) ([]string, error) {
242253
q := d.Select("pg_attribute.attname AS pkey").
243254
From("pg_index", "pg_class", "pg_attribute").
244255
Where(`
245-
pg_class.oid = '` + tableName + `'::regclass
256+
pg_class.oid = '` + quotedTableName(tableName) + `'::regclass
246257
AND indrelid = pg_class.oid
247258
AND pg_attribute.attrelid = pg_class.oid
248259
AND pg_attribute.attnum = ANY(pg_index.indkey)
@@ -261,6 +272,8 @@ func (d *database) FindTablePrimaryKeys(tableName string) ([]string, error) {
261272
}
262273
pk = append(pk, k)
263274
}
264-
275+
if err := iter.Err(); err != nil {
276+
return nil, err
277+
}
265278
return pk, nil
266279
}

0 commit comments

Comments
 (0)