Search in sources :

Example 21 with SketchEdge

use of easik.sketch.edge.SketchEdge in project fql by CategoricalData.

the class DeleteAttributeAction method actionPerformed.

/**
 * Deletes the currently selected attribute
 *
 * @param e
 *            The action event
 */
@Override
public void actionPerformed(ActionEvent e) {
    // If there is nothing seleceted then just do nothing
    if (_theFrame.getInfoTreeUI().getInfoTree().isSelectionEmpty()) {
        return;
    }
    // cancel operation
    if (_theFrame.getMModel().isSynced()) {
        int choice = JOptionPane.showConfirmDialog(_theFrame, "Warning: this sketch is currently synced with a db; continue and break synchronization?", "Warning!", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
        if (choice == JOptionPane.CANCEL_OPTION) {
            return;
        }
    }
    // Get currently selected object
    DefaultMutableTreeNode curSelected = (DefaultMutableTreeNode) _theFrame.getInfoTreeUI().getInfoTree().getSelectionPath().getLastPathComponent();
    // Selection is an attribute
    if (curSelected instanceof EntityAttribute) {
        @SuppressWarnings("unchecked") EntityAttribute<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> curAttribute = (EntityAttribute<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) curSelected;
        EntityNode parentEntity = curAttribute.getEntity();
        // Show a confirmation dialog box for the deletion
        if (JOptionPane.showConfirmDialog(_theFrame, "Are you sure you want to delete the '" + curAttribute.getName() + "' attribute from the '" + parentEntity + "' entity?", "Confirm Delete", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
            // Delete the attribute from entity
            parentEntity.removeEntityAttribute(curAttribute);
            _theFrame.getMModel().setDirty();
            _theFrame.getMModel().setSynced(false);
        }
    } else // Selection is not an attribute
    {
        JOptionPane.showMessageDialog(_theFrame, "You don't have an attribute selected. \nPlease select an attribute and try again.", "No Attribute Selected", JOptionPane.ERROR_MESSAGE);
        return;
    }
    _theFrame.getInfoTreeUI().revertExpansion();
}
Also used : SketchFrame(easik.ui.SketchFrame) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) EntityAttribute(easik.model.attribute.EntityAttribute) SketchEdge(easik.sketch.edge.SketchEdge) Sketch(easik.sketch.Sketch) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) EntityNode(easik.sketch.vertex.EntityNode)

Example 22 with SketchEdge

use of easik.sketch.edge.SketchEdge in project fql by CategoricalData.

the class KosarajuSCC method transposeDFS.

/**
 * Depth first search of the transpose
 *
 * @param v
 */
public void transposeDFS(EntityNode v) {
    visited.add(v);
    sccList.add(v);
    for (SketchEdge sk : graph.getEdges().values()) {
        if (sk instanceof NormalEdge || sk instanceof InjectiveEdge) {
            if (sk.getTargetEntity().equals(v)) {
                EntityNode t = sk.getSourceEntity();
                if (!visited.contains(t)) {
                    transposeDFS(t);
                }
            }
        }
    }
}
Also used : InjectiveEdge(easik.sketch.edge.InjectiveEdge) SketchEdge(easik.sketch.edge.SketchEdge) NormalEdge(easik.sketch.edge.NormalEdge) EntityNode(easik.sketch.vertex.EntityNode)

Example 23 with SketchEdge

use of easik.sketch.edge.SketchEdge in project fql by CategoricalData.

the class KosarajuSCC method dfs.

/**
 * Depth first search
 */
public void dfs(EntityNode v) {
    visited.add(v);
    for (SketchEdge sk : v.getOutgoingEdges()) {
        if (sk instanceof NormalEdge || sk instanceof InjectiveEdge) {
            EntityNode t = sk.getTargetEntity();
            if (!visited.contains(t)) {
                dfs(t);
            }
        }
    }
    stack.push(v);
}
Also used : InjectiveEdge(easik.sketch.edge.InjectiveEdge) SketchEdge(easik.sketch.edge.SketchEdge) NormalEdge(easik.sketch.edge.NormalEdge) EntityNode(easik.sketch.vertex.EntityNode)

