Skip to content

Commit b73aa80

Browse files
authored
Merge pull request #94 from PgBulkInsert/primitive-functions
Add Primitive Functions
2 parents 4b9048a + f28b87e commit b73aa80

9 files changed

Lines changed: 86 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# CHANGELOG #
22

3+
## 7.0.1 ##
4+
5+
* Re-added support for primtive data types, but used method names:
6+
* ``AbstractMapping#mapBooleanPrimitive``
7+
* ``AbstractMapping#mapBytePrimitive``
8+
* ``AbstractMapping#mapShortPrimtive``
9+
* ``AbstractMapping#mapIntegerPrimitive``
10+
* ``AbstractMapping#mapFloatPrimitive``
11+
* ``AbstractMapping#mapDoublePrimitive``
12+
313
## 7.0.0 ##
414

515
* The ``JpaMapping<>`` has been dropped from the library.

PgBulkInsert/pgbulkinsert-bulkprocessor/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<parent>
99
<groupId>de.bytefish.pgbulkinsert</groupId>
1010
<artifactId>pgbulkinsert-parent</artifactId>
11-
<version>7.0.0</version>
11+
<version>7.0.1</version>
1212
<relativePath>..</relativePath>
1313
</parent>
1414

PgBulkInsert/pgbulkinsert-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<parent>
99
<groupId>de.bytefish.pgbulkinsert</groupId>
1010
<artifactId>pgbulkinsert-parent</artifactId>
11-
<version>7.0.0</version>
11+
<version>7.0.1</version>
1212
<relativePath>..</relativePath>
1313
</parent>
1414

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
2+
3+
package de.bytefish.pgbulkinsert.function;
4+
5+
@FunctionalInterface
6+
public interface ToBooleanFunction<T> {
7+
8+
/**
9+
* Applies this function to the given argument.
10+
*
11+
* @param value the function argument
12+
* @return the function result
13+
*/
14+
boolean applyAsBoolean(T value);
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
2+
3+
package de.bytefish.pgbulkinsert.function;
4+
5+
@FunctionalInterface
6+
public interface ToFloatFunction<T> {
7+
8+
/**
9+
* Applies this function to the given argument.
10+
*
11+
* @param value the function argument
12+
* @return the function result
13+
*/
14+
float applyAsFloat(T value);
15+
}

PgBulkInsert/pgbulkinsert-core/src/main/java/de/bytefish/pgbulkinsert/mapping/AbstractMapping.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
package de.bytefish.pgbulkinsert.mapping;
44

5+
import de.bytefish.pgbulkinsert.function.ToBooleanFunction;
6+
import de.bytefish.pgbulkinsert.function.ToFloatFunction;
57
import de.bytefish.pgbulkinsert.model.ColumnDefinition;
68
import de.bytefish.pgbulkinsert.model.TableDefinition;
79
import de.bytefish.pgbulkinsert.pgsql.PgBinaryWriter;
@@ -20,10 +22,7 @@
2022
import java.time.LocalTime;
2123
import java.time.ZonedDateTime;
2224
import java.util.*;
23-
import java.util.function.BiConsumer;
24-
import java.util.function.Function;
25-
import java.util.function.ToDoubleFunction;
26-
import java.util.function.ToIntFunction;
25+
import java.util.function.*;
2726
import java.util.stream.Collectors;
2827

2928
public abstract class AbstractMapping<TEntity> {
@@ -81,11 +80,17 @@ protected void mapBoolean(String columnName, Function<TEntity, Boolean> property
8180
map(columnName, DataType.Boolean, propertyGetter);
8281
}
8382

83+
protected void mapBooleanPrimitive(String columnName, ToBooleanFunction<TEntity> propertyGetter) {
84+
addColumn(columnName, (binaryWriter, entity) -> {
85+
binaryWriter.writeBoolean(propertyGetter.applyAsBoolean(entity));
86+
});
87+
}
88+
8489
protected void mapByte(String columnName, Function<TEntity, Number> propertyGetter) {
8590
map(columnName, DataType.Char, propertyGetter);
8691
}
8792

88-
protected void mapByte(String columnName, ToIntFunction<TEntity> propertyGetter) {
93+
protected void mapBytePrimitive(String columnName, ToIntFunction<TEntity> propertyGetter) {
8994
addColumn(columnName, (binaryWriter, entity) -> {
9095
binaryWriter.writeByte(propertyGetter.applyAsInt(entity));
9196
});
@@ -95,7 +100,7 @@ protected void mapShort(String columnName, Function<TEntity, Number> propertyGet
95100
map(columnName, DataType.Int2, propertyGetter);
96101
}
97102

98-
protected void mapShort(String columnName, ToIntFunction<TEntity> propertyGetter) {
103+
protected void mapShortPrimitive(String columnName, ToIntFunction<TEntity> propertyGetter) {
99104
addColumn(columnName, (binaryWriter, entity) -> {
100105
binaryWriter.writeShort(propertyGetter.applyAsInt(entity));
101106
});
@@ -105,7 +110,7 @@ protected void mapInteger(String columnName, Function<TEntity, Number> propertyG
105110
map(columnName, DataType.Int4, propertyGetter);
106111
}
107112

108-
protected void mapInteger(String columnName, ToIntFunction<TEntity> propertyGetter) {
113+
protected void mapIntegerPrimitive(String columnName, ToIntFunction<TEntity> propertyGetter) {
109114
addColumn(columnName, (binaryWriter, entity) -> {
110115
binaryWriter.writeInt(propertyGetter.applyAsInt(entity));
111116
});
@@ -119,15 +124,27 @@ protected void mapLong(String columnName, Function<TEntity, Number> propertyGett
119124
map(columnName, DataType.Int8, propertyGetter);
120125
}
121126

127+
protected void mapLongPrimitive(String columnName, ToLongFunction<TEntity> propertyGetter) {
128+
addColumn(columnName, (binaryWriter, entity) -> {
129+
binaryWriter.writeLong(propertyGetter.applyAsLong(entity));
130+
});
131+
}
132+
122133
protected void mapFloat(String columnName, Function<TEntity, Number> propertyGetter) {
123134
map(columnName, DataType.SinglePrecision, propertyGetter);
124135
}
125136

137+
protected void mapFloatPrimitive(String columnName, ToFloatFunction<TEntity> propertyGetter) {
138+
addColumn(columnName, (binaryWriter, entity) -> {
139+
binaryWriter.writeFloat(propertyGetter.applyAsFloat(entity));
140+
});
141+
}
142+
126143
protected void mapDouble(String columnName, Function<TEntity, Number> propertyGetter) {
127144
map(columnName, DataType.DoublePrecision, propertyGetter);
128145
}
129146

130-
protected void mapDouble(String columnName, ToDoubleFunction<TEntity> propertyGetter) {
147+
protected void mapDoublePrimitive(String columnName, ToDoubleFunction<TEntity> propertyGetter) {
131148
addColumn(columnName, (binaryWriter, entity) -> {
132149
binaryWriter.writeDouble(propertyGetter.applyAsDouble(entity));
133150
});

PgBulkInsert/pgbulkinsert-rowwriter/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<parent>
99
<groupId>de.bytefish.pgbulkinsert</groupId>
1010
<artifactId>pgbulkinsert-parent</artifactId>
11-
<version>7.0.0</version>
11+
<version>7.0.1</version>
1212
<relativePath>..</relativePath>
1313
</parent>
1414

PgBulkInsert/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<groupId>de.bytefish.pgbulkinsert</groupId>
88
<artifactId>pgbulkinsert-parent</artifactId>
99
<packaging>pom</packaging>
10-
<version>7.0.0</version>
10+
<version>7.0.1</version>
1111
<name>pgbulkinsert</name>
1212
<description>PgBulkInsert is a Java library for Bulk Inserts with PostgreSQL.</description>
1313
<url>http://www.github.com/bytefish/PgBulkInsert</url>

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ You can add the following dependencies to your pom.xml to include [PgBulkInsert]
3030
<dependency>
3131
<groupId>de.bytefish.pgbulkinsert</groupId>
3232
<artifactId>pgbulkinsert-core</artifactId>
33-
<version>7.0.0</version>
33+
<version>7.0.1</version>
3434
</dependency>
3535

3636
<dependency>
3737
<groupId>de.bytefish.pgbulkinsert</groupId>
3838
<artifactId>pgbulkinsert-rowwriter</artifactId>
39-
<version>7.0.0</version>
39+
<version>7.0.1</version>
4040
</dependency>
4141
```
4242

@@ -47,14 +47,14 @@ If you are working with Java8 you have to add a classifier ``jdk8`` to the depen
4747
<dependency>
4848
<groupId>de.bytefish.pgbulkinsert</groupId>
4949
<artifactId>pgbulkinsert-core</artifactId>
50-
<version>7.0.0</version>
50+
<version>7.0.1</version>
5151
<classifier>jdk8</classifier>
5252
</dependency>
5353

5454
<dependency>
5555
<groupId>de.bytefish.pgbulkinsert</groupId>
5656
<artifactId>pgbulkinsert-rowwriter</artifactId>
57-
<version>7.0.0</version>
57+
<version>7.0.1</version>
5858
<classifier>jdk8</classifier>
5959
</dependency>
6060
```
@@ -334,6 +334,19 @@ private List<Person> getPersonList(int num) {
334334

335335
## FAQ ##
336336

337+
### How can I write Primitive Types (``boolean``, ``float``, ``double``)? ###
338+
339+
By default methods like ``mapBoolean`` map the boxed type ``Boolean``, ``Integer``, ``Long``. This might be problematic
340+
if you need to squeeze out the last seconds when doing bulk inserts, see Issue:
341+
342+
* [https://github.com/PgBulkInsert/PgBulkInsert/issues/93](https://github.com/PgBulkInsert/PgBulkInsert/issues/93)
343+
344+
So for every data type that also has a primitive type, you can add a "Primitive" suffix to the method name like:
345+
346+
* ```mapBooleanPrimitive``
347+
348+
This will use the primitive type and prevent boxing and unboxing of values.
349+
337350
### How can I write a ``java.sql.Timestamp``? ###
338351

339352
You probably have Java classes with a ``java.sql.Timestamp`` in your application. Now if you use the ``AbstractMapping`` or a ``SimpleRowWriter`` it expects a ``LocalDateTime``. Here is how to map a ``java.sql.Timestamp``.

0 commit comments

Comments
 (0)