Skip to content

Commit 2f878ca

Browse files
committed
fix: also quote reserved identifiers for insert / update
1 parent f51eb0d commit 2f878ca

4 files changed

Lines changed: 44 additions & 4 deletions

File tree

builder/insert_builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func (b InsertBuilder) innerWriteSQL(sb *SQLBuilder) {
278278
sb.WriteRune(' ')
279279
}
280280
}
281-
sb.WriteString(columnName)
281+
sb.WriteString(quoteIdentifierIfKeyword(columnName))
282282
}
283283
sb.WriteString(")")
284284
}
@@ -362,7 +362,7 @@ func (b InsertBuilder) innerWriteSQL(sb *SQLBuilder) {
362362
if i > 0 {
363363
sb.WriteString(",")
364364
}
365-
sb.WriteString(item.columnName)
365+
sb.WriteString(quoteIdentifierIfKeyword(item.columnName))
366366
sb.WriteString(" = ")
367367
item.value.WriteSQL(sb)
368368
}

builder/update_builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (b UpdateBuilder) innerWriteSQL(sb *SQLBuilder) {
175175
if i > 0 {
176176
sb.WriteString(",")
177177
}
178-
sb.WriteString(setItem.columnName)
178+
sb.WriteString(quoteIdentifierIfKeyword(setItem.columnName))
179179
sb.WriteString(" = ")
180180
setItem.value.WriteSQL(sb)
181181
}

insert_builder_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,27 @@ WHERE d.zipcode <> '21201'`,
340340
)
341341
})
342342

343+
t.Run("set map with reserved keywords", func(t *testing.T) {
344+
q := qrb.
345+
InsertInto(qrb.N("events")).
346+
SetMap(map[string]any{
347+
"event_id": "123",
348+
"from": "2021-01-01",
349+
"to": "2021-01-02",
350+
"user": "john",
351+
})
352+
353+
testhelper.AssertSQLWriterEquals(
354+
t,
355+
`
356+
INSERT INTO events (event_id,"from","to","user") VALUES
357+
($1, $2, $3, $4)
358+
`,
359+
[]any{"123", "2021-01-01", "2021-01-02", "john"},
360+
q,
361+
)
362+
})
363+
343364
t.Run("values with args", func(t *testing.T) {
344365
q := qrb.
345366
InsertInto(qrb.N("films")).

update_builder_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,32 @@ func TestUpdateBuilder(t *testing.T) {
130130
testhelper.AssertSQLWriterEquals(
131131
t,
132132
`
133-
UPDATE films SET code = $1, kind = $2 WHERE kind = 'Drama'
133+
UPDATE films SET code = $1, kind = $2 WHERE kind = 'Drama'
134134
`,
135135
[]any{"UA502", "Comedy"},
136136
q,
137137
)
138138
})
139139

140+
t.Run("set map with reserved keywords", func(t *testing.T) {
141+
q := qrb.
142+
Update(qrb.N("events")).
143+
SetMap(map[string]any{
144+
"from": "2021-01-01",
145+
"to": "2021-01-02",
146+
}).
147+
Where(qrb.N("event_id").Eq(qrb.String("123")))
148+
149+
testhelper.AssertSQLWriterEquals(
150+
t,
151+
`
152+
UPDATE events SET "from" = $1, "to" = $2 WHERE event_id = '123'
153+
`,
154+
[]any{"2021-01-01", "2021-01-02"},
155+
q,
156+
)
157+
})
158+
140159
t.Run("apply if", func(t *testing.T) {
141160
q := qrb.
142161
Update(qrb.N("films")).

0 commit comments

Comments
 (0)