• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SpiNNakerManchester / JavaSpiNNaker / 6233274834

19 Sep 2023 08:46AM UTC coverage: 36.409% (-0.6%) from 36.982%
6233274834

Pull #658

github

dkfellows
Merge branch 'master' into java-17
Pull Request #658: Update Java version to 17

1656 of 1656 new or added lines in 260 files covered. (100.0%)

8373 of 22997 relevant lines covered (36.41%)

0.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

42.86
/SpiNNaker-allocserv/src/main/java/uk/ac/manchester/spinnaker/alloc/db/Utils.java
1
/*
2
 * Copyright (c) 2021 The University of Manchester
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package uk.ac.manchester.spinnaker.alloc.db;
17

18
import static java.lang.System.arraycopy;
19
import static java.util.Objects.isNull;
20

21
import java.sql.SQLException;
22
import java.sql.SQLTimeoutException;
23
import java.sql.SQLTransientConnectionException;
24

25
import org.springframework.dao.DataAccessException;
26
import org.springframework.jdbc.InvalidResultSetAccessException;
27
import org.springframework.jdbc.UncategorizedSQLException;
28

29
/**
30
 * Miscellaneous database-related utility operations.
31
 *
32
 * @author Donal Fellows
33
 */
34
public abstract class Utils {
35
        private static final int TRIM_LENGTH = 80;
36

37
        private static final String ELLIPSIS = "...";
38

39
        private Utils() {
40
        }
41

42
        /**
43
         * Exclude comments and compress whitespace from the SQL of a statement.
44
         *
45
         * @param sql
46
         *            The text of the SQL to trim.
47
         * @return The trimmed SQL.
48
         */
49
        public static String trimSQL(String sql) {
50
                return trimSQL(sql, TRIM_LENGTH);
×
51
        }
52

53
        /**
54
         * Exclude comments and compress whitespace from the SQL of a statement.
55
         *
56
         * @param sql
57
         *            The text of the SQL to trim.
58
         * @param length
59
         *            The point to insert an ellipsis if required.
60
         * @return The trimmed SQL.
61
         */
62
        public static String trimSQL(String sql, int length) {
63
                if (isNull(sql)) {
×
64
                        return null;
×
65
                }
66
                sql = trimSQLComments(sql);
×
67
                // Try to trim long queries to no more than TRIM_LENGTH...
68
                var sql2 = sql.replaceAll("^(.{0," + length + "})\\b.*$", "$1");
×
69
                // If that did nothing, return the whole string
70
                if (sql2.equals(sql)) {
×
71
                        return sql2;
×
72
                }
73
                // We're using the trimming, so add an ellipsis
74
                return sql2 + ELLIPSIS;
×
75
        }
76

77
        private static String trimSQLComments(String sql) {
78
                if (isNull(sql)) {
1✔
79
                        return null;
×
80
                }
81
                return sql.replaceAll("--[^\n]*\n", " ").replaceAll("\\s+", " ")
1✔
82
                                .strip();
1✔
83
        }
84

85
        /**
86
         * Utility for testing whether an exception was thrown because the database
87
         * was busy.
88
         *
89
         * @param exception
90
         *            The outer wrapping exception.
91
         * @return Whether it was caused by the database being busy.
92
         */
93
        public static boolean isBusy(DataAccessException exception) {
94
                var root = exception.getMostSpecificCause();
×
95
                if (root instanceof SQLException e) {
×
96
                        return isBusy(e);
×
97
                }
98
                return false;
×
99
        }
100

101
        /**
102
         * Utility for testing whether an exception was thrown because the database
103
         * was busy.
104
         *
105
         * @param exception
106
         *            The exception to test.
107
         * @return Whether it was caused by the database being busy.
108
         */
109
        public static boolean isBusy(SQLException exception) {
110
                return exception instanceof SQLTimeoutException
×
111
                                || exception instanceof SQLTransientConnectionException;
112
        }
113

114
        /**
115
         * Convert an SQL-related exception into an unchecked exception.
116
         *
117
         * @param exception
118
         *            The exception to convert.
119
         * @param sql
120
         *            Optional SQL that caused the problem. May be {@code null}.
121
         * @return The converted exception; this is <em>not</em> thrown by this
122
         *         method!
123
         */
124
        public static DataAccessException mapException(SQLException exception,
125
                        String sql) {
126
                if (exception.getMessage().contains("no such column: ")) {
1✔
127
                        return restack(new InvalidResultSetAccessException(
×
128
                                        exception.getMessage(), trimSQLComments(sql), exception));
×
129
                }
130
                return restack(new UncategorizedSQLException("general SQL exception",
1✔
131
                                trimSQLComments(sql), exception));
1✔
132
        }
133

134
        // 2 = restack() and mapException() themselves
135
        private static final int CHOP_LEN = 2;
136

137
        private static <T extends Throwable> T restack(T exn) {
138
                exn.fillInStackTrace();
1✔
139
                var st = exn.getStackTrace();
1✔
140
                var newst = new StackTraceElement[st.length - CHOP_LEN];
1✔
141
                arraycopy(st, CHOP_LEN, newst, 0, newst.length);
1✔
142
                exn.setStackTrace(newst);
1✔
143
                return exn;
1✔
144
        }
145
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc