Skip to content

Commit

Permalink
Merge pull request #6 from erupts/dev
Browse files Browse the repository at this point in the history
0.0.5
  • Loading branch information
erupts authored May 26, 2024
2 parents 1f1ad9c + 645a934 commit 12d48b3
Show file tree
Hide file tree
Showing 18 changed files with 266 additions and 199 deletions.
15 changes: 7 additions & 8 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<dependency>
<groupId>xyz.erupt</groupId>
<artifactId>linq.j</artifactId>
<version>0.0.4</version>
<version>0.0.5</version>
</dependency>
```

Expand Down Expand Up @@ -83,11 +83,11 @@ public class ObjectQuery{
*/
public void select(){
// select *
Linq.from(source).select(Columns.all(TestSource.class));
Linq.from(source).select(TestSource.class);
// select a, b, c
Linq.from(source)
.select(TestSource::getName, TestSource::getDate, TestSource::getTags)
.select(Columns.of(TestSource::getTags, "tag2")) // alias
.select(TestSource::getTags, "tag2") // alias
.select(Columns.ofx(TestSource::getId, id -> id + "xxx")); // value convert
// select count(*), sum(id), max(id)
Linq.from(source)
Expand All @@ -102,11 +102,10 @@ public class ObjectQuery{
*/
public void join(){
// left join
Linq.from(source).leftJoin(target, TestSourceExt::getId, TestSource::getId).select(
Columns.all(TestSource.class),
Columns.of(TestSourceExt::getName),
Columns.of(TestSourceExt2::getValue)
);
Linq.from(source).leftJoin(target, TestSourceExt::getId, TestSource::getId)
.select(TestSource.class)
.select(TestSourceExt::getName)
.select(TestSourceExt2::getValue);
// right join
Linq.from(source).rightJoin(target, TestSourceExt::getId, TestSource::getId);
// inner join
Expand Down
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ It has zero external dependencies and is only 50kb in size
<dependency>
<groupId>xyz.erupt</groupId>
<artifactId>linq.j</artifactId>
<version>0.0.4</version>
<version>0.0.5</version>
</dependency>
```

