Search in sources :

Example 6 with ColumnEntry

use of easik.ui.datamanip.ColumnEntry in project fql by CategoricalData.

the class JDBCUpdateMonitor method promptAndInsert.

/**
 * Initializes a data insertion dialog and waits for the user to insert and
 * accept the data. Once Accepted, forms an input statement and a map of
 * input to its type which is handed to Database.executePreparedUpdate(..).
 *
 * @param table
 *            the EntityNode
 * @param dOpts
 *            A DialogOptions object containing maps as specified in
 *            easik.ui.datamanip.RowEntryDialog
 * @param forced
 *            A set of ColumnEntry objects that will be used in the
 *            generated INSERT statement which the user has no control over
 *            (i.e. the columns hadn't not shown up in the entry dialog, but
 *            we still wish to include them).
 *
 *            No column name in this set should match one in dOpts. Should
 *            this happen, is is undetermined which which of the two entries
 *            will appear first in the input, and therefore the result after
 *            INSERT is undetermined.
 * @return Success of insert
 *
 * @throws SQLException
 */
private boolean promptAndInsert(final EntityNode table, final DialogOptions dOpts, final Set<ColumnEntry> forced) throws SQLException {
    final String tableName = table.getName();
    final RowEntryDialog red = new RowEntryDialog(table.getMModel().getFrame(), "Add row to table: " + tableName, dOpts.attToType, dOpts.fKeys);
    if (!red.isAccepted()) {
        return false;
    }
    final Set<ColumnEntry> input = new LinkedHashSet<>(red.getInput());
    input.addAll(forced);
    final StringBuilder sb = new StringBuilder("INSERT INTO " + dbd.quoteId(tableName) + ' ');
    final Collection<String> colNames = new LinkedList<>();
    final Collection<String> values = new LinkedList<>();
    for (final ColumnEntry entry : input) {
        colNames.add(dbd.quoteId(entry.getColumnName()));
        values.add("?");
    }
    if (colNames.isEmpty()) {
        sb.append(dbd.emptyInsertClause());
    } else {
        sb.append('(').append(EasikTools.join(", ", colNames)).append(") VALUES (").append(EasikTools.join(", ", values)).append(')');
    }
    dbd.executePreparedUpdate(sb.toString(), input);
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RowEntryDialog(easik.ui.datamanip.RowEntryDialog) ColumnEntry(easik.ui.datamanip.ColumnEntry) LinkedList(java.util.LinkedList)

Example 7 with ColumnEntry

use of easik.ui.datamanip.ColumnEntry in project fql by CategoricalData.

the class JDBCUpdateMonitor method updateRow.

/**
 * Pops up a row entry dialog for the given table. The default field values
 * will be the values stored in the row of the table who's primary ID
 * matches the one given. Some cases call for use to restrict the update of
 * foreign keys. Those cases are: a) A pullback b) The summands of a sum
 * constrain c) The product of a product constraint If the table is in a
 * constraint and the user wishes to break it the update is aborted leaving
 * the old values in the record. Note: There are currently no checks to
 * ensure that pk matches a record in our table.
 *
 * @param table
 *            The EntityNode representing the table we wish to update.
 * @param pk
 *            The primary ID of the row whos data will be used to set
 *            defaults for our row entry dialog.
 * @return The Success of the update.
 */
@Override
public boolean updateRow(final EntityNode table, final int pk) {
    final DialogOptions dOpts = getDialogOptions(table);
    try {
        // Remove option to update foreign keys restricted by constraints
        for (final ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> c : table.getConstraints()) {
            for (final SketchEdge se : table.getOutgoingEdges()) {
                if (c.hasEdge(se)) {
                    dOpts.fKeys.remove(cn.tableFK(se));
                }
            }
        }
        // Display dialog for user with defaults as old record values
        final RowEntryDialog red = new RowEntryDialog(_theSketch.getFrame(), "Update " + table.getName() + ": " + cn.tablePK(table) + " = " + pk, dOpts.attToType, dOpts.fKeys, DatabaseUtil.getRecord(table, pk));
        if (red.isAccepted()) {
            final Set<ColumnEntry> input = new LinkedHashSet<>(red.getInput());
            // if we do not have any new input, no need to execute an update
            if (input.isEmpty()) {
                return true;
            }
            final StringBuilder sb = new StringBuilder("UPDATE " + table.getName() + " SET ");
            for (final ColumnEntry ce : input) {
                sb.append(dbd.quoteId(ce.getColumnName())).append('=' + "?,");
            }
            // remove last comma
            sb.delete(sb.length() - 1, sb.length());
            sb.append(" WHERE ").append(cn.tablePK(table)).append('=').append(pk);
            dbd.executePreparedUpdate(sb.toString(), input);
            return true;
        }
        return false;
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(table.getMModel().getFrame(), "Could not update record: " + e.getMessage());
        return false;
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SQLException(java.sql.SQLException) ColumnEntry(easik.ui.datamanip.ColumnEntry) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) EntityNode(easik.sketch.vertex.EntityNode) SketchFrame(easik.ui.SketchFrame) SketchEdge(easik.sketch.edge.SketchEdge) Sketch(easik.sketch.Sketch) RowEntryDialog(easik.ui.datamanip.RowEntryDialog)

Example 8 with ColumnEntry

use of easik.ui.datamanip.ColumnEntry in project fql by CategoricalData.

the class JDBCViewUpdateMonitor method promptAndInsert.

/**
 * Initializes a data insertion dialog and waits for the user to insert and
 * accept the data. Once Accepted, forms an input statement and a map of
 * input to its type which is handed to Database.executePreparedUpdate(..).
 *
 * @param table
 *            the EntityNode
 * @param dOpts
 *            A DialogOptions object containing maps as specified in
 *            easik.ui.datamanip.RowEntryDialog
 * @param forced
 *            A set of ColumnEntry objects that will be used in the
 *            generated INSERT statement which the user has no control over
 *            (i.e. the columns hadn't not shown up in the entry dialog, but
 *            we still wish to include them).
 *
 *            No column name in this set should match one in dOpts. Should
 *            this happen, is is undetermined which which of the two entries
 *            will appear first in the input, and therefore the result after
 *            INSERT is undetermined.
 * @return Success of insert
 *
 * @throws SQLException
 */
private boolean promptAndInsert(final EntityNode table, final DialogOptions dOpts, final Set<ColumnEntry> forced) throws SQLException {
    final String tableName = table.getName();
    final RowEntryDialog red = new RowEntryDialog(table.getMModel().getFrame(), "Add row to table: " + tableName, dOpts.attToType, dOpts.fKeys);
    if (!red.isAccepted()) {
        return false;
    }
    final Set<ColumnEntry> input = new LinkedHashSet<>(red.getInput());
    input.addAll(forced);
    final StringBuilder sb = new StringBuilder("INSERT INTO " + dbd.quoteId(tableName) + ' ');
    final Collection<String> colNames = new LinkedList<>();
    final Collection<String> values = new LinkedList<>();
    for (final ColumnEntry entry : input) {
        colNames.add(dbd.quoteId(entry.getColumnName()));
        values.add("?");
    }
    if (colNames.isEmpty()) {
        sb.append(dbd.emptyInsertClause());
    } else {
        sb.append('(').append(EasikTools.join(", ", colNames)).append(") VALUES (").append(EasikTools.join(", ", values)).append(')');
    }
    dbd.executePreparedUpdate(sb.toString(), input);
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RowEntryDialog(easik.ui.datamanip.RowEntryDialog) ColumnEntry(easik.ui.datamanip.ColumnEntry) LinkedList(java.util.LinkedList)

Aggregations

ColumnEntry (easik.ui.datamanip.ColumnEntry)8 LinkedHashSet (java.util.LinkedHashSet)7 SQLException (java.sql.SQLException)5 Int (easik.database.types.Int)4 LimitConstraint (easik.model.constraint.LimitConstraint)4 ModelConstraint (easik.model.constraint.ModelConstraint)4 ProductConstraint (easik.model.constraint.ProductConstraint)4 PullbackConstraint (easik.model.constraint.PullbackConstraint)4 SumConstraint (easik.model.constraint.SumConstraint)4 Sketch (easik.sketch.Sketch)4 SketchEdge (easik.sketch.edge.SketchEdge)4 SketchGraphModel (easik.sketch.util.graph.SketchGraphModel)4 EntityNode (easik.sketch.vertex.EntityNode)4 SketchFrame (easik.ui.SketchFrame)4 RowEntryDialog (easik.ui.datamanip.RowEntryDialog)3 ResultSet (java.sql.ResultSet)3 CommutativeDiagram (easik.model.constraint.CommutativeDiagram)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 EasikType (easik.database.types.EasikType)1