Skip to content

Commit

Permalink
Merge branch 'master' into rxsocks
Browse files Browse the repository at this point in the history
  • Loading branch information
RockyLOMO committed Nov 2, 2024
2 parents b0f431f + 8679bdd commit d01a73d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
4 changes: 2 additions & 2 deletions rxlib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
<caffeine.version>2.9.3</caffeine.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
<commons-collections4.version>4.4</commons-collections4.version>
<commons-io.version>2.11.0</commons-io.version>
<byte-buddy.version>1.14.19</byte-buddy.version>
<commons-io.version>2.14.0</commons-io.version>
<byte-buddy.version>1.15.7</byte-buddy.version>
<zip4j.version>2.11.3</zip4j.version>
<h2.version>2.2.224</h2.version>
<netty.version>4.1.111.Final</netty.version>
Expand Down
28 changes: 21 additions & 7 deletions rxlib/src/main/java/org/rx/io/EntityDatabaseImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.rx.core.*;
import org.rx.exception.InvalidException;
import org.rx.exception.TraceHandler;
import org.rx.third.guava.CaseFormat;
import org.rx.util.function.BiAction;
import org.rx.util.function.BiFunc;

Expand Down Expand Up @@ -47,7 +46,7 @@ static class SqlMeta {
final Map.Entry<String, Tuple<Field, DbColumn>> primaryKey;
@Getter
final Map<String, Tuple<Field, DbColumn>> columns;
final Map<String, Tuple<String, Tuple<Field, DbColumn>>> upperColumns = new HashMap<>();
final Map<String, Tuple<String, Tuple<Field, DbColumn>>> jdbcColumns = new HashMap<>();
final Linq<Map.Entry<String, Tuple<Field, DbColumn>>> insertView;
final Linq<Map.Entry<String, Tuple<Field, DbColumn>>> secondaryView;
final String insertSql;
Expand All @@ -60,7 +59,7 @@ public SqlMeta(String primaryKey, Map<String, Tuple<Field, DbColumn>> columns
this.primaryKey = new AbstractMap.SimpleEntry<>(primaryKey, columns.get(primaryKey));
this.columns = columns;
for (Map.Entry<String, Tuple<Field, DbColumn>> entry : columns.entrySet()) {
upperColumns.put(entry.getKey().toUpperCase(), Tuple.of(entry.getKey(), entry.getValue()));
jdbcColumns.put(entry.getKey().toUpperCase(), Tuple.of(entry.getKey(), entry.getValue()));
}
insertView = Linq.from(columns.entrySet()).where(p -> p.getValue().right == null || !p.getValue().right.autoIncrement());
secondaryView = Linq.from(columns.entrySet()).where(p -> !eq(p.getKey(), getPrimaryKey().getKey()));
Expand All @@ -70,6 +69,19 @@ public SqlMeta(String primaryKey, Map<String, Tuple<Field, DbColumn>> columns
this.deleteSql = deleteSql;
this.selectSql = selectSql;
}

public Tuple<String, Tuple<Field, DbColumn>> getJdbcColumn(String col) {
Tuple<String, Tuple<Field, DbColumn>> bi = jdbcColumns.get(col);
if (bi == null) {
for (Map.Entry<String, Tuple<String, Tuple<Field, DbColumn>>> entry : jdbcColumns.entrySet()) {
if (entry.getKey().replace("_", "").equals(col)) {
jdbcColumns.put(col, bi = entry.getValue());
break;
}
}
}
return bi;
}
}

static final String SQL_CREATE = "CREATE TABLE IF NOT EXISTS $TABLE\n" +
Expand Down Expand Up @@ -733,7 +745,7 @@ public <T> DataTable executeQuery(String sql, Class<T> entityType) {
SqlMeta meta = getMeta(entityType);
for (int i = 0; i < dt.getColumns().size(); i++) {
DataColumn<?> column = dt.getColumn(i);
Tuple<String, Tuple<Field, DbColumn>> bi = meta.upperColumns.get(column.getColumnName());
Tuple<String, Tuple<Field, DbColumn>> bi = meta.getJdbcColumn(column.getColumnName());
if (bi == null) {
continue;
}
Expand Down Expand Up @@ -789,9 +801,11 @@ <T> List<T> executeQuery(String sql, List<Object> params, Class<T> entityType) {
T t = entityType.newInstance();
for (int i = 1; i <= metaData.getColumnCount(); i++) {
//metaData.getColumnName is capitalized
Tuple<Field, DbColumn> bi = meta.upperColumns.get(metaData.getColumnName(i)).right;
if (bi == null) {
throw new InvalidException("Mapping {} not found", metaData.getColumnName(i));
String jdbcCol = metaData.getColumnName(i);
Tuple<String, Tuple<Field, DbColumn>> pbi = meta.getJdbcColumn(jdbcCol);
Tuple<Field, DbColumn> bi;
if (pbi == null || (bi = pbi.right) == null) {
throw new InvalidException("Mapping {} not found", jdbcCol);
}
Class<?> type = bi.left.getType();
bi.left.set(t, convertCell(type, rs.getObject(i)));
Expand Down
9 changes: 8 additions & 1 deletion rxlib/src/test/java/org/rx/io/TestIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.rx.annotation.DbColumn;
import org.rx.bean.*;
import org.rx.core.*;
import org.rx.exception.TraceHandler;
import org.rx.net.http.HttpClient;
import org.rx.net.socks.SocksUser;
import org.rx.test.GirlBean;
Expand Down Expand Up @@ -181,7 +182,7 @@ public void h2Db() {
System.out.println(db.executeQuery("select * from `person` limit 2", PersonBean.class));
System.out.println(db.count(queryLambda));
List<PersonBean> list = db.findBy(queryLambda);
assert list.isEmpty();
assert !list.isEmpty();
ULID pk = entity.getId();
assert db.existsById(PersonBean.class, pk);
PersonBean byId = db.findById(PersonBean.class, pk);
Expand All @@ -197,6 +198,12 @@ public void h2Db() {
System.out.println(q.toString(params));
System.out.println(toJsonString(params));
System.out.println(q.orderByRand());


List<Object> paramsX = new ArrayList<>();
String sql = new EntityQueryLambda<>(TraceHandler.ExceptionEntity.class)
.lt(TraceHandler.ExceptionEntity::getModifyTime, DateTime.now()).toString(paramsX);
System.out.println(sql + "(" + paramsX + ")");
}
//endregion

Expand Down

0 comments on commit d01a73d

Please sign in to comment.