comparison src/main/java/org/monetdb/jdbc/MonetConnection.java @ 914:e0120c7052bb

Check for rs == null to prevent NullPoinerExceptions.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 18 Jul 2024 19:59:25 +0200 (9 months ago)
parents 8c8c423dc619
children ce6bc9908735
comparison
equal deleted inserted replaced
913:a8ca336e7f1a 914:e0120c7052bb
1282 * or this method is called on a closed connection 1282 * or this method is called on a closed connection
1283 * @since 1.6 1283 * @since 1.6
1284 */ 1284 */
1285 @Override 1285 @Override
1286 public String getClientInfo(final String name) throws SQLException { 1286 public String getClientInfo(final String name) throws SQLException {
1287 // only MonetDB Server 11.51 (Aug2024) or higher supports Client Info Properties
1287 String attrName = getClientInfoAttributeNames().get(name); 1288 String attrName = getClientInfoAttributeNames().get(name);
1288 if (attrName == null) 1289 if (attrName == null)
1289 return null; 1290 return null;
1290 String query = "SELECT " + attrName + " FROM sys.sessions WHERE sessionid = current_sessionid()"; 1291 String query = "SELECT " + attrName + " FROM sys.sessions WHERE sessionid = current_sessionid()";
1291 try (Statement st = createStatement(); ResultSet rs = st.executeQuery(query)) { 1292 try (Statement st = createStatement(); ResultSet rs = st.executeQuery(query)) {
1292 if (rs.next()) 1293 if (rs != null && rs.next())
1293 return rs.getString(1); 1294 return rs.getString(1);
1294 else 1295 else
1295 return null; 1296 return null;
1296 } 1297 }
1297 } 1298 }
1308 * or this method is called on a closed connection 1309 * or this method is called on a closed connection
1309 * @since 1.6 1310 * @since 1.6
1310 */ 1311 */
1311 @Override 1312 @Override
1312 public Properties getClientInfo() throws SQLException { 1313 public Properties getClientInfo() throws SQLException {
1313 // MonetDB doesn't support any Client Info Properties yet
1314 Properties props = new Properties(); 1314 Properties props = new Properties();
1315 1315
1316 // only MonetDB Server 11.51 (Aug2024) or higher supports Client Info Properties
1316 if (server.canClientInfo()) { 1317 if (server.canClientInfo()) {
1317 StringBuilder builder = new StringBuilder("SELECT "); 1318 StringBuilder builder = new StringBuilder("SELECT ");
1318 String sep = ""; 1319 String sep = "";
1319 for (Entry<String, String> entry: getClientInfoAttributeNames().entrySet()) { 1320 for (Entry<String, String> entry: getClientInfoAttributeNames().entrySet()) {
1320 String jdbcName = entry.getKey(); 1321 String jdbcName = entry.getKey();
1326 builder.append(jdbcName); 1327 builder.append(jdbcName);
1327 builder.append("\""); 1328 builder.append("\"");
1328 } 1329 }
1329 builder.append(" FROM sys.sessions WHERE sessionid = current_sessionid()"); 1330 builder.append(" FROM sys.sessions WHERE sessionid = current_sessionid()");
1330 1331
1331 try ( 1332 try (Statement st = createStatement(); ResultSet rs = st.executeQuery(builder.toString())) {
1332 Statement st = createStatement(); 1333 if (rs != null && rs.next()) {
1333 ResultSet rs = st.executeQuery(builder.toString())
1334 ) {
1335 if (rs.next()) {
1336 ResultSetMetaData md = rs.getMetaData(); 1334 ResultSetMetaData md = rs.getMetaData();
1337 for (int i = 1; i <= md.getColumnCount(); i++) { 1335 for (int i = 1; i <= md.getColumnCount(); i++) {
1338 String key = md.getColumnName(i); 1336 String key = md.getColumnName(i);
1339 String value = rs.getString(i); 1337 String value = rs.getString(i);
1340 props.setProperty(key, value != null ? value : ""); 1338 props.setProperty(key, value != null ? value : "");
1346 } 1344 }
1347 1345
1348 private HashMap<String,String> getClientInfoAttributeNames() throws SQLException { 1346 private HashMap<String,String> getClientInfoAttributeNames() throws SQLException {
1349 if (clientInfoAttributeNames == null) { 1347 if (clientInfoAttributeNames == null) {
1350 HashMap<String, String> map = new HashMap<>(); 1348 HashMap<String, String> map = new HashMap<>();
1349 // only MonetDB Server 11.51 (Aug2024) or higher has table sys.clientinfo_properties with 5 rows
1351 if (server.canClientInfo()) { 1350 if (server.canClientInfo()) {
1352 try (Statement st = createStatement(); ResultSet rs = st.executeQuery("SELECT prop, session_attr FROM sys.clientinfo_properties")) { 1351 try (Statement st = createStatement(); ResultSet rs = st.executeQuery("SELECT prop, session_attr FROM sys.clientinfo_properties")) {
1353 while (rs.next()) { 1352 while (rs != null && rs.next()) {
1354 String jdbcName = rs.getString(1); 1353 String jdbcName = rs.getString(1);
1355 String attrName = rs.getString(2); 1354 String attrName = rs.getString(2);
1356 map.put(jdbcName, attrName); 1355 map.put(jdbcName, attrName);
1357 } 1356 }
1358 } 1357 }