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 350f5bc7..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) } @@ -84,8 +85,6 @@ type Table struct { Joins []string // 父表 Inherited string - // 该表是否仅用于同步/复制数据 - IsCopy bool } func String(tmplname, tmpl string, data interface{}, pf PlaceholderFormat) (string, error) { @@ -99,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 } @@ -114,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 44345aef..29098a61 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 }} ` @@ -36,12 +22,12 @@ ALTER TABLE "{{.Name}}" ALTER updated_at TYPE timestamptz USING updated_at AT TI 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"}} @@ -60,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}} 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"