diff --git a/internal/arcgen/lang/go/dialect.go b/internal/arcgen/lang/go/dialect.go index b017e14..cd19c63 100644 --- a/internal/arcgen/lang/go/dialect.go +++ b/internal/arcgen/lang/go/dialect.go @@ -7,7 +7,8 @@ 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": // ?, ?, ?, ... @@ -15,28 +16,31 @@ func columnValuesPlaceholder(columns []string) string { 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: @@ -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 = ... @@ -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": diff --git a/internal/arcgen/lang/go/generate_orm_create.go b/internal/arcgen/lang/go/generate_orm_create.go index 483e937..7ba54ad 100644 --- a/internal/arcgen/lang/go/generate_orm_create.go +++ b/internal/arcgen/lang/go/generate_orm_create.go @@ -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) + ")`", }}, }, }, diff --git a/internal/arcgen/lang/go/generate_orm_delete.go b/internal/arcgen/lang/go/generate_orm_delete.go index cbb527c..41c9987 100644 --- a/internal/arcgen/lang/go/generate_orm_delete.go +++ b/internal/arcgen/lang/go/generate_orm_delete.go @@ -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) + "`", }}, }, }, diff --git a/internal/arcgen/lang/go/generate_orm_read.go b/internal/arcgen/lang/go/generate_orm_read.go index cb8c8d7..380a209 100644 --- a/internal/arcgen/lang/go/generate_orm_read.go +++ b/internal/arcgen/lang/go/generate_orm_read.go @@ -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) + "`", }}, }, }, @@ -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) + "`", }}, }, }, @@ -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) + "`", }}, }, }, diff --git a/internal/arcgen/lang/go/generate_orm_update.go b/internal/arcgen/lang/go/generate_orm_update.go index 681045c..8b55e38 100644 --- a/internal/arcgen/lang/go/generate_orm_update.go +++ b/internal/arcgen/lang/go/generate_orm_update.go @@ -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) + "`", }}, }, },