Expand Down Expand Up @@ -83,11 +83,11 @@ public class ObjectQuery{
*/
public void select(){
// select *
Linq.from(source).select(Columns.all(TestSource.class));
Linq.from(source).select(TestSource.class);
// select a, b, c
Linq.from(source)
.select(TestSource::getName, TestSource::getDate, TestSource::getTags)
.select(Columns.of(TestSource::getTags, "tag2")) // alias
.select(TestSource::getTags, "tag2") // alias
.select(Columns.ofx(TestSource::getId, id -> id + "xxx")); // value convert
// select count(*), sum(id), max(id)
Linq.from(source)
Expand All @@ -102,11 +102,10 @@ public class ObjectQuery{
*/
public void join(){
// left join
Linq.from(source).leftJoin(target, TestSourceExt::getId, TestSource::getId).select(
Columns.all(TestSource.class),
Columns.of(TestSourceExt::getName),
Columns.of(TestSourceExt2::getValue)
);
Linq.from(source).leftJoin(target, TestSourceExt::getId, TestSource::getId)
.select(TestSource.class)
.select(TestSourceExt::getName)
.select(TestSourceExt2::getValue);
// right join
Linq.from(source).rightJoin(target, TestSourceExt::getId, TestSource::getId);
// inner join
Expand Down
5 changes: 2 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>xyz.erupt</groupId>
<artifactId>linq.j</artifactId>
<version>0.0.4</version>
<version>0.0.5</version>
<name>Linq.j</name>

<description>Linq.J in Java , Lambda Language Integrated Query library</description>
Expand Down Expand Up @@ -89,7 +89,7 @@
<!-- java8版本导致javadoc打包编译失败时候,添加-->
<properties>
<javadoc.opts>-Xdoclint:none</javadoc.opts>
<!-- <javadocExecutable>${java.home}/../bin/javadoc</javadocExecutable>-->
<javadocExecutable>${java.home}/../bin/javadoc</javadocExecutable>
</properties>
</profile>
<profile>
Expand Down Expand Up @@ -167,5 +167,4 @@
</profile>
</profiles>


</project>
106 changes: 88 additions & 18 deletions src/main/java/xyz/erupt/linq/Linq.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
import xyz.erupt.linq.engine.Engine;
import xyz.erupt.linq.engine.EruptEngine;
import xyz.erupt.linq.grammar.*;
import xyz.erupt.linq.lambda.LambdaSee;
import xyz.erupt.linq.lambda.SFunction;
import xyz.erupt.linq.lambda.Th;
import xyz.erupt.linq.schema.*;
import xyz.erupt.linq.util.Columns;
import xyz.erupt.linq.util.ReflectField;
import xyz.erupt.linq.util.VirtualColumn;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand All @@ -24,7 +29,7 @@ public Linq() {

private final Dql dql = new Dql();

public static Linq from(Collection<?> data) {
public static Linq from(List<?> data) {
Linq linq = new Linq();
linq.dql.setFrom(data);
return linq;
Expand All @@ -36,40 +41,40 @@ public static <T> Linq from(T... data) {
}

public static Linq from(Boolean... data) {
return Linq.from(Arrays.stream(data).collect(Collectors.toList())).select(Columns.of(Th::is));
return Linq.from(Arrays.stream(data).collect(Collectors.toList())).select(Th::is);
}

public static Linq from(Byte... data) {
return Linq.from(Arrays.stream(data).collect(Collectors.toList())).select(Columns.of(Th::is));
return Linq.from(Arrays.stream(data).collect(Collectors.toList())).select(Th::is);
}

public static Linq from(Character... table) {
return Linq.from(Arrays.stream(table).collect(Collectors.toList())).select(Columns.of(Th::is));
return Linq.from(Arrays.stream(table).collect(Collectors.toList())).select(Th::is);
}

public static Linq from(String... data) {
return Linq.from(Arrays.stream(data).collect(Collectors.toList())).select(Columns.of(Th::is));
return Linq.from(Arrays.stream(data).collect(Collectors.toList())).select(Th::is);
}

public static Linq from(Short... data) {
return Linq.from(Arrays.stream(data).collect(Collectors.toList())).select(Columns.of(Th::is));
return Linq.from(Arrays.stream(data).collect(Collectors.toList())).select(Th::is);
}

public static Linq from(Integer... data) {
return Linq.from(Arrays.stream(data).collect(Collectors.toList())).select(Columns.of(Th::is));
return Linq.from(Arrays.stream(data).collect(Collectors.toList())).select(Th::is);
}

public static Linq from(Long... data) {
return Linq.from(Arrays.stream(data).collect(Collectors.toList())).select(Columns.of(Th::is));
return Linq.from(Arrays.stream(data).collect(Collectors.toList())).select(Th::is);
}

public static Linq from(Float... data) {
return Linq.from(Arrays.stream(data).collect(Collectors.toList())).select(Columns.of(Th::is));
return Linq.from(Arrays.stream(data).collect(Collectors.toList())).select(Th::is);
}


public static Linq from(Double... data) {
return Linq.from(Arrays.stream(data).collect(Collectors.toList())).select(Columns.of(Th::is));
return Linq.from(Arrays.stream(data).collect(Collectors.toList())).select(Th::is);
}

@Override
Expand All @@ -80,7 +85,36 @@ public Linq distinct() {

@Override
public Linq select(Column... columns) {
this.dql.getColumns().addAll(Columns.columnsUnfold(columns));
for (Column column : columns) {
this.dql.getColumns().add(column);
}
return this;
}

@Override
public <T> Linq select(Class<T> table) {
List<Column> columns = new ArrayList<>();
for (Field field : ReflectField.getFields(table)) {
columns.add(new Column(table, field.getName(), field.getName()));
}
this.dql.getColumns().addAll(columns);
return this;
}

@SafeVarargs
@Override
public final <T> Linq select(SFunction<T, ?>... columns) {
for (SFunction<T, ?> column : columns) {
this.dql.getColumns().add(Columns.of(column));
}
return this;
}

@Override
public <T, F> Linq select(SFunction<T, F> column, BiFunction<Row, F, Object> convert) {
Column col = Columns.of(column);
col.setRowConvert(row -> convert.apply(row, row.get(column)));
this.dql.getColumns().add(col);
return this;
}

Expand All @@ -99,22 +133,51 @@ public final <T> Linq selectExclude(SFunction<T, ?>... columns) {
}

@Override
@SafeVarargs
public final <T> Linq select(SFunction<T, ?>... columns) {
for (SFunction<T, ?> col : columns) {
this.dql.getColumns().add(Columns.of(col));
}
public <T> Linq selectAs(SFunction<T, ?> column, String alias) {
this.dql.getColumns().add(Columns.of(column, alias));
return this;
}

@Override
public <T, A> Linq selectAs(SFunction<T, ?> column, SFunction<A, ?> alias) {
this.dql.getColumns().add(Columns.of(column, LambdaSee.field(alias)));
return this;
}

@Override
public <T, F> Linq selectAs(SFunction<T, F> column, BiFunction<Row, F, Object> convert, String alias) {
Column col = Columns.of(column, alias);
col.setRowConvert(row -> convert.apply(row, row.get(column)));
this.dql.getColumns().add(col);
return this;
}

@Override
public <T, A, F> Linq selectAs(SFunction<T, F> column, BiFunction<Row, F, Object> convert, SFunction<A, ?> alias) {
return selectAs(column, convert, LambdaSee.field(alias));
}

@Override
public Linq selectRowAs(Function<Row, Object> convert, String alias) {
Column column = Columns.of(VirtualColumn::col, alias);
column.setRowConvert(convert);
this.dql.getColumns().add(column);
return this;
}

@Override
public <A> Linq selectRowAs(Function<Row, Object> convert, SFunction<A, ?> alias) {
return selectRowAs(convert, LambdaSee.field(alias));
}

@Override
public <T> Linq join(JoinSchema<T> joinSchema) {
this.dql.getJoinSchemas().add(joinSchema);
return this;
}

@Override
public <T, S> Linq join(JoinMethod joinMethod, Collection<T> target, SFunction<T, Object> onL, SFunction<S, Object> onR) {
public <T, S> Linq join(JoinMethod joinMethod, List<T> target, SFunction<T, Object> onL, SFunction<S, Object> onR) {
this.dql.getJoinSchemas().add(new JoinSchema<>(joinMethod, target, onL, onR));
return this;
}
Expand Down Expand Up @@ -149,6 +212,13 @@ public final <T> Linq groupBy(SFunction<T, ?>... columns) {
return groupBy(cols);
}

@Override
public <T, F> Linq groupBy(SFunction<T, F> column, BiFunction<Row, F, Object> convert) {
Column col = Columns.of(column);
col.setRowConvert(row -> convert.apply(row, row.get(column)));
return groupBy(col);
}

@Override
public Linq having(Function<Row, Boolean> condition) {
this.dql.getHaving().add(condition);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/xyz/erupt/linq/engine/EruptEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import xyz.erupt.linq.consts.OrderByDirection;
import xyz.erupt.linq.exception.LinqException;
import xyz.erupt.linq.schema.*;
import xyz.erupt.linq.util.RowUtil;
import xyz.erupt.linq.util.Columns;
import xyz.erupt.linq.util.RowUtil;

import java.util.*;
import java.util.function.Function;
Expand All @@ -15,7 +15,7 @@ public class EruptEngine extends Engine {

@Override
public List<Row> query(Dql dql) {
List<Row> dataset = RowUtil.listObjectToRow(dql.getFrom());
List<Row> dataset = RowUtil.listToTable(dql.getFrom());
// join process
if (!dql.getJoinSchemas().isEmpty()) {
this.join(dql, dataset);
Expand Down Expand Up @@ -87,7 +87,7 @@ public void join(Dql dql, List<Row> dataset) {
Column lon = Columns.of(joinSchema.getLon());
Column ron = Columns.of(joinSchema.getRon());
if (joinSchema.getJoinExchange() == JoinExchange.HASH) {
List<Row> targetData = RowUtil.listObjectToRow(joinSchema.getTarget());
List<Row> targetData = RowUtil.listToTable(joinSchema.getTarget());
switch (joinSchema.getJoinMethod()) {
case LEFT:
this.crossHashJoin(dataset, ron, targetData, lon);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/xyz/erupt/linq/grammar/GroupBy.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import xyz.erupt.linq.lambda.SFunction;
import xyz.erupt.linq.schema.Column;
import xyz.erupt.linq.schema.Row;
import xyz.erupt.linq.util.Columns;

import java.util.function.BiFunction;
import java.util.function.Function;

public interface GroupBy {
Expand All @@ -14,8 +14,9 @@ public interface GroupBy {

<T> Linq groupBy(SFunction<T, ?>... columns);

Linq having(Function<Row, Boolean> condition);
<T, F> Linq groupBy(SFunction<T, F> column, BiFunction<Row, F, Object> convert);

Linq having(Function<Row, Boolean> condition);

default <R, S> Linq having(SFunction<R, S> fun, Function<S, Boolean> condition) {
return having(row -> condition.apply(row.get(fun)));
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/xyz/erupt/linq/grammar/Join.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,34 @@
import xyz.erupt.linq.schema.JoinSchema;
import xyz.erupt.linq.schema.Row;

import java.util.Collection;
import java.util.List;
import java.util.function.BiFunction;

public interface Join {

<T> Linq join(JoinSchema<T> joinSchema);


<T, S> Linq join(JoinMethod joinMethod, Collection<T> target, SFunction<T, Object> onL, SFunction<S, Object> onR);
<T, S> Linq join(JoinMethod joinMethod, List<T> target, SFunction<T, Object> onL, SFunction<S, Object> onR);


default <T> Linq join(JoinMethod joinMethod, Collection<T> target, BiFunction<T, Row, Boolean> on) {
default <T> Linq join(JoinMethod joinMethod, List<T> target, BiFunction<T, Row, Boolean> on) {
return join(new JoinSchema<>(joinMethod, target, on));
}

default <L, R> Linq innerJoin(Collection<L> t, SFunction<L, Object> lon, SFunction<R, Object> ron) {
default <L, R> Linq innerJoin(List<L> t, SFunction<L, Object> lon, SFunction<R, Object> ron) {
return this.join(JoinMethod.INNER, t, lon, ron);
}

default <L, R> Linq leftJoin(Collection<L> t, SFunction<L, Object> lon, SFunction<R, Object> ron) {
default <L, R> Linq leftJoin(List<L> t, SFunction<L, Object> lon, SFunction<R, Object> ron) {
return this.join(JoinMethod.LEFT, t, lon, ron);
}

default <L, R> Linq rightJoin(Collection<L> t, SFunction<L, Object> lon, SFunction<R, Object> ron) {
default <L, R> Linq rightJoin(List<L> t, SFunction<L, Object> lon, SFunction<R, Object> ron) {
return this.join(JoinMethod.RIGHT, t, lon, ron);
}

default <L, R> Linq fullJoin(Collection<L> t, SFunction<L, Object> lon, SFunction<R, Object> ron) {
default <L, R> Linq fullJoin(List<L> t, SFunction<L, Object> lon, SFunction<R, Object> ron) {
return this.join(JoinMethod.FULL, t, lon, ron);
}

Expand Down
Loading

0 comments on commit 12d48b3

Please sign in to comment.