Example 24 with SketchEdge

use of easik.sketch.edge.SketchEdge in project fql by CategoricalData.

the class MySQLExporter method createConstraint.

/**
 * @param constraint
 * @param id
 *
 * @return
 */
@Override
public List<String> createConstraint(final ProductConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> constraint, final String id) {
    final List<String> sql = new LinkedList<>();
    final String delConName = "productConstraint" + id + "Delete";
    StringBuilder proc = new StringBuilder(500);
    proc.append("CREATE PROCEDURE ").append(quoteId(delConName)).append("(id ").append(pkType()).append(") BEGIN").append(lineSep);
    EntityNode begin = null;
    int j = 0;
    // c.b_id = b.id JOIN d ON d.c_id = c.id...> WHERE <begin> = id;
    for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> p : constraint.getPaths()) {
        if (begin == null) {
            begin = p.getDomain();
        }
        proc.append("   DELETE ").append(quoteId(p.getCoDomain())).append(" FROM ").append(joinPath(p)).append(lineSep).append("           WHERE ").append(qualifiedPK(begin)).append(" = id;").append(lineSep);
        // Federico Mora
        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 (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> q : constraint.getPaths()) {
            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 " + quoteId("Update" + constraint.getID() + "Proj" + (j - 1)) + "()");
        }
    // end Federico Mora
    }
    // Select B.id From A Join B On A.f1 = B.id Join D On A.f3 = D.id Where
    // D.id = 1
    proc.append("END");
    sql.add(proc.toString());
    // Now create the trigger to call the new procedure
    addTrigger(begin, "BEFORE DELETE", "CALL " + quoteId(delConName) + "(OLD." + quoteId(tablePK(begin)) + ')');
    for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> p : constraint.getPaths()) {
        final EntityNode dest = p.getCoDomain();
        final String conName = "productConstraint" + id + "Insert" + cleanId(dest);
        final Collection<String> args = new LinkedList<>();
        final Collection<String> params = new LinkedList<>();
        args.add("id " + pkType());
        params.add("NEW." + quoteId(tablePK(dest)));
        // commented out by Sarah van der Laan -- caused error in generated
        // SQL file (invalid foreign keys)
        /**
         * for (final SketchEdge shadow : dest.getShadowEdges()) {
         * args.add(quoteId("NEW_shadow_" + tableFK(shadow)) + ' ' +
         * pkType()); params.add("NEW." + quoteId(tableFK(shadow))); }
         */
        proc = new StringBuilder(500);
        proc.append("CREATE PROCEDURE ").append(quoteId(conName)).append('(').append(EasikTools.join(", ", args)).append(") BEGIN").append(lineSep).append(" DECLARE _lastId ").append(pkType()).append(';').append(lineSep);
        final StringBuilder createIntermediates = new StringBuilder(250);
        final Collection<String> clauses = new LinkedList<>();
        for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> q : constraint.getPaths()) {
            if (p == q) {
                continue;
            }
            // Older versions were doing "SELECT COUNT(*) FROM TABLE", but
            // that has a big
            // performance hit for very large, busy InnoDB tables, when all
            // we really care
            // about is non-empty:
            clauses.add("(SELECT 1 FROM " + quoteId(q.getCoDomain()) + " LIMIT 1) = 1");
            // If we end up putting anything in createIntermediate, we'll
            // use these
            // insertions when we insert the first row into one of the path
            // targets
            final LinkedList<SketchEdge> sketchEdgeLinkedList = q.getEdges();
            final SketchEdge[] edges = sketchEdgeLinkedList.toArray(new SketchEdge[sketchEdgeLinkedList.size()]);
            for (int i = edges.length - 1; i > 0; i--) {
                final SketchEdge e = edges[i];
                final EntityNode source = e.getSourceEntity();
                final EntityNode target = e.getTargetEntity();
                // after source there used to be dest.getShadowEdges();
                createIntermediates.append("                        ").append(insertInto(true, source, qualifiedPK(target), quoteId(target), Collections.singletonList(quoteId(tableFK(e))), null));
            }
        }
        // In words: If the tables forming the domains of the other paths
        // contain items.
        proc.append("       IF ").append(EasikTools.join(" AND ", clauses)).append(" THEN").append(lineSep);
        if (createIntermediates.length() > 0) {
            // If we just inserted the first row, we're going to have to
            // build a path of intermediate tables
            // for the other paths, which we built in createIntermediate.
            proc.append("           IF (SELECT COUNT(*) FROM (SELECT 1 FROM ").append(quoteId(dest)).append(" LIMIT 2) a) = 1 THEN").append(lineSep).append(createIntermediates).append("               END IF;").append(lineSep).append("").append(lineSep);
        }
        // Produce the intermediate path insertion strings
        final LinkedList<SketchEdge> sketchEdgeLinkedList = p.getEdges();
        final SketchEdge[] edges = sketchEdgeLinkedList.toArray(new SketchEdge[sketchEdgeLinkedList.size()]);
        if (edges.length > 1) {
            for (int i = edges.length - 1; i > 0; i--) {
                final SketchEdge e = edges[i];
                final EntityNode source = e.getSourceEntity();
                @SuppressWarnings("unused") final EntityNode target = e.getTargetEntity();
                // after source there used to be dest.getShadowEdges();
                proc.append("               ").append(insertInto(true, source, null, null, Collections.singletonList(quoteId(tableFK(e))), Collections.singletonList((i == edges.length - 1) ? "id" : "LAST_INSERT_ID()")));
            }
            proc.append("           SET _lastId = LAST_INSERT_ID();").append(lineSep);
        } else {
            proc.append("           SET _lastId = id;").append(lineSep);
        }
        // Now the proper insertion
        final List<String> columns = new LinkedList<>();
        final Collection<String> values = new LinkedList<>();
        final Collection<String> from = new LinkedList<>();
        EntityNode thisTarget = null;
        for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> q : constraint.getPaths()) {
            final EntityNode target = q.getFirstEdge().getTargetEntity();
            columns.add(quoteId(tableFK(q.getFirstEdge())));
            values.add(qualifiedPK(target));
            from.add(quoteId(target));
            if (q == p) {
                thisTarget = target;
            }
        }
        // after begin there used to be dest.getShadowEdges();
        proc.append("               ").append(insertInto(true, begin, EasikTools.join(", ", values), EasikTools.join(" CROSS JOIN ", from) + " WHERE " + qualifiedPK(thisTarget) + " = _lastId", columns, null)).append("   END IF;").append(lineSep).append("END");
        sql.add(proc.toString());
        addTrigger(dest, "AFTER INSERT", "CALL " + quoteId(conName) + '(' + EasikTools.join(", ", params) + ')');
    }
    return delimit("$$", sql);
}
Also used : LinkedList(java.util.LinkedList) LimitConstraint(easik.model.constraint.LimitConstraint) EqualizerConstraint(easik.model.constraint.EqualizerConstraint) ProductConstraint(easik.model.constraint.ProductConstraint) SumConstraint(easik.model.constraint.SumConstraint) PullbackConstraint(easik.model.constraint.PullbackConstraint) EntityNode(easik.sketch.vertex.EntityNode) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) SketchFrame(easik.ui.SketchFrame) SketchEdge(easik.sketch.edge.SketchEdge) Sketch(easik.sketch.Sketch)

