Skip to content

Commit fb2186e

Browse files
committed
refs huandu#40. add table name as col prefix in Struct#SelectFrom/SelectFromTag
1 parent 872ca24 commit fb2186e

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

cond_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestCond(t *testing.T) {
3131
"$$a NOT BETWEEN $0 AND $1": func() string { return newTestCond().NotBetween("$a", 123, 456) },
3232
"(1 = 1 OR 2 = 2 OR 3 = 3)": func() string { return newTestCond().Or("1 = 1", "2 = 2", "3 = 3") },
3333
"(1 = 1 AND 2 = 2 AND 3 = 3)": func() string { return newTestCond().And("1 = 1", "2 = 2", "3 = 3") },
34-
"$0": func() string { return newTestCond().Var(123) },
34+
"$0": func() string { return newTestCond().Var(123) },
3535
}
3636

3737
for expected, f := range cases {

struct.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package sqlbuilder
55

66
import (
7+
"bytes"
78
"reflect"
89
"strings"
910
)
@@ -147,7 +148,19 @@ func (s *Struct) SelectFromForTag(table string, tag string) *SelectBuilder {
147148

148149
if ok {
149150
fields = s.quoteFields(fields)
150-
sb.Select(EscapeAll(fields...)...)
151+
152+
buf := &bytes.Buffer{}
153+
cols := make([]string, 0, len(fields))
154+
155+
for _, field := range fields {
156+
buf.WriteString(table)
157+
buf.WriteRune('.')
158+
buf.WriteString(field)
159+
cols = append(cols, buf.String())
160+
buf.Reset()
161+
}
162+
163+
sb.Select(cols...)
151164
} else {
152165
sb.Select("*")
153166
}

struct_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestStructSelectFrom(t *testing.T) {
2323
sb := userForTest.SelectFrom("user")
2424
sql, args := sb.Build()
2525

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

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

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

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

107107
testInsert := map[*InsertBuilder]string{
108-
userForTest.InsertInto("user", user): "INSERT ",
108+
userForTest.InsertInto("user", user): "INSERT ",
109109
userForTest.InsertIgnoreInto("user", user): "INSERT IGNORE ",
110-
userForTest.ReplaceInto("user", user): "REPLACE ",
110+
userForTest.ReplaceInto("user", user): "REPLACE ",
111111
}
112112

113113
testMulitInsert := map[*InsertBuilder]string{
114-
userForTest.InsertInto("user", users...): "INSERT ",
114+
userForTest.InsertInto("user", users...): "INSERT ",
115115
userForTest.InsertIgnoreInto("user", users...): "INSERT IGNORE ",
116-
userForTest.ReplaceInto("user", users...): "REPLACE ",
116+
userForTest.ReplaceInto("user", users...): "REPLACE ",
117117
}
118118

119-
for ib, exceptedVerb := range testInsert{
119+
for ib, exceptedVerb := range testInsert {
120120
sql, args := ib.Build()
121121

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

131-
for ib, exceptedVerb := range testMulitInsert{
131+
for ib, exceptedVerb := range testMulitInsert {
132132
sql, args := ib.Build()
133133

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

166166
testInsertForTag := map[*InsertBuilder]string{
167-
userForTest.InsertIntoForTag("user","important", user): "INSERT ",
168-
userForTest.InsertIgnoreIntoForTag("user","important", user): "INSERT IGNORE ",
169-
userForTest.ReplaceIntoForTag("user","important", user): "REPLACE ",
167+
userForTest.InsertIntoForTag("user", "important", user): "INSERT ",
168+
userForTest.InsertIgnoreIntoForTag("user", "important", user): "INSERT IGNORE ",
169+
userForTest.ReplaceIntoForTag("user", "important", user): "REPLACE ",
170170
}
171171

172172
testMulitInsertForTag := map[*InsertBuilder]string{
173-
userForTest.InsertIntoForTag("user","important", users...): "INSERT ",
174-
userForTest.InsertIgnoreIntoForTag("user","important", users...): "INSERT IGNORE ",
175-
userForTest.ReplaceIntoForTag("user","important", users...): "REPLACE ",
173+
userForTest.InsertIntoForTag("user", "important", users...): "INSERT ",
174+
userForTest.InsertIgnoreIntoForTag("user", "important", users...): "INSERT IGNORE ",
175+
userForTest.ReplaceIntoForTag("user", "important", users...): "REPLACE ",
176176
}
177177

178-
for ib, exceptedVerb := range testInsertForTag{
178+
for ib, exceptedVerb := range testInsertForTag {
179179
sql, args := ib.Build()
180180

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

190-
for ib, exceptedVerb := range testMulitInsertForTag{
190+
for ib, exceptedVerb := range testMulitInsertForTag {
191191
sql, args := ib.Build()
192192

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

344344
// Output:
345-
// SELECT id, name, status FROM user WHERE id = ?
345+
// SELECT user.id, user.name, user.status FROM user WHERE id = ?
346346
// [1234]
347347
// sqlbuilder.User{ID:1234, Name:"huandu", Status:1}
348348
}
@@ -552,7 +552,7 @@ func ExampleStruct_forPostgreSQL() {
552552
fmt.Println(args)
553553

554554
// Output:
555-
// SELECT id, name, status FROM user WHERE id = $1
555+
// SELECT user.id, user.name, user.status FROM user WHERE id = $1
556556
// [1234]
557557
}
558558

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

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

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

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

0 commit comments

Comments
 (0)