use of easik.sketch.vertex.EntityNode in project fql by CategoricalData.
the class ViewUpdateAction method actionPerformed.
/**
* @param e
* The action event
* @author Sarah van der Laan
*/
@Override
public void actionPerformed(ActionEvent e) {
Object[] currentSelection = _ourView.getSelectionCells();
QueryNode currNode = (QueryNode) currentSelection[0];
String queryString = currNode.getQuery();
String entityNodeName = null;
// find corresponding entity node name
String[] tokens = queryString.split("\\s+");
for (int i = 0; i < tokens.length; i++) {
if (tokens[i].equalsIgnoreCase("from")) {
entityNodeName = tokens[i + 1];
}
}
EntityNode _ourEntityNode = null;
// set corresponding node in order to use
for (EntityNode node : _ourSketch.getEntities()) {
if (node.getName().equalsIgnoreCase(entityNodeName)) {
_ourEntityNode = node;
}
}
if (!_ourSketch.hasDatabase()) {
JOptionPane.showMessageDialog(null, "Not currently connected to a database.");
return;
} else {
UpdateMonitor um = _ourSketch.getDatabase().newUpdateMonitor();
if (um == null) {
JOptionPane.showMessageDialog(null, "Could not perform update: problem accessing db driver");
return;
}
if (_ourEntityNode != null)
um.updateRow(_ourEntityNode);
}
}
use of easik.sketch.vertex.EntityNode 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;
}
}
}
use of easik.sketch.vertex.EntityNode 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;
}
}
use of easik.sketch.vertex.EntityNode in project fql by CategoricalData.
the class AddRowAction method actionPerformed.
/**
* Checks that our sketch has a db driver, and sends the selected entity
* node off to the sketch's update monitor.
*
* @param e
* The action event
*/
@Override
public void actionPerformed(ActionEvent e) {
Object[] currentSelection = _theSketch.getSelectionCells();
if (!(currentSelection[0] instanceof EntityNode) || !_theSketch.hasDatabase()) {
return;
}
UpdateMonitor um = _theSketch.getDatabase().newUpdateMonitor();
um.insert((EntityNode) currentSelection[0]);
}
use of easik.sketch.vertex.EntityNode in project fql by CategoricalData.
the class ViewAddAction method actionPerformed.
/**
* @param e
* The action event
* @author Sarah van der Laan
*/
@Override
public void actionPerformed(ActionEvent e) {
Object[] currentSelection = _ourView.getSelectionCells();
QueryNode currNode = (QueryNode) currentSelection[0];
EntityNode _ourEntityNode = currNode.getQueriedEntity();
if (!_ourSketch.hasDatabase()) {
JOptionPane.showMessageDialog(null, "Not currently connected to a database.");
return;
} else {
// final JDBCDriver dbd = _ourSketch.getDatabase().getJDBCDriver();
UpdateMonitor um = _ourSketch.getDatabase().newUpdateMonitor();
if (um == null) {
JOptionPane.showMessageDialog(null, "Could not perform update: problem accessing db driver");
return;
}
if (_ourEntityNode != null)
um.insert(_ourEntityNode);
}
}
Aggregations