/** * loop through entry types to get required, optional, general and utility fields for this type. */ private static void refreshFields() { if (SQLUtil.allFields == null) { SQLUtil.allFields = new ArrayList<>(); } else { SQLUtil.allFields.clear(); } SQLUtil.uniqueListInsert(SQLUtil.allFields, BibtexFields.getAllFieldNames()); SQLUtil.uniqueListInsert(SQLUtil.allFields, BibtexFields.getAllPrivateFieldNames()); }
/** * Process a query and returns only the first result of a result set as a String. To be used when * it is certain that only one String (single cell) will be returned from the DB * * @param conn The Connection object to which the DML should be sent * @param query The query statements to be processed * @return String with the result returned from the database * @throws SQLException */ public static String processQueryWithSingleResult(Connection conn, String query) throws SQLException { try (Statement sm = SQLUtil.executeQueryWithResults(conn, query); ResultSet rs = sm.getResultSet()) { rs.next(); String result = rs.getString(1); rs.getStatement().close(); return result; } }
/** * Utility method for processing DML with proper output * * @param out The output (PrintStream or Connection) object to which the DML should be sent * @param dml The DML statements to be processed */ public static void processQuery(Object out, String dml) throws SQLException { if (out instanceof PrintStream) { PrintStream fout = (PrintStream) out; fout.println(dml); } if (out instanceof Connection) { Connection conn = (Connection) out; SQLUtil.executeQuery(conn, dml); } }
/** @return Create a common separated field names */ public static String getFieldStr() { // create comma separated list of field names String field; List<String> fieldNames = new ArrayList<>(); for (int i = 0; i < SQLUtil.getAllFields().size(); i++) { field = SQLUtil.allFields.get(i); if (SQLUtil.reservedDBWords.contains(field)) { field += "_"; } fieldNames.add(field); } return String.join(", ", fieldNames); }
/** * Utility method for processing DML with proper output * * @param out The output (PrintStream or Connection) object to which the DML should be sent * @param query The DML statements to be processed * @return the result of the statement */ public static AutoCloseable processQueryWithResults(Object out, String query) throws SQLException { if (out instanceof PrintStream) { // TODO: how to handle the PrintStream // case? PrintStream fout = (PrintStream) out; fout.println(query); return fout; } if (out instanceof Connection) { Connection conn = (Connection) out; return SQLUtil.executeQueryWithResults(conn, query); } return null; }
/** @return All existent fields for a bibtex entry */ public static ArrayList<String> getAllFields() { if (SQLUtil.allFields == null) { SQLUtil.refreshFields(); } return SQLUtil.allFields; }
/** * return a ResultSet with the result of a "SELECT *" query for a given table * * @param conn Connection to the database * @param tableName String containing the name of the table you want to get the results. * @return a ResultSet with the query result returned from the DB * @throws SQLException */ public static ResultSet queryAllFromTable(Connection conn, String tableName) throws SQLException { String query = "SELECT * FROM " + tableName + ';'; try (Statement res = (Statement) SQLUtil.processQueryWithResults(conn, query)) { return res.getResultSet(); } }