Example 25 with SketchEdge

use of easik.sketch.edge.SketchEdge in project fql by CategoricalData.

the class MySQLExporter method createConstraint.

/**
 * @param constraint
 * @param id
 *
 * @return
 */
@Override
public List<String> createConstraint(final SumConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> constraint, final String id) {
    final List<String> sql = new LinkedList<>();
    for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> path : constraint.getPaths()) {
        final EntityNode dom = path.getDomain();
        final EntityNode codo = path.getCoDomain();
        final String baseConName = "sumConstraint" + id + cleanId(dom);
        final StringBuilder proc = new StringBuilder(500);
        proc.append("CREATE PROCEDURE ").append(quoteId(baseConName + "Delete")).append("(_deleteFK ").append(pkType()).append(") BEGIN").append(lineSep).append("   DELETE FROM ").append(quoteId(codo)).append(" WHERE ").append(qualifiedPK(codo)).append(" = ");
        if (path.getEdges().size() == 1) {
            proc.append("_deleteFK");
        } else {
            final LinkedList<SketchEdge> trailing = new LinkedList<>(path.getEdges());
            trailing.removeFirst();
            proc.append("(SELECT ").append(qualifiedFK(trailing.getLast())).append(" FROM ").append(joinPath(trailing, false)).append(" WHERE ").append(qualifiedPK(path.getFirstCoDomain())).append(" = _deleteFK)");
        }
        proc.append(';').append(lineSep).append("END");
        sql.add(proc.toString());
        addTrigger(dom, "AFTER DELETE", "CALL " + quoteId(baseConName + "Delete") + "(OLD." + quoteId(tableFK(path.getFirstEdge())) + ')');
        final List<String> insertions = new LinkedList<>();
        // after codo there used to be dest.getShadowEdges();
        insertions.add(insertInto(false, codo, null, null, null, null));
        final SketchEdge[] edges = path.getEdges().toArray(new SketchEdge[path.getEdges().size()]);
        for (int i = edges.length - 1; i > 0; i--) {
            final EntityNode source = edges[i].getSourceEntity();
            // after source there used to be dest.getShadowEdges();
            insertions.add(insertInto(false, source, null, null, Collections.singletonList(quoteId(tableFK(edges[i]))), Collections.singletonList("LAST_INSERT_ID()")));
        }
        insertions.add("SET NEW." + quoteId(tableFK(edges[0])) + " = LAST_INSERT_ID()");
        addTrigger(dom, "BEFORE INSERT", insertions);
    }
    return delimit("$$", sql);
}
Also used : LinkedList(java.util.LinkedList) LimitConstraint(easik.model.constraint.LimitConstraint) EqualizerConstraint(easik.model.constraint.EqualizerConstraint) ProductConstraint(easik.model.constraint.ProductConstraint) SumConstraint(easik.model.constraint.SumConstraint) PullbackConstraint(easik.model.constraint.PullbackConstraint) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) EntityNode(easik.sketch.vertex.EntityNode) SketchFrame(easik.ui.SketchFrame) SketchEdge(easik.sketch.edge.SketchEdge) Sketch(easik.sketch.Sketch)

