Skip to content

Commit

Permalink
Merge pull request #41 from huandu/feature-struct-SelectFrom-add-tabl…
Browse files Browse the repository at this point in the history
…e-name-as-col-prefix

fix #40. add table name as col prefix in Struct#SelectFrom/SelectFromTag
  • Loading branch information
huandu authored Nov 21, 2019
2 parents 872ca24 + fb2186e commit ccb767d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cond_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestCond(t *testing.T) {
"$$a NOT BETWEEN $0 AND $1": func() string { return newTestCond().NotBetween("$a", 123, 456) },
"(1 = 1 OR 2 = 2 OR 3 = 3)": func() string { return newTestCond().Or("1 = 1", "2 = 2", "3 = 3") },
"(1 = 1 AND 2 = 2 AND 3 = 3)": func() string { return newTestCond().And("1 = 1", "2 = 2", "3 = 3") },
"$0": func() string { return newTestCond().Var(123) },
"$0": func() string { return newTestCond().Var(123) },
}

for expected, f := range cases {
Expand Down
15 changes: 14 additions & 1 deletion struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package sqlbuilder

import (
"bytes"
"reflect"
"strings"
)
Expand Down Expand Up @@ -147,7 +148,19 @@ func (s *Struct) SelectFromForTag(table string, tag string) *SelectBuilder {

if ok {
fields = s.quoteFields(fields)
sb.Select(EscapeAll(fields...)...)

buf := &bytes.Buffer{}
cols := make([]string, 0, len(fields))

for _, field := range fields {
buf.WriteString(table)
buf.WriteRune('.')
buf.WriteString(field)
cols = append(cols, buf.String())
buf.Reset()
}

sb.Select(cols...)
} else {
sb.Select("*")
}
Expand Down
40 changes: 20 additions & 20 deletions struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestStructSelectFrom(t *testing.T) {
sb := userForTest.SelectFrom("user")
sql, args := sb.Build()

if expected := "SELECT id, Name, status, created_at FROM user"; expected != sql {
if expected := "SELECT user.id, user.Name, user.status, user.created_at FROM user"; expected != sql {
t.Fatalf("invalid SQL. [expected:%v] [actual:%v]", expected, sql)
}

Expand All @@ -36,7 +36,7 @@ func TestStructSelectFromForTag(t *testing.T) {
sb := userForTest.SelectFromForTag("user", "important")
sql, args := sb.Build()

if expected := "SELECT id, Name, status FROM user"; expected != sql {
if expected := "SELECT user.id, user.Name, user.status FROM user"; expected != sql {
t.Fatalf("invalid SQL. [expected:%v] [actual:%v]", expected, sql)
}

Expand Down Expand Up @@ -105,18 +105,18 @@ func TestStructInsertInto(t *testing.T) {
users := []interface{}{user, user2, &fakeUser}

testInsert := map[*InsertBuilder]string{
userForTest.InsertInto("user", user): "INSERT ",
userForTest.InsertInto("user", user): "INSERT ",
userForTest.InsertIgnoreInto("user", user): "INSERT IGNORE ",
userForTest.ReplaceInto("user", user): "REPLACE ",
userForTest.ReplaceInto("user", user): "REPLACE ",
}

testMulitInsert := map[*InsertBuilder]string{
userForTest.InsertInto("user", users...): "INSERT ",
userForTest.InsertInto("user", users...): "INSERT ",
userForTest.InsertIgnoreInto("user", users...): "INSERT IGNORE ",
userForTest.ReplaceInto("user", users...): "REPLACE ",
userForTest.ReplaceInto("user", users...): "REPLACE ",
}

for ib, exceptedVerb := range testInsert{
for ib, exceptedVerb := range testInsert {
sql, args := ib.Build()

if expected := exceptedVerb + "INTO user (id, Name, status, created_at) VALUES (?, ?, ?, ?)"; expected != sql {
Expand All @@ -128,7 +128,7 @@ func TestStructInsertInto(t *testing.T) {
}
}

for ib, exceptedVerb := range testMulitInsert{
for ib, exceptedVerb := range testMulitInsert {
sql, args := ib.Build()

if expected := exceptedVerb + "INTO user (id, Name, status, created_at) VALUES (?, ?, ?, ?), (?, ?, ?, ?)"; expected != sql {
Expand Down Expand Up @@ -164,18 +164,18 @@ func TestStructInsertIntoForTag(t *testing.T) {
users := []interface{}{user, user2, &fakeUser}

testInsertForTag := map[*InsertBuilder]string{
userForTest.InsertIntoForTag("user","important", user): "INSERT ",
userForTest.InsertIgnoreIntoForTag("user","important", user): "INSERT IGNORE ",
userForTest.ReplaceIntoForTag("user","important", user): "REPLACE ",
userForTest.InsertIntoForTag("user", "important", user): "INSERT ",
userForTest.InsertIgnoreIntoForTag("user", "important", user): "INSERT IGNORE ",
userForTest.ReplaceIntoForTag("user", "important", user): "REPLACE ",
}

testMulitInsertForTag := map[*InsertBuilder]string{
userForTest.InsertIntoForTag("user","important", users...): "INSERT ",
userForTest.InsertIgnoreIntoForTag("user","important", users...): "INSERT IGNORE ",
userForTest.ReplaceIntoForTag("user","important", users...): "REPLACE ",
userForTest.InsertIntoForTag("user", "important", users...): "INSERT ",
userForTest.InsertIgnoreIntoForTag("user", "important", users...): "INSERT IGNORE ",
userForTest.ReplaceIntoForTag("user", "important", users...): "REPLACE ",
}

for ib, exceptedVerb := range testInsertForTag{
for ib, exceptedVerb := range testInsertForTag {
sql, args := ib.Build()

if expected := exceptedVerb + "INTO user (id, Name, status) VALUES (?, ?, ?)"; expected != sql {
Expand All @@ -187,7 +187,7 @@ func TestStructInsertIntoForTag(t *testing.T) {
}
}

for ib, exceptedVerb := range testMulitInsertForTag{
for ib, exceptedVerb := range testMulitInsertForTag {
sql, args := ib.Build()

if expected := exceptedVerb + "INTO user (id, Name, status) VALUES (?, ?, ?), (?, ?, ?)"; expected != sql {
Expand Down Expand Up @@ -342,7 +342,7 @@ func ExampleStruct_useStructAsORM() {
fmt.Printf("%#v", user)

// Output:
// SELECT id, name, status FROM user WHERE id = ?
// SELECT user.id, user.name, user.status FROM user WHERE id = ?
// [1234]
// sqlbuilder.User{ID:1234, Name:"huandu", Status:1}
}
Expand Down Expand Up @@ -552,7 +552,7 @@ func ExampleStruct_forPostgreSQL() {
fmt.Println(args)

// Output:
// SELECT id, name, status FROM user WHERE id = $1
// SELECT user.id, user.name, user.status FROM user WHERE id = $1
// [1234]
}

Expand All @@ -566,14 +566,14 @@ func TestStructWithQuote(t *testing.T) {
sb := NewStruct(new(structWithQuote)).For(MySQL).SelectFrom("foo")
sql, _ := sb.Build()

if expected := "SELECT `aa`, ccc FROM foo"; sql != expected {
if expected := "SELECT foo.`aa`, foo.ccc FROM foo"; sql != expected {
t.Fatalf("invalid sql. [expected:%v] [actual:%v]", expected, sql)
}

sb = NewStruct(new(structWithQuote)).For(PostgreSQL).SelectFrom("foo")
sql, _ = sb.Build()

if expected := `SELECT "aa", ccc FROM foo`; sql != expected {
if expected := `SELECT foo."aa", foo.ccc FROM foo`; sql != expected {
t.Fatalf("invalid sql. [expected:%v] [actual:%v]", expected, sql)
}

Expand Down

0 comments on commit ccb767d

Please sign in to comment.