Skip to content

Commit 49fa78c

Browse files
committed
Remove legacy UTC Nano code. Separated tests for String to Varchar and Nvarchar
1 parent cd36a39 commit 49fa78c

3 files changed

Lines changed: 92 additions & 58 deletions

File tree

JSqlServerBulkInsert/src/main/java/de/bytefish/jsqlserverbulkinsert/mapping/AbstractMapping.java

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -162,61 +162,6 @@ protected void mapDateTimeWithTimeZone(String columnName, Function<TEntity, Offs
162162
mapProperty(columnName, SqlServerTypes.DateTimeWithTimeZone, propertyGetter, new OffsetDateTimeConverter());
163163
}
164164

165-
//
166-
// protected void mapUTCNano(String dateColumnName, String timeColumnName, Function<TEntity, Long> propertyGetter) {
167-
//
168-
// // We need to scale the incoming LocalDateTime and cast it to Timestamp so that the scaling sticks, before writing it to SQL Server:
169-
// final Function<TEntity, Date> dateWrapper = entity -> {
170-
// Long result = propertyGetter.apply(entity);
171-
//
172-
// if (result == null) {
173-
// return null;
174-
// }
175-
//
176-
// AbstractMap.Entry<Long,Integer> convertedDT = convertUTCNanoToEpochSecAndNano(result);
177-
//
178-
// return new Date(convertedDT.getKey()*1000);
179-
// };
180-
//
181-
// final Function<TEntity, String> timeWrapper = entity -> {
182-
// Long result = propertyGetter.apply(entity);
183-
//
184-
// if (result == null) {
185-
// return null;
186-
// }
187-
//
188-
// AbstractMap.Entry<Long,Integer> convertedDT = convertUTCNanoToEpochSecAndNano(result);
189-
// Time t = new Time(convertedDT.getKey() * 1000);
190-
// return t.toString() + "." + convertedDT.getValue()/100; // send as string bc java.sql.Time supports only millisecond precision!?
191-
// };
192-
//
193-
// addColumn(dateColumnName, Types.DATE, dateWrapper);
194-
// addColumn(timeColumnName, Types.TIME, timeWrapper);
195-
// }
196-
//
197-
// protected void mapUTCNano(String columnName, Function<TEntity, Long> propertyGetter) {
198-
//
199-
// // We need to scale the incoming LocalDateTime and cast it to Timestamp so that the scaling sticks, before writing it to SQL Server:
200-
// final Function<TEntity, Timestamp> wrapper = entity -> {
201-
//
202-
// Long result = propertyGetter.apply(entity);
203-
//
204-
// if (result == null) {
205-
// return null;
206-
// }
207-
//
208-
// AbstractMap.Entry<Long,Integer> convertedDT = convertUTCNanoToEpochSecAndNano(result);
209-
//
210-
// Timestamp castedResult = new Timestamp(convertedDT.getKey() * 1000); // convert to milliseconds to create the Timestamp
211-
// castedResult.setNanos(convertedDT.getValue());
212-
//
213-
// return castedResult;
214-
// };
215-
//
216-
// addColumn(columnName, Types.TIMESTAMP, wrapper);
217-
// }
218-
219-
220165
// endregion
221166

222167
// region Binary

JSqlServerBulkInsert/src/test/java/de/bytefish/jsqlserverbulkinsert/test/mapping/StringMappingTest.java renamed to JSqlServerBulkInsert/src/test/java/de/bytefish/jsqlserverbulkinsert/test/mapping/NvarcharMappingTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import java.util.Arrays;
1616
import java.util.List;
1717

18-
public class StringMappingTest extends TransactionalTestBase {
18+
public class NvarcharMappingTest extends TransactionalTestBase {
1919

2020
private class StringEntity {
2121

@@ -48,7 +48,7 @@ protected void onSetUpInTransaction() throws Exception {
4848
@Test
4949
public void bulkInsertPersonDataTest() throws SQLException {
5050
String stringData = "Halli Hallo Hallöchen";
51-
// Create te
51+
// Create the entity
5252
List<StringEntity> entities = Arrays.asList(new StringEntity(stringData));
5353
// Create the BulkInserter:
5454
StringEntityMapping mapping = new StringEntityMapping();
@@ -58,7 +58,7 @@ public void bulkInsertPersonDataTest() throws SQLException {
5858
ResultSet rs = getAll();
5959
// We have a Value:
6060
Assert.assertEquals(true, rs.next());
61-
// Get the Date we have written:
61+
// Get the string we have written:
6262
String resultString = rs.getString("StringValue");
6363
// Assert both are equal:
6464
Assert.assertEquals(stringData, resultString);
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (c) Philipp Wagner and Victor Lee. 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.mapping.AbstractMapping;
8+
import de.bytefish.jsqlserverbulkinsert.test.base.TransactionalTestBase;
9+
import org.junit.Assert;
10+
import org.junit.Test;
11+
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 VarcharMappingTest extends TransactionalTestBase {
19+
20+
private class StringEntity {
21+
22+
private final String value;
23+
24+
public StringEntity(String value) {
25+
this.value = value;
26+
}
27+
28+
public String getValue() {
29+
return value;
30+
}
31+
}
32+
33+
private class StringEntityMapping extends AbstractMapping<StringEntity> {
34+
35+
public StringEntityMapping() {
36+
super("dbo", "UnitTest");
37+
38+
mapVarchar("StringValue", StringEntity::getValue);
39+
}
40+
41+
}
42+
43+
@Override
44+
protected void onSetUpInTransaction() throws Exception {
45+
createTestTable();
46+
}
47+
48+
@Test
49+
public void bulkInsertPersonDataTest() throws SQLException {
50+
String stringData = "Halli Hallo Hallöchen";
51+
// Create the entity
52+
List<StringEntity> entities = Arrays.asList(new StringEntity(stringData));
53+
// Create the BulkInserter:
54+
StringEntityMapping mapping = new StringEntityMapping();
55+
// Now save all entities of a given stream:
56+
new SqlServerBulkInsert<>(mapping).saveAll(connection, entities.stream());
57+
// And assert all have been written to the database:
58+
ResultSet rs = getAll();
59+
// We have a Value:
60+
Assert.assertEquals(true, rs.next());
61+
// Get the string we have written:
62+
String resultString = rs.getString("StringValue");
63+
// Assert both are equal:
64+
Assert.assertEquals(stringData, resultString);
65+
// Assert only one record was read:
66+
Assert.assertEquals(false, rs.next());
67+
}
68+
69+
private ResultSet getAll() throws SQLException {
70+
71+
String sqlStatement = "SELECT * FROM dbo.UnitTest";
72+
73+
Statement statement = connection.createStatement();
74+
75+
return statement.executeQuery(sqlStatement);
76+
}
77+
78+
private void createTestTable() throws SQLException {
79+
String sqlStatement = "CREATE TABLE [dbo].[UnitTest]\n" +
80+
" (\n" +
81+
" StringValue VARCHAR(30)\n" +
82+
" );";
83+
84+
Statement statement = connection.createStatement();
85+
86+
statement.execute(sqlStatement);
87+
}
88+
89+
}

0 commit comments

Comments
 (0)