Aggregations

SketchEdge (easik.sketch.edge.SketchEdge)51 EntityNode (easik.sketch.vertex.EntityNode)45 Sketch (easik.sketch.Sketch)31 SketchGraphModel (easik.sketch.util.graph.SketchGraphModel)31 SketchFrame (easik.ui.SketchFrame)30 LinkedList (java.util.LinkedList)22 LimitConstraint (easik.model.constraint.LimitConstraint)17 ProductConstraint (easik.model.constraint.ProductConstraint)16 PullbackConstraint (easik.model.constraint.PullbackConstraint)16 SumConstraint (easik.model.constraint.SumConstraint)16 EqualizerConstraint (easik.model.constraint.EqualizerConstraint)13 ModelConstraint (easik.model.constraint.ModelConstraint)13 ArrayList (java.util.ArrayList)11 ModelPath (easik.model.path.ModelPath)9 LinkedHashSet (java.util.LinkedHashSet)9 InjectiveEdge (easik.sketch.edge.InjectiveEdge)7 UniqueIndexable (easik.model.keys.UniqueIndexable)5 XSDAnnotation (easik.xml.xsd.nodes.XSDAnnotation)5 XSDType (easik.xml.xsd.nodes.types.XSDType)5 EasikType (easik.database.types.EasikType)4