Skip to content

Commit

Permalink
Merge pull request #74 from yuu-nkjm/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
yuu-nkjm authored Jul 4, 2023
2 parents 6a4a41f + da7bf38 commit 793240a
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 24 deletions.
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<artifactId>sorm4j-parent</artifactId>
<name>sorm4j-parent</name>
<description>Simple micro Object-Relation Mapper for Java</description>
<version>1.4.15</version>
<version>1.4.16</version>
<packaging>pom</packaging>
<url>https://github.com/yuu-nkjm/sorm4j</url>
<licenses>
Expand All @@ -29,7 +29,6 @@

<properties>
<sorm4j-version>${project.version}</sorm4j-version>
<xstream-version-in-plugin>1.4.15</xstream-version-in-plugin>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand Down
8 changes: 4 additions & 4 deletions sorm4j-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.nkjmlab</groupId>
<artifactId>sorm4j-example</artifactId>
<version>1.4.13</version>
<version>1.4.16</version>
<description>Example of Sorm4j</description>

<properties>
<sorm4j-version>1.4.15</sorm4j-version>
<sorm4j-version>1.4.16</sorm4j-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<additionalparam>-Xdoclint:none</additionalparam>
<downloadSources>true</downloadSources>
Expand All @@ -24,13 +24,13 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<version>2.1.214</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.1</version>
<version>2.20.0</version>
</dependency>
</dependencies>
<build>
Expand Down
2 changes: 1 addition & 1 deletion sorm4j-jmh/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<name>Sorm4j benchmark</name>

<properties>
<sorm4j-version>1.4.15</sorm4j-version>
<sorm4j-version>1.4.16</sorm4j-version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

