use of easik.model.constraint.PullbackConstraint in project fql by CategoricalData.
the class ModelInfoTreeUI method _addConstraint.
/**
* Internal method that actually does the work of adding the constraint to
* the info tree; this is separate from the public method so that the public
* method can handle undo/redo controlling.
*
* @param constraint
*/
private void _addConstraint(ModelConstraint<F, GM, M, N, E> constraint) {
DefaultMutableTreeNode node = constraint.getTreeNode();
if (constraint instanceof ProductConstraint) {
_tree_constraints_product.add(node);
} else if (constraint instanceof SumConstraint) {
_tree_constraints_sum.add(node);
} else if (constraint instanceof PullbackConstraint) {
_tree_constraints_pullback.add(node);
} else if (constraint instanceof EqualizerConstraint) {
_tree_constraints_equalizer.add(node);
} else if (constraint instanceof CommutativeDiagram) {
_tree_constraints_commutative.add(node);
} else if (constraint instanceof LimitConstraint) {
_tree_constraints_limit.add(node);
}
// Add the paths to the constraint
for (ModelPath<F, GM, M, N, E> path : constraint.getPaths()) {
node.add(path);
}
refreshTree((DefaultMutableTreeNode) node.getParent());
}
use of easik.model.constraint.PullbackConstraint 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.model.constraint.PullbackConstraint in project fql by CategoricalData.
the class MySQLExporter method createConstraint.
/**
* @param constraint
* @param id
*
* @return
*/
@Override
public List<String> createConstraint(final PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> constraint, final String id) {
final List<String> sql = new LinkedList<>();
String tmpS = null;
// Create the trigger for inserting into the pullback table:
final EntityNode pb = constraint.getSource();
final EntityNode target = constraint.getTarget();
final String BASE_PATH_NAME = "_pathId";
final int constraintWidth = constraint.getWidth();
final String insConName = "pullbackConstraint" + id + "Insert" + cleanId(pb);
StringBuilder proc = new StringBuilder(500);
proc.append("CREATE PROCEDURE ");
proc.append(quoteId(insConName));
proc.append("(_newPBid ");
proc.append(pkType());
proc.append(") BEGIN");
proc.append(lineSep);
proc.append(" DECLARE ");
for (int i = 0; i < constraintWidth; i++) {
proc.append(BASE_PATH_NAME);
proc.append(i);
if (i != constraintWidth - 1) {
proc.append(", ");
} else {
proc.append(" ");
}
}
proc.append(pkType()).append(';').append(lineSep);
final String basePK = qualifiedPK(pb);
final String targetPK = qualifiedPK(target);
for (int i = 0; i < constraintWidth; i++) {
proc.append(" SELECT ").append(targetPK).append(" INTO " + BASE_PATH_NAME + i).append(lineSep).append(" FROM ").append(joinPath(constraint.getFullPath(i))).append(lineSep).append(" WHERE ").append(basePK).append(" = _newPBid;").append(lineSep);
}
// Make sure the paths match
proc.append(" IF NOT");
for (int i = 1; i < constraintWidth; i++) {
proc.append(" (" + BASE_PATH_NAME + (i - 1) + " <=> " + BASE_PATH_NAME + i + ')');
if (i != constraintWidth - 1) {
proc.append(" OR NOT");
}
}
proc.append(" THEN").append(lineSep).append(" CALL constraint_failure('Invalid entry in pullback constraint.');").append(lineSep).append(" END IF;").append(lineSep);
addFail = true;
final String pbQ = quoteId(pb);
SketchEdge[] firstEdges = new SketchEdge[constraintWidth];
for (int i = 0; i < constraintWidth; i++) {
firstEdges[i] = constraint.getFullPath(i).getFirstEdge();
}
if (!addedUnique.isUnique(pb, firstEdges)) {
tmpS = "ALTER TABLE " + pbQ + " ADD UNIQUE " + quoteId("pullbackConstraint" + id + "UniqueIndex") + " (";
for (int i = 0; i < constraintWidth; i++) {
tmpS += quoteId(tableFK(firstEdges[i]));
if (i != constraintWidth - 1) {
tmpS += ", ";
}
}
tmpS += ')' + $;
sql.add(tmpS);
addedUnique.add(pb, firstEdges);
}
proc.append("END");
sql.add(proc.toString());
addTrigger(pb, "AFTER INSERT", "CALL " + quoteId(insConName) + "(NEW." + quoteId(tablePK(pb)) + ')');
for (int i = 0; i < constraintWidth; i++) {
ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> tar = constraint.getTargetPath(i);
ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> pro = constraint.getProjectionPath(i);
pbAfterInsertProcs(sql, id, constraint, tar, pro, i);
}
// Finally, the before-delete on the pullback:
final String delConName = "pullbackConstraint" + id + "Delete" + cleanId(pb);
proc = new StringBuilder(500);
proc.append("CREATE PROCEDURE ").append(quoteId(delConName)).append("(toDeleteId ").append(pkType()).append(") BEGIN").append(lineSep);
proc.append(" DELETE ").append(quoteId(target)).append(" FROM ").append(pbQ).append(" JOIN ").append('(');
for (int i = 0; i < constraintWidth; i++) {
LinkedList<SketchEdge> tmpPath = new LinkedList<>(constraint.getFullPath(i).getEdges());
SketchEdge firstEdge = tmpPath.removeFirst();
@SuppressWarnings("unused") SketchEdge lastEdge = tmpPath.getLast();
@SuppressWarnings("unused") SketchEdge secondEdge = tmpPath.removeFirst();
EntityNode first = firstEdge.getTargetEntity();
proc.append(quoteId(first));
if (i != constraintWidth - 1) {
proc.append(", ");
}
}
proc.append(") ON ");
for (int i = 0; i < constraintWidth; i++) {
LinkedList<SketchEdge> tmpPath = new LinkedList<>(constraint.getFullPath(i).getEdges());
SketchEdge firstEdge = tmpPath.removeFirst();
@SuppressWarnings("unused") SketchEdge lastEdge = tmpPath.getLast();
@SuppressWarnings("unused") SketchEdge secondEdge = tmpPath.removeFirst();
EntityNode first = firstEdge.getTargetEntity();
proc.append(qualifiedFK(firstEdge)).append(" = ").append(qualifiedPK(first));
if (i != constraintWidth - 1) {
proc.append(" AND ");
}
}
for (int i = 0; i < constraintWidth; i++) {
LinkedList<SketchEdge> tmpPath = new LinkedList<>(constraint.getFullPath(i).getEdges());
SketchEdge firstEdge = tmpPath.removeFirst();
@SuppressWarnings("unused") SketchEdge lastEdge = tmpPath.getLast();
SketchEdge secondEdge = tmpPath.removeFirst();
@SuppressWarnings("unused") EntityNode first = firstEdge.getTargetEntity();
if (tmpPath.size() > 0) {
proc.append(" JOIN ").append(joinPath(tmpPath, false, qualifiedFK(secondEdge) + " = " + qualifiedPK(secondEdge.getTargetEntity())));
}
}
proc.append(" JOIN ").append(quoteId(target)).append(" ON ");
for (int i = 0; i < constraintWidth; i++) {
LinkedList<SketchEdge> tmpPath = new LinkedList<>(constraint.getFullPath(i).getEdges());
SketchEdge firstEdge = tmpPath.removeFirst();
SketchEdge lastEdge = tmpPath.getLast();
@SuppressWarnings("unused") SketchEdge secondEdge = tmpPath.removeFirst();
@SuppressWarnings("unused") EntityNode first = firstEdge.getTargetEntity();
proc.append(qualifiedFK(lastEdge)).append(" = ").append(qualifiedPK(target));
if (i != constraintWidth - 1) {
proc.append(" AND ");
}
}
proc.append(lineSep).append(" WHERE ").append(qualifiedPK(pb)).append(" = toDeleteId;").append(lineSep).append("END");
sql.add(proc.toString());
addTrigger(pb, "BEFORE DELETE", "CALL " + quoteId(delConName) + "(OLD." + quoteId(tablePK(pb)) + ')');
// Federico Mora
int j = 0;
// c.b_id = b.id JOIN d ON d.c_id = c.id...> WHERE <begin> = id;
for (int i = 0; i < constraintWidth; i++) {
ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> p = constraint.getProjectionPath(i);
StringBuilder proc1 = new StringBuilder(500);
StringBuilder body = new StringBuilder(50);
proc1.append("CREATE PROCEDURE ").append("Update" + constraint.getID() + "Proj" + j++).append("() BEGIN").append(lineSep);
for (int k = 0; k < constraintWidth; k++) {
ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> q = constraint.getProjectionPath(k);
if (p == q) {
// do nothing, cascade will take care of it
} else {
// making the update string which is used by all the delete
// procedures.
final LinkedList<SketchEdge> edges = q.getEdges();
if (edges.size() >= 2) {
Iterator<SketchEdge> iter = edges.iterator();
SketchEdge e = iter.next();
body.append(" UPDATE ").append(leftJoinPath(q)).append(lineSep);
body.append(" SET");
while (iter.hasNext()) {
SketchEdge ske = iter.next();
body.append(" " + quoteId(ske.getSourceEntity()) + ".BC" + constraint.getID() + " = " + false + ",");
}
body.delete(body.length() - 1, body.length());
body.append(" WHERE " + qualifiedFK(e) + " IS NULL;" + lineSep);
}
}
}
if (body.length() > 0) {
proc1.append(body);
proc1.append("END");
sql.add(proc1.toString());
// Now create the triggers to call the new procedure
addTrigger(p.getCoDomain(), "AFTER DELETE", "CALL Update" + constraint.getID() + "Proj" + (j - 1) + "()");
addTrigger(p.getCoDomain(), "AFTER UPDATE", "CALL Update" + constraint.getID() + "Proj" + (j - 1) + "()");
}
}
return delimit("$$", sql);
}
use of easik.model.constraint.PullbackConstraint in project fql by CategoricalData.
the class MySQLExporter method pbAfterInsertProcs.
/**
* @param sql
* @param id
* @param constraint
* @param thisPath
* @param thisFirstPath
* @param index
*/
private void pbAfterInsertProcs(final List<String> sql, final String id, final PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> constraint, final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> thisPath, final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> thisFirstPath, int index) {
final EntityNode source = thisPath.getDomain();
final EntityNode pb = thisFirstPath.getDomain();
final EntityNode target = thisPath.getCoDomain();
final String insConName = "pullbackConstraint" + id + "Insert" + cleanId(source);
final Collection<String> args = new LinkedList<>();
final Collection<String> params = new LinkedList<>();
args.add("_newId " + pkType());
params.add("NEW." + quoteId(tablePK(source)));
// commented out by Sarah van der Laan -- caused error in generated SQL
// file (invalid foreign keys)
/**
* for (final SketchEdge shadow : source.getShadowEdges()) {
* args.add(quoteId("NEW_shadow_" + tableFK(shadow)) + ' ' + pkType());
* params.add("NEW." + quoteId(tableFK(shadow))); }
*/
final StringBuilder proc = new StringBuilder(500);
proc.append("CREATE PROCEDURE ").append(quoteId(insConName)).append('(').append(EasikTools.join(", ", args)).append(") BEGIN").append(lineSep);
// trigger body
// omitting last
final String thisJoinPath = joinPath(thisPath, false);
// edge
String lastJoinOn = "JOIN " + quoteId(target) + " ON " + qualifiedFK(thisPath.getLastEdge()) + " = " + qualifiedPK(target);
for (int i = 0; i < constraint.getWidth(); i++) {
if (i == index) {
continue;
}
ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> otherPath = constraint.getTargetPath(i);
lastJoinOn += " AND ";
lastJoinOn += qualifiedFK(otherPath.getLastEdge()) + " = " + qualifiedPK(target);
}
proc.append("\tIF (SELECT COUNT(*) FROM (").append(thisJoinPath);
for (int i = 0; i < constraint.getWidth(); i++) {
if (i == index) {
continue;
}
ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> otherPath = constraint.getTargetPath(i);
proc.append(", ").append(joinPath(otherPath, false));
}
proc.append(") ").append(lastJoinOn).append(" WHERE ").append(qualifiedPK(source)).append(" = _newId) > 0 THEN").append(lineSep);
// constraint condition met
SketchEdge[] edges;
for (int i = 0; i < constraint.getWidth(); i++) {
if (i == index) {
continue;
}
final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> otherPath = constraint.getTargetPath(i);
final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> otherFirstPath = constraint.getProjectionPath(i);
final LinkedList<SketchEdge> sketchEdgeLinkedList = otherFirstPath.getEdges();
edges = sketchEdgeLinkedList.toArray(new SketchEdge[sketchEdgeLinkedList.size()]);
if (edges.length >= 2) {
final String joinOn = "JOIN " + quoteId(target) + " ON " + qualifiedFK(thisPath.getLastEdge()) + " = " + qualifiedPK(target) + " AND " + qualifiedFK(otherPath.getLastEdge()) + " = " + qualifiedPK(target);
// proc.append("\t\tIF (SELECT COUNT(*) FROM
// (").append(joinPath(sketchEdgeLinkedList, true));
proc.append("\t\tIF (SELECT COUNT(*) FROM (").append(joinPath(constraint.getFullPath(i).getEdges(), false));
// TODO might have the same bug if the target path is longer
// than 1, check this
proc.append(", ").append(thisJoinPath).append(") ").append(joinOn).append(" WHERE ").append(qualifiedPK(source)).append(" = _newId) = 0 THEN").append(lineSep);
final LinkedList<SketchEdge> partialPath = new LinkedList<>(otherPath.getEdges());
for (int j = edges.length - 1; j > 0; j--) {
final EntityNode s = edges[j].getSourceEntity();
final EntityNode t = edges[j].getTargetEntity();
// after s there used to be dest.getShadowEdges();
proc.append("\t\t\t").append(insertInto(// SELECT
true, // SELECT
s, // SELECT
qualifiedPK(t), // FROM:
'(' + joinPath(partialPath, false) + ", " + thisJoinPath + ") " + joinOn + " WHERE " + qualifiedPK(source) + " = _newId", Collections.singletonList(quoteId(tableFK(edges[j]))), null));
partialPath.addFirst(edges[j]);
}
proc.append("\t\tEND IF;").append(lineSep);
}
}
final LinkedList<SketchEdge> firstPathEdges = thisFirstPath.getEdges();
edges = firstPathEdges.toArray(new SketchEdge[firstPathEdges.size()]);
final LinkedList<SketchEdge> partialPath = new LinkedList<>(thisPath.getEdges());
for (int i = edges.length - 1; i > 0; i--) {
final SketchEdge e = edges[i];
final EntityNode s = e.getSourceEntity();
final EntityNode t = e.getTargetEntity();
// after s there used to be dest.getShadowEdges();
proc.append("\t\t").append(insertInto(true, s, qualifiedPK(t), joinPath(partialPath) + " WHERE " + qualifiedPK(source) + " = _newId", Collections.singletonList(quoteId(tableFK(e))), null));
partialPath.addFirst(e);
}
final EntityNode firstStop = thisFirstPath.getFirstCoDomain();
final List<String> cols = new LinkedList<>();
cols.add(quoteId(tableFK(thisFirstPath.getFirstEdge())));
String tmpStr1 = qualifiedPK(firstStop);
LinkedList<SketchEdge> tmpPath = new LinkedList<>(constraint.getFullPath(0).getEdges());
tmpPath.removeFirst();
String tmpStr2 = '(' + joinPath(tmpPath, false);
for (int i = 0; i < constraint.getWidth(); i++) {
final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> otherFirstPath = constraint.getProjectionPath(i);
final EntityNode otherFirstStop = otherFirstPath.getFirstCoDomain();
if (otherFirstPath != thisFirstPath) {
tmpStr1 += ", " + qualifiedPK(otherFirstStop);
cols.add(quoteId(tableFK(otherFirstPath.getFirstEdge())));
}
if (i > 0) {
tmpPath = new LinkedList<>(constraint.getFullPath(i).getEdges());
tmpPath.removeFirst();
tmpStr2 += ", " + joinPath(tmpPath, false);
}
}
// after pb there used to be dest.getShadowEdges();
proc.append("\t\t").append(insertInto(true, pb, tmpStr1, tmpStr2 + ") " + lastJoinOn + " WHERE " + qualifiedPK(source) + " = _newId", cols, null));
// end constraint condition met
proc.append("\tEND IF;");
// end trigger body
proc.append(lineSep).append("END");
sql.add(proc.toString());
// Now create the trigger to call the new procedure
addTrigger(source, "AFTER INSERT", "CALL " + quoteId(insConName) + '(' + EasikTools.join(", ", params) + ')');
}
use of easik.model.constraint.PullbackConstraint in project fql by CategoricalData.
the class ModelInfoTreeUI method _removeConstraint.
/**
* Internal method that actually does the work of adding the constraint to
* the info tree; this is separate from the public method so that the public
* method can handle undo/redo controlling.
*
* @param c
*/
private void _removeConstraint(ModelConstraint<F, GM, M, N, E> c) {
DefaultMutableTreeNode constraint = c.getTreeNode();
if (c instanceof CommutativeDiagram) {
_tree_constraints_commutative.remove(constraint);
} else if (c instanceof ProductConstraint) {
_tree_constraints_product.remove(constraint);
} else if (c instanceof PullbackConstraint) {
_tree_constraints_pullback.remove(constraint);
} else if (c instanceof EqualizerConstraint) {
_tree_constraints_equalizer.remove(constraint);
} else if (c instanceof SumConstraint) {
_tree_constraints_sum.remove(constraint);
} else if (c instanceof LimitConstraint) {
_tree_constraints_limit.remove(constraint);
}
refreshTree();
}
Aggregations