Skip to content

Commit

Permalink
Allow empty alias in referencer (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Apr 28, 2021
1 parent 9cc8116 commit 93d033a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
12 changes: 10 additions & 2 deletions referencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func (r *Referencer) ColumnsOf(rowStructPtr interface{}) func(o *Options) {
}

// AddTableAlias creates string references for row pointer and all suitable field pointers in it.
//
// Empty alias is not added to column reference.
func (r *Referencer) AddTableAlias(rowStructPtr interface{}, alias string) {
f, err := r.Mapper.FindColumnNames(rowStructPtr)
if err != nil {
Expand All @@ -93,10 +95,16 @@ func (r *Referencer) AddTableAlias(rowStructPtr interface{}, alias string) {
r.refs = make(map[interface{}]string, len(f)+1)
}

r.refs[rowStructPtr] = r.Q(alias)
if alias != "" {
r.refs[rowStructPtr] = r.Q(alias)
}

for ptr, fieldName := range f {
r.refs[ptr] = r.Q(alias, fieldName)
if alias == "" {
r.refs[ptr] = r.Q(fieldName)
} else {
r.refs[ptr] = r.Q(alias, fieldName)
}
}
}

Expand Down
23 changes: 17 additions & 6 deletions referencer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ func TestReferencer_Fmt(t *testing.T) {
rf := sqluct.Referencer{}

type User struct {
ID int `db:"id"`
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
ID int `db:"id,omitempty"`
FirstName string `db:"first_name,omitempty"`
LastName string `db:"last_name,omitempty"`
}

type DirectReport struct {
Expand Down Expand Up @@ -42,16 +42,18 @@ func TestReferencer_Fmt(t *testing.T) {
&dr.EmployeeID, &employee.ID)).
Where(rf.Fmt("%s = %s", &manager.LastName, &employee.LastName)).
Where(rf.Fmt("%s != ?", &manager.FirstName), "John").
Where(m.WhereEq(User{FirstName: "Larry", LastName: "Page"}, rf.ColumnsOf(employee), sqluct.SkipZeroValues))
Where(m.WhereEq(User{FirstName: "Larry", LastName: "Page"}, rf.ColumnsOf(employee))).
Where(squirrel.NotEq(m.WhereEq(User{FirstName: "Sergey", LastName: "Brin"}, rf.ColumnsOf("manager"))))

stmt, args, err := qb.ToSql()
assert.NoError(t, err)
assert.Equal(t, `SELECT dr.manager_id, dr.employee_id `+
`FROM users AS manager `+
`INNER JOIN direct_reports AS dr ON dr.manager_id = manager.id AND dr.employee_id = employee.id `+
`WHERE manager.last_name = employee.last_name AND manager.first_name != ? `+
`AND employee.first_name = ? AND employee.last_name = ?`, stmt)
assert.Equal(t, []interface{}{"John", "Larry", "Page"}, args)
`AND employee.first_name = ? AND employee.last_name = ? `+
`AND manager.first_name <> ? AND manager.last_name <> ?`, stmt)
assert.Equal(t, []interface{}{"John", "Larry", "Page", "Sergey", "Brin"}, args)
}

func TestReferencer_Ref(t *testing.T) {
Expand All @@ -62,9 +64,18 @@ func TestReferencer_Ref(t *testing.T) {
ID int `db:"id,omitempty"`
}{}

row2 := &struct {
ID int `db:"id,omitempty"`
}{}

rf.AddTableAlias(row, "some_table")
rf.AddTableAlias(row2, "")
assert.Equal(t, `"some_table"`, rf.Ref(row))
assert.Equal(t, `"some_table"."id"`, rf.Ref(&row.ID))
assert.Panics(t, func() {
rf.Ref(row2)
})
assert.Equal(t, `"id"`, rf.Ref(&row2.ID))
assert.Panics(t, func() {
rf.Ref(nil)
})
Expand Down

0 comments on commit 93d033a

Please sign in to comment.