Skip to content

Commit

Permalink
fix: Fix UPDATE query VALUES placeholder (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent authored Aug 12, 2024
2 parents 556b09d + 1756b48 commit 2a02f01
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
34 changes: 19 additions & 15 deletions internal/arcgen/lang/go/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,40 @@ import (
"github.com/kunitsucom/arcgen/internal/config"
)

func columnValuesPlaceholder(columns []string) string {
//nolint:cyclop
func columnValuesPlaceholder(columns []string, initialNumber int) string {
switch config.Dialect() {
case "mysql", "sqlite3":
// ?, ?, ?, ...
return "?" + strings.Repeat(", ?", len(columns)-1)
case "postgres", "cockroach":
// $1, $2, $3, ...
var s strings.Builder
s.WriteString("$1")
for i := 2; i <= len(columns); i++ {
s.WriteString(", $")
s.WriteString(strconv.Itoa(i))
for i := range columns {
if i > 0 {
s.WriteString(", ")
}
s.WriteString("$" + strconv.Itoa(i+initialNumber))
}
return s.String()
case "spanner":
// @column_1, @column_2, @column_3, ...
var s strings.Builder
s.WriteString("@" + columns[0])
for i := 2; i <= len(columns); i++ {
s.WriteString(", @")
s.WriteString(columns[i-1])
for i := range columns {
if i > 0 {
s.WriteString(", ")
}
s.WriteString("@" + columns[i])
}
return s.String()
case "oracle":
// :column_1, :column_2, :column_3, ...
var s strings.Builder
s.WriteString(":" + columns[0])
for i := 2; i <= len(columns); i++ {
s.WriteString(", :")
s.WriteString(columns[i-1])
for i := range columns {
if i > 0 {
s.WriteString(", ")
}
s.WriteString(":" + columns[i])
}
return s.String()
default:
Expand All @@ -46,7 +50,7 @@ func columnValuesPlaceholder(columns []string) string {
}

//nolint:unparam,cyclop
func whereColumnsPlaceholder(columns []string, op string) string {
func whereColumnsPlaceholder(columns []string, op string, initialNumber int) string {
switch config.Dialect() {
case "mysql", "sqlite3":
// column1 = ? AND column2 = ? AND column3 = ...
Expand All @@ -60,7 +64,7 @@ func whereColumnsPlaceholder(columns []string, op string) string {
}
s.WriteString(column)
s.WriteString(" = $")
s.WriteString(strconv.Itoa(i + 1))
s.WriteString(strconv.Itoa(i + initialNumber))
}
return s.String()
case "spanner":
Expand Down
2 changes: 1 addition & 1 deletion internal/arcgen/lang/go/generate_orm_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func generateCREATEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
Names: []*ast.Ident{{Name: queryName}},
Values: []ast.Expr{&ast.BasicLit{
Kind: token.STRING,
Value: "`INSERT INTO " + tableName + " (" + strings.Join(columnNames, ", ") + ") VALUES (" + columnValuesPlaceholder(columnNames) + ")`",
Value: "`INSERT INTO " + tableName + " (" + strings.Join(columnNames, ", ") + ") VALUES (" + columnValuesPlaceholder(columnNames, 1) + ")`",
}},
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/arcgen/lang/go/generate_orm_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func generateDELETEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
Names: []*ast.Ident{{Name: queryName}},
Values: []ast.Expr{&ast.BasicLit{
Kind: token.STRING,
Value: "`DELETE FROM " + tableName + " WHERE " + whereColumnsPlaceholder(pkColumnNames, "AND") + "`",
Value: "`DELETE FROM " + tableName + " WHERE " + whereColumnsPlaceholder(pkColumnNames, "AND", 1) + "`",
}},
},
},
Expand Down
6 changes: 3 additions & 3 deletions internal/arcgen/lang/go/generate_orm_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
Names: []*ast.Ident{{Name: byPKQueryName}},
Values: []ast.Expr{&ast.BasicLit{
Kind: token.STRING,
Value: "`SELECT " + strings.Join(columnNames, ", ") + " FROM " + tableName + " WHERE " + whereColumnsPlaceholder(pks.ColumnNames(), "AND") + "`",
Value: "`SELECT " + strings.Join(columnNames, ", ") + " FROM " + tableName + " WHERE " + whereColumnsPlaceholder(pks.ColumnNames(), "AND", 1) + "`",
}},
},
},
Expand Down Expand Up @@ -189,7 +189,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
Names: []*ast.Ident{{Name: byHasOneTagQueryName}},
Values: []ast.Expr{&ast.BasicLit{
Kind: token.STRING,
Value: "`SELECT " + strings.Join(columnNames, ", ") + " FROM " + tableName + " WHERE " + whereColumnsPlaceholder(hasOneColumns.ColumnNames(), "AND") + "`",
Value: "`SELECT " + strings.Join(columnNames, ", ") + " FROM " + tableName + " WHERE " + whereColumnsPlaceholder(hasOneColumns.ColumnNames(), "AND", 1) + "`",
}},
},
},
Expand Down Expand Up @@ -354,7 +354,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
Names: []*ast.Ident{{Name: byHasOneTagQueryName}},
Values: []ast.Expr{&ast.BasicLit{
Kind: token.STRING,
Value: "`SELECT " + strings.Join(columnNames, ", ") + " FROM " + tableName + " WHERE " + whereColumnsPlaceholder(hasManyColumns.ColumnNames(), "AND") + "`",
Value: "`SELECT " + strings.Join(columnNames, ", ") + " FROM " + tableName + " WHERE " + whereColumnsPlaceholder(hasManyColumns.ColumnNames(), "AND", 1) + "`",
}},
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/arcgen/lang/go/generate_orm_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func generateUPDATEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
Names: []*ast.Ident{{Name: queryName}},
Values: []ast.Expr{&ast.BasicLit{
Kind: token.STRING,
Value: "`UPDATE " + tableName + " SET (" + strings.Join(nonPKColumnNames, ", ") + ") = (?" + strings.Repeat(", ?", len(nonPKColumns)-1) + ") WHERE " + whereColumnsPlaceholder(pkColumns.ColumnNames(), "AND") + "`",
Value: "`UPDATE " + tableName + " SET (" + strings.Join(nonPKColumnNames, ", ") + ") = (" + columnValuesPlaceholder(nonPKColumnNames, 1) + ") WHERE " + whereColumnsPlaceholder(pkColumns.ColumnNames(), "AND", len(nonPKColumnNames)+1) + "`",
}},
},
},
Expand Down

0 comments on commit 2a02f01

Please sign in to comment.