Skip to content

Commit

Permalink
lorm.DoListTx / lorm.DoSetListTx shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
sdfsdhgjkbmnmxc committed Jul 5, 2021
1 parent 73b200a commit d14add1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v0.10.0
* lorm.DoListTx / lorm.DoSetListTx shortcuts
* lorm.List interface

v0.9.5
* pq v1.10.2

Expand Down
20 changes: 20 additions & 0 deletions generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ func DoInTx(r Record, fn func() error) error {
return fn()
}

func DoListTx(l List) *Tx {
byTx := make(map[*Tx]struct{}, 1)
for _, r := range l.Records() {
byTx[r.Tx()] = struct{}{}
}
if len(byTx) > 1 {
log.Panicln("invalid transaction number:", len(byTx))
}
for tx := range byTx {
return tx
}
return nil
}

func DoSetListTx(tx *Tx, l List) {
for _, r := range l.Records() {
r.SetTx(tx)
}
}

func DoDelete(r Record, t op.Table) error {
args := op.NewArgs()
query := op.Delete(t).Where(r.PkCond(&args))
Expand Down
5 changes: 5 additions & 0 deletions list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package lorm

type List interface {
Records() []Record
}

0 comments on commit d14add1

Please sign in to comment.