Mercurial > gemma
annotate pkg/imports/misc.go @ 4550:aa2d0006e742 iso-areas
Write iso lines between classes to SVG for debugging.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 30 Sep 2019 16:34:55 +0200 |
parents | 51e90370eced |
children | 1d50ac8dd688 |
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 |
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
|
33 // 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
|
34 // 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
|
35 // 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
|
36 // the callback is run in a SAVEPOINT. |
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 // If the callback returns w/o an error the SAVEPOINT |
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 // 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
|
39 func Savepoint( |
2661
542d3441c2d8
Bring context.Context parameters to front.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2660
diff
changeset
|
40 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
|
41 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
|
42 name string, |
2758
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
43 ) func(func() error) error { |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
44 |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
45 var ( |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
46 savepoint = "SAVEPOINT " + name |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
47 rollback = "ROLLBACK TO SAVEPOINT " + name |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
48 release = "RELEASE SAVEPOINT " + name |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
49 ) |
2659
00b0a7b2225a
Waterway area import: do not fail if INSERT of one item fails
Tom Gottfried <tom@intevation.de>
parents:
1785
diff
changeset
|
50 |
2758
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
51 return func(fn func() error) (err error) { |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
52 if _, err = tx.ExecContext(ctx, savepoint); err != nil { |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
53 return |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
54 } |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
55 var done bool |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
56 defer func() { |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
57 if !done { |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
58 _, err2 := tx.ExecContext(ctx, rollback) |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
59 if err == nil { |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
60 err = err2 |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
61 } |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
62 } |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
63 }() |
4146
f464cbcdf2f2
BN import: More clean-ups. Typo fixes. Code simplifications. Calling conventions ...
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2758
diff
changeset
|
64 if err = fn(); err == nil { |
2758
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
65 done = true |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
66 _, err = tx.ExecContext(ctx, release) |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
67 } |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
68 return |
a996f2ca9fa5
Simplified savepoint handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2661
diff
changeset
|
69 } |
2659
00b0a7b2225a
Waterway area import: do not fail if INSERT of one item fails
Tom Gottfried <tom@intevation.de>
parents:
1785
diff
changeset
|
70 } |