From 3de00bc2528e5c9fd0412932a76b47394f775fc0 Mon Sep 17 00:00:00 2001 From: wubin1989 <328454505@qq.com> Date: Sat, 30 Dec 2023 02:23:38 +0800 Subject: [PATCH 1/4] ... --- toolkit/dbvendor/ivendor.go | 2 -- toolkit/dbvendor/postgres/template.go | 20 +++----------------- 2 files changed, 3 insertions(+), 19 deletions(-) diff --git a/toolkit/dbvendor/ivendor.go b/toolkit/dbvendor/ivendor.go index 350f5bc7..a25daec4 100644 --- a/toolkit/dbvendor/ivendor.go +++ b/toolkit/dbvendor/ivendor.go @@ -84,8 +84,6 @@ type Table struct { Joins []string // 父表 Inherited string - // 该表是否仅用于同步/复制数据 - IsCopy bool } func String(tmplname, tmpl string, data interface{}, pf PlaceholderFormat) (string, error) { diff --git a/toolkit/dbvendor/postgres/template.go b/toolkit/dbvendor/postgres/template.go index 44345aef..6ee1d93b 100644 --- a/toolkit/dbvendor/postgres/template.go +++ b/toolkit/dbvendor/postgres/template.go @@ -9,23 +9,9 @@ PRIMARY KEY ("{{.Pk}}")) {{- if .Inherited }} INHERITS ({{.Inherited}}) {{- end }}; - -{{- if and (not .Inherited) (not .IsCopy) }} -CREATE OR REPLACE FUNCTION update_updated_at_column() -RETURNS TRIGGER AS $$ -BEGIN - IF row(NEW.*) IS DISTINCT FROM row(OLD.*) THEN - NEW.updated_at = now(); - RETURN NEW; - ELSE - RETURN OLD; - END IF; -END; -$$ language 'plpgsql'; - -CREATE TRIGGER update_{{.Name}}_updated_at BEFORE UPDATE ON {{.Name}} FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column(); -ALTER TABLE "{{.Name}}" ALTER created_at TYPE timestamptz USING created_at AT TIME ZONE 'Asia/Shanghai'; -ALTER TABLE "{{.Name}}" ALTER updated_at TYPE timestamptz USING updated_at AT TIME ZONE 'Asia/Shanghai'; + +{{- range $co := .Columns }} +COMMENT ON COLUMN {{if $.TablePrefix }}"{{$.TablePrefix}}".{{end}}"{{$.Name}}"."{{$co.Name}}" IS {{if $co.Comment}}'{{$co.Comment}}'{{else}}''{{end}}; {{- end }} ` From dee422ff8cdb81c03f5add3755395288bbb99da7 Mon Sep 17 00:00:00 2001 From: wubin1989 <328454505@qq.com> Date: Mon, 1 Jan 2024 03:36:34 +0800 Subject: [PATCH 2/4] dbvendor batch insert --- cmd/internal/templates/mainmain.go | 3 +++ cmd/internal/templates/mainmodule.go | 5 ++++- toolkit/dbvendor/ivendor.go | 5 +++-- toolkit/dbvendor/mysql/mysql.go | 7 ++++++- toolkit/dbvendor/postgres/postgres.go | 15 ++++++++++++++- toolkit/dbvendor/postgres/template.go | 14 ++++++++++++++ 6 files changed, 44 insertions(+), 5 deletions(-) diff --git a/cmd/internal/templates/mainmain.go b/cmd/internal/templates/mainmain.go index f5178cd7..64c79a33 100644 --- a/cmd/internal/templates/mainmain.go +++ b/cmd/internal/templates/mainmain.go @@ -44,6 +44,9 @@ func main() { v.Initialize(srv, grpcServer, dialCtx) } defer func() { + if r := recover(); r != nil { + zlogger.Info().Msgf("Recovered. Error:\n", r) + } for _, v := range plugin.GetServicePlugins() { v.Close() } diff --git a/cmd/internal/templates/mainmodule.go b/cmd/internal/templates/mainmodule.go index c4d261df..d22907c5 100644 --- a/cmd/internal/templates/mainmodule.go +++ b/cmd/internal/templates/mainmodule.go @@ -16,6 +16,9 @@ func main() { v.Initialize(srv, grpcServer, nil) } defer func() { + if r := recover(); r != nil { + zlogger.Info().Msgf("Recovered. Error:\n", r) + } for _, v := range plugin.GetServicePlugins() { v.Close() } @@ -25,4 +28,4 @@ func main() { }() srv.Run() } -` \ No newline at end of file +` diff --git a/toolkit/dbvendor/ivendor.go b/toolkit/dbvendor/ivendor.go index a25daec4..7db9c0c7 100644 --- a/toolkit/dbvendor/ivendor.go +++ b/toolkit/dbvendor/ivendor.go @@ -44,6 +44,7 @@ type IVendor interface { Delete(ctx context.Context, db *gorm.DB, dml DMLSchema, args ...interface{}) error SelectById(ctx context.Context, db *gorm.DB, dml DMLSchema, args ...interface{}) (map[string]interface{}, error) GetInsertStatement(dml DMLSchema) (statement string, err error) + GetBatchInsertStatement(dml DMLSchema, rows []interface{}) (statement string, err error) GetUpdateStatement(dml DMLSchema) (statement string, err error) } @@ -97,7 +98,7 @@ func String(tmplname, tmpl string, data interface{}, pf PlaceholderFormat) (stri return "", errors.WithStack(err) } } - zlogger.Info().Msg(result) + zlogger.Debug().Msg(result) return result, nil } @@ -112,7 +113,7 @@ func StringBlock(tmplname, tmpl string, block string, data interface{}, pf Place return "", errors.WithStack(err) } } - zlogger.Info().Msg(result) + zlogger.Debug().Msg(result) return result, nil } diff --git a/toolkit/dbvendor/mysql/mysql.go b/toolkit/dbvendor/mysql/mysql.go index c9f28bcc..5c8ae755 100644 --- a/toolkit/dbvendor/mysql/mysql.go +++ b/toolkit/dbvendor/mysql/mysql.go @@ -3,10 +3,10 @@ package mysql import ( "context" "database/sql" - "github.com/unionj-cloud/go-doudou/v2/toolkit/dbvendor" "fmt" "github.com/pkg/errors" "github.com/unionj-cloud/go-doudou/v2/framework/database" + "github.com/unionj-cloud/go-doudou/v2/toolkit/dbvendor" "gorm.io/gorm" ) @@ -19,6 +19,11 @@ var _ dbvendor.IVendor = (*Vendor)(nil) type Vendor struct { } +func (v *Vendor) GetBatchInsertStatement(dml dbvendor.DMLSchema, rows []interface{}) (statement string, err error) { + //TODO implement me + panic("implement me") +} + func (v *Vendor) ToColumnType(goType string, _ bool) string { switch goType { case "int", "int16", "int32": diff --git a/toolkit/dbvendor/postgres/postgres.go b/toolkit/dbvendor/postgres/postgres.go index f68b383e..7846e816 100644 --- a/toolkit/dbvendor/postgres/postgres.go +++ b/toolkit/dbvendor/postgres/postgres.go @@ -3,10 +3,10 @@ package postgres import ( "context" "database/sql" - "github.com/unionj-cloud/go-doudou/v2/toolkit/dbvendor" "fmt" "github.com/pkg/errors" "github.com/unionj-cloud/go-doudou/v2/framework/database" + "github.com/unionj-cloud/go-doudou/v2/toolkit/dbvendor" "gorm.io/gorm" ) @@ -132,6 +132,19 @@ func (v *Vendor) GetInsertStatement(dml dbvendor.DMLSchema) (statement string, e return statement, nil } +func (v *Vendor) GetBatchInsertStatement(dml dbvendor.DMLSchema, rows []interface{}) (statement string, err error) { + if statement, err = dbvendor.String(insertIntoBatch, insertIntoBatch, struct { + dbvendor.DMLSchema + Rows []interface{} + }{ + DMLSchema: dml, + Rows: rows, + }, dbvendor.Dollar); err != nil { + return "", errors.WithStack(err) + } + return statement, nil +} + func (v *Vendor) GetUpdateStatement(dml dbvendor.DMLSchema) (statement string, err error) { if statement, err = dbvendor.String(updateTable, updateTable, dml, dbvendor.Dollar); err != nil { return "", errors.WithStack(err) diff --git a/toolkit/dbvendor/postgres/template.go b/toolkit/dbvendor/postgres/template.go index 6ee1d93b..96cc71c2 100644 --- a/toolkit/dbvendor/postgres/template.go +++ b/toolkit/dbvendor/postgres/template.go @@ -46,6 +46,20 @@ VALUES ({{- range $i, $co := .InsertColumns}} {{- end }}) RETURNING "{{.Pk.Name}}"; ` + insertIntoBatch = `INSERT INTO {{if .TablePrefix }}"{{.TablePrefix}}".{{end}}"{{.TableName}}" +({{- range $i, $co := .InsertColumns}} +{{- if $i}},{{end}} +"{{$co.Name}}" +{{- end }}) +VALUES {{- range $i, $ro := .Rows}} +{{- if $i}},{{end}} +({{- range $i, $co := $.InsertColumns}} + {{- if $i}},{{end}} + ? +{{- end }}) +{{- end }}; +` + updateTable = `UPDATE {{if .TablePrefix }}"{{.TablePrefix}}".{{end}}"{{.TableName}}" SET {{- range $i, $co := .UpdateColumns}} From 417fde03559e860d6b12816a0a1988d8db86b397 Mon Sep 17 00:00:00 2001 From: wubin1989 <328454505@qq.com> Date: Tue, 2 Jan 2024 01:11:53 +0800 Subject: [PATCH 3/4] fix comment on bug --- toolkit/dbvendor/postgres/template.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/toolkit/dbvendor/postgres/template.go b/toolkit/dbvendor/postgres/template.go index 96cc71c2..29098a61 100644 --- a/toolkit/dbvendor/postgres/template.go +++ b/toolkit/dbvendor/postgres/template.go @@ -11,7 +11,7 @@ INHERITS ({{.Inherited}}) {{- end }}; {{- range $co := .Columns }} -COMMENT ON COLUMN {{if $.TablePrefix }}"{{$.TablePrefix}}".{{end}}"{{$.Name}}"."{{$co.Name}}" IS {{if $co.Comment}}'{{$co.Comment}}'{{else}}''{{end}}; +COMMENT ON COLUMN {{if $.TablePrefix }}"{{$.TablePrefix}}".{{end}}"{{$.Name}}"."{{$co.Name}}" IS {{if $co.Comment}}$${{$co.Comment}}$${{else}}''{{end}}; {{- end }} ` @@ -22,12 +22,12 @@ COMMENT ON COLUMN {{if $.TablePrefix }}"{{$.TablePrefix}}".{{end}}"{{$.Name}}"." ALTER TABLE {{if .TablePrefix }}"{{.TablePrefix}}".{{end}}"{{.Table}}" ALTER COLUMN "{{.Name}}" TYPE {{.Type}}; ALTER TABLE {{if .TablePrefix }}"{{.TablePrefix}}".{{end}}"{{.Table}}" ALTER COLUMN "{{.Name}}" {{if .Nullable}}DROP{{else}}SET{{end}} NOT NULL; ALTER TABLE {{if .TablePrefix }}"{{.TablePrefix}}".{{end}}"{{.Table}}" ALTER COLUMN "{{.Name}}" {{if .Default}}SET DEFAULT {{.Default}}{{else}}DROP DEFAULT{{end}}; -COMMENT ON COLUMN {{if .TablePrefix }}"{{.TablePrefix}}".{{end}}"{{.Table}}"."{{.Name}}" IS {{if .Comment}}'{{.Comment}}'{{else}}''{{end}}; +COMMENT ON COLUMN {{if .TablePrefix }}"{{.TablePrefix}}".{{end}}"{{.Table}}"."{{.Name}}" IS {{if .Comment}}$${{.Comment}}$${{else}}''{{end}}; {{end}} {{define "add"}} ALTER TABLE {{if .TablePrefix }}"{{.TablePrefix}}".{{end}}"{{.Table}}" ADD COLUMN "{{.Name}}" {{.Type}} {{if .Nullable}}NULL{{else}}NOT NULL{{end}} {{if .Default}}DEFAULT {{.Default}}{{end}}; -COMMENT ON COLUMN {{if .TablePrefix }}"{{.TablePrefix}}".{{end}}"{{.Table}}"."{{.Name}}" IS {{if .Comment}}'{{.Comment}}'{{else}}''{{end}}; +COMMENT ON COLUMN {{if .TablePrefix }}"{{.TablePrefix}}".{{end}}"{{.Table}}"."{{.Name}}" IS {{if .Comment}}$${{.Comment}}$${{else}}''{{end}}; {{end}} {{define "drop"}} From a13cfe756832fb8cbe88ba2dabfb5df96e50ca55 Mon Sep 17 00:00:00 2001 From: wubin1989 <328454505@qq.com> Date: Tue, 2 Jan 2024 01:17:44 +0800 Subject: [PATCH 4/4] fix comment on bug --- version/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version/version.go b/version/version.go index 3acdf7db..7ab70f7b 100644 --- a/version/version.go +++ b/version/version.go @@ -1,3 +1,3 @@ package version -const Release = "v2.2.3" +const Release = "v2.2.4"