From d179b342b259a01f9c100157f5e3477117503224 Mon Sep 17 00:00:00 2001 From: toven tang Date: Sun, 5 Sep 2021 22:27:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BD=BF=E7=94=A8=20postgres?= =?UTF-8?q?=20=E6=95=B0=E6=8D=AE=E5=BA=93=E6=97=B6=EF=BC=8C=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=E5=8F=82=E6=95=B0=E9=87=8D=E5=A4=8D=EF=BC=8C=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E5=8F=82=E6=95=B0=E4=B8=8E=E5=80=BC=E4=B8=8D=E5=AF=B9?= =?UTF-8?q?=E5=BA=94=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82=20(#960)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修复使用 postgres 数据库时,位置参数重复,导致参数与值不对应的问题。 * 修复使用 postgres 数据库时,位置参数重复,导致参数与值不对应的问题。 Co-authored-by: toven --- tools/goctl/model/sql/builderx/builder.go | 2 +- tools/goctl/model/sql/builderx/builder_test.go | 2 +- tools/goctl/model/sql/gen/update.go | 6 +++++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/tools/goctl/model/sql/builderx/builder.go b/tools/goctl/model/sql/builderx/builder.go index f579c0e38..bcf380155 100644 --- a/tools/goctl/model/sql/builderx/builder.go +++ b/tools/goctl/model/sql/builderx/builder.go @@ -125,7 +125,7 @@ func RawFieldNames(in interface{}, postgresSql ...bool) []string { func PostgreSqlJoin(elems []string) string { b := new(strings.Builder) for index, e := range elems { - b.WriteString(fmt.Sprintf("%s = $%d, ", e, index+1)) + b.WriteString(fmt.Sprintf("%s = $%d, ", e, index+2)) } if b.Len() == 0 { diff --git a/tools/goctl/model/sql/builderx/builder_test.go b/tools/goctl/model/sql/builderx/builder_test.go index 31bf7680a..5f3f46148 100644 --- a/tools/goctl/model/sql/builderx/builder_test.go +++ b/tools/goctl/model/sql/builderx/builder_test.go @@ -121,5 +121,5 @@ func TestBuildSqlLike(t *testing.T) { func TestJoin(t *testing.T) { ret := PostgreSqlJoin([]string{"name", "age"}) - assert.Equal(t, "name = $1, age = $2", ret) + assert.Equal(t, "name = $2, age = $3", ret) } diff --git a/tools/goctl/model/sql/gen/update.go b/tools/goctl/model/sql/gen/update.go index 892482843..7887826a0 100644 --- a/tools/goctl/model/sql/gen/update.go +++ b/tools/goctl/model/sql/gen/update.go @@ -33,7 +33,11 @@ func genUpdate(table Table, withCache, postgreSql bool) (string, string, error) keyVariableSet.AddStr(key.KeyLeft) } - expressionValues = append(expressionValues, "data."+table.PrimaryKey.Name.ToCamel()) + if postgreSql { + expressionValues = append([]string{"data." + table.PrimaryKey.Name.ToCamel()}, expressionValues...) + } else { + expressionValues = append(expressionValues, "data."+table.PrimaryKey.Name.ToCamel()) + } camelTableName := table.Name.ToCamel() text, err := util.LoadTemplate(category, updateTemplateFile, template.Update) if err != nil {