Skip to content

Commit 994ae7e

Browse files
author
Jeff Wendling
committed
less strict unique tracking
fixes #25
1 parent a4dfdd9 commit 994ae7e

File tree

5 files changed

+143
-10
lines changed

5 files changed

+143
-10
lines changed

ir/helpers.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ func queryUnique(targets []*Model, joins []*Join, wheres []*Where) (out bool) {
119119
}
120120
}
121121

122-
// if any table from the set of targets is unique, then only one row would
122+
// if all tables from the set of targets is unique, then only one row would
123123
// ever be returned, so it is a "unique" query.
124124
for _, target := range targets {
125-
if unique[target.Name] {
126-
return true
125+
if !unique[target.Name] {
126+
return false
127127
}
128128
}
129129

130-
return false
130+
return true
131131
}
132132

133133
func SortModels(models []*Model) (sorted []*Model, err error) {

run_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ func testRunFile(t *testutil.T, dbx_file string) {
4343
t.Context("dbx", linedSource(dbx_source))
4444
d := loadDirectives(t, dbx_source)
4545

46-
ext := filepath.Ext(dbx_file)
47-
go_file := dbx_file[:len(dbx_file)-len(ext)] + ".go"
48-
go_source, err := ioutil.ReadFile(go_file)
49-
t.AssertNoError(err)
50-
t.Context("go", linedSource(go_source))
51-
5246
dir, err := ioutil.TempDir("", "dbx")
5347
t.AssertNoError(err)
5448
defer os.RemoveAll(dir)
@@ -64,6 +58,12 @@ func testRunFile(t *testutil.T, dbx_file string) {
6458
t.AssertNoError(err)
6559
}
6660

61+
ext := filepath.Ext(dbx_file)
62+
go_file := dbx_file[:len(dbx_file)-len(ext)] + ".go"
63+
go_source, err := ioutil.ReadFile(go_file)
64+
t.AssertNoError(err)
65+
t.Context("go", linedSource(go_source))
66+
6767
t.Logf("[%s] copying go source...", dbx_file)
6868
t.AssertNoError(ioutil.WriteFile(
6969
filepath.Join(dir, filepath.Base(go_file)), go_source, 0644))

testdata/build/unique_checking.dbx

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
model a (
2+
key id
3+
field id serial64
4+
)
5+
6+
model b (
7+
key id
8+
field id serial64
9+
field a_id a.id restrict
10+
)
11+
12+
model c (
13+
key id
14+
unique b_id
15+
field id serial64
16+
field lat float
17+
field lon float
18+
field b_id b.id restrict
19+
)
20+
21+
read all (
22+
select a b c
23+
24+
join a.id = b.a_id
25+
join b.id = c.b_id
26+
27+
where a.id = ?
28+
where c.lat < ?
29+
where c.lat > ?
30+
where c.lon < ?
31+
where c.lon > ?
32+
)
33+

testdata/run/unique_checking.dbx

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
model a (
2+
table a
3+
key id
4+
field id serial64
5+
)
6+
7+
model b (
8+
table b
9+
key id
10+
field id serial64
11+
field a_id a.id restrict
12+
)
13+
14+
model c (
15+
table c
16+
key id
17+
unique b_id
18+
field id serial64
19+
field lat float
20+
field lon float
21+
field b_id b.id restrict
22+
)
23+
24+
create a ()
25+
create b ()
26+
create c ()
27+
28+
read all (
29+
select a b c
30+
31+
join a.id = b.a_id
32+
join b.id = c.b_id
33+
34+
where a.id = ?
35+
where c.lat < ?
36+
where c.lat > ?
37+
where c.lon < ?
38+
where c.lon > ?
39+
)

testdata/run/unique_checking.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package main
2+
3+
import (
4+
"context"
5+
)
6+
7+
func erre(err error) {
8+
if err != nil {
9+
panic(err)
10+
}
11+
}
12+
13+
func assert(x bool) {
14+
if !x {
15+
panic("assertion failed")
16+
}
17+
}
18+
19+
var ctx = context.Background()
20+
21+
func main() {
22+
db, err := Open("sqlite3", ":memory:")
23+
erre(err)
24+
defer db.Close()
25+
26+
_, err = db.Exec(db.Schema())
27+
erre(err)
28+
29+
a, err := db.Create_A(ctx)
30+
erre(err)
31+
32+
b1, err := db.Create_B(ctx, B_AId(a.Id))
33+
erre(err)
34+
c1, err := db.Create_C(ctx, C_Lat(0.0), C_Lon(0.0), C_BId(b1.Id))
35+
erre(err)
36+
37+
b2, err := db.Create_B(ctx, B_AId(a.Id))
38+
erre(err)
39+
c2, err := db.Create_C(ctx, C_Lat(1.0), C_Lon(1.0), C_BId(b2.Id))
40+
erre(err)
41+
42+
rows, err := db.All_A_B_C_By_A_Id_And_C_Lat_Less_And_C_Lat_Greater_And_C_Lon_Less_And_C_Lon_Greater(ctx,
43+
A_Id(a.Id),
44+
C_Lat(10.0), C_Lat(-10.0),
45+
C_Lon(10.0), C_Lon(-10.0))
46+
erre(err)
47+
48+
assert(len(rows) == 2)
49+
50+
assert(rows[0].A.Id == a.Id)
51+
assert(rows[0].B.Id == b1.Id)
52+
assert(rows[0].C.Id == c1.Id)
53+
assert(rows[0].C.Lat == 0)
54+
assert(rows[0].C.Lon == 0)
55+
56+
assert(rows[1].A.Id == a.Id)
57+
assert(rows[1].B.Id == b2.Id)
58+
assert(rows[1].C.Id == c2.Id)
59+
assert(rows[1].C.Lat == 1)
60+
assert(rows[1].C.Lon == 1)
61+
}

0 commit comments

Comments
 (0)