Skip to content

Commit 47a88c8

Browse files
author
Philipp Wagner
committed
Fix Ordinals
1 parent 0890bdb commit 47a88c8

5 files changed

Lines changed: 182 additions & 6 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ protected void mapDateTimeWithTimeZone(String columnName, Func2<TEntity, OffsetD
123123
addColumn(columnName, 2014, propertyGetter);
124124
}
125125

126+
protected void mapString(String columnName, Func2<TEntity, String> propertyGetter) {
127+
addColumn(columnName, Types.NVARCHAR, propertyGetter);
128+
}
129+
126130
private void addColumn(String name, int type, boolean isAutoIncrement, Func2<TEntity, Object> propertyGetter)
127131
{
128132
// Create the current Column Meta Data:

JSqlServerBulkInsert/src/main/java/de/bytefish/jsqlserverbulkinsert/records/SqlServerRecord.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,34 @@ public SqlServerRecord(List<ColumnMetaData> columnMetaData, Object[] values) {
3232
public Set<Integer> getColumnOrdinals() {
3333
// Simply return the length of the List:
3434
return IntStream
35-
.range(0, columnMetaData.size())
35+
.range(1, columnMetaData.size() + 1)
3636
.boxed()
3737
.collect(Collectors.toSet());
3838
}
3939

4040
@Override
4141
public String getColumnName(int i) {
42-
return columnMetaData.get(i).getName();
42+
return columnMetaData.get(i-1).getName();
4343
}
4444

4545
@Override
4646
public int getColumnType(int i) {
47-
return columnMetaData.get(i).getType();
47+
return columnMetaData.get(i-1).getType();
4848
}
4949

5050
@Override
5151
public int getPrecision(int i) {
52-
return columnMetaData.get(i).getPrecision();
52+
return columnMetaData.get(i-1).getPrecision();
5353
}
5454

5555
@Override
5656
public int getScale(int i) {
57-
return columnMetaData.get(i).getScale();
57+
return columnMetaData.get(i-1).getScale();
5858
}
5959

6060
@Override
6161
public boolean isAutoIncrement(int i) {
62-
return columnMetaData.get(i).isAutoIncrement();
62+
return columnMetaData.get(i-1).isAutoIncrement();
6363
}
6464

6565
@Override
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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;
5+
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import java.sql.ResultSet;
10+
import java.sql.SQLException;
11+
import java.sql.Statement;
12+
import java.time.LocalDate;
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
public class IntegrationTest extends TransactionalTestBase {
17+
18+
private class PersonBulkInserter extends SqlServerBulkInsert<Person> {
19+
20+
public PersonBulkInserter() {
21+
super("dbo", "UnitTest");
22+
23+
mapString("FirstName", Person::getFirstName);
24+
mapString("LastName", Person::getLastName);
25+
mapDate("BirthDate", Person::getBirthDate);
26+
}
27+
}
28+
29+
@Override
30+
protected void onSetUpInTransaction() throws Exception {
31+
createTable();
32+
}
33+
34+
@Test
35+
public void bulkInsertPersonDataTest() throws SQLException {
36+
// Create a large list of Persons:
37+
List<Person> persons = getPersonList(100000);
38+
// Create the BulkInserter:
39+
PersonBulkInserter personBulkInserter = new PersonBulkInserter();
40+
// Now save all entities of a given stream:
41+
personBulkInserter.saveAll(connection, persons.stream());
42+
// And assert all have been written to the database:
43+
Assert.assertEquals(100000, getRowCount());
44+
}
45+
46+
private List<Person> getPersonList(int numPersons) {
47+
List<Person> persons = new ArrayList<>();
48+
49+
for (int pos = 0; pos < numPersons; pos++) {
50+
Person p = new Person();
51+
52+
p.setFirstName("Philipp");
53+
p.setLastName("Wagner");
54+
p.setBirthDate(LocalDate.of(1986, 5, 12));
55+
56+
persons.add(p);
57+
}
58+
59+
return persons;
60+
}
61+
62+
private boolean createTable() throws SQLException {
63+
64+
String sqlStatement = "CREATE TABLE [dbo].[UnitTest]\n" +
65+
" (\n" +
66+
" FirstName NVARCHAR(255),\n" +
67+
" LastName NVARCHAR(255),\n" +
68+
" BirthDate DATE\n" +
69+
" );";
70+
71+
Statement statement = connection.createStatement();
72+
73+
return statement.execute(sqlStatement);
74+
}
75+
76+
private int getRowCount() throws SQLException {
77+
78+
Statement s = connection.createStatement();
79+
80+
ResultSet r = s.executeQuery("SELECT COUNT(*) AS rowcount FROM [dbo].[UnitTest]");
81+
r.next();
82+
int count = r.getInt("rowcount");
83+
r.close();
84+
85+
return count;
86+
}
87+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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;
5+
6+
import java.time.LocalDate;
7+
8+
public class Person {
9+
10+
private String firstName;
11+
12+
private String lastName;
13+
14+
private LocalDate birthDate;
15+
16+
public Person() {
17+
}
18+
19+
public String getFirstName() {
20+
return firstName;
21+
}
22+
23+
public void setFirstName(String firstName) {
24+
this.firstName = firstName;
25+
}
26+
27+
public String getLastName() {
28+
return lastName;
29+
}
30+
31+
public void setLastName(String lastName) {
32+
this.lastName = lastName;
33+
}
34+
35+
public LocalDate getBirthDate() {
36+
return birthDate;
37+
}
38+
39+
public void setBirthDate(LocalDate birthDate) {
40+
this.birthDate = birthDate;
41+
}
42+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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;
5+
6+
7+
import org.junit.After;
8+
import org.junit.Before;
9+
10+
import java.sql.Connection;
11+
import java.sql.DriverManager;
12+
13+
public abstract class TransactionalTestBase {
14+
15+
protected Connection connection;
16+
17+
@Before
18+
public void setUp() throws Exception {
19+
connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=TestDatabase", "philipp", "test_pwd");
20+
21+
onSetUpBeforeTransaction();
22+
connection.setAutoCommit(false); // Start the Transaction:
23+
onSetUpInTransaction();
24+
}
25+
26+
@After
27+
public void tearDown() throws Exception {
28+
29+
onTearDownInTransaction();
30+
connection.rollback();
31+
onTearDownAfterTransaction();
32+
33+
connection.close();
34+
}
35+
36+
protected void onSetUpInTransaction() throws Exception {}
37+
38+
protected void onSetUpBeforeTransaction() throws Exception {}
39+
40+
protected void onTearDownInTransaction() throws Exception {}
41+
42+
protected void onTearDownAfterTransaction() throws Exception {}
43+
}

0 commit comments

Comments
 (0)