use of easik.sketch.vertex.EntityNode in project fql by CategoricalData.
the class JDBCExporter method leftJoinPath.
/**
* Constructs a special join clause for this path to be used in an update
* statement for constraint consistency in projections.
*
* @param p
* the path for which the selection is being made
* @return the string, suitable for use in a SELECT query.
*
* @author Federico Mora
*/
protected String leftJoinPath(final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> p) {
final LinkedList<SketchEdge> edges = p.getEdges();
final StringBuilder joinClause = new StringBuilder();
Iterator<SketchEdge> iter = edges.descendingIterator();
iter.next();
int i = 0;
while (iter.hasNext()) {
final SketchEdge e = iter.next();
final EntityNode source = e.getSourceEntity();
final EntityNode target = e.getTargetEntity();
if (i == 0) {
i++;
joinClause.append(quoteId(target) + " LEFT JOIN ").append(quoteId(source)).append(" ON ").append(qualifiedPK(target)).append(" = ").append(qualifiedFK(e));
} else {
joinClause.append(" LEFT JOIN ").append(quoteId(source)).append(" ON ").append(qualifiedPK(target)).append(" = ").append(qualifiedFK(e));
}
}
return joinClause.toString();
}
use of easik.sketch.vertex.EntityNode in project fql by CategoricalData.
the class QueryNode method processQuery.
/**
* Processes the inQuery, assigning to instance variables
*
* @param String
* inQuery of the new node
* @author Federico Mora
* @throws QueryException
*/
private void processQuery(String inQuery) throws QueryException {
if (inQuery.isEmpty()) {
return;
}
boolean warnings = _ourSketch.useWarnings();
String oldEntityNodeName = "";
if (_queriedEntityNode != null) {
oldEntityNodeName = _queriedEntityNode.getName();
}
String entityNodeName = null;
String errMess = "";
String tempWhere = "";
ArrayList<String> tempQColumns = new ArrayList<>();
EntityNode tempNode = null;
boolean update = true;
int fromToken = 0;
String[] tokens = inQuery.split("[,\\s]+");
if (!tokens[0].toUpperCase().equals("SELECT")) {
// tell user has to start with Select
throw new QueryException("Query must start with select");
}
for (int i = 0; i < tokens.length; i++) {
if (tokens[i].equalsIgnoreCase("from")) {
fromToken = i;
entityNodeName = tokens[i + 1];
}
}
// get everything between Select and from
for (int i = 1; i < fromToken; i++) {
tempQColumns.add(tokens[i]);
}
if (!tempQColumns.contains("*") && tempQColumns.size() == 1) {
// warn user that this may not be updatable
update = false;
errMess += "View will not be updatebale if not Select * \n";
}
// get everything after the table being queried
for (int i = fromToken + 2; i < tokens.length; i++) {
tempWhere += tokens[i] + " ";
}
if (tempWhere.toUpperCase().startsWith("WHERE")) {
// warn user against where clause
update = false;
errMess += "Views with WHERE queries not updatebale \n";
} else if (!tempWhere.isEmpty()) {
// error, can't query from more than one table
throw new QueryException("Cannot query from more than one table");
}
boolean found = false;
// set corresponding node in order to use
for (EntityNode sketchNode : _ourSketch.getEntities()) {
if (sketchNode.getName().equals(entityNodeName)) {
// same node.
if (_theModel.getEntityNodePairs().containsKey(sketchNode) && !sketchNode.getName().equals(oldEntityNodeName)) {
// this node is already being queried. Not allowed.
throw new QueryException("Entity Node is already being queried.");
}
tempNode = sketchNode;
found = true;
}
}
if (!found) {
throw new QueryException("Entity node being queried does not exist");
}
if (!errMess.isEmpty() && warnings) {
JOptionPane.showMessageDialog(_theModel, this.getMModel().getName() + ", due to node " + this.getName() + ": " + errMess, "Warning", JOptionPane.ERROR_MESSAGE);
}
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String s : tempQColumns) {
if (!first) {
sb.append(", ");
}
sb.append(s);
first = false;
}
_queriedColumns = sb.toString();
_whereStatement = tempWhere;
_queriedEntityNode = tempNode;
updateable = update;
processAttributes();
}
use of easik.sketch.vertex.EntityNode in project fql by CategoricalData.
the class View method autoAddExistingEdges.
/**
* Call this method when a new QueryNode or Edge is created to automatically
* add whatever existing edges It has in the underlying sketch with other
* existing QueryNodes.
*
* @author Federico Mora
*/
public void autoAddExistingEdges() {
Collection<SketchEdge> sketchEdges = _ourSketch.getEdges().values();
HashMap<EntityNode, QueryNode> nodeMatches = getEntityNodePairs();
for (SketchEdge se : sketchEdges) {
if (nodeMatches.containsKey(se.getTargetEntity()) && nodeMatches.containsKey(se.getSourceEntity()) && !_edges.containsKey(se.getName())) {
View_Edge vEdge;
// need to move down??
if (se.isPartial()) {
vEdge = new PartialViewEdge(nodeMatches.get(se.getSourceEntity()), nodeMatches.get(se.getTargetEntity()), se.getName());
} else if (se.isInjective()) {
// System.out.println("Edge is injective");
// **NEED TO FIGURE OUT CASCADING
vEdge = new InjectiveViewEdge(nodeMatches.get(se.getSourceEntity()), nodeMatches.get(se.getTargetEntity()), se.getName(), Cascade.RESTRICT);
} else {
vEdge = new NormalViewEdge(nodeMatches.get(se.getSourceEntity()), nodeMatches.get(se.getTargetEntity()), se.getName());
}
this.addEdge(vEdge);
}
}
}
use of easik.sketch.vertex.EntityNode 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.vertex.EntityNode in project fql by CategoricalData.
the class ViewDataAction method actionPerformed.
/**
* @param e
* The action event
*/
@Override
public void actionPerformed(ActionEvent e) {
Object[] currentSelection = _theSketch.getSelectionCells();
if (!(currentSelection[0] instanceof EntityNode)) {
return;
}
EntityNode node = (EntityNode) currentSelection[0];
DatabaseUtil.selectRowPKs(node.getMModel().getFrame(), node);
}
Aggregations