Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 2a02f01

Browse files
authored
fix: Fix UPDATE query VALUES placeholder (#48)
2 parents 556b09d + 1756b48 commit 2a02f01

File tree

5 files changed

+25
-21
lines changed

5 files changed

+25
-21
lines changed

internal/arcgen/lang/go/dialect.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,40 @@ import (
77
"github.com/kunitsucom/arcgen/internal/config"
88
)
99

10-
func columnValuesPlaceholder(columns []string) string {
10+
//nolint:cyclop
11+
func columnValuesPlaceholder(columns []string, initialNumber int) string {
1112
switch config.Dialect() {
1213
case "mysql", "sqlite3":
1314
// ?, ?, ?, ...
1415
return "?" + strings.Repeat(", ?", len(columns)-1)
1516
case "postgres", "cockroach":
1617
// $1, $2, $3, ...
1718
var s strings.Builder
18-
s.WriteString("$1")
19-
for i := 2; i <= len(columns); i++ {
20-
s.WriteString(", $")
21-
s.WriteString(strconv.Itoa(i))
19+
for i := range columns {
20+
if i > 0 {
21+
s.WriteString(", ")
22+
}
23+
s.WriteString("$" + strconv.Itoa(i+initialNumber))
2224
}
2325
return s.String()
2426
case "spanner":
2527
// @column_1, @column_2, @column_3, ...
2628
var s strings.Builder
27-
s.WriteString("@" + columns[0])
28-
for i := 2; i <= len(columns); i++ {
29-
s.WriteString(", @")
30-
s.WriteString(columns[i-1])
29+
for i := range columns {
30+
if i > 0 {
31+
s.WriteString(", ")
32+
}
33+
s.WriteString("@" + columns[i])
3134
}
3235
return s.String()
3336
case "oracle":
3437
// :column_1, :column_2, :column_3, ...
3538
var s strings.Builder
36-
s.WriteString(":" + columns[0])
37-
for i := 2; i <= len(columns); i++ {
38-
s.WriteString(", :")
39-
s.WriteString(columns[i-1])
39+
for i := range columns {
40+
if i > 0 {
41+
s.WriteString(", ")
42+
}
43+
s.WriteString(":" + columns[i])
4044
}
4145
return s.String()
4246
default:
@@ -46,7 +50,7 @@ func columnValuesPlaceholder(columns []string) string {
4650
}
4751

4852
//nolint:unparam,cyclop
49-
func whereColumnsPlaceholder(columns []string, op string) string {
53+
func whereColumnsPlaceholder(columns []string, op string, initialNumber int) string {
5054
switch config.Dialect() {
5155
case "mysql", "sqlite3":
5256
// column1 = ? AND column2 = ? AND column3 = ...
@@ -60,7 +64,7 @@ func whereColumnsPlaceholder(columns []string, op string) string {
6064
}
6165
s.WriteString(column)
6266
s.WriteString(" = $")
63-
s.WriteString(strconv.Itoa(i + 1))
67+
s.WriteString(strconv.Itoa(i + initialNumber))
6468
}
6569
return s.String()
6670
case "spanner":

internal/arcgen/lang/go/generate_orm_create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func generateCREATEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
3636
Names: []*ast.Ident{{Name: queryName}},
3737
Values: []ast.Expr{&ast.BasicLit{
3838
Kind: token.STRING,
39-
Value: "`INSERT INTO " + tableName + " (" + strings.Join(columnNames, ", ") + ") VALUES (" + columnValuesPlaceholder(columnNames) + ")`",
39+
Value: "`INSERT INTO " + tableName + " (" + strings.Join(columnNames, ", ") + ") VALUES (" + columnValuesPlaceholder(columnNames, 1) + ")`",
4040
}},
4141
},
4242
},

internal/arcgen/lang/go/generate_orm_delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func generateDELETEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
4141
Names: []*ast.Ident{{Name: queryName}},
4242
Values: []ast.Expr{&ast.BasicLit{
4343
Kind: token.STRING,
44-
Value: "`DELETE FROM " + tableName + " WHERE " + whereColumnsPlaceholder(pkColumnNames, "AND") + "`",
44+
Value: "`DELETE FROM " + tableName + " WHERE " + whereColumnsPlaceholder(pkColumnNames, "AND", 1) + "`",
4545
}},
4646
},
4747
},

internal/arcgen/lang/go/generate_orm_read.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
4242
Names: []*ast.Ident{{Name: byPKQueryName}},
4343
Values: []ast.Expr{&ast.BasicLit{
4444
Kind: token.STRING,
45-
Value: "`SELECT " + strings.Join(columnNames, ", ") + " FROM " + tableName + " WHERE " + whereColumnsPlaceholder(pks.ColumnNames(), "AND") + "`",
45+
Value: "`SELECT " + strings.Join(columnNames, ", ") + " FROM " + tableName + " WHERE " + whereColumnsPlaceholder(pks.ColumnNames(), "AND", 1) + "`",
4646
}},
4747
},
4848
},
@@ -189,7 +189,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
189189
Names: []*ast.Ident{{Name: byHasOneTagQueryName}},
190190
Values: []ast.Expr{&ast.BasicLit{
191191
Kind: token.STRING,
192-
Value: "`SELECT " + strings.Join(columnNames, ", ") + " FROM " + tableName + " WHERE " + whereColumnsPlaceholder(hasOneColumns.ColumnNames(), "AND") + "`",
192+
Value: "`SELECT " + strings.Join(columnNames, ", ") + " FROM " + tableName + " WHERE " + whereColumnsPlaceholder(hasOneColumns.ColumnNames(), "AND", 1) + "`",
193193
}},
194194
},
195195
},
@@ -354,7 +354,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
354354
Names: []*ast.Ident{{Name: byHasOneTagQueryName}},
355355
Values: []ast.Expr{&ast.BasicLit{
356356
Kind: token.STRING,
357-
Value: "`SELECT " + strings.Join(columnNames, ", ") + " FROM " + tableName + " WHERE " + whereColumnsPlaceholder(hasManyColumns.ColumnNames(), "AND") + "`",
357+
Value: "`SELECT " + strings.Join(columnNames, ", ") + " FROM " + tableName + " WHERE " + whereColumnsPlaceholder(hasManyColumns.ColumnNames(), "AND", 1) + "`",
358358
}},
359359
},
360360
},

internal/arcgen/lang/go/generate_orm_update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func generateUPDATEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
4242
Names: []*ast.Ident{{Name: queryName}},
4343
Values: []ast.Expr{&ast.BasicLit{
4444
Kind: token.STRING,
45-
Value: "`UPDATE " + tableName + " SET (" + strings.Join(nonPKColumnNames, ", ") + ") = (?" + strings.Repeat(", ?", len(nonPKColumns)-1) + ") WHERE " + whereColumnsPlaceholder(pkColumns.ColumnNames(), "AND") + "`",
45+
Value: "`UPDATE " + tableName + " SET (" + strings.Join(nonPKColumnNames, ", ") + ") = (" + columnValuesPlaceholder(nonPKColumnNames, 1) + ") WHERE " + whereColumnsPlaceholder(pkColumns.ColumnNames(), "AND", len(nonPKColumnNames)+1) + "`",
4646
}},
4747
},
4848
},

0 commit comments

Comments
 (0)