Expand Down
2 changes: 1 addition & 1 deletion sorm4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.nkjmlab</groupId>
<artifactId>sorm4j-parent</artifactId>
<version>1.4.15</version>
<version>1.4.16</version>
</parent>
<properties>
<javassist-version>3.27.0-GA</javassist-version>
Expand Down
7 changes: 7 additions & 0 deletions sorm4j/src/main/java/org/nkjmlab/sorm4j/Orm.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ public interface Orm {
*/
<T> int[] deleteIn(String tableName, @SuppressWarnings("unchecked") T... objects);

@Experimental
boolean execute(ParameterizedSql sql);

@Experimental
boolean execute(String sql, Object... parameters);


/**
* Executes the query with the given PreparedStatement and applies the given
* {@link ResultSetTraverser}. If you want to set parameters to a PreparedStatement object by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,19 @@ private final <T, R> R execSqlIfParameterExists(T[] objects,
return sqlFunction.apply(mapping);
}

@Override
public boolean execute(ParameterizedSql sql) {
return execute(sql.getSql(), sql.getParameters());

}

@Override
public boolean execute(String sql, Object... parameters) {
return executeAndClose(getLoggerContext(), getJdbcConnection(), getPreparedStatementSupplier(),
getSqlParametersSetter(), sql, parameters);
}


@Override
public <T> T executeQuery(FunctionHandler<Connection, PreparedStatement> statementSupplier,
ResultSetTraverser<T> traverser) {
Expand Down Expand Up @@ -933,6 +946,18 @@ private static <R> R executeQueryAndClose(Connection connection,
}
}

private static <R> boolean executeAndClose(LoggerContext loggerContext, Connection connection,
PreparedStatementSupplier statementSupplier, SqlParametersSetter sqlParametersSetter,
String sql, Object[] parameters) {
try (PreparedStatement stmt = statementSupplier.prepareStatement(connection, sql)) {
sqlParametersSetter.setParameters(stmt, parameters);
boolean ret = stmt.execute();
return ret;
} catch (SQLException e) {
throw Try.rethrow(e);
}
}

private static <R> R executeQueryAndClose(LoggerContext loggerContext, Connection connection,
PreparedStatementSupplier statementSupplier, SqlParametersSetter sqlParametersSetter,
String sql, Object[] parameters, ResultSetTraverser<R> resultSetTraverser) {
Expand Down
10 changes: 10 additions & 0 deletions sorm4j/src/main/java/org/nkjmlab/sorm4j/internal/SormImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,16 @@ public TableSql getTableSql(String tableName) {
return applyAndClose(conn -> conn.getTableSql(tableName));
}

@Override
public boolean execute(ParameterizedSql sql) {
return applyAndClose(conn -> conn.execute(sql));
}

@Override
public boolean execute(String sql, Object... parameters) {
return applyAndClose(conn -> conn.execute(sql, parameters));
}

@Override
public <T> T executeQuery(FunctionHandler<Connection, PreparedStatement> statementSupplier,
ResultSetTraverser<T> traverser) {
Expand Down
17 changes: 8 additions & 9 deletions sorm4j/src/main/java/org/nkjmlab/sorm4j/result/BasicRowMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import org.nkjmlab.sorm4j.internal.util.ArrayUtils;
import org.nkjmlab.sorm4j.internal.util.StringCache;

public final class BasicRowMap implements RowMap {
public class BasicRowMap implements RowMap {

private final LinkedHashMap<String, Object> map;

Expand All @@ -28,12 +27,12 @@ public BasicRowMap(int initialCapacity, float loadFactor) {
}

public BasicRowMap(Map<String, Object> map) {
this.map = map.entrySet().stream().collect(Collectors.toMap(en -> toKey(en.getKey()),
en -> en.getValue(), (v1, v2) -> v1, LinkedHashMap::new));
this.map = List.copyOf(map.entrySet()).stream().collect(LinkedHashMap::new,
(m, v) -> m.put(toKey(v.getKey()), v.getValue()), LinkedHashMap::putAll);
}

private static String toKey(Object key) {
return StringCache.toCanonicalCase(key.toString());
private static String toKey(String key) {
return RowMap.toKey(key);
}

@Override
Expand All @@ -48,7 +47,7 @@ public boolean isEmpty() {

@Override
public boolean containsKey(Object key) {
return map.containsKey(toKey(key));
return map.containsKey(toKey(key.toString()));
}

@Override
Expand All @@ -58,7 +57,7 @@ public boolean containsValue(Object value) {

@Override
public Object get(Object key) {
return map.get(toKey(key));
return map.get(toKey(key.toString()));
}

@Override
Expand All @@ -68,7 +67,7 @@ public Object put(String key, Object value) {

@Override
public Object remove(Object key) {
return map.remove(toKey(key));
return map.remove(toKey(key.toString()));
}

@Override
Expand Down
12 changes: 12 additions & 0 deletions sorm4j/src/main/java/org/nkjmlab/sorm4j/result/RowMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.time.LocalTime;
import java.util.List;
import java.util.Map;
import org.nkjmlab.sorm4j.annotation.Experimental;
import org.nkjmlab.sorm4j.internal.util.StringCache;

/**
* A instance represents a row in a table. This interface extends {@link Map<String, Object>}. The
Expand Down Expand Up @@ -51,6 +53,16 @@ static RowMap of(String k1, Object v1, String k2, Object v2, String k3, Object v
return create(Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5));
}

/**
* Key to canonical key
*
* @param key
* @return
*/
@Experimental
static String toKey(String key) {
return StringCache.toCanonicalCase(key);
}

<T> T[] getArray(String key, Class<T> componentType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.nkjmlab.sorm4j.internal.util.ParameterizedStringFormatter;
import org.nkjmlab.sorm4j.internal.util.StringCache;
import org.nkjmlab.sorm4j.util.table_def.TableDefinition;
import org.nkjmlab.sorm4j.util.table_def.annotation.AutoIncrement;

@Experimental
public class H2CsvReadSql {
Expand Down Expand Up @@ -87,7 +86,8 @@ public H2CsvReadSql build() {
int index = selectedColumns.indexOf(en.getKey());
if (index == -1) {
Object[] params = {en.getKey(), columns};
throw new IllegalStateException(ParameterizedStringFormatter.LENGTH_256.format("{} is not found in Columns {}", params));
throw new IllegalStateException(ParameterizedStringFormatter.LENGTH_256
.format("{} is not found in Columns {}", params));
}
selectedColumns.set(index, en.getValue());
});
Expand Down Expand Up @@ -164,7 +164,7 @@ public Builder setOrmRecordClass(Class<?> ormRecordClass) {
.filter(f -> !java.lang.reflect.Modifier.isStatic(f.getModifiers()))
.toArray(Field[]::new);

List<Field> autoGeneratedColumn = new ArrayList<>();
List<Field> csvSkipColumns = new ArrayList<>();

for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
Expand All @@ -180,14 +180,16 @@ public Builder setOrmRecordClass(Class<?> ormRecordClass) {
if (ann instanceof CsvColumn) {
mapCsvColumnToTableColumn(((CsvColumn) ann).value(),
StringCache.toUpperSnakeCase(field.getName()));
} else if (ann instanceof AutoIncrement) {
autoGeneratedColumn.add(field);
} else if (ann instanceof SkipCsvRead) {
csvSkipColumns.add(field);
}

}
}
setTableColumns(Stream.of(fields).filter(f -> !autoGeneratedColumn.contains(f))
.map(f -> StringCache.toUpperSnakeCase(f.getName())).toArray(String[]::new));
setTableColumns(Stream.of(fields).map(f -> {
return csvSkipColumns.contains(f) ? "null as " + StringCache.toUpperSnakeCase(f.getName())
: StringCache.toUpperSnakeCase(f.getName());
}).toArray(String[]::new));

return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.nkjmlab.sorm4j.util.h2.sql;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


/**
* The csvread function of the H2 database skips columns based on their annotations.
*
* @author nkjm
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER, ElementType.FIELD})
public @interface SkipCsvRead {

}

0 comments on commit 793240a

Please sign in to comment.