use of easik.database.api.jdbc.JDBCDriver in project fql by CategoricalData.
the class DatabaseUtil method getPKs.
/**
* Returns an array of the primary keys found in a given table. An array of
* size 0 is returned in the event of a db access error.
*
* @param table
* The entity node representing the table from which we wish to
* get the primary IDs
* @return The primary IDs of all records found in the given table
*/
public static int[] getPKs(final EntityNode table) {
final JDBCDriver dbd;
final ColumnNaming cn;
final ResultSet result;
try {
dbd = table.getMModel().getDatabase().getJDBCDriver();
cn = new ColumnNaming(dbd);
result = dbd.executeQuery("SELECT * FROM " + table.getName());
// move to last row to get row count
result.last();
final int[] pKs = new int[result.getRow()];
// move back to first row
result.beforeFirst();
result.next();
final String pIdName = cn.tablePK(table);
for (int i = 0; i < pKs.length; i++) {
pKs[i] = result.getInt(pIdName);
result.next();
}
return pKs;
} catch (SQLException e) {
System.err.println("Error in DatabaseUtil: " + e.getMessage());
return new int[0];
}
}
use of easik.database.api.jdbc.JDBCDriver in project fql by CategoricalData.
the class DatabaseUtil method getTable.
/**
* Gets the contents of a given table, and returns them as a JTable. The
* first column is the record's primary key, and the remaining columns are
* layed out in the order EntityNode.getEntityAttributes() is stored.
*
* @param table
* The EntityNode representing the table we wish to view
* @return The contents of the given table wrapped as a JTable
*
* @throws PersistenceDriver.LoadException
* @throws SQLException
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static JTable getTable(final EntityNode table) throws SQLException, PersistenceDriver.LoadException {
final JDBCDriver dbd;
@SuppressWarnings("unused") final ColumnNaming cn;
final Vector<Object[]> data;
final String[] columnNames;
final int cols;
// Get column names of "shadow edges". We will ignore these columns.
/**
* Removing all traces of shadow edges
*
* final Collection<SketchEdge> shadowEdges = new
* HashSet<SketchEdge>(table.getShadowEdges()); final Collection<String>
* shadowColumns = new HashSet<String>(shadowEdges.size());
*
* for (final SketchEdge edge : shadowEdges) {
* shadowColumns.add(edge.getName()); }
*/
final ResultSet result;
dbd = table.getMModel().getDatabase().getJDBCDriver();
cn = new ColumnNaming(dbd);
final String mess = "SELECT * FROM " + dbd.quoteId(table.getName());
result = dbd.executeQuery(mess);
final ResultSetMetaData rsmd = result.getMetaData();
// build 2D array of data
// - shadowEdges.size();
cols = rsmd.getColumnCount();
data = new Vector();
columnNames = new String[cols];
int currRow, i;
// Populate array of column names
for (i = 0; i < cols; i++) {
final String columnName = rsmd.getColumnName(i + 1);
// if (!(shadowColumns.contains(columnName)))
// {
columnNames[i] = columnName;
// }
}
while (result.next()) {
currRow = result.getRow();
// if we have no rows, break
if (currRow == 0) {
break;
}
final Object[] row = new Object[cols];
for (i = 0; i < cols; i++) {
row[i] = result.getString(columnNames[i]);
}
data.add(row);
}
final Object[][] dataObj = data.toArray(new Object[0][0]);
return new JTable(dataObj, columnNames);
}
Aggregations