Skip to content

Commit 005f47c

Browse files
authored
Fix JoinAssoc field escape and add spec (#36)
* Fix JoinAssoc field escape and add spec * Update query.go * fix dep * add wait for replication * support older testify * Downgrade testify to v1.7.1 * Bump rel
1 parent c14c7e3 commit 005f47c

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

builder/buffer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (b Buffer) escape(table, value string) string {
146146
} else if _, err := strconv.Atoi(value); err == nil {
147147
escapedValue = value
148148
} else if i := strings.Index(strings.ToLower(value), " as "); i > -1 {
149-
escapedValue = b.escape(table, value[:i]) + " AS " + b.escape("", value[i+4:])
149+
escapedValue = b.escape(table, value[:i]) + " AS " + b.Quoter.ID(value[i+4:])
150150
} else if start, end := strings.IndexRune(value, '('), strings.IndexRune(value, ')'); start >= 0 && end >= 0 && end > start {
151151
escapedValue = value[:start+1] + b.escape(table, value[start+1:end]) + value[end:]
152152
} else {

builder/buffer_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ func TestBuffer_escape(t *testing.T) {
6262
field: "person.address as home_address",
6363
result: "[person].[address] AS [home_address]",
6464
},
65+
{
66+
table: "user",
67+
field: "person.address as person.address",
68+
result: "[person].[address] AS [person.address]",
69+
},
6570
}
6671
for _, test := range tests {
6772
t.Run(test.result, func(t *testing.T) {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module github.com/go-rel/sql
33
go 1.16
44

55
require (
6-
github.com/go-rel/rel v0.36.0
6+
github.com/go-rel/rel v0.37.0
77
github.com/stretchr/testify v1.8.0
88
)

go.sum

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
44
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
55
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
66
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
7-
github.com/go-rel/rel v0.36.0 h1:GgKB+0btoZVQ+6YBboPJvRksNtbMJOTv+iMO79t+QMo=
8-
github.com/go-rel/rel v0.36.0/go.mod h1:SHjO9VOInj8GD2cTVWmTnOS+rj5IJZoDy8LEyd5oDUQ=
7+
github.com/go-rel/rel v0.37.0 h1:9Ca1u0E6tu3hZGbzgpk6VxQI8b50JV7AeTi+v5Q/1jU=
8+
github.com/go-rel/rel v0.37.0/go.mod h1:Zq18pQqXZbDh2JBCo29jgt+y90nZWkUvI+W9Ls29ans=
99
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1010
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
1111
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
@@ -37,7 +37,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
3737
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
3838
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
3939
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
40-
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
4140
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
4241
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
4342
github.com/subosito/gotenv v1.4.0/go.mod h1:mZd6rFysKEcUhUHXJk0C/08wAgyDBFuwEYL7vWWGaGo=

specs/query.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,30 @@ func QueryJoin(t *testing.T, repo rel.Repository) {
8484
run(t, repo, tests)
8585
}
8686

87+
// QueryJoinAssoc tests query specifications with join.
88+
func QueryJoinAssoc(t *testing.T, repo rel.Repository) {
89+
dbUser := User{Addresses: []Address{{}}}
90+
repo.MustInsert(ctx, &dbUser)
91+
92+
waitForReplication()
93+
94+
t.Run("HasMany", func(t *testing.T) {
95+
assert.Nil(t, repo.Find(ctx, &User{}, rel.JoinAssoc("addresses").Where(where.Eq("id", dbUser.ID))))
96+
})
97+
98+
t.Run("HasOne", func(t *testing.T) {
99+
var user User
100+
assert.Nil(t, repo.Find(ctx, &user, rel.Select("*", "primary_address.*").JoinAssoc("primary_address").Where(where.Eq("id", dbUser.ID))))
101+
assert.Equal(t, dbUser.Addresses[0].ID, user.PrimaryAddress.ID)
102+
})
103+
104+
t.Run("BelongsTo", func(t *testing.T) {
105+
var address Address
106+
assert.Nil(t, repo.Find(ctx, &address, rel.Select("*", "user.*").JoinAssoc("user").Where(where.Eq("id", dbUser.Addresses[0].ID))))
107+
assert.Equal(t, dbUser.ID, address.User.ID)
108+
})
109+
}
110+
87111
// Query tests query specifications without join.
88112
func QueryWhereSubQuery(t *testing.T, repo rel.Repository, flags ...Flag) {
89113
tests := []rel.Querier{

0 commit comments

Comments
 (0)