Skip to content

Commit 6bdd4b2

Browse files
author
Philipp Wagner
committed
Fix Boolean Handler
1 parent 95d0e47 commit 6bdd4b2

3 files changed

Lines changed: 177 additions & 4 deletions

File tree

JSqlServerBulkInsert/src/main/java/de/bytefish/jsqlserverbulkinsert/SqlServerBulkInsert.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,20 @@ public void internalWriteToServer(SQLServerBulkCopy sqlServerBulkCopy, ISQLServe
7070
}
7171

7272
protected void mapBoolean(String columnName, Func2<TEntity, Boolean> propertyGetter) {
73-
addColumn(columnName, Types.BOOLEAN, propertyGetter);
73+
addColumn(columnName, Types.BIT, propertyGetter);
7474
}
7575

76-
protected void mapNumeric(String columnName, int scale, int precision, Func2<TEntity, BigDecimal> propertyGetter)
77-
{
78-
addColumn(columnName, Types.NUMERIC, precision, scale, false, propertyGetter);
76+
protected void mapNumeric(String columnName, int scale, int precision, Func2<TEntity, BigDecimal> propertyGetter) {
77+
// We need to scale the incoming decimal, before writing it to SQL Server:
78+
final Func2<TEntity, BigDecimal> wrapper = entity -> {
79+
BigDecimal result = propertyGetter
80+
.invoke(entity)
81+
.setScale(10, BigDecimal.ROUND_HALF_UP);
82+
83+
return result;
84+
};
85+
86+
addColumn(columnName, Types.NUMERIC, precision, scale, false, wrapper);
7987
}
8088

8189
protected void mapDecimal(String columnName, int precision, int scale, Func2<TEntity, BigDecimal> propertyGetter)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (c) Philipp Wagner. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
package de.bytefish.jsqlserverbulkinsert.test.mapping;
5+
6+
import de.bytefish.jsqlserverbulkinsert.SqlServerBulkInsert;
7+
import de.bytefish.jsqlserverbulkinsert.test.base.TransactionalTestBase;
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
11+
import java.sql.ResultSet;
12+
import java.sql.SQLException;
13+
import java.sql.Statement;
14+
import java.util.Arrays;
15+
import java.util.List;
16+
17+
public class BooleanMappingTest extends TransactionalTestBase {
18+
19+
private class BooleanEntity extends SampleEntity<Boolean> {
20+
21+
public BooleanEntity(Boolean value) {
22+
super(value);
23+
}
24+
}
25+
26+
private class BooleanInsert extends SqlServerBulkInsert<BooleanEntity> {
27+
28+
public BooleanInsert() {
29+
super("dbo", "UnitTest");
30+
31+
mapBoolean("BooleanValue", BooleanEntity::getValue);
32+
}
33+
34+
}
35+
36+
@Override
37+
protected void onSetUpInTransaction() throws Exception {
38+
createTestTable();
39+
}
40+
41+
@Test
42+
public void bulkInsertPersonDataTest() throws SQLException {
43+
boolean booleanValue = true;
44+
// Create the Value:
45+
List<BooleanEntity> entities = Arrays.asList(new BooleanEntity(booleanValue));
46+
// Create the BulkInserter:
47+
BooleanInsert localDateInsert = new BooleanInsert();
48+
// Now save all entities of a given stream:
49+
localDateInsert.saveAll(connection, entities.stream());
50+
// And assert all have been written to the database:
51+
ResultSet rs = getAll();
52+
// We have a Value:
53+
Assert.assertEquals(true, rs.next());
54+
// Get the Date we have written:
55+
boolean resultBooleanValue = rs.getBoolean("BooleanValue");
56+
// Assert both are equal:
57+
Assert.assertEquals(booleanValue, resultBooleanValue);
58+
// Assert only one record was read:
59+
Assert.assertEquals(false, rs.next());
60+
}
61+
62+
private ResultSet getAll() throws SQLException {
63+
64+
String sqlStatement = "SELECT * FROM dbo.UnitTest";
65+
66+
Statement statement = connection.createStatement();
67+
68+
return statement.executeQuery(sqlStatement);
69+
}
70+
71+
private void createTestTable() throws SQLException {
72+
String sqlStatement = "CREATE TABLE [dbo].[UnitTest]\n" +
73+
" (\n" +
74+
" BooleanValue BIT\n" +
75+
" );";
76+
77+
Statement statement = connection.createStatement();
78+
79+
statement.execute(sqlStatement);
80+
}
81+
82+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) Philipp Wagner. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
package de.bytefish.jsqlserverbulkinsert.test.mapping;
5+
6+
import de.bytefish.jsqlserverbulkinsert.SqlServerBulkInsert;
7+
import de.bytefish.jsqlserverbulkinsert.test.base.TransactionalTestBase;
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
11+
import java.math.BigDecimal;
12+
import java.sql.ResultSet;
13+
import java.sql.SQLException;
14+
import java.sql.Statement;
15+
import java.util.Arrays;
16+
import java.util.List;
17+
18+
public class RealMappingTest extends TransactionalTestBase {
19+
20+
private class FloatEntity extends SampleEntity<Float> {
21+
22+
public FloatEntity(Float value) {
23+
super(value);
24+
}
25+
}
26+
27+
private class BigDecimalInsert extends SqlServerBulkInsert<FloatEntity> {
28+
29+
public BigDecimalInsert() {
30+
super("dbo", "UnitTest");
31+
32+
mapReal("FloatValue", FloatEntity::getValue);
33+
}
34+
35+
}
36+
37+
@Override
38+
protected void onSetUpInTransaction() throws Exception {
39+
createTestTable();
40+
}
41+
42+
@Test
43+
public void bulkInsertPersonDataTest() throws SQLException {
44+
float floatValue = 123.123f;
45+
// Create the Value:
46+
List<FloatEntity> entities = Arrays.asList(new FloatEntity(floatValue));
47+
// Create the BulkInserter:
48+
BigDecimalInsert localDateInsert = new BigDecimalInsert();
49+
// Now save all entities of a given stream:
50+
localDateInsert.saveAll(connection, entities.stream());
51+
// And assert all have been written to the database:
52+
ResultSet rs = getAll();
53+
// We have a Value:
54+
Assert.assertEquals(true, rs.next());
55+
// Get the Date we have written:
56+
float resultFloatValue = rs.getFloat("FloatValue");
57+
// Assert both are equal:
58+
Assert.assertEquals(floatValue, resultFloatValue, 1e-10);
59+
// Assert only one record was read:
60+
Assert.assertEquals(false, rs.next());
61+
}
62+
63+
private ResultSet getAll() throws SQLException {
64+
65+
String sqlStatement = "SELECT * FROM dbo.UnitTest";
66+
67+
Statement statement = connection.createStatement();
68+
69+
return statement.executeQuery(sqlStatement);
70+
}
71+
72+
private void createTestTable() throws SQLException {
73+
String sqlStatement = "CREATE TABLE [dbo].[UnitTest]\n" +
74+
" (\n" +
75+
" FloatValue FLOAT\n" +
76+
" );";
77+
78+
Statement statement = connection.createStatement();
79+
80+
statement.execute(sqlStatement);
81+
}
82+
83+
}

0 commit comments

Comments
 (0)