Mercurial > gemma
view pkg/imports/misc.go @ 3795:b330e014c655 yworks-svg2pdf
merge from default
author | Bernhard Reiter <bernhard@intevation.de> |
---|---|
date | Thu, 04 Jul 2019 08:33:12 +0200 |
parents | a996f2ca9fa5 |
children | f464cbcdf2f2 |
line wrap: on
line source
// SPDX-License-Identifier: AGPL-3.0-or-later // License-Filename: LICENSES/AGPL-3.0.txt // // Copyright (C) 2018 by via donau // – Österreichische Wasserstraßen-Gesellschaft mbH // Software engineering by Intevation GmbH // // Author(s): // * Sascha L. Teichmann <sascha.teichmann@intevation.de> package imports import ( "context" "database/sql" "fmt" "strings" ) type stringCounter map[string]int func (sc stringCounter) String() string { var b strings.Builder for t, c := range sc { if b.Len() > 0 { b.WriteString(", ") } b.WriteString(fmt.Sprintf("%s: %d", t, c)) } return b.String() } func Savepoint( ctx context.Context, tx *sql.Tx, name string, ) func(func() error) error { var ( savepoint = "SAVEPOINT " + name rollback = "ROLLBACK TO SAVEPOINT " + name release = "RELEASE SAVEPOINT " + name ) return func(fn func() error) (err error) { if _, err = tx.ExecContext(ctx, savepoint); err != nil { return } var done bool defer func() { if !done { _, err2 := tx.ExecContext(ctx, rollback) if err == nil { err = err2 } } }() err = fn() if err == nil { done = true _, err = tx.ExecContext(ctx, release) } return } }