Skip to content

Commit

Permalink
Merge pull request #59 from oneteme/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
usfalami authored Sep 26, 2024
2 parents 6bb057c + 0e30883 commit 2406aac
Show file tree
Hide file tree
Showing 157 changed files with 6,381 additions and 3,472 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '11'
java-version: '17'
distribution: 'temurin'
- name: Build & Scan Project
env:
Expand All @@ -44,7 +44,7 @@ jobs:
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
java-version: '17'
distribution: 'temurin'
server-id: ossrh
server-username: MAVEN_USERNAME
Expand Down
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 DBExpression extends DBObject {
public interface AggregateFunction extends FunctionOperator {

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

This file was deleted.

20 changes: 20 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,20 @@
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
*
*/
@FunctionalInterface
interface ArgTypeRef extends Function<Object[], JDBCType> {

static ArgTypeRef firstArgJdbcType() {
return arr-> typeOf(requireAtLeastNArgs(1, arr, ArgTypeRef.class::getSimpleName)[0])
.orElse(null);
}
}
22 changes: 22 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,22 @@
package org.usf.jquery.core;

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

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

@Override
default void sql(SqlStringBuilder sb, QueryContext ctx, Object[] args) {
requireNArgs(2, args, ArithmeticException.class::getSimpleName);
sb.parenthesis(()->{
ctx.appendLiteral(sb, args[0]);
sb.append(id());
ctx.appendLiteral(sb, args[1]);
});
}
}
22 changes: 6 additions & 16 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,27 +87,17 @@ 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;
}

private static boolean isNumer(int type) {
switch (type) {
case BOOLEAN:
case BIT:
case TINYINT:
case SMALLINT:
case INTEGER:
case BIGINT:
case REAL:
case FLOAT:
case DOUBLE:
case NUMERIC:
case DECIMAL: return true;
default: return false;
}
return switch (type) {
case BOOLEAN, BIT, TINYINT, SMALLINT, INTEGER, BIGINT, REAL, FLOAT, DOUBLE, NUMERIC, DECIMAL: yield true;
default: yield false;
};
}

private static Object[] array(int size, String v) {
Expand Down
42 changes: 42 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,42 @@
package org.usf.jquery.core;

import static java.lang.String.format;
import static org.usf.jquery.core.SqlStringBuilder.SCOMA;
import static org.usf.jquery.core.Utils.joinAndDelemitArray;
import static org.usf.jquery.core.Utils.joinArray;

import org.usf.jquery.core.JavaType.Typed;

/**
*
* @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 badArgumentCountException(int count, int expect) {
return new BadArgumentException(format("bad argument count: [%d], expected: %d", count, expect));
}

public static BadArgumentException badArgumentTypeException(Object obj, JavaType[] types) {
var type = obj instanceof Typed t ? t.getType() : JDBCType.typeOf(obj);
return new BadArgumentException(format("bad argument type: %s[%s], expected: %s", obj, type, joinArray("|", types)));
}

public static BadArgumentException badArgumentsException(String type, String id, Object[] args, Exception e) {
return new BadArgumentException(format("bad %s arguments: ", type) + badArgumentsFormat(id, args), e);
}

public static String badArgumentsFormat(String id, Object... args) {
return id + joinAndDelemitArray(SCOMA, "([", "])", args);
}
}
14 changes: 5 additions & 9 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,13 @@
*
*/
@FunctionalInterface
public interface BasicComparator extends DBComparator {

String symbol();
public interface BasicComparator extends Comparator {

@Override
default String sql(QueryParameterBuilder builder, Object[] args) {
default void sql(SqlStringBuilder sb, QueryContext ctx, 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;
ctx.appendParameter(sb, args[0]);
sb.append(id());
ctx.appendParameter(sb, args[1]);
}
}
59 changes: 30 additions & 29 deletions src/main/java/org/usf/jquery/core/CaseColumn.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
package org.usf.jquery.core;

import static java.util.stream.Collectors.joining;
import static org.usf.jquery.core.QueryParameterBuilder.addWithValue;
import static org.usf.jquery.core.SqlStringBuilder.SPACE;
import static org.usf.jquery.core.Validation.requireAtLeastNArgs;

import java.util.Collection;
import java.util.LinkedList;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
*
* @author u$f
*
*/
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public final class CaseColumn implements DBColumn {

private final Collection<WhenExpression> expressions = new LinkedList<>();
private final WhenCase[] whenCases;

CaseColumn(WhenCase[] whenCases) {
this.whenCases = requireAtLeastNArgs(1, whenCases, CaseColumn.class::getSimpleName);
}

@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);
}
public void sql(SqlStringBuilder sb, QueryContext ctx) {
var sub = ctx.withValue(); //force literal parameter
sb.runForeach(whenCases, SPACE, o-> o.sql(sb, sub), "CASE ", " END");
}

public CaseColumn append(WhenExpression we) {
expressions.add(we);
return this;

@Override
public JDBCType getType() {
return Stream.of(whenCases)
.map(WhenCase::getType)
.filter(Objects::nonNull) // should have same type
.findAny().orElse(null);
}

@Override
public String toString() {
return sql(addWithValue());
public int columns(QueryBuilder builder, Consumer<? super DBColumn> groupKeys) {
return Nested.resolveColumn(this, builder, groupKeys, whenCases);
}

public static CaseColumn caseWhen(DBFilter filter, Object value){
return new CaseColumn()
.append(new WhenExpression(filter, value));

@Override
public void views(Consumer<DBView> cons) {
Nested.resolveViews(cons, whenCases);
}

@Override
public String toString() {
return DBObject.toSQL(this);
}
}
Loading

0 comments on commit 2406aac

Please sign in to comment.