Mercurial > gemma
annotate pkg/imports/misc.go @ 2787:2ce5ee6d65b5
client:translations: merge translations from weblate
author | Fadi Abbud <fadi.abbud@intevation.de> |
---|---|
date | Mon, 25 Mar 2019 10:34:33 +0100 |
parents | a996f2ca9fa5 |
children | f464cbcdf2f2 |
rev | line source |
---|---|
1785
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
1 // SPDX-License-Identifier: AGPL-3.0-or-later |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
2 // License-Filename: LICENSES/AGPL-3.0.txt |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
3 // |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
4 // Copyright (C) 2018 by via donau |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
5 // – Österreichische Wasserstraßen-Gesellschaft mbH |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
6 // Software engineering by Intevation GmbH |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
7 // |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
8 // Author(s): |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
9 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
10 |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
11 package imports |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
12 |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
13 import ( |
2659
00b0a7b2225a
Waterway area import: do not fail if INSERT of one item fails
Tom Gottfried <tom@intevation.de>
parents:
1785
diff
changeset
|
14 "context" |
00b0a7b2225a
Waterway area import: do not fail if INSERT of one item fails
Tom Gottfried <tom@intevation.de>
parents:
1785
diff
changeset
|
15 "database/sql" |
1785
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
16 "fmt" |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
17 "strings" |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
18 ) |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
19 |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
20 type stringCounter map[string]int |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
21 |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
22 func (sc stringCounter) String() string { |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
23 var b strings.Builder |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
24 for t, c := range sc { |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
25 if b.Len() > 0 { |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
26 b.WriteString(", ") |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
27 } |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
28 b.WriteString(fmt.Sprintf("%s: %d", t, c)) |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
29 } |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
30 return b.String() |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
31 } |
2659
00b0a7b2225a
Waterway area import: do not fail if INSERT of one item fails
Tom Gottfried <tom@intevation.de>
parents:
1785
diff
changeset
|
32 |
00b0a7b2225a
Waterway area import: do not fail if INSERT of one item fails
Tom Gottfried <tom@intevation.de>
parents:
1785
diff
changeset
|
33 func Savepoint( |
2661
542d3441c2d8
Bring context.Context parameters to front.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2660
diff
changeset
|
34 ctx context.Context, |
2659
00b0a7b2225a
Waterway area import: do not fail if INSERT of one item fails
Tom Gottfried <tom@intevation.de>
parents:
1785
diff
changeset
|
35 tx *sql.Tx, |
00b0a7b2225a
Waterway area import: do not fail if INSERT of one item fails
Tom Gottfried <tom@intevation.de>
parents:
1785
diff
changeset
|
36 name string, |
2758
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
37 ) func(func() error) error { |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
38 |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
39 var ( |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
40 savepoint = "SAVEPOINT " + name |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
41 rollback = "ROLLBACK TO SAVEPOINT " + name |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
42 release = "RELEASE SAVEPOINT " + name |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
43 ) |
2659
00b0a7b2225a
Waterway area import: do not fail if INSERT of one item fails
Tom Gottfried <tom@intevation.de>
parents:
1785
diff
changeset
|
44 |
2758
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
45 return func(fn func() error) (err error) { |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
46 if _, err = tx.ExecContext(ctx, savepoint); err != nil { |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
47 return |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
48 } |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
49 var done bool |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
50 defer func() { |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
51 if !done { |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
52 _, err2 := tx.ExecContext(ctx, rollback) |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
53 if err == nil { |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
54 err = err2 |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
55 } |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
56 } |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
57 }() |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
58 err = fn() |
2659
00b0a7b2225a
Waterway area import: do not fail if INSERT of one item fails
Tom Gottfried <tom@intevation.de>
parents:
1785
diff
changeset
|
59 |
2758
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
60 if err == nil { |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
61 done = true |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
62 _, err = tx.ExecContext(ctx, release) |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
63 } |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
64 return |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
65 } |
2659
00b0a7b2225a
Waterway area import: do not fail if INSERT of one item fails
Tom Gottfried <tom@intevation.de>
parents:
1785
diff
changeset
|
66 } |