Lon time not commit, I think there are : MySqlMapper base + refractor of usage of the peer@code's parser
This commit is contained in:
parent
b3195d1960
commit
4e9e1034fa
6 changed files with 111 additions and 96 deletions
Binary file not shown.
|
@ -33,8 +33,9 @@ import dev.peerat.parser.java.Function;
|
|||
import dev.peerat.parser.java.JavaElement;
|
||||
import dev.peerat.parser.java.JavaFile;
|
||||
import dev.peerat.parser.java.JavaParser;
|
||||
import dev.peerat.parser.java.Value.InstanceValue;
|
||||
import dev.peerat.parser.java.operation.ReturnOperation;
|
||||
import dev.peerat.parser.java.value.InstanceValue;
|
||||
import dev.peerat.parser.java.value.StaticValue;
|
||||
|
||||
public class TreasureProcessor extends AbstractProcessor{
|
||||
|
||||
|
@ -86,7 +87,7 @@ public class TreasureProcessor extends AbstractProcessor{
|
|||
}, values);
|
||||
if(values.size() < 1) continue;
|
||||
InstanceValue ship = (InstanceValue)values.get(0);
|
||||
String providerType = ship.getParameters().get(0).getToken().getValue().replace("\"", "");
|
||||
String providerType = ((StaticValue)ship.getParameters().get(0)).getToken().getValue().replace("\"", "");
|
||||
provider = ProviderManager.getInstance().getProvider(providerType);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
package dev.peerat.mapping.providers.mysql;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MySQLDatabaseState{
|
||||
|
||||
private List<Table> tables;
|
||||
|
||||
public MySQLDatabaseState(){
|
||||
this.tables = new ArrayList<>();
|
||||
}
|
||||
|
||||
public static class Table{
|
||||
|
||||
private String name;
|
||||
private List<Column> columns;
|
||||
|
||||
public Table(String name){
|
||||
this.name = name;
|
||||
this.columns = new ArrayList<>();
|
||||
}
|
||||
|
||||
public String getName(){
|
||||
return this.name;
|
||||
}
|
||||
|
||||
|
||||
public static class Column{
|
||||
|
||||
private String name;
|
||||
private String type;
|
||||
private boolean primary;
|
||||
private Column foreign;
|
||||
|
||||
public Column(String name, String type, boolean primary){
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.primary = primary;
|
||||
}
|
||||
|
||||
public void setForeign(Column column){
|
||||
this.foreign = column;
|
||||
}
|
||||
|
||||
public String getName(){
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public String getType(){
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public boolean isPrimary(){
|
||||
return this.primary;
|
||||
}
|
||||
|
||||
public Column getForeign(){
|
||||
return this.foreign;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,10 +4,10 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
|
||||
import dev.peerat.parser.java.Function;
|
||||
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.MethodCallValue;
|
||||
import dev.peerat.parser.java.value.BiValue;
|
||||
import dev.peerat.parser.java.value.LambdaValue;
|
||||
import dev.peerat.parser.java.value.MethodCallValue;
|
||||
import dev.peerat.parser.java.value.Value;
|
||||
|
||||
public class MySQLRequestBuilder{
|
||||
|
||||
|
@ -36,7 +36,7 @@ public class MySQLRequestBuilder{
|
|||
MethodCallValue keep = (MethodCallValue)include.getOperations().get(0);
|
||||
List<String> binds = new ArrayList<>();
|
||||
for(Value param : keep.getParameters()){
|
||||
binds.add(param.getToken().getValue());
|
||||
// binds.add(param.getToken().getValue());
|
||||
}
|
||||
String result = ""; //optimise with iterator
|
||||
for(String bind : binds) result+=","+bind;
|
||||
|
@ -64,9 +64,9 @@ public class MySQLRequestBuilder{
|
|||
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 \"%?%\"";
|
||||
if(action.equals("startsWith()")) return call.base().getToken().getValue()+" LIKE \"?%\"";
|
||||
if(action.equals("endsWith()")) return call.base().getToken().getValue()+" LIKE \"%?\"";
|
||||
// if(action.equals("contains()")) return call.base().getToken().getValue()+" LIKE \"%?%\"";
|
||||
// if(action.equals("startsWith()")) return call.base().getToken().getValue()+" LIKE \"?%\"";
|
||||
// if(action.equals("endsWith()")) return call.base().getToken().getValue()+" LIKE \"%?\"";
|
||||
return action;
|
||||
}
|
||||
return "default "+value.getClass();
|
||||
|
|
|
@ -1,5 +1,12 @@
|
|||
package dev.peerat.mapping.providers.mysql;
|
||||
|
||||
import static dev.peerat.parser.java.builder.JavaBuilder.ofFile;
|
||||
import static dev.peerat.parser.java.builder.JavaBuilder.ofClass;
|
||||
import static dev.peerat.parser.java.builder.JavaBuilder.ofFunction;
|
||||
import static dev.peerat.parser.java.builder.JavaBuilder.ofVariable;
|
||||
import static dev.peerat.parser.java.builder.JavaBuilder.ofOperation;
|
||||
import static dev.peerat.parser.java.builder.JavaBuilder.ofValue;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
|
@ -14,16 +21,18 @@ import dev.peerat.mapping.providers.Provider;
|
|||
import dev.peerat.mapping.providers.ProviderManager;
|
||||
import dev.peerat.parser.ElementBuilder.Builder;
|
||||
import dev.peerat.parser.Token;
|
||||
import dev.peerat.parser.java.BuildedJavaFile;
|
||||
import dev.peerat.parser.java.Class;
|
||||
import dev.peerat.parser.java.Function;
|
||||
import dev.peerat.parser.java.Value;
|
||||
import dev.peerat.parser.java.Value.MethodCallValue;
|
||||
import dev.peerat.parser.java.JavaFile;
|
||||
import dev.peerat.parser.java.Variable;
|
||||
import dev.peerat.parser.java.builder.JavaBuilder;
|
||||
import dev.peerat.parser.java.builder.JavaClassBuilder;
|
||||
import dev.peerat.parser.java.builder.JavaFunctionBuilder;
|
||||
import dev.peerat.parser.java.operation.CatchOperation;
|
||||
import dev.peerat.parser.java.operation.MethodCallOperation;
|
||||
import dev.peerat.parser.java.operation.ReturnOperation;
|
||||
import dev.peerat.parser.java.operation.TryOperation;
|
||||
import dev.peerat.parser.java.value.MethodCallValue;
|
||||
import dev.peerat.parser.java.value.Value;
|
||||
|
||||
public class MySQLProvider implements Provider{
|
||||
|
||||
|
@ -78,52 +87,32 @@ public class MySQLProvider implements Provider{
|
|||
|
||||
@Override
|
||||
public void complete(){
|
||||
try{
|
||||
BuildedJavaFile file = new BuildedJavaFile();
|
||||
file.setPackage("dev.peerat.mapping.sql.MySQlRepository");
|
||||
//
|
||||
// file.addImport("");
|
||||
//
|
||||
Class clazz = new Class(null, new Token(0,0,"MySQlRepository",null));
|
||||
JavaClassBuilder clazz = ofClass("MySQlRepository");
|
||||
|
||||
for(MySQLRequestBuilder request : this.requests){
|
||||
Function base = request.getFunction();
|
||||
Function func = new Function(
|
||||
null,
|
||||
Modifier.PUBLIC,
|
||||
null,
|
||||
new Token(0,0,base.getReturnType().getValue(), null),
|
||||
new Token(0,0,"getter_"+base.getName().getValue(),null),
|
||||
base.getParameters());
|
||||
|
||||
TryOperation tryOp = new TryOperation();
|
||||
tryOp.addOperation(new MethodCallOperation((Value)null, new Token(0,0,"ensureConnection()",null), null));
|
||||
tryOp.addVariable(
|
||||
new Variable(
|
||||
null,
|
||||
0,
|
||||
new Token(0,0,"PreparedStatement",null),
|
||||
new Token(0,0,"stmt",null),
|
||||
false,
|
||||
new MethodCallValue(
|
||||
new Value(new Token(0,0,"con",null)),
|
||||
null,
|
||||
new Token(0,0,"prepareStatement",null),
|
||||
Arrays.asList(new Value(new Token(0,0,request.build(),null))))));
|
||||
|
||||
CatchOperation catchOp = new CatchOperation(Arrays.asList(new Token(0,0,"Exception",null)), new Token(0,0,"e", null));
|
||||
catchOp.addOperation(new MethodCallOperation(new Value(new Token(0,0,"e",null)), new Token(0,0,"printStackTrace()", null), null));
|
||||
|
||||
func.addOperation(tryOp);
|
||||
func.addOperation(catchOp);
|
||||
|
||||
clazz.addFunction(func);
|
||||
JavaFunctionBuilder func = ofFunction(base.getReturnType().getValue(), "getter_"+base.getName().getValue()).setPublic();
|
||||
for(Variable params : base.getParameters()){
|
||||
func.parameter(ofVariable(params.getType().getValue(), params.getName().getValue(), params.getValue()));
|
||||
}
|
||||
clazz.element(
|
||||
func.element(
|
||||
ofOperation("try{"
|
||||
+ "ensureConnection();"
|
||||
+ "PreparedStatement stmt = con.prepareStatement();"
|
||||
+ "}catch(Exception e){"
|
||||
+ "e.printStackTrace();"
|
||||
+ "}")
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
file.setClass(clazz);
|
||||
|
||||
JavaFile file = ofFile("dev.peerat.mapping.sql.MySQlRepository")
|
||||
.clazz(clazz)
|
||||
.build();
|
||||
try{
|
||||
Builder builder = new Builder();
|
||||
file.build().build(builder);
|
||||
file.build(builder);
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(this.repo));
|
||||
builder.build(writer);
|
||||
writer.flush();
|
||||
|
|
|
@ -1,38 +0,0 @@
|
|||
package dev.peerat.parser.java;
|
||||
|
||||
import dev.peerat.parser.Bag;
|
||||
import dev.peerat.parser.Token;
|
||||
|
||||
public class BuildedJavaFile{
|
||||
|
||||
private JavaFile file;
|
||||
|
||||
public BuildedJavaFile(){
|
||||
this.file = new JavaFile();
|
||||
}
|
||||
|
||||
public BuildedJavaFile setPackage(String value){
|
||||
Bag bag = new Bag();
|
||||
bag.set(new Token(0, 0, value, null));
|
||||
this.file.setPackage(bag);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BuildedJavaFile addImport(String... values){
|
||||
for(String value : values){
|
||||
Import imp = new Import(false, new Token(0,0,value,null));
|
||||
this.file.addImport(imp);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public BuildedJavaFile setClass(Class clazz){
|
||||
this.file.addClass(clazz);
|
||||
return this;
|
||||
}
|
||||
|
||||
public JavaFile build(){
|
||||
return this.file;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue