Skip to content

Commit

Permalink
dbvendor batch insert
Browse files Browse the repository at this point in the history
  • Loading branch information
wubin1989 committed Dec 31, 2023
1 parent 3de00bc commit dee422f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 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()
}
`
`
5 changes: 3 additions & 2 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 @@ -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
}

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

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

0 comments on commit dee422f

Please sign in to comment.