Search in sources :

Example 1 with JDBCDriver

use of easik.database.api.jdbc.JDBCDriver in project fql by CategoricalData.

the class DatabaseUtil method displayQueryNode.

/**
 * @param node
 * @param parent
 */
@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
public static void displayQueryNode(final QueryNode node, final JFrame parent) {
    final JDBCDriver dbd;
    final ResultSet result;
    final ResultSetMetaData rsmd;
    final Vector<Object[]> data;
    final JTable asTable;
    final String[] columnNames;
    final int cols;
    try {
        dbd = node.getMModel().getSketch().getDatabase().getJDBCDriver();
        result = dbd.executeQuery(node.getQuery());
        rsmd = result.getMetaData();
        cols = rsmd.getColumnCount();
        // build 2D array of data
        data = new Vector();
        columnNames = new String[cols];
        int currRow, i;
        // Populate array of column names
        for (i = 0; i < cols; i++) {
            columnNames[i] = rsmd.getColumnName(i + 1);
        }
        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]);
        asTable = new JTable(dataObj, columnNames);
        new SelectDataDialog(parent, node.getName(), asTable);
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(node.getMModel().getSketch().getFrame(), "Could not get record: " + e.getMessage());
    }
}
Also used : SQLException(java.sql.SQLException) SelectDataDialog(easik.ui.datamanip.SelectDataDialog) ResultSetMetaData(java.sql.ResultSetMetaData) JTable(javax.swing.JTable) ResultSet(java.sql.ResultSet) JDBCDriver(easik.database.api.jdbc.JDBCDriver) Vector(java.util.Vector)

Example 2 with JDBCDriver

use of easik.database.api.jdbc.JDBCDriver 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 3 with JDBCDriver

use of easik.database.api.jdbc.JDBCDriver in project fql by CategoricalData.

the class ExecPreparedInsertAction method actionPerformed.

/**
 * Create the new entity and set up its name
 *
 * @param e
 *            The action event
 */
@Override
public void actionPerformed(ActionEvent e) {
    Object[] currentSelection = _theSketch.getSelectionCells();
    if (!(currentSelection[0] instanceof EntityNode)) {
        return;
    }
    EntityNode table = (EntityNode) currentSelection[0];
    JDBCDriver dbd = null;
    dbd = _theSketch.getDatabase().getJDBCDriver();
    if (dbd == null) {
        // The user hit Cancel
        return;
    }
    FreeQueryDialog afqd;
    String text = "INSERT INTO " + table.getName() + "() VALUES()";
    while (true) {
        afqd = new FreeQueryDialog(_theSketch.getFrame(), text);
        if (!afqd.isAccepted()) {
            return;
        }
        try {
            String input = afqd.getInput();
            dbd.executeUpdate(input);
            return;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
            text = afqd.getInput();
            continue;
        }
    }
}
Also used : SQLException(java.sql.SQLException) FreeQueryDialog(easik.ui.datamanip.FreeQueryDialog) JDBCDriver(easik.database.api.jdbc.JDBCDriver) EntityNode(easik.sketch.vertex.EntityNode)

Example 4 with JDBCDriver

use of easik.database.api.jdbc.JDBCDriver in project fql by CategoricalData.

the class ExecPreparedDeleteAction method actionPerformed.

/**
 * Create the new entity and set up its name
 *
 * @param e
 *            The action event
 */
@Override
public void actionPerformed(ActionEvent e) {
    Object[] currentSelection = _theSketch.getSelectionCells();
    if (!(currentSelection[0] instanceof EntityNode)) {
        return;
    }
    EntityNode table = (EntityNode) currentSelection[0];
    JDBCDriver dbd = null;
    dbd = _theSketch.getDatabase().getJDBCDriver();
    if (dbd == null) {
        // The user hit Cancel
        return;
    }
    FreeQueryDialog afqd;
    String text = "DELETE FROM " + table.getName() + "\n WHERE()";
    while (true) {
        afqd = new FreeQueryDialog(_theSketch.getFrame(), text);
        if (!afqd.isAccepted()) {
            return;
        }
        try {
            String input = afqd.getInput();
            dbd.executeUpdate(input);
            return;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
            text = afqd.getInput();
            continue;
        }
    }
}
Also used : SQLException(java.sql.SQLException) FreeQueryDialog(easik.ui.datamanip.FreeQueryDialog) JDBCDriver(easik.database.api.jdbc.JDBCDriver) EntityNode(easik.sketch.vertex.EntityNode)

Example 5 with JDBCDriver

use of easik.database.api.jdbc.JDBCDriver in project fql by CategoricalData.

the class DatabaseUtil method selectRowPKs.

/**
 * Pops up a dialog for the user to select records from the table
 * represented by the given entity node. The primary IDs of the records are
 * returned.
 *
 * @param frame
 *            the JFrame to attach the dialog to
 * @param table
 *            The entity node representing the table from which we wish to
 *            select the records
 * @return An array of primary IDs representing the records we selected
 */
public static int[] selectRowPKs(final JFrame frame, final EntityNode table) {
    @SuppressWarnings("unused") final JDBCDriver dbd;
    final JTable tableData;
    try {
        dbd = table.getMModel().getDatabase().getJDBCDriver();
        tableData = getTable(table);
    } catch (PersistenceDriver.LoadException e) {
        System.err.println("Error in DatabaseUtil: " + e.getMessage());
        return null;
    } catch (SQLException e) {
        System.err.println("Error in DatabaseUtil: " + e.getMessage());
        return null;
    }
    final SelectDataDialog sdd = new SelectDataDialog(frame, table.getName(), tableData);
    if (sdd.isAccepted()) {
        return sdd.getSelectedPKs();
    }
    return new int[0];
}
Also used : SQLException(java.sql.SQLException) PersistenceDriver(easik.database.base.PersistenceDriver) JTable(javax.swing.JTable) SelectDataDialog(easik.ui.datamanip.SelectDataDialog) JDBCDriver(easik.database.api.jdbc.JDBCDriver)

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