Search in sources :

Example 6 with JDBCDriver

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];
    }
}
Also used : ColumnNaming(easik.database.base.ColumnNaming) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) JDBCDriver(easik.database.api.jdbc.JDBCDriver)

Example 7 with JDBCDriver

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);
}
Also used : ColumnNaming(easik.database.base.ColumnNaming) ResultSetMetaData(java.sql.ResultSetMetaData) JTable(javax.swing.JTable) ResultSet(java.sql.ResultSet) JDBCDriver(easik.database.api.jdbc.JDBCDriver) Vector(java.util.Vector)

Aggregations

JDBCDriver (easik.database.api.jdbc.JDBCDriver)7 SQLException (java.sql.SQLException)6 ResultSet (java.sql.ResultSet)4 ColumnNaming (easik.database.base.ColumnNaming)3 ResultSetMetaData (java.sql.ResultSetMetaData)3 JTable (javax.swing.JTable)3 EntityNode (easik.sketch.vertex.EntityNode)2 FreeQueryDialog (easik.ui.datamanip.FreeQueryDialog)2 SelectDataDialog (easik.ui.datamanip.SelectDataDialog)2 Vector (java.util.Vector)2 PersistenceDriver (easik.database.base.PersistenceDriver)1 HashMap (java.util.HashMap)1