use of easik.sketch.util.graph.SketchGraphModel in project fql by CategoricalData.
the class MySQLExporter method createTable.
/**
* @param table
* @param includeRefs
*
* @return
*/
@Override
public List<String> createTable(final EntityNode table, final boolean includeRefs) {
final StringBuilder tableDef = new StringBuilder("CREATE TABLE ");
tableDef.append(quoteId(table)).append(" (").append(lineSep);
tableDef.append('\t').append(quoteId(tablePK(table))).append(' ').append(pkType()).append(" PRIMARY KEY AUTO_INCREMENT");
for (final EntityAttribute<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> attr : table.getEntityAttributes()) {
// FIXME -- what about NOT NULL, etc. on the column? Defaults?
tableDef.append(',').append(lineSep).append('\t').append(quoteId(attr.getName())).append(' ').append(dbDriver.getTypeString(attr.getType()));
}
// includeRefs set)
for (final SketchEdge edge : table.getOutgoingEdges()) {
tableDef.append(',').append(lineSep).append('\t').append(quoteId(tableFK(edge))).append(' ').append(pkType());
if (!edge.isPartial()) {
tableDef.append(" NOT NULL");
}
if (edge.isInjective()) {
tableDef.append(" UNIQUE");
}
if (includeRefs) {
tableDef.append(',').append(lineSep).append(" FOREIGN KEY (").append(quoteId(tableFK(edge))).append(") REFERENCES ").append(quoteId(edge.getTargetEntity())).append(" (").append(quoteId(tablePK(edge.getTargetEntity()))).append(')');
// Effectively
final String refMode = (edge.getCascading() == SketchEdge.Cascade.SET_NULL) ? "SET NULL" : (edge.getCascading() == SketchEdge.Cascade.CASCADE) ? "CASCADE" : "NO ACTION";
// the
// same
// as
// RESTRICT,
// but
// RESTRICT
// won't
// even
// work
// if
// cleaned
// up
// in
// the
// same
// transaction
tableDef.append(" ON DELETE ").append(refMode).append(" ON UPDATE ").append(refMode);
}
}
// Add the unique keys
for (final UniqueKey<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> key : table.getUniqueKeys()) {
tableDef.append(',').append(lineSep).append(" UNIQUE ").append(quoteId(table.getName() + '_' + key.getKeyName())).append(" (");
final List<String> keyCols = new LinkedList<>();
for (final UniqueIndexable elem : key.getElements()) {
if (elem instanceof SketchEdge) {
keyCols.add(quoteId(tableFK((SketchEdge) elem)));
} else {
// Probably an attribute
keyCols.add(quoteId(elem.getName()));
}
}
tableDef.append(EasikTools.join(", ", keyCols)).append(')');
}
// add hidden attributes last
for (final EntityAttribute<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> attr : table.getHiddenEntityAttributes()) {
// Default is false so that when user adds rows manually we don't
// give option for hidden attributes
// and just leave them false
tableDef.append(',').append(lineSep).append('\t').append(quoteId(attr.getName())).append(' ').append(dbDriver.getTypeString(attr.getType())).append(" Default " + false);
}
// commented out by Sarah van der Laan -- caused error in generated SQL
// file (invalid foreign keys)
/**
* if (includeRefs) { // We need a copy of any sum tables' not-null
* columns (which are simply the // outgoing, non-partial edges) if this
* table is a summand; we shadow the // columns here, then handle
* propagating them in the SumConstraint triggers. for (final SketchEdge
* edge : table.getShadowEdges()) {
* tableDef.append(',').append(lineSep).append('\t').append(quoteId(tableFK(edge))).append('
* ').append(pkType()); tableDef.append(',').append(lineSep).append("
* FOREIGN KEY (").append(quoteId(tableFK(edge))).append(") REFERENCES
* ").append( quoteId(edge.getTargetEntity())).append("
* (").append(quoteId(tablePK(edge.getTargetEntity()))).append( ") ON
* DELETE SET NULL ON UPDATE SET NULL"); } }
*/
tableDef.append(lineSep).append(')').append(TABLE_OPTIONS).append($);
return Collections.singletonList(tableDef.toString());
}
use of easik.sketch.util.graph.SketchGraphModel in project fql by CategoricalData.
the class XSDExporter method createConstraint.
/**
* Add an annotation explaining the equalizer and effect the isA.
* <p/>
* Today, this has two parts. First the annotation. For example in the
* standard equalizer constraint in constraints.easik gives this annotation:
*
* <pre>
* <xs:annotation>
* <xs:documentation>
* Equalizer(f3); B(f4) ;C(f5) ;Codomain =
* Equalizer(f1); D(f2) ;Codomain
* </xs:documentation>
* </xs:annotation>
* </pre>
* <p/>
* In addition, the equalizer entity has an "isA" relationship to another
* entity at the start of the diagram, so this is reflected in the typing.
* First, an element of equalizer type is added to the target of the "isA"
* relationship. Second, since there is no need for a separate element in
* the schema for the equalizer entity, its element is removed from the
* sketch entity.
*
* @param eq
* the equalizer diagram constraint.
* @todo Why not do this with standard isA relationships as well?
* @todo The equalizer element should be added in some way so there is a
* "minoccurs" of zero.
* @todo The equalizer does not really need a "key" element, but what if
* other constraints refer to it.
*/
private void createConstraint(final EqualizerConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> eq) {
final List<ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>> paths = eq.getEqualizerPaths();
final EntityNode equalizer = eq.getEqualizerEntity();
final XSDType domType = equalizer.getXsdType();
final String keyrefName = Easik.getInstance().getSettings().getProperty("xml_keyref_name");
final List<String> values = new ArrayList<>(paths.size());
for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> path : paths) {
final LinkedList<SketchEdge> tmpPath = new LinkedList<>(path.getEdges());
tmpPath.removeFirst();
if (tmpPath.size() == 0) {
values.add(equalizer.getName() + '(' + path.getFirstEdge().getForeignKeyName(keyrefName) + ')');
} else {
values.add(equalizer.getName() + '(' + path.getFirstEdge().getForeignKeyName(keyrefName) + ')' + xmlJoinPath(tmpPath, true));
}
}
final EntityNode isaNode = paths.get(0).getDomain();
final XSDComplexType isaNodeType = (XSDComplexType) isaNode.getXsdType();
final XSDElement element = equalizer.getXsdElement();
element.setParent(isaNode.getXsdElement());
isaNodeType.addAtom(element);
equalizer.setXsdElement(null);
domType.addAnnotation(new XSDAnnotation(EasikTools.join(" = " + lineSep, values)));
}
use of easik.sketch.util.graph.SketchGraphModel in project fql by CategoricalData.
the class XSDExporter method createConstraint.
/**
* Add an annotation explaining the commutativity of the diagram.
* <p/>
* Today, this is simply done by creating an annotation. For example in the
* standard cd in constraints.easik gives this annotation:
*
* <pre>
* <xs:annotation>
* <xs:documentation>
* Domain(f1); A(f2) ;Codomain =
* Domain(f3); B(f4) ;Codomain
* </xs:documentation>
* </xs:annotation>
* </pre>
*
* @param cd
* the commutative diagram constraint.
*/
private void createConstraint(final CommutativeDiagram<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> cd) {
final List<ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>> paths = cd.getPaths();
final EntityNode dom = paths.get(0).getDomain();
final XSDType domType = dom.getXsdType();
final String keyrefName = Easik.getInstance().getSettings().getProperty("xml_keyref_name");
final List<String> values = new ArrayList<>(paths.size());
for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> path : paths) {
final LinkedList<SketchEdge> tmpPath = new LinkedList<>(path.getEdges());
tmpPath.removeFirst();
if (tmpPath.size() == 0) {
values.add(dom.getName() + '(' + path.getFirstEdge().getForeignKeyName(keyrefName) + ')');
} else {
values.add(dom.getName() + '(' + path.getFirstEdge().getForeignKeyName(keyrefName) + ')' + xmlJoinPath(tmpPath, true));
}
}
domType.addAnnotation(new XSDAnnotation(EasikTools.join(" = " + lineSep, values)));
}
use of easik.sketch.util.graph.SketchGraphModel in project fql by CategoricalData.
the class XSDElement method setKeys.
/**
* Use an entity node to set the keys, including foreign keyrefs and
* uniques.
* <p/>
* Key is set from the primary key. KeyRefs are set from the outgoing edges.
* Uniques are set by Uniques and by noninclusion injective outgoing edges.
*
* @param node
* we are working with
*/
@SuppressWarnings("unused")
public void setKeys(final EntityNode node) {
final List<UniqueKey<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>> uniqueKeyList = node.getUniqueKeys();
final XSDComplexType myType = (XSDComplexType) getElementType();
constraints = new ArrayList<XSDAbstractKey>(uniqueKeyList.size() + 3);
final String idName = Easik.getInstance().getSettings().getProperty("xml_id_name");
final String keyrefName = Easik.getInstance().getSettings().getProperty("xml_keyref_name");
final boolean idIsAttribute = Boolean.valueOf(Easik.getInstance().getSettings().getProperty("xml_id_is_attribute"));
final XSDKey primaryKey = node.createXMLPrimaryKey(this);
final XSDElement theParent = (XSDElement) getParent();
theParent.addConstraint(primaryKey);
for (final SketchEdge edge : node.getOutgoingEdges()) {
final boolean isInclusion = edge.getTargetEntity().getName().equals(theParent.getName());
if (edge.isInjective()) {
if (!isInclusion) {
constraints.add(new XSDUniqueKey(edge.getForeignKeyName(keyrefName), this, edge.getName()));
}
}
if (!isInclusion) {
theParent.addConstraint(new XSDKeyRef(this, edge.getTargetEntity().getXMLPrimaryKeyName(), edge.getName()));
myType.addAtom(new XSDElement(edge.getName(), edge.isPartial(), XSDBaseType.xsInt));
}
}
for (final UniqueKey<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> k : uniqueKeyList) {
this.addConstraint(new XSDUniqueKey(this, k));
}
}
use of easik.sketch.util.graph.SketchGraphModel 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();
}
Aggregations