From 93d033a1110a70235bff938ae392385dd6a4fcae Mon Sep 17 00:00:00 2001 From: Viacheslav Poturaev Date: Wed, 28 Apr 2021 22:27:01 +0200 Subject: [PATCH] Allow empty alias in referencer (#7) --- referencer.go | 12 ++++++++++-- referencer_test.go | 23 +++++++++++++++++------ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/referencer.go b/referencer.go index f586ae5..f214afa 100644 --- a/referencer.go +++ b/referencer.go @@ -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 { @@ -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) + } } } diff --git a/referencer_test.go b/referencer_test.go index 1c2d328..00d3799 100644 --- a/referencer_test.go +++ b/referencer_test.go @@ -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 { @@ -42,7 +42,8 @@ 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) @@ -50,8 +51,9 @@ func TestReferencer_Fmt(t *testing.T) { `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) { @@ -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) })