Skip to content

Commit

Permalink
Merge pull request #180 from wubin1989/main
Browse files Browse the repository at this point in the history
v2.2.4
  • Loading branch information
wubin1989 authored Jan 1, 2024
2 parents 197db22 + a13cfe7 commit a4dccac
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 27 deletions.
3 changes: 3 additions & 0 deletions cmd/internal/templates/mainmain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/internal/templates/mainmodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -25,4 +28,4 @@ func main() {
}()
srv.Run()
}
`
`
7 changes: 3 additions & 4 deletions toolkit/dbvendor/ivendor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -84,8 +85,6 @@ type Table struct {
Joins []string
// 父表
Inherited string
// 该表是否仅用于同步/复制数据
IsCopy bool
}

func String(tmplname, tmpl string, data interface{}, pf PlaceholderFormat) (string, error) {
Expand All @@ -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
}

Expand All @@ -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
}

Expand Down
7 changes: 6 additions & 1 deletion toolkit/dbvendor/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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":
Expand Down
15 changes: 14 additions & 1 deletion toolkit/dbvendor/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down
38 changes: 19 additions & 19 deletions toolkit/dbvendor/postgres/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
`

Expand All @@ -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"}}
Expand All @@ -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}}
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package version

const Release = "v2.2.3"
const Release = "v2.2.4"

0 comments on commit a4dccac

Please sign in to comment.