Beispiel #1
0
 /**
  * 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());
 }
Beispiel #2
0
 /**
  * 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;
   }
 }
Beispiel #3
0
 /**
  * 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);
   }
 }
Beispiel #4
0
 /** @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);
 }
Beispiel #5
0
 /**
  * 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;
 }
Beispiel #6
0
 /** @return All existent fields for a bibtex entry */
 public static ArrayList<String> getAllFields() {
   if (SQLUtil.allFields == null) {
     SQLUtil.refreshFields();
   }
   return SQLUtil.allFields;
 }
Beispiel #7
0
 /**
  * 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();
   }
 }