comparison src/main/java/org/monetdb/jdbc/MonetConnection.java @ 685:35653312f9cb

Optimise check on existence of specific tables. Instead of doing a query per table name, combine them in one query.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 10 Nov 2022 20:05:25 +0100 (2022-11-10)
parents 9cc67e98e9c6
children f7946d36d1dd
comparison
equal deleted inserted replaced
684:9cc67e98e9c6 685:35653312f9cb
1989 * The result is cached and reused, so that we only test the query once per connection. 1989 * The result is cached and reused, so that we only test the query once per connection.
1990 * This method is used by methods from MonetDatabaseMetaData. 1990 * This method is used by methods from MonetDatabaseMetaData.
1991 */ 1991 */
1992 boolean privilege_codesTableExists() { 1992 boolean privilege_codesTableExists() {
1993 if (!queriedPrivilege_codesTable) { 1993 if (!queriedPrivilege_codesTable) {
1994 hasPrivilege_codesTable = existsSysTable("privilege_codes"); 1994 querySysTable();
1995 queriedPrivilege_codesTable = true; // set flag, so the querying is done only at first invocation. 1995 queriedPrivilege_codesTable = true; // set flag, so the querying is done only at first invocation.
1996 } 1996 }
1997 return hasPrivilege_codesTable; 1997 return hasPrivilege_codesTable;
1998 } 1998 }
1999 1999
2006 * The result is cached and reused, so that we only test the query once per connection. 2006 * The result is cached and reused, so that we only test the query once per connection.
2007 * This method is used by methods from MonetDatabaseMetaData. 2007 * This method is used by methods from MonetDatabaseMetaData.
2008 */ 2008 */
2009 boolean commentsTableExists() { 2009 boolean commentsTableExists() {
2010 if (!queriedCommentsTable) { 2010 if (!queriedCommentsTable) {
2011 hasCommentsTable = existsSysTable("comments"); 2011 querySysTable();
2012 queriedCommentsTable = true; // set flag, so the querying is done only at first invocation. 2012 queriedCommentsTable = true; // set flag, so the querying is done only at first invocation.
2013 } 2013 }
2014 return hasCommentsTable; 2014 return hasCommentsTable;
2015 } 2015 }
2016 2016
2017 2017
2018 /** 2018 /**
2019 * Internal utility method to query the server to find out if it has a specific system table sys.<tablename>. 2019 * Internal utility method to query the server to find out if it has a specific system table sys.<tablename>.
2020 */ 2020 */
2021 private boolean existsSysTable(final String tablename) { 2021 private void querySysTable() {
2022 boolean exists = false;
2023 Statement stmt = null; 2022 Statement stmt = null;
2024 ResultSet rs = null; 2023 ResultSet rs = null;
2025 try { 2024 try {
2026 stmt = createStatement(); 2025 stmt = createStatement();
2027 if (stmt != null) { 2026 if (stmt != null) {
2028 rs = stmt.executeQuery("SELECT id FROM sys._tables WHERE name = '" 2027 rs = stmt.executeQuery("SELECT name FROM sys._tables WHERE name in ('privilege_codes', 'comments')"
2029 + tablename 2028 + " AND schema_id IN (SELECT id FROM sys.schemas WHERE name = 'sys')");
2030 + "' AND schema_id IN (SELECT id FROM sys.schemas WHERE name = 'sys')");
2031 if (rs != null) { 2029 if (rs != null) {
2032 exists = rs.next(); // if a row is available it exists, else not 2030 while (rs.next()) {
2031 String name = rs.getString(1);
2032 // for debug: System.out.println("querySysTable() retrieved name: " + name);
2033 if ("comments".equals(name)) {
2034 hasCommentsTable = true;
2035 queriedCommentsTable = true;
2036 } else
2037 if ("privilege_codes".equals(name)) {
2038 hasPrivilege_codesTable = true;
2039 queriedPrivilege_codesTable = true;
2040 }
2041 }
2033 } 2042 }
2034 } 2043 }
2035 } catch (SQLException se) { 2044 } catch (SQLException se) {
2036 /* ignore */ 2045 /* ignore */
2037 } finally { 2046 } finally {
2038 closeResultsetStatement(rs, stmt); 2047 closeResultsetStatement(rs, stmt);
2039 } 2048 }
2040 // for debug: System.out.println("testTableExists(" + tablename + ") returns: " + exists);
2041 return exists;
2042 } 2049 }
2043 2050
2044 /** 2051 /**
2045 * Closes a ResultSet and/or Statement object without throwing any SQLExceptions 2052 * Closes a ResultSet and/or Statement object without throwing any SQLExceptions
2046 * It can be used in the finally clause after creating a Statement and 2053 * It can be used in the finally clause after creating a Statement and