Search in sources :

Example 1 with ColumnEntry

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

the class JDBCUpdateMonitor method deleteFrom.

/**
 * Trys to delete from a given table. If the action will break a constraint,
 * it is aborted and the user is notified.
 *
 * @param table
 *            The table from which we will attempt the delete
 * @return The success of the delete
 */
@Override
public boolean deleteFrom(final EntityNode table) {
    final int[] selectedPKs = DatabaseUtil.selectRowPKs(table.getMModel().getFrame(), table);
    // if there was a selection
    if (selectedPKs.length > 0) {
        final String PKcolumn = cn.tablePK(table);
        final StringBuilder sb = new StringBuilder("DELETE FROM " + dbd.quoteId(table.getName()) + " WHERE ");
        // populate input set for prepared statement while adding column
        // names
        final Set<ColumnEntry> input = new LinkedHashSet<>(selectedPKs.length);
        for (final int pk : selectedPKs) {
            for (EntityAttribute<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> ea : table.getHiddenEntityAttributes()) {
                try {
                    ResultSet result = dbd.executeQuery("SELECT * FROM " + table.getName() + " Where id=" + pk + " AND " + ea.getName() + "= 1");
                    // we do this by .isBeforeFirst()
                    if (result.isBeforeFirst()) {
                        JOptionPane.showMessageDialog(null, "Unable to execute DELETE: Deleting row will cause constraint inconsistency");
                        return false;
                    }
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            sb.append(PKcolumn).append("=? OR ");
            input.add(new ColumnEntry(PKcolumn, Integer.toString(pk), new Int()));
        }
        // remove last ',OR '
        sb.delete(sb.length() - 4, sb.length());
        try {
            dbd.executePreparedUpdate(sb.toString(), input);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Unable to execute DELETE: " + e.getMessage());
        }
    }
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SQLException(java.sql.SQLException) ColumnEntry(easik.ui.datamanip.ColumnEntry) LimitConstraint(easik.model.constraint.LimitConstraint) ProductConstraint(easik.model.constraint.ProductConstraint) SumConstraint(easik.model.constraint.SumConstraint) PullbackConstraint(easik.model.constraint.PullbackConstraint) ModelConstraint(easik.model.constraint.ModelConstraint) Int(easik.database.types.Int) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) EntityNode(easik.sketch.vertex.EntityNode) SketchFrame(easik.ui.SketchFrame) SketchEdge(easik.sketch.edge.SketchEdge) ResultSet(java.sql.ResultSet) Sketch(easik.sketch.Sketch)

Example 2 with ColumnEntry

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

the class JDBCViewUpdateMonitor method deleteFrom.

/**
 * Trys to delete from a given table. If the action will break a constraint,
 * it is aborted and the user is notified.
 *
 * @param table
 *            The table from which we will attempt the delete
 * @return The success of the delete
 */
@Override
public boolean deleteFrom(final EntityNode table) {
    final int[] selectedPKs = DatabaseUtil.selectRowPKs(table.getMModel().getFrame(), table);
    // if there was a selection
    if (selectedPKs.length > 0) {
        final String PKcolumn = cn.tablePK(table);
        final StringBuilder sb = new StringBuilder("DELETE FROM " + dbd.quoteId(table.getName()) + " WHERE ");
        // populate input set for prepared statement while adding column
        // names
        final Set<ColumnEntry> input = new LinkedHashSet<>(selectedPKs.length);
        for (final int pk : selectedPKs) {
            sb.append(PKcolumn).append("=? OR ");
            input.add(new ColumnEntry(PKcolumn, Integer.toString(pk), new Int()));
        }
        // remove last ',OR '
        sb.delete(sb.length() - 4, sb.length());
        try {
            dbd.executePreparedUpdate(sb.toString(), input);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Unable to execute DELETE: " + e.getMessage());
        }
    }
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SQLException(java.sql.SQLException) ColumnEntry(easik.ui.datamanip.ColumnEntry) LimitConstraint(easik.model.constraint.LimitConstraint) ProductConstraint(easik.model.constraint.ProductConstraint) SumConstraint(easik.model.constraint.SumConstraint) PullbackConstraint(easik.model.constraint.PullbackConstraint) ModelConstraint(easik.model.constraint.ModelConstraint) Int(easik.database.types.Int)

Example 3 with ColumnEntry

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

the class JDBCViewUpdateMonitor method insert.

/**
 * Determines if insertion into a given table requires special handling due
 * to constraints it may be in. As of now, special cases that may result
 * from being in multiple constraints are not supported.
 *
 * @param table
 *            The table into which we wish to insert data
 * @return Success of the insertion
 */
@Override
public boolean insert(final EntityNode table) {
    final DialogOptions dOpts = getDialogOptions(table);
    final String lineSep = EasikTools.systemLineSeparator();
    // a set of column-value pairs of which we wish to force a specific
    // value, leaving the user out
    final Set<ColumnEntry> forced = new HashSet<>(10);
    // contstraint. Tighten up?
    for (final ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> c : table.getConstraints()) {
        if (c instanceof SumConstraint) {
            // of its foreign key, so remove it from the dialog's selection
            for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> sp : c.getPaths()) {
                if (sp.getDomain() == table) {
                    // we force the value 0 to avoid out driver to kick back
                    // an error for having a null fKey
                    final String columnName = cn.tableFK(sp.getFirstEdge());
                    dOpts.fKeys.remove(columnName);
                    forced.add(new ColumnEntry(columnName, "0", new Int()));
                    break;
                }
            }
        }
        if (c instanceof CommutativeDiagram) {
            // commute
            if (c.getPaths().get(0).getDomain() == table) {
                JOptionPane.showMessageDialog(null, "Be sure that the following paths commute:" + lineSep + EasikTools.join(lineSep, c.getPaths()), "Commutative diagram", JOptionPane.INFORMATION_MESSAGE);
                try {
                    return promptAndInsert(table, dOpts, forced);
                } catch (SQLException e) {
                    JOptionPane.showMessageDialog(null, "Not all of the following paths commute -- insert aborted!" + lineSep + EasikTools.join(lineSep, c.getPaths()), "Commutative diagram failure", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
        if (c instanceof PullbackConstraint) {
            // happens, we want to let the user update the new record
            if (((PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c).getTarget() != table) {
                final EntityNode pullback = ((PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c).getSource();
                try {
                    // get row count pre-insert
                    ResultSet result = dbd.executeQuery("SELECT COUNT(*) FROM " + pullback.getName() + " X");
                    result.next();
                    final int preRowCount = result.getInt(1);
                    if (!promptAndInsert(table, dOpts)) {
                        return false;
                    }
                    // get row count post-insert
                    result = dbd.executeQuery("SELECT COUNT(*) FROM " + pullback.getName() + " X");
                    result.next();
                    final int postRowCount = result.getInt(1);
                    // new row (the one with the highest primary ID)
                    if (postRowCount > preRowCount) {
                        result = dbd.executeQuery("SELECT MAX(" + cn.tablePK(pullback) + ") FROM " + pullback.getName() + " X");
                        result.next();
                        final int pk = result.getInt(1);
                        if (JOptionPane.showConfirmDialog(null, "New record in pullback table '" + pullback.getName() + "'. Enter column data?", "Insert column data?", JOptionPane.YES_NO_OPTION) == 0) {
                            updateRow(pullback, pk);
                        }
                    }
                    return true;
                } catch (SQLException e) {
                    JOptionPane.showMessageDialog(null, "Could not execute update: " + e.getMessage());
                }
            }
        }
        if (c instanceof ProductConstraint) {
            // inserting into the product.
            for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> sp : c.getPaths()) {
                if (sp.getCoDomain() == table) {
                    final EntityNode product = sp.getDomain();
                    try {
                        if (!promptAndInsert(table, dOpts)) {
                            return false;
                        }
                        // get the new records from the product. They are
                        // any record who's fk to our INSERT factor matches
                        // the primary id of the last insert
                        ResultSet result = dbd.executeQuery("SELECT MAX(" + cn.tablePK(table) + ") FROM " + table.getName() + " X");
                        result.next();
                        final int newPK = result.getInt(1);
                        result = dbd.executeQuery("SELECT * FROM " + product.getName() + " WHERE " + cn.tableFK(sp.getFirstEdge()) + " = " + newPK);
                        // get count of new rows as result of INSERT
                        result.last();
                        final int newRows = result.getRow();
                        result.beforeFirst();
                        if ((newRows > 0) && (JOptionPane.showConfirmDialog(null, newRows + " new rows in product table '" + product.getName() + "'. Insert column data?", "Insert column data?", JOptionPane.YES_NO_OPTION) == 0)) {
                            while (result.next()) {
                                updateRow(product, result.getInt(1));
                            }
                        }
                    } catch (SQLException e) {
                        JOptionPane.showMessageDialog(null, e.getMessage());
                    }
                    return true;
                }
            }
        }
        if (c instanceof LimitConstraint) {
        // TRIANGLES TODO CF2012 Incomplete
        }
    }
    try {
        return promptAndInsert(table, dOpts, forced);
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, "Could not execute update: " + e.getMessage());
        return false;
    }
}
Also used : LimitConstraint(easik.model.constraint.LimitConstraint) SQLException(java.sql.SQLException) ColumnEntry(easik.ui.datamanip.ColumnEntry) PullbackConstraint(easik.model.constraint.PullbackConstraint) SumConstraint(easik.model.constraint.SumConstraint) Int(easik.database.types.Int) LimitConstraint(easik.model.constraint.LimitConstraint) ProductConstraint(easik.model.constraint.ProductConstraint) SumConstraint(easik.model.constraint.SumConstraint) PullbackConstraint(easik.model.constraint.PullbackConstraint) ModelConstraint(easik.model.constraint.ModelConstraint) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) EntityNode(easik.sketch.vertex.EntityNode) SketchFrame(easik.ui.SketchFrame) ProductConstraint(easik.model.constraint.ProductConstraint) SketchEdge(easik.sketch.edge.SketchEdge) ResultSet(java.sql.ResultSet) Sketch(easik.sketch.Sketch) CommutativeDiagram(easik.model.constraint.CommutativeDiagram) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with ColumnEntry

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

the class JDBCDriver method executePreparedUpdate.

/**
 * Executes a given SQL updating statement. Which may be an INSERT, UPDATE,
 * or DELETE statement. The statement must be in the form expected by
 * java.sql.PreparedStatement
 *
 * @param sql
 *            The SQL statement to execute
 * @param input
 *            The set of ColumnEntry objects from which our values are
 *            retrieved. Note: This works but relys on 'input' being
 *            iterated over in the same order that it was when 'sql' was
 *            generated.
 *
 * @throws SQLException
 */
public void executePreparedUpdate(final String sql, final Set<ColumnEntry> input) throws SQLException {
    final PreparedStatement ps = prepareStatement(sql);
    int col = 0;
    for (final ColumnEntry entry : input) {
        final EasikType type = entry.getType();
        @SuppressWarnings("unused") final String value = entry.getValue();
        col++;
        // if value is NULL, assign and continue
        if ("".equals(entry.getValue())) {
            ps.setNull(col, type.getSqlType());
            continue;
        }
        // bind appropriate type to the prepared statement
        type.bindValue(ps, col, entry.getValue());
    }
    ps.execute();
}
Also used : PreparedStatement(java.sql.PreparedStatement) EasikType(easik.database.types.EasikType) ColumnEntry(easik.ui.datamanip.ColumnEntry)

Example 5 with ColumnEntry

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

the class JDBCUpdateMonitor method insert.

/**
 * Determines if insertion into a given table requires special handling due
 * to constraints it may be in. As of now, special cases that may result
 * from being in multiple constraints are not supported.
 *
 * @param table
 *            The table into which we wish to insert data
 * @return Success of the insertion
 */
@Override
public boolean insert(final EntityNode table) {
    final DialogOptions dOpts = getDialogOptions(table);
    final String lineSep = EasikTools.systemLineSeparator();
    // a set of column-value pairs of which we wish to force a specific
    // value, leaving the user out
    final Set<ColumnEntry> forced = new HashSet<>(10);
    // contstraint. Tighten up?
    for (final ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> c : table.getConstraints()) {
        if (c instanceof SumConstraint) {
            // of its foreign key, so remove it from the dialog's selection
            for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> sp : c.getPaths()) {
                if (sp.getDomain() == table) {
                    // we force the value 0 to avoid out driver to kick back
                    // an error for having a null fKey
                    final String columnName = cn.tableFK(sp.getFirstEdge());
                    dOpts.fKeys.remove(columnName);
                    forced.add(new ColumnEntry(columnName, "0", new Int()));
                    break;
                }
            }
        }
        if (c instanceof CommutativeDiagram) {
            // commute
            if (c.getPaths().get(0).getDomain() == table) {
                JOptionPane.showMessageDialog(null, "Be sure that the following paths commute:" + lineSep + EasikTools.join(lineSep, c.getPaths()), "Commutative diagram", JOptionPane.INFORMATION_MESSAGE);
                try {
                    return promptAndInsert(table, dOpts, forced);
                } catch (SQLException e) {
                    /*
						 * if(e instanceof com.mysql.jdbc.exceptions.jdbc4.
						 * MySQLIntegrityConstraintViolationException){
						 * //injective property violated
						 * JOptionPane.showMessageDialog(null, e.getMessage(),
						 * "Injective property violation",
						 * JOptionPane.ERROR_MESSAGE); }else{
						 */
                    JOptionPane.showMessageDialog(null, "Not all of the following paths commute -- insert aborted!" + lineSep + EasikTools.join(lineSep, c.getPaths()), "Commutative diagram failure", JOptionPane.ERROR_MESSAGE);
                // }
                }
            }
        }
        if (c instanceof PullbackConstraint) {
            // happens, we want to let the user update the new record
            if (((PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c).getTarget() != table) {
                final EntityNode pullback = ((PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c).getSource();
                try {
                    // get row count pre-insert
                    ResultSet result = dbd.executeQuery("SELECT COUNT(*) FROM " + pullback.getName() + " X");
                    result.next();
                    final int preRowCount = result.getInt(1);
                    if (!promptAndInsert(table, dOpts)) {
                        return false;
                    }
                    // get row count post-insert
                    result = dbd.executeQuery("SELECT COUNT(*) FROM " + pullback.getName() + " X");
                    result.next();
                    final int postRowCount = result.getInt(1);
                    // new row (the one with the highest primary ID)
                    if (postRowCount > preRowCount) {
                        result = dbd.executeQuery("SELECT MAX(" + cn.tablePK(pullback) + ") FROM " + pullback.getName() + " X");
                        result.next();
                        final int pk = result.getInt(1);
                        if (JOptionPane.showConfirmDialog(null, "New record in pullback table '" + pullback.getName() + "'. Enter column data?", "Insert column data?", JOptionPane.YES_NO_OPTION) == 0) {
                            updateRow(pullback, pk);
                        }
                    }
                    return true;
                } catch (SQLException e) {
                    JOptionPane.showMessageDialog(null, "Could not execute update. MYSQL Error output:\n" + e.getMessage());
                }
            }
        }
        if (c instanceof ProductConstraint) {
            // inserting into the product.
            for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> sp : c.getPaths()) {
                if (sp.getCoDomain() == table) {
                    final EntityNode product = sp.getDomain();
                    try {
                        if (!promptAndInsert(table, dOpts)) {
                            return false;
                        }
                        // get the new records from the product. They are
                        // any record who's fk to our INSERT factor matches
                        // the primary id of the last insert
                        ResultSet result = dbd.executeQuery("SELECT MAX(" + cn.tablePK(table) + ") FROM " + table.getName() + " X");
                        result.next();
                        final int newPK = result.getInt(1);
                        result = dbd.executeQuery("SELECT * FROM " + product.getName() + " WHERE " + cn.tableFK(sp.getFirstEdge()) + " = " + newPK);
                        // get count of new rows as result of INSERT
                        result.last();
                        final int newRows = result.getRow();
                        result.beforeFirst();
                        if ((newRows > 0) && (JOptionPane.showConfirmDialog(null, newRows + " new rows in product table '" + product.getName() + "'. Insert column data?", "Insert column data?", JOptionPane.YES_NO_OPTION) == 0)) {
                            while (result.next()) {
                                updateRow(product, result.getInt(1));
                            }
                        }
                    } catch (SQLException e) {
                        JOptionPane.showMessageDialog(null, e.getMessage());
                    }
                    return true;
                }
            }
        }
        if (c instanceof LimitConstraint) {
        // TRIANGLES TODO CF2012 Incomplete
        }
    }
    try {
        return promptAndInsert(table, dOpts, forced);
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, "Could not execute update. MYSQL Error output:\n" + e.getMessage());
        System.err.println(e.getMessage());
        return false;
    }
}
Also used : LimitConstraint(easik.model.constraint.LimitConstraint) SQLException(java.sql.SQLException) ColumnEntry(easik.ui.datamanip.ColumnEntry) PullbackConstraint(easik.model.constraint.PullbackConstraint) SumConstraint(easik.model.constraint.SumConstraint) Int(easik.database.types.Int) LimitConstraint(easik.model.constraint.LimitConstraint) ProductConstraint(easik.model.constraint.ProductConstraint) SumConstraint(easik.model.constraint.SumConstraint) PullbackConstraint(easik.model.constraint.PullbackConstraint) ModelConstraint(easik.model.constraint.ModelConstraint) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) EntityNode(easik.sketch.vertex.EntityNode) SketchFrame(easik.ui.SketchFrame) ProductConstraint(easik.model.constraint.ProductConstraint) SketchEdge(easik.sketch.edge.SketchEdge) ResultSet(java.sql.ResultSet) Sketch(easik.sketch.Sketch) CommutativeDiagram(easik.model.constraint.CommutativeDiagram) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

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