Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
831a734668 |
@@ -31,11 +31,6 @@ public class DbSet<T extends SerializableObject> implements Queryable<T> {
|
|||||||
return new SelectExpression(List.of(DatabaseSelectAllExpression.INSTANCE), new TableExpression(table), "");
|
return new SelectExpression(List.of(DatabaseSelectAllExpression.INSTANCE), new TableExpression(table), "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public DBSet<T> clone() {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "SELECT * FROM `" + table + "`";
|
return "SELECT * FROM `" + table + "`";
|
||||||
|
|||||||
@@ -20,8 +20,6 @@ public interface Queryable<T extends Serializable> {
|
|||||||
|
|
||||||
Expression getExpression();
|
Expression getExpression();
|
||||||
|
|
||||||
Queryable<T> clone();
|
|
||||||
|
|
||||||
String toString();
|
String toString();
|
||||||
|
|
||||||
//stream functions
|
//stream functions
|
||||||
@@ -167,27 +165,27 @@ public interface Queryable<T extends Serializable> {
|
|||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// default long count() {
|
// default long count() {
|
||||||
// return new CountOp<>(this).execute();
|
// return 0;
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// default boolean anyMatch(SerializablePredicate<? super T> predicate) {
|
// default boolean anyMatch(SerializablePredicate<? super T> SerializablePredicate) {
|
||||||
// return filter(predicate).count() > 0;
|
// return false;
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// default boolean allMatch(SerializablePredicate<? super T> predicate) {
|
// default boolean allMatch(SerializablePredicate<? super T> SerializablePredicate) {
|
||||||
// return filter(predicate).count() == clone().count();
|
// return false;
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// default boolean noneMatch(SerializablePredicate<? super T> predicate) {
|
// default boolean noneMatch(SerializablePredicate<? super T> SerializablePredicate) {
|
||||||
// return filter(predicate).count() == 0;
|
// return false;
|
||||||
// }
|
// }
|
||||||
|
//
|
||||||
// default Optional<T> findFirst() {
|
// default Optional<T> findFirst() {
|
||||||
// return limit();
|
// return Optional.empty();
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// default Optional<T> findAny() {
|
// default Optional<T> findAny() {
|
||||||
// return findFirst();
|
// return Optional.empty();
|
||||||
// }
|
// }
|
||||||
//</editor-fold>
|
//</editor-fold>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,11 +24,6 @@ public class QueryableProxy<T extends Serializable> implements Queryable<T> {
|
|||||||
return delegate.getExpression();
|
return delegate.getExpression();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Queryable<T> clone() {
|
|
||||||
return new QueryableProxy<>(delegate.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<T> iterator() {
|
public Iterator<T> iterator() {
|
||||||
return delegate.iterator();
|
return delegate.iterator();
|
||||||
|
|||||||
38
src/main/java/jef/expressions/IncludeExpression.java
Normal file
38
src/main/java/jef/expressions/IncludeExpression.java
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package jef.expressions;
|
||||||
|
|
||||||
|
import jef.expressions.selectable.DatabaseSelectAllExpression;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class IncludeExpression implements Expression {
|
||||||
|
private final Expression expr;
|
||||||
|
private final List<Include> includes;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Type getType() {
|
||||||
|
return Type.INCLUDE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Priority getPriority() {
|
||||||
|
return Priority.UNDEFINED;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
var ret = new SelectExpression(List.of(new DatabaseSelectAllExpression()), expr, "").toString();
|
||||||
|
ret += " LEFT JOIN "
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class Include {
|
||||||
|
private final FieldExpression expr;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
package jef.operations;
|
|
||||||
|
|
||||||
import jef.Queryable;
|
|
||||||
import jef.expressions.Expression;
|
|
||||||
import jef.expressions.SelectExpression;
|
|
||||||
import jef.expressions.selectable.DatabaseFunctionExpression;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class CountOp<T extends Serializable> implements Operation<T> {
|
|
||||||
private final Queryable<T> queryable;
|
|
||||||
|
|
||||||
public CountOp(Queryable<T> queryable) {
|
|
||||||
this.queryable = queryable;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getTableAlias() {
|
|
||||||
return String.valueOf((char) (queryable.getTableAlias().charAt(0) + (char) 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Expression getExpression() {
|
|
||||||
return new SelectExpression(List.of(new DatabaseFunctionExpression("count(*)")), queryable.getExpression(), getTableAlias());
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Override
|
|
||||||
// public CountOp<T> clone() {
|
|
||||||
// return new CountOp<>(queryable.clone());
|
|
||||||
// }
|
|
||||||
|
|
||||||
public long execute() {
|
|
||||||
return 0;//TODO implement when sql execution part is ready
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -16,11 +16,12 @@ import jef.serializable.SerializablePredicate;
|
|||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
public class FilterOp<T extends Serializable> implements Queryable<T>, Operation<T> {
|
public class FilterOp<T extends Serializable> implements Queryable<T>, Operation<T> {
|
||||||
|
|
||||||
private final Queryable<T> queryable;
|
private final Queryable<T> queryable;
|
||||||
private final SerializablePredicate<? super T> predicate;
|
private final Predicate<? super T> predicate;
|
||||||
private final Expression predicateExpr;
|
private final Expression predicateExpr;
|
||||||
// private final Expression finalExpr;
|
// private final Expression finalExpr;
|
||||||
|
|
||||||
@@ -54,11 +55,6 @@ public class FilterOp<T extends Serializable> implements Queryable<T>, Operation
|
|||||||
return new WhereExpression(new SelectExpression(List.of(DatabaseSelectAllExpression.INSTANCE), queryable.getExpression(), getTableAlias()), predicateExpr);
|
return new WhereExpression(new SelectExpression(List.of(DatabaseSelectAllExpression.INSTANCE), queryable.getExpression(), getTableAlias()), predicateExpr);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public FilterOp<T> clone() {
|
|
||||||
return new FilterOp<>(queryable, predicate);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return getExpression().toString();
|
return getExpression().toString();
|
||||||
|
|||||||
75
src/main/java/jef/operations/IncludeOp.java
Normal file
75
src/main/java/jef/operations/IncludeOp.java
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
package jef.operations;
|
||||||
|
|
||||||
|
import jef.Queryable;
|
||||||
|
import jef.asm.AsmParseException;
|
||||||
|
import jef.asm.AsmParser;
|
||||||
|
import jef.expressions.Expression;
|
||||||
|
import jef.expressions.FieldExpression;
|
||||||
|
import jef.expressions.SelectExpression;
|
||||||
|
import jef.expressions.modifier.ExpressionOptimizerBottomUp;
|
||||||
|
import jef.expressions.modifier.IConst0Fixer;
|
||||||
|
import jef.expressions.modifier.TableAliasInjector;
|
||||||
|
import jef.expressions.modifier.TernaryRewriter;
|
||||||
|
import jef.expressions.selectable.DatabaseSelectAllExpression;
|
||||||
|
import jef.serializable.SerializableFunction;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class IncludeOp<T extends Serializable, I> implements Queryable<T> {
|
||||||
|
private final Queryable<T> queryable;
|
||||||
|
private final List<IncludeExpression.Include> includes = new ArrayList<>();
|
||||||
|
|
||||||
|
public IncludeOp(Queryable<T> queryable, SerializableFunction<? super T, ?> fieldSelector) {
|
||||||
|
this.queryable = queryable;
|
||||||
|
var f = parseFunction(fieldSelector);
|
||||||
|
this.includes.add(new IncludeExpression.Include(f, direction));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getTableAlias() {
|
||||||
|
return String.valueOf((char) (queryable.getTableAlias().charAt(0) + (char) 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Expression getExpression() {
|
||||||
|
return new IncludeExpression(new SelectExpression(List.of(new DatabaseSelectAllExpression()), queryable.getExpression(), getTableAlias()), includes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return getExpression().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IncludeOp<T, I> thenInclude(SerializableFunction<? super T, ?> fieldSelector) {
|
||||||
|
var f = parseFunction(fieldSelector);
|
||||||
|
this.includes.add(new IncludeExpression.Include(f));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private FieldExpression parseFunction(SerializableFunction<? super T, ?> fieldSelector) {
|
||||||
|
var parser = new AsmParser(fieldSelector);
|
||||||
|
Expression expr;
|
||||||
|
try {
|
||||||
|
expr = parser.parse();
|
||||||
|
} catch (AsmParseException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
System.out.println(expr);
|
||||||
|
expr = new TernaryRewriter().modify(expr);
|
||||||
|
System.out.println(expr);
|
||||||
|
expr = new IConst0Fixer().modify(expr);
|
||||||
|
System.out.println(expr);
|
||||||
|
// expr = new ExpressionOptimizer().modify(expr);
|
||||||
|
expr = new ExpressionOptimizerBottomUp().modify(expr);
|
||||||
|
expr = new TableAliasInjector(getTableAlias()).modify(expr);
|
||||||
|
if (expr instanceof FieldExpression f) {
|
||||||
|
return f;
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException(expr + " is not a field");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,11 +30,6 @@ public class LimitOp<T extends Serializable> implements Queryable<T> {
|
|||||||
return new LimitExpression(new SelectExpression(List.of(DatabaseSelectAllExpression.INSTANCE), queryable.getExpression(), getTableAlias()), start, count);
|
return new LimitExpression(new SelectExpression(List.of(DatabaseSelectAllExpression.INSTANCE), queryable.getExpression(), getTableAlias()), start, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public LimitOp<T> clone() {
|
|
||||||
return new LimitOp<>(queryable.clone(), start, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return getExpression().toString();
|
return getExpression().toString();
|
||||||
|
|||||||
Reference in New Issue
Block a user