Skip to content

Commit

Permalink
[Feature #19] Refactor SOQL-related code - move grammar into correct …
Browse files Browse the repository at this point in the history
…folder, use Antlr maven plugin to generate lexer and parser, delete obsolete auxiliary files. Rename grammar to Soql.
  • Loading branch information
ledsoft committed Feb 26, 2020
1 parent d3e1d8d commit 06a93f0
Show file tree
Hide file tree
Showing 17 changed files with 341 additions and 2,819 deletions.
19 changes: 18 additions & 1 deletion jopa-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<artifactId>jopa-impl</artifactId>
<packaging>jar</packaging>

<properties>
<org.antlr4.version>4.8-1</org.antlr4.version>
</properties>

<dependencies>
<dependency>
<groupId>cz.cvut.kbss.jopa</groupId>
Expand Down Expand Up @@ -45,13 +49,26 @@
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.8-1</version>
<version>${org.antlr4.version}</version>
</dependency>
</dependencies>


<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>${org.antlr4.version}</version>
<executions>
<execution>
<id>antlr</id>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
grammar soql;
grammar Soql;


querySentence : selectStatement whereClausuleWrapper? groupByClausule? orderByClausule? ;
Expand Down Expand Up @@ -137,4 +137,4 @@ NUMBER: DIGIT+ ;

VALUE: NUMBER ;

WHITESPACE: (' ')+ -> skip;
WHITESPACE: (' ')+ -> skip;

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,86 +20,118 @@ public SoqlAttribute() {
super();
}

public String getValue() { return value; }
public String getValue() {
return value;
}

public void setValue(String value) { this.value = value; }
public void setValue(String value) {
this.value = value;
}

public boolean isNot() { return isNot; }
public boolean isNot() {
return isNot;
}

public void setNot(boolean not) { isNot = not; }
public void setNot(boolean not) {
isNot = not;
}

public void setOperator(String operator) { this.operator = operator; }
public void setOperator(String operator) {
this.operator = operator;
}

public String getOperator() { return operator; }
public String getOperator() {
return operator;
}

public void setPrefix(String prefix){ this.prefix = prefix; }
public void setPrefix(String prefix) {
this.prefix = prefix;
}

public String getPrefix(){ return this.prefix; }
public String getPrefix() {
return this.prefix;
}

public void setRdfType(String rdfType){ this.rdfType = rdfType; }
public void setRdfType(String rdfType) {
this.rdfType = rdfType;
}

public String getRdfType(){ return this.rdfType; }
public String getRdfType() {
return this.rdfType;
}

public boolean isOrderBy() { return isOrderBy; }
public boolean isOrderBy() {
return isOrderBy;
}

public void setOrderBy(boolean orderBy) { isOrderBy = orderBy; }
public void setOrderBy(boolean orderBy) {
isOrderBy = orderBy;
}

public boolean isGroupBy() { return isGroupBy; }
public boolean isGroupBy() {
return isGroupBy;
}

public void setGroupBy(boolean groupBy) { isGroupBy = groupBy; }
public void setGroupBy(boolean groupBy) {
isGroupBy = groupBy;
}

public boolean isFilter() {
return !operator.isEmpty() && !operator.equals("=");
}

public boolean isObject(){ return !getFirstNode().hasNextChild(); }
public boolean isObject() {
return !getFirstNode().hasNextChild();
}

private boolean isValueParam(){
private boolean isValueParam() {
return !operator.isEmpty() && value.charAt(0) == ':';
}

public String getFilter(){
public String getFilter() {
StringBuilder buildFilter = new StringBuilder();
if(operator.equals("LIKE")){
buildFilter.append("regex(").append(getAsParam()).append(", ?").append(this.value.substring(1)).append(") ");
if (operator.equals("LIKE")) {
buildFilter.append("regex(").append(getAsParam()).append(", ?").append(this.value.substring(1))
.append(") ");
} else {
buildFilter.append(getAsParam()).append(" ").append(this.operator).append(" ").append("?").append(this.value.substring(1));
buildFilter.append(getAsParam()).append(" ").append(this.operator).append(" ").append("?")
.append(this.value.substring(1));
}
return buildFilter.toString();
}

public String getTripplePattern(){
public String getTripplePattern() {
StringBuilder buildTP = new StringBuilder("?x ");
if(isObject()){
if (isObject()) {
buildTP.append(getRdfType()).append(" ")
.append(toIri(getFirstNode())).append(" . ");
}else{
.append(toIri(getFirstNode())).append(" . ");
} else {
SoqlNode pointer = getFirstNode().getChild();
StringBuilder buildParam = new StringBuilder("?");
buildParam.append(getFirstNode().getValue());
buildParam.append(pointer.getCapitalizedvalue());
String param = "";
if (pointer.hasNextChild()){
buildParam.append(pointer.getCapitalizedValue());
String param;
if (pointer.hasNextChild()) {
param = "?" + pointer.getValue();
}else{
if (isFilter()){
} else {
if (isFilter()) {
param = buildParam.toString();
}else{
} else {
param = "?" + this.value.substring(1);
}
}
buildTP.append(toIri(pointer)).append(" ").append(param).append(" . ");
while(pointer.hasNextChild()){
while (pointer.hasNextChild()) {
SoqlNode newPointer = pointer.getChild();
buildTP.append("?").append(pointer.getValue())
.append(" ").append(toIri(newPointer)).append(" ");
buildParam.append(newPointer.getCapitalizedvalue());
if(newPointer.hasNextChild()){
.append(" ").append(toIri(newPointer)).append(" ");
buildParam.append(newPointer.getCapitalizedValue());
if (newPointer.hasNextChild()) {
buildTP.append("?").append(pointer.getChild().getValue());
}else{
if (isFilter()){
} else {
if (isFilter()) {
buildTP.append(buildParam);
}else{
} else {
buildTP.append("?").append(this.value.substring(1));
}
}
Expand All @@ -110,7 +142,7 @@ public String getTripplePattern(){
return buildTP.toString();
}

private StringBuilder toIri(SoqlNode node){
private StringBuilder toIri(SoqlNode node) {
StringBuilder sb = new StringBuilder("<");
String prefix = node.getIri().isEmpty() ? getPrefix() + node.getValue() : node.getIri();
sb.append(prefix).append(">");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void setAttribute(SoqlAttribute attribute) {
this.attribute = attribute;
}

public String getGroupByPart(){
public String getGroupByPart() {
String param = attribute.isFilter() ? getAsParam().substring(1) : attribute.getValue().substring(1);
return "?" + param + " ";
}
Expand Down
29 changes: 17 additions & 12 deletions jopa-impl/src/main/java/cz/cvut/kbss/jopa/query/soql/SoqlNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,37 @@ public SoqlNode(SoqlNode parent, SoqlNode child, String value) {
this.value = value;
}

public SoqlNode(String value){
public SoqlNode(String value) {
this.value = value;
}

public SoqlNode(SoqlNode parent, String value){
public SoqlNode(SoqlNode parent, String value) {
this.parent = parent;
this.value = value;
}



public boolean hasNextChild(){
public boolean hasNextChild() {
return this.child != null;
}

public SoqlNode getChild(){
public SoqlNode getChild() {
return this.child;
}

public boolean hasNextParent(){
public boolean hasNextParent() {
return this.parent != null;
}

public SoqlNode getParent(){
public SoqlNode getParent() {
return this.parent;
}

public String getValue(){
public String getValue() {
return this.value;
}

public String getCapitalizedvalue(){
public String getCapitalizedValue() {
return this.value.substring(0, 1).toUpperCase() + this.value.substring(1);
}

Expand All @@ -56,11 +55,17 @@ public void setParent(SoqlNode parent) {
this.parent = parent;
}

public void setValue(String value) { this.value = value; }
public void setValue(String value) {
this.value = value;
}

public String getIri() { return iri; }
public String getIri() {
return iri;
}

public void setIri(String iri) { this.iri = iri; }
public void setIri(String iri) {
this.iri = iri;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class SoqlOrderParameter extends SoqlParameter {

private String orderingBy ;
private String orderingBy;

private SoqlAttribute attribute;

Expand All @@ -28,12 +28,12 @@ public void setAttribute(SoqlAttribute attribute) {
this.attribute = attribute;
}

public String getOrderByPart(){
public String getOrderByPart() {
String param = attribute.isFilter() ? getAsParam().substring(1) : attribute.getValue().substring(1);
StringBuilder sb = new StringBuilder();
if(orderingBy.equals("ASC")){
if (orderingBy.equals("ASC")) {
sb.append("?").append(param).append(" ");
}else{
} else {
sb.append("DESC(?").append(param).append(") ");
}
return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ public class SoqlParameter {
public SoqlParameter() {
}

public String getAsParam(){
public String getAsParam() {
StringBuilder buildParam = new StringBuilder("?");
buildParam.append(firstNode.getValue());
SoqlNode pointer = firstNode;
while (pointer.hasNextChild()) {
pointer = pointer.getChild();
buildParam.append(pointer.getCapitalizedvalue());
buildParam.append(pointer.getCapitalizedValue());
}
return buildParam.toString();
}
Expand All @@ -26,19 +26,19 @@ public void setFirstNode(SoqlNode firstNode) {
this.firstNode = firstNode;
}

public String getAsValue(){
public String getAsValue() {
StringBuilder buildParam = new StringBuilder("?");
SoqlNode firstNode = getFirstNode();
SoqlNode pointer;
if(firstNode.hasNextChild()){
if (firstNode.hasNextChild()) {
pointer = getFirstNode().getChild();
} else {
return "?x";
}
buildParam.append(pointer.getValue());
while (pointer.hasNextChild()) {
pointer = pointer.getChild();
buildParam.append(pointer.getCapitalizedvalue());
buildParam.append(pointer.getCapitalizedValue());
}
return buildParam.toString();
}
Expand Down
Loading

0 comments on commit 06a93f0

Please sign in to comment.