Search in sources :

Example 1 with ColumnNaming

use of easik.database.base.ColumnNaming in project fql by CategoricalData.

the class DatabaseUtil method getRecord.

/**
 * Returns a Map of column names to their value for a given table/primary
 * key pair. If no record exists in the table with the given primary key, we
 * return an empty Map. Can throw a Database.LoadException or SQLException
 * in the event that a db driver can not be acquired.
 *
 * @param table
 *            The table from which we get the requested row.
 * @param pk
 *            The primary id of the row which we want
 * @return A mapping of column names to their corresponding value for the
 *         given table/id pair. If no such row exists, returns an empty Map.
 */
public static Map<String, String> getRecord(final EntityNode table, final int pk) {
    final JDBCDriver dbd;
    final ColumnNaming cn;
    final Map<String, String> data = new HashMap<>(20);
    final ResultSet result;
    try {
        dbd = table.getMModel().getDatabase().getJDBCDriver();
        cn = new ColumnNaming(dbd);
        final String select = "SELECT * FROM " + dbd.quoteId(table.getName()) + " WHERE " + cn.tablePK(table) + " = " + pk;
        result = dbd.executeQuery(select);
        final ResultSetMetaData rsmd = result.getMetaData();
        result.next();
        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
            data.put(dbd.quoteId(rsmd.getColumnName(i)), result.getString(i));
        }
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(table.getMModel().getFrame(), "Could not get record: " + e.getMessage());
    }
    return data;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ColumnNaming(easik.database.base.ColumnNaming) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) JDBCDriver(easik.database.api.jdbc.JDBCDriver)

Example 2 with ColumnNaming

use of easik.database.base.ColumnNaming 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 3 with ColumnNaming

use of easik.database.base.ColumnNaming 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)3 ColumnNaming (easik.database.base.ColumnNaming)3 ResultSet (java.sql.ResultSet)3 ResultSetMetaData (java.sql.ResultSetMetaData)2 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)1 Vector (java.util.Vector)1 JTable (javax.swing.JTable)1