Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/277 soql projection #282

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -784,19 +784,33 @@ public String getSparqlQuery() {
return sparql;
}

private String getSelectParameter(SoqlAttribute attribute) {
SoqlNode firstNode = attribute.getFirstNode();
if(!firstNode.hasChild()) {
return rootVariable;
}

setIris(firstNode); // we have to reassign the iris, because the table was not initialized when the first attribute was set
return attribute.getAsValue(rootVariable);
}

//Methods to build new Query
private void buildSparqlQueryString() {
if (attributes.isEmpty()) {
return;
}

// the first attribute is either a projected parameter or a type attribute
String selectParameter = getSelectParameter(attributes.get(0));

StringBuilder newQueryBuilder = new StringBuilder(typeDef);
if (isSelectedParamCount) {
newQueryBuilder.append(getCountPart());
newQueryBuilder.append(getCountPart(selectParameter));
} else {
if (isSelectedParamDistinct) {
newQueryBuilder.append(' ').append(SoqlConstants.DISTINCT);
}
newQueryBuilder.append(' ').append(rootVariable).append(' ');
newQueryBuilder.append(' ').append(selectParameter).append(' ');
}
newQueryBuilder.append("WHERE { ");
newQueryBuilder.append(processSupremeAttributes());
Expand All @@ -818,12 +832,12 @@ private void buildSparqlQueryString() {
LOG.trace("Translated SOQL query '{}' to SPARQL '{}'.", soql, sparql);
}

private StringBuilder getCountPart() {
private StringBuilder getCountPart(String selectParameter) {
StringBuilder countPart = new StringBuilder(" (COUNT(");
if (isSelectedParamDistinct) {
countPart.append(SoqlConstants.DISTINCT).append(' ');
}
countPart.append(rootVariable).append(") AS ?count) ");
countPart.append(selectParameter).append(") AS ?count) ");
return countPart;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,51 @@ public void testTranslateQuerySelectProperty() {
assertEquals(expectedSoqlQuery, generatedSoqlQuery);
}

@Test
void testTranslateQueryCountProperty() {
CriteriaQueryImpl<Integer> query = cb.createQuery(Integer.class);
Root<OWLClassD> root = query.from(OWLClassD.class);
query.select(cb.count(root.getAttr("owlClassA")));

final String generatedSoqlQuery = query.translateQuery(criteriaParameterFiller);
final String expectedSoqlQuery = "SELECT COUNT(owlclassd.owlClassA) FROM OWLClassD owlclassd";
assertEquals(expectedSoqlQuery, generatedSoqlQuery);
}

@Test
void testTranslateQueryDistinctCountProperty() {
CriteriaQueryImpl<Integer> query = cb.createQuery(Integer.class);
Root<OWLClassD> root = query.from(OWLClassD.class);
query.select(cb.count(root.getAttr("owlClassA"))).distinct();

final String generatedSoqlQuery = query.translateQuery(criteriaParameterFiller);
final String expectedSoqlQuery = "SELECT DISTINCT COUNT(owlclassd.owlClassA) FROM OWLClassD owlclassd";
assertEquals(expectedSoqlQuery, generatedSoqlQuery);
}

@Test
void parseQuerySupportsCountWithProjectedAttribute() {
CriteriaQueryImpl<Integer> query = cb.createQuery(Integer.class);
Root<OWLClassD> root = query.from(OWLClassD.class);
query.select(cb.count(root.getAttr("owlClassA"))).distinct();

final String generatedSoqlQuery = query.translateQuery(criteriaParameterFiller);
final String expectedSoqlQuery = "SELECT DISTINCT COUNT(owlclassd.owlClassA) FROM OWLClassD owlclassd";
assertEquals(expectedSoqlQuery, generatedSoqlQuery);
}

@Test
void parseQuerySupportsCountWithProjectedAttributeAndRelatedAttribute() {
CriteriaQueryImpl<Integer> query = cb.createQuery(Integer.class);
Root<OWLClassD> root = query.from(OWLClassD.class);
query.select(cb.count(root.getAttr("owlClassA"))).distinct()
.where(cb.equal(root.getAttr("owlClassA").getAttr("stringAttribute"), ""));

final String generatedSoqlQuery = query.translateQuery(criteriaParameterFiller);
final String expectedSoqlQuery = "SELECT DISTINCT COUNT(owlclassd.owlClassA) FROM OWLClassD owlclassd WHERE owlclassd.owlClassA.stringAttribute = :generatedName0";
assertEquals(expectedSoqlQuery, generatedSoqlQuery);
}

@Test
public void testTranslateQuerySelectPropertyPath() {
CriteriaQueryImpl<String> query = cb.createQuery(String.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,50 @@ void parseQueryWithMemberOfPluralAttribute() {
parseAndAssertEquality(soql, expectedSparql);
}

@Test
void parseQuerySupportsCountWithProjectedAttribute() {
final String soqlIdFirst = "SELECT COUNT(d.owlClassA) FROM OWLClassD d WHERE d.uri = :uri";
final String expectedSparql = "SELECT (COUNT(?owlClassA) AS ?count) WHERE { ?uri a " + strUri(Vocabulary.c_OwlClassD) + " . " +
"?uri " + strUri(Vocabulary.p_h_hasA) + " ?owlClassA . }";
parseAndAssertEquality(soqlIdFirst, expectedSparql);
}

@Test
void parseQuerySupportsDistinctCountWithProjectedAttribute() {
final String soqlIdFirst = "SELECT DISTINCT COUNT(d.owlClassA) FROM OWLClassD d WHERE d.uri = :uri";
final String expectedSparql = "SELECT (COUNT(DISTINCT ?owlClassA) AS ?count) WHERE { ?uri a " + strUri(Vocabulary.c_OwlClassD) + " . " +
"?uri " + strUri(Vocabulary.p_h_hasA) + " ?owlClassA . }";
parseAndAssertEquality(soqlIdFirst, expectedSparql);
}

@Test
void parseQueryWithProjectedAttribute() {
final String soqlIdFirst = "SELECT d.owlClassA FROM OWLClassD d WHERE d.uri = :uri";
final String expectedSparql = "SELECT ?owlClassA WHERE { ?uri a " + strUri(Vocabulary.c_OwlClassD) + " . " +
"?uri " + strUri(Vocabulary.p_h_hasA) + " ?owlClassA . }";
parseAndAssertEquality(soqlIdFirst, expectedSparql);
}

@Test
void parseQueryWithProjectedAttributeAndRelatedAttribute() {
final String soqlIdFirst = "SELECT d.owlClassA FROM OWLClassD d WHERE d.uri = :uri AND d.owlClassA.stringAttribute = :stringAtt";
final String expectedSparql = "SELECT ?owlClassA WHERE { ?uri a " + strUri(Vocabulary.c_OwlClassD) + " . " +
"?uri " + strUri(Vocabulary.p_h_hasA) + " ?owlClassA . " + // FIXME: Duplicated SPARQL #281
"?uri " + strUri(Vocabulary.p_h_hasA) + " ?owlClassA . " +
"?owlClassA " + strUri(Vocabulary.p_a_stringAttribute) + " ?stringAtt . }";
parseAndAssertEquality(soqlIdFirst, expectedSparql);
}

@Test
void parseQueryWithDistinctProjectedAttributeAndRelatedAttribute() {
final String soqlIdFirst = "SELECT DISTINCT d.owlClassA FROM OWLClassD d WHERE d.uri = :uri AND d.owlClassA.stringAttribute = :stringAtt";
final String expectedSparql = "SELECT DISTINCT ?owlClassA WHERE { ?uri a " + strUri(Vocabulary.c_OwlClassD) + " . " +
"?uri " + strUri(Vocabulary.p_h_hasA) + " ?owlClassA . " + // FIXME: Duplicated SPARQL #281
"?uri " + strUri(Vocabulary.p_h_hasA) + " ?owlClassA . " +
"?owlClassA " + strUri(Vocabulary.p_a_stringAttribute) + " ?stringAtt . }";
parseAndAssertEquality(soqlIdFirst, expectedSparql);
}

/**
* Bug #178
*/
Expand Down
Loading