in sql-gremlin/src/main/java/org/twilmes/sql/gremlin/adapter/converter/ast/nodes/select/GremlinSqlSelectSingle.java [98:154]
public GraphTraversal<?, ?> generateTraversal() throws SQLException {
if (sqlSelect.getSelectList() == null) {
throw SqlGremlinError.create(SqlGremlinError.SELECT_NO_LIST);
}
final GremlinSqlOperator gremlinSqlOperator =
GremlinSqlFactory.createOperator(sqlBasicCall.getOperator(), sqlBasicCall.getOperandList());
if (!(gremlinSqlOperator instanceof GremlinSqlAsOperator)) {
throw SqlGremlinError.create(SqlGremlinError.UNEXPECTED_FROM_FORMAT);
}
final List<GremlinSqlNode> gremlinSqlOperands = GremlinSqlFactory.createNodeList(sqlBasicCall.getOperandList());
final List<GremlinSqlIdentifier> gremlinSqlIdentifiers = new ArrayList<>();
for (final GremlinSqlNode gremlinSqlOperand : gremlinSqlOperands) {
if (!(gremlinSqlOperand instanceof GremlinSqlIdentifier)) {
throw SqlGremlinError.create(SqlGremlinError.UNEXPECTED_FROM_FORMAT);
}
gremlinSqlIdentifiers.add((GremlinSqlIdentifier) gremlinSqlOperand);
}
GraphTraversal<?, ?> graphTraversal = null;
try {
graphTraversal =
SqlTraversalEngine.generateInitialSql(gremlinSqlIdentifiers, sqlMetadata, g);
final String label = sqlMetadata.getActualTableName(gremlinSqlIdentifiers.get(0).getName(1));
// This function basically generates the latter parts of the traversal, by doing this it prepares all the
// renamed labels in the metadata so that queries like 'SELECT foo AS bar FROM baz ORDER BY bar'
// can properly recognize that bar=>foo.
// __.__() is passed in as an anonymous traversal that will be discarded.
generateDataRetrieval(gremlinSqlIdentifiers, __.__());
// Generate actual traversal.
applyWhere(graphTraversal);
applyGroupBy(graphTraversal, label);
applySelectValues(graphTraversal);
applyOrderBy(graphTraversal, label);
applyHaving(graphTraversal);
sqlMetadata.setIsDoneFilters(true);
generateDataRetrieval(gremlinSqlIdentifiers, graphTraversal);
if (sqlMetadata.getRenamedColumns() == null) {
throw SqlGremlinError.create(SqlGremlinError.COLUMN_RENAME_LIST_EMPTY);
}
if (sqlMetadata.getTables().size() != 1) {
throw SqlGremlinError.create(SqlGremlinError.NO_TRAVERSAL_TABLE);
}
return graphTraversal;
} catch (final SQLException e) {
if (graphTraversal != null) {
try {
graphTraversal.close();
} catch (final Exception ignored) {
}
}
throw e;
}
}