Skip to content

Commit 105d749

Browse files
committed
edit
1 parent 1539bf7 commit 105d749

11 files changed

+70
-44
lines changed

src/main/java/org/usf/jquery/core/CombinedOperator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface CombinedOperator extends Operator {
1111
OperationColumn args(Object... args);
1212

1313
@Override
14-
default OperationColumn args(JDBCType type, Object... args) {
14+
default OperationColumn operation(JDBCType type, Object... args) {
1515
var c = args(args);
1616
if(type == c.getType()) {
1717
return c;

src/main/java/org/usf/jquery/core/Comparator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ default ColumnSingleFilter filter(Object... args) {
2525
return new ColumnSingleFilter(args[0],
2626
expression(copyOfRange(args, 1, args.length))); // no type
2727
}
28+
29+
default boolean is(Class<? extends Comparator> type) {
30+
return type.isInstance(this);
31+
}
2832

2933
//basic comparator
3034

src/main/java/org/usf/jquery/core/OperationColumn.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public JDBCType getType() {
3535

3636
@Override
3737
public boolean isAggregation() {
38-
return operator instanceof AggregateFunction ||
38+
return operator.is(AggregateFunction.class) ||
3939
(!isOverFunction() && Stream.of(args).anyMatch(Nested::aggregation)); //can do better
4040
}
4141

@@ -44,7 +44,7 @@ public Stream<DBColumn> groupKeys() {
4444
if(isOverFunction()) {
4545
return ((Partition)args[1]).groupKeys();
4646
}
47-
return operator instanceof AggregateFunction || operator instanceof ConstantOperator
47+
return operator.is(AggregateFunction.class) || operator.is(ConstantOperator.class)
4848
? Stream.empty()
4949
: DBColumn.super.groupKeys();
5050
}

src/main/java/org/usf/jquery/core/Operator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ public interface Operator extends DBProcessor {
3131

3232
String id(); //nullable
3333

34-
default OperationColumn args(JDBCType type, Object... args) {
34+
default OperationColumn operation(JDBCType type, Object... args) {
3535
return new OperationColumn(this, args, type);
3636
}
3737

38+
default boolean is(Class<? extends Operator> type) {
39+
return type.isInstance(this);
40+
}
41+
3842
//Arithmetic operations
3943

4044
static TypedOperator plus() {

src/main/java/org/usf/jquery/core/ParameterSet.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,16 @@ public final class ParameterSet { //there is no Singleton implementation, dummy
2828
private final Parameter[] parameters;
2929

3030
public Object[] assertArguments(Object... args) {
31-
return assertArgumentsFrom(0, args);
32-
}
33-
34-
public Object[] assertArgumentsFrom(int idx, Object... args) { //partial assert
3531
var arr = isNull(args) ? new Object[0] : args;
36-
forEach(arr.length+idx, (p,i)-> {
37-
if(i>=idx && !p.accept(i-idx, arr)) {
38-
throw badArgumentTypeException(arr[i-idx], p.types(arr));
32+
eachParameter(arr.length, (p,i)-> {
33+
if(!p.accept(i, arr)) {
34+
throw badArgumentTypeException(arr[i], p.types(arr));
3935
}
4036
});
4137
return arr;
4238
}
4339

44-
public void forEach(int nArgs, ObjIntConsumer<Parameter> cons) {
40+
public void eachParameter(int nArgs, ObjIntConsumer<Parameter> cons) {
4541
if(nArgs < nReqArgs || (nArgs > parameters.length && !isVarags())) {
4642
throw badArgumentCountException(nArgs, nReqArgs);
4743
}

src/main/java/org/usf/jquery/core/QueryBuilder.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ public QueryView overView(DBView view, Supplier<QueryView> supp) {
6969
}
7070

7171
public QueryBuilder columns(@NonNull NamedColumn... columns) {
72-
for(var col : columns) {
73-
if(this.columns.stream().anyMatch(nc-> nc.getTag().equals(col.getTag()))) {
74-
throw resourceAlreadyExistsException(col.getTag());
72+
for(var col : columns) { //optional tag
73+
if(nonNull(col.getTag()) && this.columns.stream()
74+
.filter(c-> nonNull(c.getTag()))
75+
.anyMatch(nc-> nc.getTag().equals(col.getTag()))) {
76+
throw resourceAlreadyExistsException(col.getTag());
7577
}
7678
this.columns.add(col);
7779
}

src/main/java/org/usf/jquery/core/TypedComparator.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,33 @@
1111
*
1212
*/
1313
@Getter
14-
public final class TypedComparator {
14+
public final class TypedComparator implements Comparator {
1515

16-
private final Comparator comparator;
16+
private final Comparator comparator; // do not delegate
1717
private final ParameterSet parameterSet;
1818

1919
public TypedComparator(Comparator comparator, Parameter... parameters) {
2020
this.comparator = comparator;
2121
this.parameterSet = ofParameters(parameters);
2222
}
2323

24-
public ComparisonExpression expression(Object... right) {
25-
try {
26-
return comparator.expression(parameterSet.assertArgumentsFrom(1, right)); //no left
27-
} catch (BadArgumentException e) {
28-
throw badArgumentsException("comparator", comparator.id(), right, e);
29-
}
24+
@Override
25+
public String id() {
26+
return comparator.id();
3027
}
3128

32-
public ColumnSingleFilter filter(Object... args) {
29+
@Override
30+
public String sql(QueryVariables builder, Object[] args) {
3331
try {
34-
return comparator.filter(parameterSet.assertArguments(args));
32+
return comparator.sql(builder, parameterSet.assertArguments(args));
3533
} catch (BadArgumentException e) {
3634
throw badArgumentsException("comparator", comparator.id(), args, e);
3735
}
3836
}
39-
40-
public Comparator unwrap() {
41-
return comparator;
37+
38+
@Override
39+
public boolean is(Class<? extends Comparator> type) {
40+
return comparator.is(type);
4241
}
4342

4443
@Override

src/main/java/org/usf/jquery/core/TypedOperator.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
*
1212
*/
1313
@Getter
14-
public class TypedOperator {
14+
public class TypedOperator implements Operator {
1515

16-
private final Operator operator;
16+
private final Operator operator; // do not delegate
1717
private final ArgTypeRef typeFn;
1818
private final ParameterSet parameterSet;
1919

@@ -26,22 +26,37 @@ public TypedOperator(ArgTypeRef typeFn, Operator function, Parameter... paramete
2626
this.typeFn = typeFn;
2727
this.parameterSet = ofParameters(parameter);
2828
}
29-
30-
public OperationColumn operation(Object... args) {
29+
30+
@Override
31+
public String id() {
32+
return operator.id();
33+
}
34+
35+
@Override
36+
public String sql(QueryVariables builder, Object[] args) {
3137
try {
32-
args = parameterSet.assertArguments(args);
33-
return operator.args(typeFn.apply(args), args);
34-
} catch (BadArgumentException e) {
38+
return operator.sql(builder, parameterSet.assertArguments(args));
39+
}
40+
catch (BadArgumentException e) {
3541
throw badArgumentsException("operator", operator.id(), args, e);
3642
}
3743
}
44+
45+
@Override
46+
public boolean is(Class<? extends Operator> type) {
47+
return operator.is(type);
48+
}
3849

39-
public boolean isCountFunction() {
40-
return "COUNT".equals(operator.id());
50+
public OperationColumn operation(Object... args) {
51+
return Operator.super.operation(typeFn.apply(args), args);
4152
}
42-
53+
4354
public boolean isWindowFunction() {
44-
return operator instanceof WindowFunction;
55+
return operator.is(WindowFunction.class);
56+
}
57+
58+
public boolean isCountFunction() {
59+
return "COUNT".equals(operator.id());
4560
}
4661

4762
@Override

src/main/java/org/usf/jquery/core/Utils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static <T> String joinArray(String delemiter, T... args) {
4646
@SuppressWarnings("unchecked")
4747
public static <T> String joinAndDelemitArray(String delemiter, String before, String after, T... args) {
4848
return isNull(args)
49-
? null
49+
? before + after
5050
: joinAndDelemit(delemiter, before, after, Stream.of(args));
5151
}
5252

src/main/java/org/usf/jquery/web/RequestEntryChain.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import static java.util.Optional.empty;
1212
import static java.util.Optional.ofNullable;
1313
import static java.util.stream.Collectors.joining;
14+
import static org.usf.jquery.core.BadArgumentException.badArgumentsFormat;
1415
import static org.usf.jquery.core.Comparator.eq;
1516
import static org.usf.jquery.core.Comparator.in;
1617
import static org.usf.jquery.core.Comparator.lookupComparator;
@@ -44,6 +45,7 @@
4445
import java.util.function.IntFunction;
4546
import java.util.stream.Stream;
4647

48+
import org.usf.jquery.core.BadArgumentException;
4749
import org.usf.jquery.core.ComparisonExpression;
4850
import org.usf.jquery.core.DBColumn;
4951
import org.usf.jquery.core.DBFilter;
@@ -250,7 +252,7 @@ else if(nonNull(rc.col)) {
250252
if(rc.entry.isLast()) { // no criteria, no comparator
251253
if(!isEmpty(values)) {
252254
var fn = values.size() == 1 ? eq() : in(); //non empty
253-
var e = new RequestEntryChain(fn.unwrap().id(), false, null, values, null);
255+
var e = new RequestEntryChain(fn.id(), false, null, values, null);
254256
return fn.filter(e.toArgs(vd, ctx, rc.col, fn.getParameterSet())); //no chain
255257
}
256258
throw badEntrySyntaxException(this.toString(), FILTER);
@@ -429,7 +431,7 @@ private Object[] toArgs(ViewDecorator td, QueryContext ctx, DBObject col, Parame
429431
arr[0] = col;
430432
}
431433
try {
432-
ps.forEach(arr.length, (p,i)-> {
434+
ps.eachParameter(arr.length, (p,i)-> {
433435
if(i>=inc) { //arg0 already parsed
434436
var e = args.get(i-inc);
435437
arr[i] = isNull(e.value) || e.text
@@ -439,8 +441,8 @@ private Object[] toArgs(ViewDecorator td, QueryContext ctx, DBObject col, Parame
439441
});
440442
}
441443
catch (Exception e) {
442-
throw new EntrySyntaxException(format("bad entry arguments : %s[(%s)]",
443-
value, isNull(args) ? "" : joinArray(", ", args.toArray())), e);
444+
throw new EntrySyntaxException("bad entry arguments: " +
445+
badArgumentsFormat(value, nonNull(args) ? args.toArray() : null), e);
444446
}
445447
return arr;
446448
}

src/main/java/org/usf/jquery/web/RequestParameterResolver.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public QueryBuilder requestQuery(@NonNull RequestQueryParam ant, @NonNull Map<St
5555
}
5656
throw new ResourceAccessException("non-aggregate query");
5757
}
58+
catch (Exception e) {
59+
e.printStackTrace();
60+
throw e;
61+
}
5862
finally {
5963
releaseContext();
6064
}

0 commit comments

Comments
 (0)