Skip to content

Commit

Permalink
Merge pull request #252 from pdowler/main
Browse files Browse the repository at this point in the history
update to KeyValueDAO detection of missing table
  • Loading branch information
pdowler authored Sep 13, 2024
2 parents 47d8e3d + cfb37ef commit b58c70e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cadc-util/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ sourceCompatibility = 1.8

group = 'org.opencadc'

version = '1.11.3'
version = '1.11.4'

description = 'OpenCADC core utility library'
def git_url = 'https://github.com/opencadc/core'
Expand Down
49 changes: 33 additions & 16 deletions cadc-util/src/main/java/ca/nrc/cadc/db/version/KeyValueDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@

import ca.nrc.cadc.date.DateUtil;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand All @@ -93,7 +94,12 @@ public class KeyValueDAO {

protected String[] columnNames;

private final String database;
private final String schema;
private final String table;

private final String tableName;
private final DataSource dataSource;
private final JdbcTemplate jdbc;
private final ResultSetExtractor extractor;

Expand All @@ -104,6 +110,10 @@ public KeyValueDAO(DataSource dataSource, String database, String schema) {
}

public KeyValueDAO(DataSource dataSource, String database, String schema, Class tupleType) {
this.dataSource = dataSource;
this.database = database;
this.schema = schema;
this.table = tupleType.getSimpleName();
this.jdbc = new JdbcTemplate(dataSource);
StringBuilder tn = new StringBuilder();
if (database != null) {
Expand All @@ -112,7 +122,7 @@ public KeyValueDAO(DataSource dataSource, String database, String schema, Class
if (schema != null) {
tn.append(schema).append(".");
}
tn.append(tupleType.getSimpleName());
tn.append(table);
this.tableName = tn.toString();
this.extractor = new ModelVersionExtractor();
this.columnNames = new String[] { "value", "lastModified", "name" };
Expand All @@ -136,24 +146,31 @@ public KeyValue get(String name) {
sel.setValues(name);
try {
o = jdbc.query(sel, extractor);
} catch (BadSqlGrammarException ex) {
} catch (Exception ex) {
Connection con = null;
try {
// try simples query possible to see if table exists
String sql = "SELECT count(*) from " + tableName;
log.debug("check exists: " + sql);
jdbc.queryForObject(sql, new RowMapper<Integer>() {
@Override
public Integer mapRow(ResultSet rs, int i) throws SQLException {
return rs.getInt(1);
log.debug("query fail - check if table exists: " + tableName);
con = jdbc.getDataSource().getConnection();
DatabaseMetaData dm = con.getMetaData();
ResultSet rs = dm.getTables(database, schema, table, null);
if (rs != null && !rs.next()) {
log.debug("table does not exist: " + tableName);
return null;
}
} catch (SQLException oops) {
throw new RuntimeException("failed to determine if table exists: " + tableName, oops);
} finally {
if (con != null) {
try {
con.close();
} catch (SQLException ignore) {
log.debug("failed to close database metadata query result", ignore);
}
});

// some other kind of error
throw ex;
} catch (BadSqlGrammarException ex2) {
log.debug("previous install not found: " + ex2.getMessage());
o = null;
}
}

// some other kind of error
throw ex;
}

return (KeyValue) o;
Expand Down

0 comments on commit b58c70e

Please sign in to comment.