/
PipelineMavenPluginH2Dao.java
535 lines (488 loc) · 24.6 KB
/
PipelineMavenPluginH2Dao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkinsci.plugins.pipeline.maven.dao;
import hudson.model.Item;
import hudson.model.Run;
import org.apache.commons.io.IOUtils;
import org.h2.api.ErrorCode;
import org.h2.jdbcx.JdbcConnectionPool;
import org.jenkinsci.plugins.pipeline.maven.util.RuntimeIoException;
import org.jenkinsci.plugins.pipeline.maven.util.RuntimeSqlException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
/**
* @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a>
*/
public class PipelineMavenPluginH2Dao implements PipelineMavenPluginDao {
private static Logger LOGGER = Logger.getLogger(PipelineMavenPluginH2Dao.class.getName());
private transient JdbcConnectionPool jdbcConnectionPool;
public PipelineMavenPluginH2Dao(File rootDir) {
rootDir.getClass(); // check non null
File databaseFile = new File(rootDir, "jenkins-jobs");
String jdbcUrl = "jdbc:h2:file:" + databaseFile.getAbsolutePath() + ";AUTO_SERVER=TRUE;MULTI_THREADED=1";
if(LOGGER.isLoggable(Level.FINEST)) {
jdbcUrl += ";TRACE_LEVEL_SYSTEM_OUT=3";
} else if (LOGGER.isLoggable(Level.FINE)) {
jdbcUrl += ";TRACE_LEVEL_SYSTEM_OUT=2";
}
LOGGER.log(Level.INFO, "Open database {0}", jdbcUrl);
jdbcConnectionPool = JdbcConnectionPool.create(jdbcUrl, "sa", "sa");
initializeDatabase();
testDatabase();
}
public PipelineMavenPluginH2Dao(JdbcConnectionPool jdbcConnectionPool) {
jdbcConnectionPool.getClass(); // check non null
this.jdbcConnectionPool = jdbcConnectionPool;
initializeDatabase();
testDatabase();
}
public PipelineMavenPluginH2Dao(String jdbcUrl, String username, String password) {
jdbcUrl.getClass(); // check non null
username.getClass(); // check non null
password.getClass(); // check non null
this.jdbcConnectionPool = JdbcConnectionPool.create(jdbcUrl, username, password);
LOGGER.log(Level.FINE, "Open database {0}", jdbcUrl);
initializeDatabase();
}
@Override
public void recordDependency(String jobFullName, int buildNumber, String groupId, String artifactId, String version, String type, String scope, boolean ignoreUpstreamTriggers) {
LOGGER.log(Level.FINE, "recordDependency({0}#{1}, {2}:{3}:{4}:{5}, {6}, ignoreUpstreamTriggers:{7}})",
new Object[]{jobFullName, buildNumber, groupId, artifactId, version, type, scope, ignoreUpstreamTriggers});
long buildPrimaryKey = getOrCreateBuildPrimaryKey(jobFullName, buildNumber);
long artifactPrimaryKey = getOrCreateArtifactPrimaryKey(groupId, artifactId, version, type);
try (Connection cnn = jdbcConnectionPool.getConnection()) {
cnn.setAutoCommit(false);
try (PreparedStatement stmt = cnn.prepareStatement("INSERT INTO MAVEN_DEPENDENCY(ARTIFACT_ID, BUILD_ID, SCOPE, IGNORE_UPSTREAM_TRIGGERS) VALUES (?, ?, ?, ?)")) {
stmt.setLong(1, artifactPrimaryKey);
stmt.setLong(2, buildPrimaryKey);
stmt.setString(3, scope);
stmt.setBoolean(4, ignoreUpstreamTriggers);
stmt.execute();
}
cnn.commit();
} catch (SQLException e) {
throw new RuntimeSqlException(e);
}
}
@Override
public void recordGeneratedArtifact(String jobFullName, int buildNumber, String groupId, String artifactId, String version, String type, String baseVersion, boolean skipDownstreamTriggers) {
LOGGER.log(Level.FINE, "recordGeneratedArtifact({0}#{1}, {2}:{3}:{4}:{5}, version:{6}, skipDownstreamTriggers:{7})",
new Object[]{jobFullName, buildNumber, groupId, artifactId, baseVersion, type, version, skipDownstreamTriggers});
long buildPrimaryKey = getOrCreateBuildPrimaryKey(jobFullName, buildNumber);
long artifactPrimaryKey = getOrCreateArtifactPrimaryKey(groupId, artifactId, baseVersion, type);
try (Connection cnn = jdbcConnectionPool.getConnection()) {
cnn.setAutoCommit(false);
try (PreparedStatement stmt = cnn.prepareStatement("INSERT INTO GENERATED_MAVEN_ARTIFACT(ARTIFACT_ID, BUILD_ID, VERSION, SKIP_DOWNSTREAM_TRIGGERS) VALUES (?, ?, ?, ?)")) {
stmt.setLong(1, artifactPrimaryKey);
stmt.setLong(2, buildPrimaryKey);
stmt.setString(3, version);
stmt.setBoolean(4, skipDownstreamTriggers);
stmt.execute();
}
cnn.commit();
} catch (SQLException e) {
throw new RuntimeSqlException(e);
}
}
@Override
public void renameJob(String oldFullName, String newFullName) {
LOGGER.log(Level.FINER, "renameJob({0}, {1})", new Object[]{oldFullName, newFullName});
try (Connection cnn = jdbcConnectionPool.getConnection()) {
cnn.setAutoCommit(false);
try (PreparedStatement stmt = cnn.prepareStatement("UPDATE JENKINS_JOB SET FULL_NAME = ? WHERE FULL_NAME = ?")) {
stmt.setString(1, newFullName);
stmt.setString(2, oldFullName);
int count = stmt.executeUpdate();
LOGGER.log(Level.FINE, "renameJob({0}, {1}): {2}", new Object[]{oldFullName, newFullName, count});
}
cnn.commit();
} catch (SQLException e) {
throw new RuntimeSqlException(e);
}
}
@Override
public void deleteJob(String jobFullName) {
LOGGER.log(Level.FINER, "deleteJob({0})", new Object[]{jobFullName});
try (Connection cnn = jdbcConnectionPool.getConnection()) {
cnn.setAutoCommit(false);
try (PreparedStatement stmt = cnn.prepareStatement("DELETE FROM JENKINS_JOB WHERE FULL_NAME = ?")) {
stmt.setString(1, jobFullName);
int count = stmt.executeUpdate();
LOGGER.log(Level.FINE, "deleteJob({0}): {1}", new Object[]{jobFullName, count});
}
cnn.commit();
} catch (SQLException e) {
throw new RuntimeSqlException(e);
}
}
@Override
public void deleteBuild(String jobFullName, int buildNumber) {
LOGGER.log(Level.FINER, "deleteBuild({0}#{1})", new Object[]{jobFullName, buildNumber});
try (Connection cnn = jdbcConnectionPool.getConnection()) {
cnn.setAutoCommit(false);
Long jobPrimaryKey;
try (PreparedStatement stmt = cnn.prepareStatement("SELECT ID FROM JENKINS_JOB WHERE FULL_NAME = ?")) {
stmt.setString(1, jobFullName);
try (ResultSet rst = stmt.executeQuery()) {
if (rst.next()) {
jobPrimaryKey = rst.getLong(1);
} else {
jobPrimaryKey = null;
}
}
}
if (jobPrimaryKey == null) {
LOGGER.log(Level.FINE, "No record found for job {0}", new Object[]{jobFullName});
return;
}
try (PreparedStatement stmt = cnn.prepareStatement("DELETE FROM JENKINS_BUILD WHERE JOB_ID = ? AND NUMBER = ?")) {
stmt.setLong(1, jobPrimaryKey);
stmt.setInt(2, buildNumber);
int count = stmt.executeUpdate();
LOGGER.log(Level.FINE, "deleteJob({0}#{1}): {2}", new Object[]{jobFullName, buildNumber, count});
}
cnn.commit();
} catch (SQLException e) {
throw new RuntimeSqlException(e);
}
}
@Override
public void cleanup() {
try (Connection cnn = jdbcConnectionPool.getConnection()) {
cnn.setAutoCommit(false);
String sql = "DELETE FROM MAVEN_ARTIFACT WHERE ID NOT IN (SELECT DISTINCT ARTIFACT_ID FROM MAVEN_DEPENDENCY UNION SELECT DISTINCT ARTIFACT_ID FROM GENERATED_MAVEN_ARTIFACT)";
try (Statement stmt = cnn.createStatement()) {
int count = stmt.executeUpdate(sql);
LOGGER.log(Level.FINE, "cleanup(): {0}", new Object[]{count});
}
cnn.commit();
} catch (SQLException e) {
throw new RuntimeSqlException(e);
}
}
protected long getOrCreateBuildPrimaryKey(String jobFullName, int buildNumber) {
try (Connection cnn = jdbcConnectionPool.getConnection()) {
cnn.setAutoCommit(false);
Long jobPrimaryKey = null;
try (PreparedStatement stmt = cnn.prepareStatement("SELECT ID FROM JENKINS_JOB WHERE FULL_NAME=?")) {
stmt.setString(1, jobFullName);
try (ResultSet rst = stmt.executeQuery()) {
if (rst.next()) {
jobPrimaryKey = rst.getLong(1);
}
}
}
if (jobPrimaryKey == null) {
try (PreparedStatement stmt = cnn.prepareStatement("INSERT INTO JENKINS_JOB(FULL_NAME) VALUES (?)", Statement.RETURN_GENERATED_KEYS)) {
stmt.setString(1, jobFullName);
stmt.execute();
try (ResultSet rst = stmt.getGeneratedKeys()) {
if (rst.next()) {
jobPrimaryKey = rst.getLong(1);
} else {
throw new IllegalStateException();
}
}
}
}
Long buildPrimaryKey = null;
try (PreparedStatement stmt = cnn.prepareStatement("SELECT ID FROM JENKINS_BUILD WHERE JOB_ID=? AND NUMBER=?")) {
stmt.setLong(1, jobPrimaryKey);
stmt.setInt(2, buildNumber);
try (ResultSet rst = stmt.executeQuery()) {
if (rst.next()) {
buildPrimaryKey = rst.getLong(1);
}
}
}
if (buildPrimaryKey == null) {
try (PreparedStatement stmt = cnn.prepareStatement("INSERT INTO JENKINS_BUILD(JOB_ID, NUMBER) VALUES (?, ?)", Statement.RETURN_GENERATED_KEYS)) {
stmt.setLong(1, jobPrimaryKey);
stmt.setInt(2, buildNumber);
stmt.execute();
try (ResultSet rst = stmt.getGeneratedKeys()) {
if (rst.next()) {
buildPrimaryKey = rst.getLong(1);
} else {
throw new IllegalStateException();
}
}
}
}
cnn.commit();
return buildPrimaryKey;
} catch (SQLException e) {
throw new RuntimeSqlException(e);
}
}
protected long getOrCreateArtifactPrimaryKey(String groupId, String artifactId, String version, String type) {
try (Connection cnn = jdbcConnectionPool.getConnection()) {
cnn.setAutoCommit(false);
// get or create build record
Long artifactPrimaryKey = null;
try (PreparedStatement stmt = cnn.prepareStatement("SELECT ID FROM MAVEN_ARTIFACT WHERE GROUP_ID = ? AND ARTIFACT_ID = ? AND VERSION = ? AND TYPE = ?")) {
stmt.setString(1, groupId);
stmt.setString(2, artifactId);
stmt.setString(3, version);
stmt.setString(4, type);
try (ResultSet rst = stmt.executeQuery()) {
if (rst.next()) {
artifactPrimaryKey = rst.getLong(1);
}
}
}
if (artifactPrimaryKey == null) {
try (PreparedStatement stmt = cnn.prepareStatement("INSERT INTO MAVEN_ARTIFACT(GROUP_ID, ARTIFACT_ID, VERSION, TYPE) VALUES (?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS)) {
stmt.setString(1, groupId);
stmt.setString(2, artifactId);
stmt.setString(3, version);
stmt.setString(4, type);
stmt.execute();
try (ResultSet rst = stmt.getGeneratedKeys()) {
if (rst.next()) {
artifactPrimaryKey = rst.getLong(1);
} else {
throw new IllegalStateException();
}
}
}
}
cnn.commit();
return artifactPrimaryKey;
} catch (SQLException e) {
throw new RuntimeSqlException(e);
}
}
protected synchronized void initializeDatabase() {
try (Connection cnn = jdbcConnectionPool.getConnection()) {
cnn.setAutoCommit(false);
int initialSchemaVersion = getSchemaVersion(cnn);
LOGGER.log(Level.FINE, "Initialise database. Current schema version: {0}", new Object[]{initialSchemaVersion});
NumberFormat numberFormat = new DecimalFormat("00");
int idx = initialSchemaVersion;
while (true) {
idx++;
String sqlScriptPath = "sql/h2/" + numberFormat.format(idx) + "_migration.sql";
InputStream sqlScriptInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(sqlScriptPath);
if (sqlScriptInputStream == null) {
break;
} else {
try (Statement stmt = cnn.createStatement()) {
String sqlScript = IOUtils.toString(sqlScriptInputStream);
LOGGER.log(Level.INFO, "Execute database migration script {0}", sqlScriptPath);
stmt.execute(sqlScript);
} catch (IOException e) {
throw new RuntimeIoException("Exception reading " + sqlScriptPath, e);
}
}
cnn.commit();
}
int newSchemaVersion = getSchemaVersion(cnn);
if (newSchemaVersion == 0) {
// https://issues.jenkins-ci.org/browse/JENKINS-46577
throw new IllegalStateException("Failure to load database DDL files. " +
"Files 'sql/h2/xxx_migration.sql' NOT found in the Thread Context Class Loader. " +
" Pipeline Maven Plugin may be installed in an unsupported manner");
} else if (newSchemaVersion == initialSchemaVersion) {
// no migration was needed
} else {
LOGGER.log(Level.INFO, "Database successfully migrated from version {0} to version {1}", new Object[]{initialSchemaVersion, newSchemaVersion});
}
} catch (SQLException e) {
throw new RuntimeSqlException(e);
}
}
protected int getSchemaVersion(Connection cnn) throws SQLException {
int schemaVersion;
try (Statement stmt = cnn.createStatement()) {
try (ResultSet rst = stmt.executeQuery("SELECT * FROM VERSION")) {
if (rst.next()) {
schemaVersion = rst.getInt(1);
} else {
schemaVersion = 0;
}
} catch (SQLException e) {
if (e.getErrorCode() == ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1) {
schemaVersion = 0;
} else {
throw new RuntimeSqlException(e);
}
}
}
return schemaVersion;
}
/**
* Basic tests to ensure that the database is not corrupted
*/
protected synchronized void testDatabase() throws RuntimeSqlException {
try (Connection cnn = jdbcConnectionPool.getConnection()) {
List<String> tables = Arrays.asList("MAVEN_ARTIFACT", "JENKINS_JOB", "JENKINS_BUILD", "MAVEN_DEPENDENCY", "GENERATED_MAVEN_ARTIFACT");
for (String table : tables) {
try (Statement stmt = cnn.createStatement()) {
try (ResultSet rst = stmt.executeQuery("SELECT count(*) FROM " + table)) {
if (rst.next()) {
int count = rst.getInt(1);
LOGGER.log(Level.FINE, "Table {0}: {1} rows", new Object[]{table, count});
} else {
throw new IllegalStateException("Exception testing table '" + table + "'");
}
}
} catch (SQLException e) {
throw new RuntimeSqlException("Exception testing table '" + table + "' on " + cnn.toString(), e);
}
}
} catch (SQLException e) {
throw new RuntimeSqlException(e);
}
}
@Nonnull
@Override
public List<String> listDownstreamJobs(@Nonnull String jobFullName, int buildNumber) {
LOGGER.log(Level.FINER, "listDownstreamJobs({0}, {1})", new Object[]{jobFullName, buildNumber});
String generatedArtifactsSql = "SELECT DISTINCT GENERATED_MAVEN_ARTIFACT.ARTIFACT_ID " +
" FROM GENERATED_MAVEN_ARTIFACT " +
" INNER JOIN JENKINS_BUILD AS UPSTREAM_BUILD ON GENERATED_MAVEN_ARTIFACT.BUILD_ID = UPSTREAM_BUILD.ID " +
" INNER JOIN JENKINS_JOB AS UPSTREAM_JOB ON UPSTREAM_BUILD.JOB_ID = UPSTREAM_JOB.ID " +
" WHERE " +
" UPSTREAM_JOB.FULL_NAME = ? AND" +
" UPSTREAM_BUILD.NUMBER = ? AND " +
" GENERATED_MAVEN_ARTIFACT.SKIP_DOWNSTREAM_TRIGGERS = FALSE";
String sql = "SELECT DISTINCT DOWNSTREAM_JOB.FULL_NAME " +
" FROM JENKINS_JOB AS DOWNSTREAM_JOB" +
" INNER JOIN JENKINS_BUILD AS DOWNSTREAM_BUILD ON DOWNSTREAM_JOB.ID = DOWNSTREAM_BUILD.JOB_ID " +
" INNER JOIN MAVEN_DEPENDENCY ON DOWNSTREAM_BUILD.ID = MAVEN_DEPENDENCY.BUILD_ID" +
" WHERE " +
" MAVEN_DEPENDENCY.ARTIFACT_ID IN (" + generatedArtifactsSql + ") AND " +
" MAVEN_DEPENDENCY.IGNORE_UPSTREAM_TRIGGERS = FALSE AND " +
" DOWNSTREAM_BUILD.NUMBER in (SELECT MAX(JENKINS_BUILD.NUMBER) FROM JENKINS_BUILD WHERE DOWNSTREAM_JOB.ID = JENKINS_BUILD.JOB_ID)" +
" ORDER BY DOWNSTREAM_JOB.FULL_NAME";
List<String> downstreamJobsFullNames = new ArrayList<>();
LOGGER.log(Level.FINER, "sql: {0}, jobFullName:{1}, buildNumber: {2}", new Object[]{sql, jobFullName, buildNumber});
try (Connection cnn = jdbcConnectionPool.getConnection()) {
try (PreparedStatement stmt = cnn.prepareStatement(sql)) {
stmt.setString(1, jobFullName);
stmt.setInt(2, buildNumber);
try (ResultSet rst = stmt.executeQuery()) {
while (rst.next()) {
downstreamJobsFullNames.add(rst.getString(1));
}
}
}
} catch (SQLException e) {
throw new RuntimeSqlException(e);
}
LOGGER.log(Level.FINE, "listDownstreamJobs({0}, {1}): {2}", new Object[]{jobFullName, buildNumber, downstreamJobsFullNames});
return downstreamJobsFullNames;
}
/**
* List the artifacts generated by the given build
*
* @param jobFullName see {@link Item#getFullName()}
* @param buildNumber see {@link Run#getNumber()}
* @return list of artifact details stored as maps ("gav", "type", "skip_downstream_triggers")
*/
@Nonnull
public List<Map<String, String>> getGeneratedArtifacts(@Nonnull String jobFullName, @Nonnull int buildNumber) {
LOGGER.log(Level.FINER, "getGeneratedArtifacts({0}, {1})", new Object[]{jobFullName, buildNumber});
String generatedArtifactsSql = "SELECT DISTINCT MAVEN_ARTIFACT.*, GENERATED_MAVEN_ARTIFACT.* " +
" FROM MAVEN_ARTIFACT " +
" INNER JOIN GENERATED_MAVEN_ARTIFACT ON MAVEN_ARTIFACT.ID = GENERATED_MAVEN_ARTIFACT.ARTIFACT_ID" +
" INNER JOIN JENKINS_BUILD AS UPSTREAM_BUILD ON GENERATED_MAVEN_ARTIFACT.BUILD_ID = UPSTREAM_BUILD.ID " +
" INNER JOIN JENKINS_JOB AS UPSTREAM_JOB ON UPSTREAM_BUILD.JOB_ID = UPSTREAM_JOB.ID " +
" WHERE " +
" UPSTREAM_JOB.FULL_NAME = ? AND" +
" UPSTREAM_BUILD.NUMBER = ? ";
List<Map<String, String>> results = new ArrayList<>();
try (Connection cnn = this.jdbcConnectionPool.getConnection()) {
try (PreparedStatement stmt = cnn.prepareStatement(generatedArtifactsSql)) {
stmt.setString(1, jobFullName);
stmt.setInt(2, buildNumber);
try (ResultSet rst = stmt.executeQuery()) {
while (rst.next()) {
Map<String, String> artifact = new HashMap<>();
String gav =
rst.getString("maven_artifact.group_id") + ":" +
rst.getString("maven_artifact.artifact_id") + ":" +
rst.getString("maven_artifact.version");
artifact.put("gav", gav);
artifact.put("type", rst.getString("maven_artifact.type"));
artifact.put("skip_downstream_triggers", rst.getString("generated_maven_artifact.skip_downstream_triggers"));
results.add(artifact);
}
}
}
} catch(SQLException e) {
throw new RuntimeSqlException(e);
}
return results;
}
@Override
public String toPrettyString() {
List<String> prettyStrings = new ArrayList<>();
try (Connection cnn = jdbcConnectionPool.getConnection()) {
prettyStrings.add("jdbc.url: " + cnn.getMetaData().getURL());
List<String> tables = Arrays.asList("MAVEN_ARTIFACT", "JENKINS_JOB", "JENKINS_BUILD", "MAVEN_DEPENDENCY", "GENERATED_MAVEN_ARTIFACT");
for (String table : tables) {
try (Statement stmt = cnn.createStatement()) {
try (ResultSet rst = stmt.executeQuery("SELECT count(*) FROM " + table)) {
if (rst.next()) {
int count = rst.getInt(1);
prettyStrings.add("Table " + table + ": " + count + " rows");
} else {
prettyStrings.add("Table " + table + ": #IllegalStateException 'select count(*)' didn't return any row#");
}
}
} catch (SQLException e) {
prettyStrings.add("Table " + table + ": "+ e);
LOGGER.log(Level.WARNING, "SQLException counting rows on " + table, e);
}
}
} catch (SQLException e) {
prettyStrings.add("SQLException getting a connection to " + jdbcConnectionPool + ": " + e);
LOGGER.log(Level.WARNING, "SQLException getting a connection to " + jdbcConnectionPool, e);
}
StringBuilder result = new StringBuilder("PipelineMavenPluginH2Dao ");
for (String prettyString : prettyStrings) {
result.append("\r\n\t" + prettyString);
}
return result.toString();
}
}