Skip to content

Commit 858081e

Browse files
committed
fix(sqlbuilder): 正确处理 where 语句中包含表名的问题
1 parent b31a909 commit 858081e

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

core/builder.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
package core
66

7-
import "github.com/issue9/errwrap"
7+
import (
8+
"strings"
9+
10+
"github.com/issue9/errwrap"
11+
)
812

913
// 作用于表名,列名等非关键字上的引号占位符。
1014
// 在执行会自动替换成该数据库相应的符号。
@@ -56,6 +60,16 @@ func (b *Builder) Quote(str string, l, r byte) *Builder { return b.WBytes(l).WSt
5660
// QuoteKey 给 str 左右添加 [QuoteLeft] 和 [QuoteRight] 两个字符
5761
func (b *Builder) QuoteKey(str string) *Builder { return b.Quote(str, QuoteLeft, QuoteRight) }
5862

63+
// QuoteColumn 为列名添加 [QuoteLeft] 和 [QuoteRight] 两个字符
64+
//
65+
// NOTE: 列名可能包含表名或是表名别名:table.col
66+
func (b *Builder) QuoteColumn(col string) *Builder {
67+
if index := strings.IndexByte(col, '.'); index > 0 {
68+
return b.QuoteKey(col[:index]).WBytes('.').QuoteKey(col[index+1:])
69+
}
70+
return b.Quote(col, QuoteLeft, QuoteRight)
71+
}
72+
5973
// Reset 重置内容,同时也会将 err 设置为 nil
6074
func (b *Builder) Reset() *Builder {
6175
b.buffer.Reset()

sqlbuilder/where.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,91 +135,91 @@ func (stmt *WhereStmt) Or(cond string, args ...any) *WhereStmt {
135135
// AndIsNull 指定 WHERE ... AND col IS NULL
136136
func (stmt *WhereStmt) AndIsNull(col string) *WhereStmt {
137137
stmt.writeAnd(true)
138-
stmt.builder.QuoteKey(col).WString(" IS NULL ")
138+
stmt.builder.QuoteColumn(col).WString(" IS NULL ")
139139
return stmt
140140
}
141141

142142
// OrIsNull 指定 WHERE ... OR col IS NULL
143143
func (stmt *WhereStmt) OrIsNull(col string) *WhereStmt {
144144
stmt.writeAnd(false)
145-
stmt.builder.QuoteKey(col).WString(" IS NULL ")
145+
stmt.builder.QuoteColumn(col).WString(" IS NULL ")
146146
return stmt
147147
}
148148

149149
// AndIsNotNull 指定 WHERE ... AND col IS NOT NULL
150150
func (stmt *WhereStmt) AndIsNotNull(col string) *WhereStmt {
151151
stmt.writeAnd(true)
152-
stmt.builder.QuoteKey(col).WString(" IS NOT NULL ")
152+
stmt.builder.QuoteColumn(col).WString(" IS NOT NULL ")
153153
return stmt
154154
}
155155

156156
// OrIsNotNull 指定 WHERE ... OR col IS NOT NULL
157157
func (stmt *WhereStmt) OrIsNotNull(col string) *WhereStmt {
158158
stmt.writeAnd(false)
159-
stmt.builder.QuoteKey(col).WString(" IS NOT NULL ")
159+
stmt.builder.QuoteColumn(col).WString(" IS NOT NULL ")
160160
return stmt
161161
}
162162

163163
// AndBetween 指定 WHERE ... AND col BETWEEN v1 AND v2
164164
func (stmt *WhereStmt) AndBetween(col string, v1, v2 any) *WhereStmt {
165165
stmt.writeAnd(true)
166-
stmt.builder.QuoteKey(col).WString(" BETWEEN ? AND ? ")
166+
stmt.builder.QuoteColumn(col).WString(" BETWEEN ? AND ? ")
167167
stmt.args = append(stmt.args, v1, v2)
168168
return stmt
169169
}
170170

171171
// OrBetween 指定 WHERE ... OR col BETWEEN v1 AND v2
172172
func (stmt *WhereStmt) OrBetween(col string, v1, v2 any) *WhereStmt {
173173
stmt.writeAnd(false)
174-
stmt.builder.QuoteKey(col).WString(" BETWEEN ? AND ? ")
174+
stmt.builder.QuoteColumn(col).WString(" BETWEEN ? AND ? ")
175175
stmt.args = append(stmt.args, v1, v2)
176176
return stmt
177177
}
178178

179179
// AndNotBetween 指定 WHERE ... AND col NOT BETWEEN v1 AND v2
180180
func (stmt *WhereStmt) AndNotBetween(col string, v1, v2 any) *WhereStmt {
181181
stmt.writeAnd(true)
182-
stmt.builder.QuoteKey(col).WString(" NOT BETWEEN ? AND ? ")
182+
stmt.builder.QuoteColumn(col).WString(" NOT BETWEEN ? AND ? ")
183183
stmt.args = append(stmt.args, v1, v2)
184184
return stmt
185185
}
186186

187187
// OrNotBetween 指定 WHERE ... OR col BETWEEN v1 AND v2
188188
func (stmt *WhereStmt) OrNotBetween(col string, v1, v2 any) *WhereStmt {
189189
stmt.writeAnd(false)
190-
stmt.builder.QuoteKey(col).WString(" NOT BETWEEN ? AND ? ")
190+
stmt.builder.QuoteColumn(col).WString(" NOT BETWEEN ? AND ? ")
191191
stmt.args = append(stmt.args, v1, v2)
192192
return stmt
193193
}
194194

195195
// AndLike 指定 WHERE ... AND col LIKE content
196196
func (stmt *WhereStmt) AndLike(col string, content any) *WhereStmt {
197197
stmt.writeAnd(true)
198-
stmt.builder.QuoteKey(col).WString(" LIKE ?")
198+
stmt.builder.QuoteColumn(col).WString(" LIKE ?")
199199
stmt.args = append(stmt.args, content)
200200
return stmt
201201
}
202202

203203
// OrLike 指定 WHERE ... OR col LIKE content
204204
func (stmt *WhereStmt) OrLike(col string, content any) *WhereStmt {
205205
stmt.writeAnd(false)
206-
stmt.builder.QuoteKey(col).WString(" LIKE ?")
206+
stmt.builder.QuoteColumn(col).WString(" LIKE ?")
207207
stmt.args = append(stmt.args, content)
208208
return stmt
209209
}
210210

211211
// AndNotLike 指定 WHERE ... AND col NOT LIKE content
212212
func (stmt *WhereStmt) AndNotLike(col string, content any) *WhereStmt {
213213
stmt.writeAnd(true)
214-
stmt.builder.QuoteKey(col).WString(" NOT LIKE ?")
214+
stmt.builder.QuoteColumn(col).WString(" NOT LIKE ?")
215215
stmt.args = append(stmt.args, content)
216216
return stmt
217217
}
218218

219219
// OrNotLike 指定 WHERE ... OR col NOT LIKE content
220220
func (stmt *WhereStmt) OrNotLike(col string, content any) *WhereStmt {
221221
stmt.writeAnd(false)
222-
stmt.builder.QuoteKey(col).WString(" NOT LIKE ?")
222+
stmt.builder.QuoteColumn(col).WString(" NOT LIKE ?")
223223
stmt.args = append(stmt.args, content)
224224
return stmt
225225
}
@@ -250,7 +250,7 @@ func (stmt *WhereStmt) in(and, not bool, col string, v ...any) *WhereStmt {
250250
}
251251

252252
stmt.writeAnd(and)
253-
stmt.builder.QuoteKey(col)
253+
stmt.builder.QuoteColumn(col)
254254

255255
if not {
256256
stmt.builder.WString(" NOT")

sqlbuilder/where_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ func TestWhereStmt_IsNull(t *testing.T) {
7171
a.NotError(err).Empty(args)
7272
sqltest.Equal(a, query, "{col1} is null")
7373

74-
w.OrIsNull("col2")
74+
w.OrIsNull("tbl.col2")
7575
query, args, err = w.SQL()
7676
a.NotError(err).Empty(args)
77-
sqltest.Equal(a, query, "{col1} is null or {col2} is null")
77+
sqltest.Equal(a, query, "{col1} is null or {tbl}.{col2} is null")
7878

7979
w.Reset()
8080
w.AndIsNotNull("col1")

0 commit comments

Comments
 (0)