Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
bulk insert return the affected rows
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Apr 23, 2021
1 parent 10c53ac commit 3cdb457
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 40 deletions.
50 changes: 42 additions & 8 deletions internal/generated/dbrepo/book_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ type (
BookRepo interface {
Count(context.Context, ...sqkit.SelectOption) (int64, error)
Find(context.Context, ...sqkit.SelectOption) ([]*entity.Book, error)
Insert(context.Context, ...*entity.Book) (int64, error)
Insert(context.Context, *entity.Book) (int64, error)
BulkInsert(context.Context, ...*entity.Book) (int64, error)
Delete(context.Context, sqkit.DeleteOption) (int64, error)
Update(context.Context, *entity.Book, sqkit.UpdateOption) (int64, error)
Patch(context.Context, *entity.Book, sqkit.UpdateOption) (int64, error)
Expand Down Expand Up @@ -130,8 +131,8 @@ func (r *BookRepoImpl) Find(ctx context.Context, opts ...sqkit.SelectOption) (li
return
}

// Insert books
func (r *BookRepoImpl) Insert(ctx context.Context, ents ...*entity.Book) (int64, error) {
// Insert books and return last inserted id
func (r *BookRepoImpl) Insert(ctx context.Context, ent *entity.Book) (int64, error) {
txn, err := dbtxn.Use(ctx, r.DB)
if err != nil {
return -1, err
Expand All @@ -148,16 +149,13 @@ func (r *BookRepoImpl) Insert(ctx context.Context, ents ...*entity.Book) (int64,
Suffix(
fmt.Sprintf("RETURNING \"%s\"", BookTable.ID),
).
PlaceholderFormat(sq.Dollar)

for _, ent := range ents {
builder = builder.Values(
PlaceholderFormat(sq.Dollar).
Values(
ent.Title,
ent.Author,
time.Now(),
time.Now(),
)
}

scanner := builder.RunWith(txn).QueryRowContext(ctx)

Expand All @@ -169,6 +167,42 @@ func (r *BookRepoImpl) Insert(ctx context.Context, ents ...*entity.Book) (int64,
return id, nil
}

// BulkInsert books and return affected rows
func (r *BookRepoImpl) BulkInsert(ctx context.Context, ents ...*entity.Book) (int64, error) {
txn, err := dbtxn.Use(ctx, r.DB)
if err != nil {
return -1, err
}

builder := sq.
Insert(BookTableName).
Columns(
BookTable.Title,
BookTable.Author,
BookTable.UpdatedAt,
BookTable.CreatedAt,
).
PlaceholderFormat(sq.Dollar)

for _, ent := range ents {
builder = builder.Values(
ent.Title,
ent.Author,
time.Now(),
time.Now(),
)
}

res, err := builder.ExecContext(ctx)
if err != nil {
txn.SetError(err)
return -1, err
}
affectedRow, err := res.RowsAffected()
txn.SetError(err)
return affectedRow, err
}

// Update books
func (r *BookRepoImpl) Update(ctx context.Context, ent *entity.Book, opt sqkit.UpdateOption) (int64, error) {
txn, err := dbtxn.Use(ctx, r.DB)
Expand Down
40 changes: 37 additions & 3 deletions internal/generated/dbrepo/song_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ type (
SongRepo interface {
Count(context.Context, ...sqkit.SelectOption) (int64, error)
Find(context.Context, ...sqkit.SelectOption) ([]*entity.Song, error)
Insert(context.Context, ...*entity.Song) (int64, error)
Insert(context.Context, *entity.Song) (int64, error)
BulkInsert(context.Context, ...*entity.Song) (int64, error)
Delete(context.Context, sqkit.DeleteOption) (int64, error)
Update(context.Context, *entity.Song, sqkit.UpdateOption) (int64, error)
Patch(context.Context, *entity.Song, sqkit.UpdateOption) (int64, error)
Expand Down Expand Up @@ -128,8 +129,8 @@ func (r *SongRepoImpl) Find(ctx context.Context, opts ...sqkit.SelectOption) (li
return
}

// Insert songs
func (r *SongRepoImpl) Insert(ctx context.Context, ents ...*entity.Song) (int64, error) {
// BulkInsert songs and return affected row
func (r *SongRepoImpl) BulkInsert(ctx context.Context, ents ...*entity.Song) (int64, error) {
txn, err := dbtxn.Use(ctx, r.DB)
if err != nil {
return -1, err
Expand Down Expand Up @@ -159,6 +160,39 @@ func (r *SongRepoImpl) Insert(ctx context.Context, ents ...*entity.Song) (int64,
return -1, err
}

affectedRow, err := res.RowsAffected()
txn.SetError(err)
return affectedRow, err
}

// Insert songs and return last inserted id
func (r *SongRepoImpl) Insert(ctx context.Context, ent *entity.Song) (int64, error) {
txn, err := dbtxn.Use(ctx, r.DB)
if err != nil {
return -1, err
}

builder := sq.
Insert(SongTableName).
Columns(
SongTable.Title,
SongTable.Artist,
SongTable.UpdatedAt,
SongTable.CreatedAt,
).
Values(
ent.Title,
ent.Artist,
time.Now(),
time.Now(),
)

res, err := builder.RunWith(txn).ExecContext(ctx)
if err != nil {
txn.SetError(err)
return -1, err
}

lastInsertID, err := res.LastInsertId()
txn.SetError(err)
return lastInsertID, err
Expand Down
33 changes: 24 additions & 9 deletions internal/generated/dbrepo_mock/book_repo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 24 additions & 9 deletions internal/generated/dbrepo_mock/song_repo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 29 additions & 3 deletions pkg/typdb/mysql_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type (
{{.Name}}Repo interface {
Count(context.Context, ...sqkit.SelectOption) (int64, error)
Find(context.Context, ...sqkit.SelectOption) ([]*{{.SourcePkg}}.{{.Name}}, error)
Insert(context.Context, ...*{{.SourcePkg}}.{{.Name}}) (int64, error)
Insert(context.Context, *{{.SourcePkg}}.{{.Name}}) (int64, error)
BulkInsert(context.Context, ...*{{.SourcePkg}}.{{.Name}}) (int64, error)
Delete(context.Context, sqkit.DeleteOption) (int64, error)
Update(context.Context, *{{.SourcePkg}}.{{.Name}}, sqkit.UpdateOption) (int64, error)
Patch(context.Context, *{{.SourcePkg}}.{{.Name}}, sqkit.UpdateOption) (int64, error)
Expand Down Expand Up @@ -108,8 +109,8 @@ func (r *{{.Name}}RepoImpl) Find(ctx context.Context, opts ...sqkit.SelectOption
return
}
// Insert {{.Table}}
func (r *{{.Name}}RepoImpl) Insert(ctx context.Context, ents ...*{{.SourcePkg}}.{{.Name}}) (int64, error) {
// BulkInsert {{.Table}} and return affected row
func (r *{{.Name}}RepoImpl) BulkInsert(ctx context.Context, ents ...*{{.SourcePkg}}.{{.Name}}) (int64, error) {
txn, err := dbtxn.Use(ctx, r.DB)
if err != nil {
return -1, err
Expand All @@ -131,6 +132,31 @@ func (r *{{.Name}}RepoImpl) Insert(ctx context.Context, ents ...*{{.SourcePkg}}.
return -1, err
}
affectedRow, err := res.RowsAffected()
txn.SetError(err)
return affectedRow, err
}
// Insert {{.Table}} and return last inserted id
func (r *{{.Name}}RepoImpl) Insert(ctx context.Context, ent *{{.SourcePkg}}.{{.Name}}) (int64, error) {
txn, err := dbtxn.Use(ctx, r.DB)
if err != nil {
return -1, err
}
builder := sq.
Insert({{$.Name}}TableName).
Columns({{range .Fields}}{{if not .PrimaryKey}} {{$.Name}}Table.{{.Name}},{{end}}
{{end}}).
Values({{range .Fields}}{{if .DefaultValue}} {{.DefaultValue}},{{else if not .PrimaryKey}} ent.{{.Name}},{{end}}
{{end}})
res, err := builder.RunWith(txn).ExecContext(ctx)
if err != nil {
txn.SetError(err)
return -1, err
}
lastInsertID, err := res.LastInsertId()
txn.SetError(err)
return lastInsertID, err
Expand Down
43 changes: 35 additions & 8 deletions pkg/typdb/pg_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type (
{{.Name}}Repo interface {
Count(context.Context, ...sqkit.SelectOption) (int64, error)
Find(context.Context, ...sqkit.SelectOption) ([]*{{.SourcePkg}}.{{.Name}}, error)
Insert(context.Context, ...*{{.SourcePkg}}.{{.Name}}) (int64, error)
Insert(context.Context, *{{.SourcePkg}}.{{.Name}}) (int64, error)
BulkInsert(context.Context, ...*{{.SourcePkg}}.{{.Name}}) (int64, error)
Delete(context.Context, sqkit.DeleteOption) (int64, error)
Update(context.Context, *{{.SourcePkg}}.{{.Name}}, sqkit.UpdateOption) (int64, error)
Patch(context.Context, *{{.SourcePkg}}.{{.Name}}, sqkit.UpdateOption) (int64, error)
Expand Down Expand Up @@ -108,8 +109,8 @@ func (r *{{.Name}}RepoImpl) Find(ctx context.Context, opts ...sqkit.SelectOption
return
}
// Insert {{.Table}}
func (r *{{.Name}}RepoImpl) Insert(ctx context.Context, ents ...*{{.SourcePkg}}.{{.Name}}) (int64, error) {
// Insert {{.Table}} and return last inserted id
func (r *{{.Name}}RepoImpl) Insert(ctx context.Context, ent *{{.SourcePkg}}.{{.Name}}) (int64, error) {
txn, err := dbtxn.Use(ctx, r.DB)
if err != nil {
return -1, err
Expand All @@ -122,21 +123,47 @@ func (r *{{.Name}}RepoImpl) Insert(ctx context.Context, ents ...*{{.SourcePkg}}.
Suffix(
fmt.Sprintf("RETURNING \"%s\"", {{$.Name}}Table.{{.PrimaryKey.Name}}),
).
PlaceholderFormat(sq.Dollar).
Values({{range .Fields}}{{if .DefaultValue}} {{.DefaultValue}},{{else if not .PrimaryKey}} ent.{{.Name}},{{end}}
{{end}})
scanner := builder.RunWith(txn).QueryRowContext(ctx)
var id {{.PrimaryKey.Type}}
if err := scanner.Scan(&id); err != nil {
txn.SetError(err)
return -1, err
}
return id, nil
}
// BulkInsert {{.Table}} and return affected rows
func (r *{{.Name}}RepoImpl) BulkInsert(ctx context.Context, ents ...*{{.SourcePkg}}.{{.Name}}) (int64, error) {
txn, err := dbtxn.Use(ctx, r.DB)
if err != nil {
return -1, err
}
builder := sq.
Insert({{$.Name}}TableName).
Columns({{range .Fields}}{{if not .PrimaryKey}} {{$.Name}}Table.{{.Name}},{{end}}
{{end}}).
PlaceholderFormat(sq.Dollar)
for _, ent := range ents {
builder = builder.Values({{range .Fields}}{{if .DefaultValue}} {{.DefaultValue}},{{else if not .PrimaryKey}} ent.{{.Name}},{{end}}
{{end}})
}
scanner := builder.RunWith(txn).QueryRowContext(ctx)
var id {{.PrimaryKey.Type}}
if err := scanner.Scan(&id); err != nil {
res, err := builder.ExecContext(ctx)
if err != nil {
txn.SetError(err)
return -1, err
}
return id, nil
affectedRow, err := res.RowsAffected()
txn.SetError(err)
return affectedRow, err
}
Expand Down

0 comments on commit 3cdb457

Please sign in to comment.