[SQL Generator] where
This commit is contained in:
parent
5b6da187cc
commit
7a1a943f66
2 changed files with 27 additions and 4 deletions
|
@ -4,6 +4,7 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import dev.peerat.parser.java.Value;
|
import dev.peerat.parser.java.Value;
|
||||||
|
import dev.peerat.parser.java.Value.BiValue;
|
||||||
import dev.peerat.parser.java.Value.LambdaValue;
|
import dev.peerat.parser.java.Value.LambdaValue;
|
||||||
import dev.peerat.parser.java.Value.MethodCallValue;
|
import dev.peerat.parser.java.Value.MethodCallValue;
|
||||||
|
|
||||||
|
@ -11,7 +12,7 @@ public class MySQLRequestBuilder{
|
||||||
|
|
||||||
private List<MethodCallValue> calls;
|
private List<MethodCallValue> calls;
|
||||||
private String select;
|
private String select;
|
||||||
private String from;
|
private String where;
|
||||||
|
|
||||||
public MySQLRequestBuilder(List<MethodCallValue> calls){
|
public MySQLRequestBuilder(List<MethodCallValue> calls){
|
||||||
this.calls = calls;
|
this.calls = calls;
|
||||||
|
@ -34,13 +35,35 @@ public class MySQLRequestBuilder{
|
||||||
result=result.substring(1);
|
result=result.substring(1);
|
||||||
return "SELECT "+result;
|
return "SELECT "+result;
|
||||||
}
|
}
|
||||||
|
if(method.equals("filter()")){
|
||||||
|
LambdaValue include = (LambdaValue) value.getParameters().get(0);
|
||||||
|
Value condition = (Value) include.getOperations().get(0);
|
||||||
|
return toStringCondition(condition);
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void build(){
|
private String toStringCondition(Value value){
|
||||||
|
if(value instanceof BiValue){
|
||||||
|
BiValue bool = (BiValue)value;
|
||||||
|
String action = bool.getAction();
|
||||||
|
if(action.equals("||")) return toStringCondition(bool.left())+" OR "+toStringCondition(bool.right());
|
||||||
|
throw new RuntimeException("unsuported operation "+action);
|
||||||
|
}
|
||||||
|
if(value instanceof MethodCallValue){ //take method call instead of value
|
||||||
|
MethodCallValue call = (MethodCallValue)value;
|
||||||
|
String action = call.getToken().getValue();
|
||||||
|
if(action.equals("contains()")) return call.base().getToken().getValue()+" LIKE \"%?%\"";
|
||||||
|
return action;
|
||||||
|
}
|
||||||
|
return "default "+value.getClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String build(){
|
||||||
this.select = toStringOperation(calls.get(0));
|
this.select = toStringOperation(calls.get(0));
|
||||||
throw new RuntimeException(this.select);
|
this.where = toStringOperation(calls.get(1));
|
||||||
|
return select+" WHERE "+where;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,7 +49,7 @@ public class MySQLProvider implements Provider{
|
||||||
}
|
}
|
||||||
list.add(0, call);
|
list.add(0, call);
|
||||||
MySQLRequestBuilder builder = new MySQLRequestBuilder(list);
|
MySQLRequestBuilder builder = new MySQLRequestBuilder(list);
|
||||||
builder.build();
|
throw new RuntimeException(builder.build());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue