Skip to content

Commit

Permalink
Merge pull request #48 from oneteme/release-4.1.1
Browse files Browse the repository at this point in the history
Release 4.1.1
  • Loading branch information
usfalami authored Aug 23, 2024
2 parents 6bb057c + b825c39 commit 0d6e0b3
Show file tree
Hide file tree
Showing 137 changed files with 5,093 additions and 2,810 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.oneteme</groupId>
<artifactId>jquery</artifactId>
<version>3.0.0</version>
<version>4.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>java-query</name>
<description>java-query</description>
Expand All @@ -26,7 +26,7 @@
</license>
</licenses>
<properties>
<java.version>11</java.version>
<java.version>17</java.version>
<junit.version>5.8.1</junit.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
Expand All @@ -39,13 +39,13 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<version>1.18.32</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
<version>1.7.36</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
*
*/
@FunctionalInterface
public interface DBOperation extends DBCallable, NestedSql {
public interface AggregateFunction extends FunctionOperator {

}
20 changes: 0 additions & 20 deletions src/main/java/org/usf/jquery/core/AggregationFunction.java

This file was deleted.

19 changes: 19 additions & 0 deletions src/main/java/org/usf/jquery/core/ArgTypeRef.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.usf.jquery.core;

import static org.usf.jquery.core.JDBCType.typeOf;
import static org.usf.jquery.core.Validation.requireAtLeastNArgs;

import java.util.function.Function;

/**
*
* @author u$f
*
*/
interface ArgTypeRef extends Function<Object[], JDBCType> {

static ArgTypeRef firstArgJdbcType() {
return arr-> typeOf(requireAtLeastNArgs(1, arr,
()-> "ArgTypeRef function")[0]).orElse(null); // not sure
}
}
18 changes: 18 additions & 0 deletions src/main/java/org/usf/jquery/core/ArithmeticOperator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.usf.jquery.core;

import static org.usf.jquery.core.Validation.requireNArgs;

/**
*
* @author u$f
*
*/
@FunctionalInterface
interface ArithmeticOperator extends Operator {

@Override
default String sql(QueryParameterBuilder builder, Object[] args) {
requireNArgs(2, args, ArithmeticOperator.class::getSimpleName);
return "(" + builder.appendLiteral(args[0]) + id() + builder.appendLiteral(args[1]) + ")";
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/usf/jquery/core/AsciiResultMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*/
@Slf4j
@RequiredArgsConstructor
public final class AsciiResultMapper implements ResultMapper<Void> {
public final class AsciiResultMapper implements ResultSetMapper<Void> {

private static final int MAX_LENGTH = 50;

Expand Down Expand Up @@ -87,7 +87,7 @@ public Void map(ResultSet rs) throws SQLException {
}
writer.writeLine(div);
} catch (IOException e) {
throw new RuntimeException("error while mapping results", e);
throw new MappingException("error writing results", e);
}
log.info("{} rows mapped in {} ms", rw, currentTimeMillis() - bg);
return null;
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/org/usf/jquery/core/BadArgumentException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.usf.jquery.core;

import static org.usf.jquery.core.Utils.joinArray;

/**
*
* @author u$f
*
*/
@SuppressWarnings("serial")
public final class BadArgumentException extends JQueryException {

public BadArgumentException(String message) {
super(message);
}

public BadArgumentException(String message, Throwable cause) {
super(message, cause);
}

public static BadArgumentException badArgumentTypeException(JavaType[] types, Object actual) {
String type = actual instanceof DBColumn c ? ":"+ c.getType() : "";
return new BadArgumentException(formatMessage("bad argument type", joinArray("|", types), actual + type));
}

public static BadArgumentException badArgumentCountException(int count, int actual) {
return new BadArgumentException(formatMessage("bad argument count", count, actual));
}

public static BadArgumentException badArgumentsException(String exp, String actual, Exception e) {
return new BadArgumentException(formatMessage("bad arguments", exp, actual), e);
}
}
10 changes: 2 additions & 8 deletions src/main/java/org/usf/jquery/core/BasicComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,11 @@
*
*/
@FunctionalInterface
public interface BasicComparator extends DBComparator {

String symbol();
public interface BasicComparator extends Comparator {

@Override
default String sql(QueryParameterBuilder builder, Object[] args) {
requireNArgs(2, args, BasicComparator.class::getSimpleName);
return builder.appendParameter(args[0]) + symbol() + builder.appendParameter(args[1]);
}

static BasicComparator basicComparator(final String name) {
return ()-> name;
return builder.appendParameter(args[0]) + id() + builder.appendParameter(args[1]);
}
}
29 changes: 17 additions & 12 deletions src/main/java/org/usf/jquery/core/CaseColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import static org.usf.jquery.core.QueryParameterBuilder.addWithValue;
import static org.usf.jquery.core.SqlStringBuilder.SPACE;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Objects;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,21 +17,25 @@
*
*/
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public final class CaseColumn implements DBColumn {
public final class CaseColumn implements DBColumn { // TD override isAggregation

private final Collection<WhenExpression> expressions = new LinkedList<>();
private final Collection<WhenExpression> expressions = new ArrayList<>();

@Override
public String sql(QueryParameterBuilder builder) {
builder.forceValue(true); //add filters as value
try {
return expressions.stream()
.map(o-> o.sql(builder))
.collect(joining(SPACE, "CASE ", " END"));
}
finally {
builder.forceValue(false);
}
var b = builder.withValue(); //force literal parameter
return expressions.stream() //empty !?
.map(o-> o.sql(b))
.collect(joining(SPACE, "CASE ", " END"));
}

@Override
public JDBCType getType() {
return expressions.stream()
.map(WhenExpression::getType)
.filter(Objects::nonNull) // should have same type
.findAny()
.orElse(null);
}

public CaseColumn append(WhenExpression we) {
Expand Down
32 changes: 14 additions & 18 deletions src/main/java/org/usf/jquery/core/CastFunction.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
package org.usf.jquery.core;

import static org.usf.jquery.core.Validation.requireNArgs;

import java.util.function.IntFunction;
import static org.usf.jquery.core.Validation.requireAtLeastNArgs;

/**
*
* @author u$f
*
*/
@FunctionalInterface
public interface CastFunction extends DBFunction {
public interface CastFunction extends FunctionOperator {

String asType();

@Override
default String name() {
default String id() {
return "CAST";
}

@Override
default String sql(QueryParameterBuilder builder, Object[] args, IntFunction<SQLType> indexedType) {
var n = "VARCHAR".equals(asType()) ? 2 : 1; //require length
requireNArgs(n, args, ()-> name() + "." + asType());
return new SqlStringBuilder(name())
.append("(").append(builder.appendLitteral(args[0], indexedType.apply(0))).append(" AS ").append(asType())
.appendIf(n == 2, ()-> "(" + builder.appendLitteral(args[1], indexedType.apply(1))+ ")")
.append(")")
.toString();
}

static CastFunction castFunction(String type) {
return ()-> type;
default String sql(QueryParameterBuilder builder, Object[] args) {
requireAtLeastNArgs(1, args, ()-> id() + "_AS_" + asType());
var sb = new SqlStringBuilder(id())
.append("(")
.append(builder.appendLiteral(args[0])).append(" AS ").append(asType());
if(args.length > 1) {
sb.append("(")
.append(builder.appendLiteralArray(args, 1))
.append(")");
}
return sb.append(")").toString();
}

}
22 changes: 22 additions & 0 deletions src/main/java/org/usf/jquery/core/Chainable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.usf.jquery.core;

import static org.usf.jquery.core.LogicalOperator.AND;
import static org.usf.jquery.core.LogicalOperator.OR;

/**
*
* @author u$f
*
*/
public interface Chainable<T extends Chainable<T>> {

T append(LogicalOperator op, T exp);

default T and(T exp) {
return append(AND, exp);
}

default T or(T exp) {
return append(OR, exp);
}
}
35 changes: 14 additions & 21 deletions src/main/java/org/usf/jquery/core/ColumnFilterGroup.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package org.usf.jquery.core;

import static java.util.Objects.nonNull;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static org.usf.jquery.core.QueryParameterBuilder.addWithValue;
import static org.usf.jquery.core.Utils.arrayJoin;

import java.util.Collection;
import java.util.LinkedList;
import java.util.stream.Stream;

import lombok.NonNull;

/**
*
* @author u$f
Expand All @@ -19,38 +16,34 @@
public final class ColumnFilterGroup implements DBFilter {

private final LogicalOperator operator;
private final Collection<DBFilter> filters;

ColumnFilterGroup(@NonNull LogicalOperator operator, DBFilter... filters) {//assert length > 1
private final DBFilter[] filters;
ColumnFilterGroup(LogicalOperator operator, DBFilter... filters) {
this.operator = operator;
this.filters = filters == null
? new LinkedList<>()
: Stream.of(filters).collect(toList());
this.filters = filters;
}

@Override
public String sql(QueryParameterBuilder builder) {
return "(" + filters.stream().map(o-> o.sql(builder)).collect(joining(operator.sql())) + ")";
return "(" + Stream.of(filters)
.map(o-> o.sql(builder))
.collect(joining(operator.sql())) + ")";
}

@Override
public boolean isAggregation() {
return filters.stream()
.anyMatch(NestedSql::isAggregation);
return nonNull(filters) && Stream.of(filters).anyMatch(DBFilter::isAggregation);
}

@Override
public DBFilter append(LogicalOperator op, DBFilter filter) {
if(operator == op) {
filters.add(filter);
return this;
}
return new ColumnFilterGroup(op, this, filter);
return operator == op
? new ColumnFilterGroup(op, arrayJoin(filters, filter))
: new ColumnFilterGroup(op, this, filter);
}

@Override
public String toString() {
return sql(addWithValue());
}

}
Loading

0 comments on commit 0d6e0b3

Please sign in to comment.