Skip to content
Snippets Groups Projects
Commit 0e2aadbb authored by Paolo Guagliardo's avatar Paolo Guagliardo
Browse files

Add translation of selection conditions

parent c3022622
No related branches found
No related tags found
No related merge requests found
......@@ -35,4 +35,21 @@ public abstract class BinaryCondition extends Condition {
sign.addAll(rightCondition.signature());
return sign;
}
@Override
public String toSQL() {
String op;
switch (this.getType()) {
case CONJUNCTION:
op = "AND";
break;
case DISJUNCTION:
op = "OR";
break;
default:
throw new IllegalStateException();
}
return String.format("(%s %s %s)",
this.leftCondition.toSQL(), op, this.rightCondition.toSQL());
}
}
......@@ -53,4 +53,6 @@ public abstract class Condition {
public abstract boolean satisfied(String[] record, Map<String,Integer> attr);
public abstract Set<String> signature();
public abstract String toSQL();
}
......@@ -13,4 +13,10 @@ public class Conjunction extends BinaryCondition {
return super.leftCondition.satisfied(record, attr)
&& super.rightCondition.satisfied(record, attr);
}
@Override
public String toSQL() {
// TODO Auto-generated method stub
return null;
}
}
......@@ -13,4 +13,10 @@ public class Disjunction extends BinaryCondition {
return super.leftCondition.satisfied(record, attr)
|| super.rightCondition.satisfied(record, attr);
}
@Override
public String toSQL() {
// TODO Auto-generated method stub
return null;
}
}
......@@ -4,6 +4,8 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import uk.ac.ed.pguaglia.real.db.Database;
public class Equality extends Condition {
private Term left;
......@@ -48,4 +50,13 @@ public class Equality extends Condition {
String v2 = right.getValue(record, attr);
return v1.equals(v2);
}
@Override
public String toSQL() {
String left = this.left.isAttribute() ?
Database.encode(this.left.getValue()) : "'" + this.left.getValue() + "'";
String right = this.right.isAttribute() ?
Database.encode(this.right.getValue()) : "'" + this.right.getValue() + "'";
return left + " = " + right;
}
}
......@@ -4,6 +4,8 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import uk.ac.ed.pguaglia.real.db.Database;
public class LessThan extends Condition {
private Term left;
......@@ -50,4 +52,13 @@ public class LessThan extends Condition {
return v1.compareTo(v2) < 0;
}
}
@Override
public String toSQL() {
String left = this.left.isAttribute() ?
Database.encode(this.left.getValue()) : "'" + this.left.getValue() + "'";
String right = this.right.isAttribute() ?
Database.encode(this.right.getValue()) : "'" + this.right.getValue() + "'";
return left + " < " + right;
}
}
......@@ -30,4 +30,9 @@ public class Negation extends Condition {
public boolean satisfied(String[] record, Map<String, Integer> attr) {
return !cond.satisfied(record, attr);
}
@Override
public String toSQL() {
return "NOT (" + this.cond.toSQL() + ")";
}
}
......@@ -68,10 +68,7 @@ public class Selection extends UnaryOperation {
@Override
public String toSQL(boolean setEval) {
String cond = this.cond.toString()
.replace(Condition.Type.CONJUNCTION.getConnective(), " AND ")
.replace(Condition.Type.DISJUNCTION.getConnective(), " OR ")
.replace(Condition.Type.NEGATION.getConnective(), " NOT ");
return String.format("SELECT * FROM (%s) Q WHERE %s", this.operand.toSQL(setEval), cond);
return String.format("SELECT * FROM (%s) Q WHERE %s",
this.operand.toSQL(setEval), this.cond.toSQL());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment