use of easik.sketch.vertex.EntityNode 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.vertex.EntityNode in project fql by CategoricalData.
the class XSDExporter method xmlPBelemJoinPath.
/**
* protected method to assist in building "pullback element path" strings
* for the pullback constraint annotations
*
* @param inEdges
* List of sketchedges
* @param includeLast
* Include the last or not
* @return path like string
*/
protected static String xmlPBelemJoinPath(final List<SketchEdge> inEdges, final boolean includeLast) {
final Deque<SketchEdge> edges = new LinkedList<>(inEdges);
final StringBuilder joinClause = new StringBuilder(10);
if (!includeLast && !edges.isEmpty()) {
edges.removeLast();
}
for (final SketchEdge e : edges) {
final EntityNode target = e.getTargetEntity();
joinClause.append(e.getName()).append(" in ").append(target.getName());
}
return joinClause.toString();
}
use of easik.sketch.vertex.EntityNode 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.vertex.EntityNode in project fql by CategoricalData.
the class XSDExporter method createEntities.
/**
* First pass - create the XML Schema types and the associated elements.
* <p/>
* Loop through the entities in the sketch. For each one, create a complex
* type and a corresponding element.
*
* @see easik.xml.xsd.nodes.types.XSDComplexType
* @see easik.xml.xsd.nodes.elements.XSDElement
*/
private void createEntities() {
final Collection<EntityNode> nodeCollection = sketch.getEntities();
for (final EntityNode entity : nodeCollection) {
final XSDComplexType complexType = new XSDComplexType(entity, null);
entity.setXsdType(complexType);
}
for (final EntityNode entity : nodeCollection) {
entity.setXsdElement(new XSDElement(entity, topLevel));
}
}
use of easik.sketch.vertex.EntityNode 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)));
}
Aggregations