Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2876c6d2e7 |
@@ -31,6 +31,11 @@ public class DbSet<T extends SerializableObject> implements Queryable<T> {
|
||||
return new SelectExpression(List.of(DatabaseSelectAllExpression.INSTANCE), new TableExpression(table), "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public DBSet<T> clone() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SELECT * FROM `" + table + "`";
|
||||
|
||||
@@ -20,6 +20,8 @@ public interface Queryable<T extends Serializable> {
|
||||
|
||||
Expression getExpression();
|
||||
|
||||
Queryable<T> clone();
|
||||
|
||||
String toString();
|
||||
|
||||
//stream functions
|
||||
@@ -165,27 +167,27 @@ public interface Queryable<T extends Serializable> {
|
||||
// }
|
||||
//
|
||||
// default long count() {
|
||||
// return 0;
|
||||
// return new CountOp<>(this).execute();
|
||||
// }
|
||||
//
|
||||
// default boolean anyMatch(SerializablePredicate<? super T> SerializablePredicate) {
|
||||
// return false;
|
||||
// default boolean anyMatch(SerializablePredicate<? super T> predicate) {
|
||||
// return filter(predicate).count() > 0;
|
||||
// }
|
||||
//
|
||||
// default boolean allMatch(SerializablePredicate<? super T> SerializablePredicate) {
|
||||
// return false;
|
||||
// default boolean allMatch(SerializablePredicate<? super T> predicate) {
|
||||
// return filter(predicate).count() == clone().count();
|
||||
// }
|
||||
//
|
||||
// default boolean noneMatch(SerializablePredicate<? super T> SerializablePredicate) {
|
||||
// return false;
|
||||
// default boolean noneMatch(SerializablePredicate<? super T> predicate) {
|
||||
// return filter(predicate).count() == 0;
|
||||
// }
|
||||
//
|
||||
|
||||
// default Optional<T> findFirst() {
|
||||
// return Optional.empty();
|
||||
// return limit();
|
||||
// }
|
||||
//
|
||||
// default Optional<T> findAny() {
|
||||
// return Optional.empty();
|
||||
// return findFirst();
|
||||
// }
|
||||
//</editor-fold>
|
||||
}
|
||||
|
||||
@@ -24,6 +24,11 @@ public class QueryableProxy<T extends Serializable> implements Queryable<T> {
|
||||
return delegate.getExpression();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queryable<T> clone() {
|
||||
return new QueryableProxy<>(delegate.clone());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return delegate.iterator();
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
36
src/main/java/jef/operations/CountOp.java
Normal file
36
src/main/java/jef/operations/CountOp.java
Normal file
@@ -0,0 +1,36 @@
|
||||
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,12 +16,11 @@ import jef.serializable.SerializablePredicate;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class FilterOp<T extends Serializable> implements Queryable<T>, Operation<T> {
|
||||
|
||||
private final Queryable<T> queryable;
|
||||
private final Predicate<? super T> predicate;
|
||||
private final SerializablePredicate<? super T> predicate;
|
||||
private final Expression predicateExpr;
|
||||
// private final Expression finalExpr;
|
||||
|
||||
@@ -55,6 +54,11 @@ public class FilterOp<T extends Serializable> implements Queryable<T>, Operation
|
||||
return new WhereExpression(new SelectExpression(List.of(DatabaseSelectAllExpression.INSTANCE), queryable.getExpression(), getTableAlias()), predicateExpr);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FilterOp<T> clone() {
|
||||
return new FilterOp<>(queryable, predicate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getExpression().toString();
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
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,6 +30,11 @@ public class LimitOp<T extends Serializable> implements Queryable<T> {
|
||||
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
|
||||
public String toString() {
|
||||
return getExpression().toString();
|
||||
|
||||
Reference in New Issue
Block a user