List of usage examples for weka.experiment ResultProducer getResultNames
String[] getResultNames() throws Exception;
From source file:py.fpuna.lib.ExtendedDatabaseUtils.java
License:Open Source License
/** * Executes a database query to extract a result for the supplied key from the * database./*from w w w .j av a 2 s .c o m*/ * * @param tableName the name of the table where the result is stored * @param rp the ResultProducer who will generate the result if required * @param key the key for the result * @return true if the result with that key is in the database already * @throws Exception if an error occurs */ public Object[] getResultFromTable(String tableName, ResultProducer rp, Object[] key) throws Exception { String query = "SELECT "; String[] resultNames = rp.getResultNames(); for (int i = 0; i < resultNames.length; i++) { if (i != 0) { query += ", "; } query += resultNames[i]; } query += " FROM " + tableName; String[] keyNames = rp.getKeyNames(); if (keyNames.length != key.length) { throw new Exception("Key names and key values of different lengths"); } boolean first = true; for (int i = 0; i < key.length; i++) { if (key[i] != null) { if (first) { query += " WHERE "; first = false; } else { query += " AND "; } query += "Key_" + keyNames[i] + '='; if (key[i] instanceof String) { query += "'" + processKeyString(key[i].toString()) + "'"; } else { query += key[i].toString(); } } } ResultSet rs = select(query); ResultSetMetaData md = rs.getMetaData(); int numAttributes = md.getColumnCount(); if (!rs.next()) { throw new Exception("No result for query: " + query); } // Extract the columns for the result Object[] result = new Object[numAttributes]; for (int i = 1; i <= numAttributes; i++) { switch (translateDBColumnType(md.getColumnTypeName(i))) { case STRING: result[i - 1] = rs.getString(i); if (rs.wasNull()) { result[i - 1] = null; } break; case FLOAT: case DOUBLE: result[i - 1] = new Double(rs.getDouble(i)); if (rs.wasNull()) { result[i - 1] = null; } break; default: throw new Exception("Unhandled SQL result type (field " + (i + 1) + "): " + ExtendedDatabaseUtils.typeName(md.getColumnType(i))); } } if (rs.next()) { throw new Exception("More than one result entry " + "for result key: " + query); } close(rs); return result; }
From source file:py.fpuna.lib.ExtendedDatabaseUtils.java
License:Open Source License
/** * Creates a results table for the supplied result producer. * * @param rp the ResultProducer generating the results * @param tableName the name of the resultsTable * @return the name of the created results table * @throws Exception if an error occurs. *///from w w w.j a va 2s.c om public String createResultsTable(ResultProducer rp, String tableName) throws Exception { if (m_Debug) { System.err.println("Creating results table " + tableName + "..."); } String query = "CREATE TABLE " + tableName + " ( "; // Loop over the key fields String[] names = rp.getKeyNames(); Object[] types = rp.getKeyTypes(); if (names.length != types.length) { throw new Exception("key names types differ in length"); } for (int i = 0; i < names.length; i++) { query += "Key_" + names[i] + " "; if (types[i] instanceof Double) { query += m_doubleType; } else if (types[i] instanceof String) { // Workaround for MySQL (doesn't support LONGVARCHAR) // Also for InstantDB which attempts to interpret numbers when storing // in LONGVARBINARY /* * if (m_Connection.getMetaData().getDriverName(). * equals("Mark Matthews' MySQL Driver") || * (m_Connection.getMetaData().getDriverName(). * indexOf("InstantDB JDBC Driver")) != -1) { query += "TEXT "; } else { */ // query += "LONGVARCHAR "; query += m_stringType + " "; // } } else { throw new Exception("Unknown/unsupported field type in key"); } query += ", "; } // Loop over the result fields names = rp.getResultNames(); types = rp.getResultTypes(); if (names.length != types.length) { throw new Exception("result names and types differ in length"); } for (int i = 0; i < names.length; i++) { query += names[i] + " "; if (types[i] instanceof Double) { query += m_doubleType; } else if (types[i] instanceof String) { // Workaround for MySQL (doesn't support LONGVARCHAR) // Also for InstantDB which attempts to interpret numbers when storing // in LONGVARBINARY /* * if (m_Connection.getMetaData().getDriverName(). * equals("Mark Matthews' MySQL Driver") || * (m_Connection.getMetaData().getDriverName(). * equals("InstantDB JDBC Driver"))) { query += "TEXT "; } else { */ // query += "LONGVARCHAR "; query += m_stringType + " "; // } } else { throw new Exception("Unknown/unsupported field type in key"); } if (i < names.length - 1) { query += ", "; } } query += " )"; update(query); if (m_Debug) { System.err.println("table created"); } close(); if (m_createIndex) { query = "CREATE UNIQUE INDEX Key_IDX ON " + tableName + " ("; String[] keyNames = rp.getKeyNames(); boolean first = true; for (String keyName : keyNames) { if (keyName != null) { if (first) { first = false; query += "Key_" + keyName; } else { query += ",Key_" + keyName; } } } query += ")"; update(query); } return tableName; }