Mercurial > gemma
annotate pkg/imports/misc.go @ 5457:fb3e127bccb4 uiimprovements
fixed linting errors
author | Thomas Junk <thomas.junk@intevation.de> |
---|---|
date | Thu, 15 Jul 2021 10:11:49 +0200 |
parents | 5c43427fc2bf |
children |
rev | line source |
---|---|
4949
1d50ac8dd688
More missing header lines.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4178
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
1d50ac8dd688
More missing header lines.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4178
diff
changeset
|
2 // without warranty, see README.md and license for details. |
1d50ac8dd688
More missing header lines.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4178
diff
changeset
|
3 // |
1785
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
6 // |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
7 // Copyright (C) 2018 by via donau |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
9 // Software engineering by Intevation GmbH |
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 // Author(s): |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
13 |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
14 package imports |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
15 |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
2659
00b0a7b2225a
Waterway area import: do not fail if INSERT of one item fails
Tom Gottfried <tom@intevation.de>
parents:
1785
diff
changeset
|
17 "context" |
00b0a7b2225a
Waterway area import: do not fail if INSERT of one item fails
Tom Gottfried <tom@intevation.de>
parents:
1785
diff
changeset
|
18 "database/sql" |
1785
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
19 "fmt" |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
20 "strings" |
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 |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
23 type stringCounter map[string]int |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
24 |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
25 func (sc stringCounter) String() string { |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
26 var b strings.Builder |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
27 for t, c := range sc { |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
28 if b.Len() > 0 { |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
29 b.WriteString(", ") |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
30 } |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
31 b.WriteString(fmt.Sprintf("%s: %d", t, c)) |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
32 } |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
33 return b.String() |
614c6c766691
Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
34 } |
2659
00b0a7b2225a
Waterway area import: do not fail if INSERT of one item fails
Tom Gottfried <tom@intevation.de>
parents:
1785
diff
changeset
|
35 |
4178
51e90370eced
A few more doc strings to make 'golint' a little bit more happy with the imports package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4146
diff
changeset
|
36 // Savepoint is a function wrapper to hide the details |
51e90370eced
A few more doc strings to make 'golint' a little bit more happy with the imports package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4146
diff
changeset
|
37 // of managing database SAVEPOINTs. |
51e90370eced
A few more doc strings to make 'golint' a little bit more happy with the imports package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4146
diff
changeset
|
38 // If the returned function is called with a callback |
51e90370eced
A few more doc strings to make 'golint' a little bit more happy with the imports package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4146
diff
changeset
|
39 // the callback is run in a SAVEPOINT. |
4955
5c43427fc2bf
Do not rollback savepoint if statement returned no rows
Tom Gottfried <tom@intevation.de>
parents:
4949
diff
changeset
|
40 // If the callback returns w/o an error or with sql.ErrNoRows the SAVEPOINT |
4178
51e90370eced
A few more doc strings to make 'golint' a little bit more happy with the imports package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4146
diff
changeset
|
41 // is released. Otherwise the SAVEPOINT is rolled back. |
2659
00b0a7b2225a
Waterway area import: do not fail if INSERT of one item fails
Tom Gottfried <tom@intevation.de>
parents:
1785
diff
changeset
|
42 func Savepoint( |
2661
542d3441c2d8
Bring context.Context parameters to front.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2660
diff
changeset
|
43 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
|
44 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
|
45 name string, |
2758
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
46 ) func(func() error) error { |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
47 |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
48 var ( |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
49 savepoint = "SAVEPOINT " + name |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
50 rollback = "ROLLBACK TO SAVEPOINT " + name |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
51 release = "RELEASE SAVEPOINT " + name |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
52 ) |
2659
00b0a7b2225a
Waterway area import: do not fail if INSERT of one item fails
Tom Gottfried <tom@intevation.de>
parents:
1785
diff
changeset
|
53 |
2758
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
54 return func(fn func() error) (err error) { |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
55 if _, err = tx.ExecContext(ctx, savepoint); err != nil { |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
56 return |
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 var done bool |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
59 defer func() { |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
60 if !done { |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
61 _, err2 := tx.ExecContext(ctx, rollback) |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
62 if err == nil { |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
63 err = err2 |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
64 } |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
65 } |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
66 }() |
4955
5c43427fc2bf
Do not rollback savepoint if statement returned no rows
Tom Gottfried <tom@intevation.de>
parents:
4949
diff
changeset
|
67 |
5c43427fc2bf
Do not rollback savepoint if statement returned no rows
Tom Gottfried <tom@intevation.de>
parents:
4949
diff
changeset
|
68 // Release SAVEPOINT if statements run in fn returned no database error |
5c43427fc2bf
Do not rollback savepoint if statement returned no rows
Tom Gottfried <tom@intevation.de>
parents:
4949
diff
changeset
|
69 if err = fn(); err == nil || err == sql.ErrNoRows { |
2758
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
70 done = true |
4955
5c43427fc2bf
Do not rollback savepoint if statement returned no rows
Tom Gottfried <tom@intevation.de>
parents:
4949
diff
changeset
|
71 if _, err2 := tx.ExecContext(ctx, release); err2 != nil { |
5c43427fc2bf
Do not rollback savepoint if statement returned no rows
Tom Gottfried <tom@intevation.de>
parents:
4949
diff
changeset
|
72 err = err2 |
5c43427fc2bf
Do not rollback savepoint if statement returned no rows
Tom Gottfried <tom@intevation.de>
parents:
4949
diff
changeset
|
73 } |
2758
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
74 } |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
75 return |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
76 } |
2659
00b0a7b2225a
Waterway area import: do not fail if INSERT of one item fails
Tom Gottfried <tom@intevation.de>
parents:
1785
diff
changeset
|
77 } |