66import com .microsoft .sqlserver .jdbc .ISQLServerBulkRecord ;
77import com .microsoft .sqlserver .jdbc .SQLServerBulkCopy ;
88import com .microsoft .sqlserver .jdbc .SQLServerBulkCopyOptions ;
9- import de .bytefish .jsqlserverbulkinsert .functional .Func2 ;
10- import de .bytefish .jsqlserverbulkinsert .model .ColumnDefinition ;
11- import de .bytefish .jsqlserverbulkinsert .model .ColumnMetaData ;
12- import de .bytefish .jsqlserverbulkinsert .model .TableDefinition ;
9+ import de .bytefish .jsqlserverbulkinsert .mapping .AbstractMapping ;
1310import de .bytefish .jsqlserverbulkinsert .records .SqlServerRecord ;
14- import de .bytefish .jsqlserverbulkinsert .records .SqlServerRecordBuilder ;
1511
16- import java .math .BigDecimal ;
17- import java .math .BigInteger ;
1812import java .sql .Connection ;
1913import java .sql .SQLException ;
20- import java .sql .Types ;
21- import java .time .LocalDate ;
22- import java .time .OffsetDateTime ;
23- import java .time .OffsetTime ;
24- import java .util .*;
2514import java .util .stream .Stream ;
2615
27- public abstract class SqlServerBulkInsert <TEntity > implements ISqlServerBulkInsert <TEntity > {
16+ public class SqlServerBulkInsert <TEntity > implements ISqlServerBulkInsert <TEntity > {
2817
29- private TableDefinition table ;
18+ private final AbstractMapping < TEntity > mapping ;
3019
31- private List <ColumnDefinition <TEntity >> columns ;
32-
33- public SqlServerBulkInsert (String schemaName , String tableName )
20+ public SqlServerBulkInsert (AbstractMapping <TEntity > mapping )
3421 {
35- this .table = new TableDefinition (schemaName , tableName );
36- this .columns = new ArrayList <>();
22+ this .mapping = mapping ;
3723 }
3824
3925 public void saveAll (Connection connection , Stream <TEntity > entities ) throws SQLException {
4026 saveAll (connection , new SQLServerBulkCopyOptions (), entities );
4127 }
4228
4329 public void saveAll (Connection connection , SQLServerBulkCopyOptions options , Stream <TEntity > entities ) throws SQLException {
44-
45- final SqlServerRecordBuilder <TEntity > sqlServerRecordBuilder = new SqlServerRecordBuilder <>(columns );
46-
30+ // Create a new SQLServerBulkCopy Instance on the given Connection:
4731 try (SQLServerBulkCopy sqlServerBulkCopy = new SQLServerBulkCopy (connection )) {
48-
4932 // Set the Options:
5033 sqlServerBulkCopy .setBulkCopyOptions (options );
51-
5234 // The Destination Table to write to:
53- sqlServerBulkCopy .setDestinationTableName (table .GetFullQualifiedTableName ());
54-
35+ sqlServerBulkCopy .setDestinationTableName (mapping .getTableDefinition ().GetFullQualifiedTableName ());
5536 // The SQL Records to insert:
56- ISQLServerBulkRecord record = new SqlServerRecord <TEntity >(columns , entities .iterator ());
57-
37+ ISQLServerBulkRecord record = new SqlServerRecord <TEntity >(mapping .getColumns (), entities .iterator ());
5838 // Finally start the Bulk Copy Process:
5939 internalWriteToServer (sqlServerBulkCopy , record );
6040 }
@@ -67,131 +47,4 @@ public void internalWriteToServer(SQLServerBulkCopy sqlServerBulkCopy, ISQLServe
6747 throw new RuntimeException (e );
6848 }
6949 }
70-
71- protected void mapBoolean (String columnName , Func2 <TEntity , Boolean > propertyGetter ) {
72- addColumn (columnName , Types .BIT , propertyGetter );
73- }
74-
75- protected void mapNumeric (String columnName , int precision , int scale , Func2 <TEntity , BigDecimal > propertyGetter ) {
76-
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 (scale , BigDecimal .ROUND_HALF_UP );
82-
83- return result ;
84- };
85-
86- addColumn (columnName , Types .NUMERIC , precision , scale , false , wrapper );
87- }
88-
89- protected void mapDecimal (String columnName , int precision , int scale , Func2 <TEntity , BigDecimal > propertyGetter )
90- {
91- // We need to scale the incoming decimal, before writing it to SQL Server:
92- final Func2 <TEntity , BigDecimal > wrapper = entity -> {
93- BigDecimal result = propertyGetter
94- .invoke (entity )
95- .setScale (scale , BigDecimal .ROUND_HALF_UP );
96-
97- return result ;
98- };
99-
100- addColumn (columnName , Types .DECIMAL , precision , scale , false , wrapper );
101- }
102-
103- protected void mapReal (String columnName , Func2 <TEntity , Float > propertyGetter )
104- {
105- addColumn (columnName , Types .REAL , propertyGetter );
106- }
107-
108- protected void mapBigInt (String columnName , Func2 <TEntity , BigInteger > propertyGetter ) {
109-
110- // SQL Server expects the Big Integer as a Long Value:
111- final Func2 <TEntity , Long > wrapper = entity -> {
112- BigInteger resultAsBigInteger = propertyGetter .invoke (entity );
113-
114- return resultAsBigInteger .longValueExact ();
115- };
116-
117- addColumn (columnName , Types .BIGINT , wrapper );
118- }
119-
120- protected void mapBigIntLong (String columnName , Func2 <TEntity , Long > propertyGetter ) {
121- addColumn (columnName , Types .BIGINT , propertyGetter );
122- }
123-
124- protected void mapDate (String columnName , Func2 <TEntity , LocalDate > propertyGetter ) {
125- addColumn (columnName , Types .DATE , propertyGetter );
126- }
127-
128- protected void mapDouble (String columnName , Func2 <TEntity , Double > propertyGetter )
129- {
130- addColumn (columnName , Types .DOUBLE , propertyGetter );
131- }
132-
133- protected void mapInt (String columnName , Func2 <TEntity , Integer > propertyGetter )
134- {
135- addColumn (columnName , Types .INTEGER , propertyGetter );
136- }
137-
138- protected void mapSmallInt (String columnName , Func2 <TEntity , Short > propertyGetter )
139- {
140- addColumn (columnName , Types .SMALLINT , propertyGetter );
141- }
142-
143- protected void mapTinyInt (String columnName , Func2 <TEntity , Byte > propertyGetter )
144- {
145- addColumn (columnName , Types .TINYINT , propertyGetter );
146- }
147-
148- protected void mapTimeWithTimeZone (String columnName , Func2 <TEntity , OffsetTime > propertyGetter ) {
149- addColumn (columnName , 2013 , propertyGetter );
150- }
151-
152- protected void mapDateTimeWithTimeZone (String columnName , Func2 <TEntity , OffsetDateTime > propertyGetter ) {
153- addColumn (columnName , 2014 , propertyGetter );
154- }
155-
156- protected void mapString (String columnName , Func2 <TEntity , String > propertyGetter ) {
157- addColumn (columnName , Types .NVARCHAR , propertyGetter );
158- }
159-
160- protected void mapVarBinary (String columnName , int maxLength , Func2 <TEntity , byte []> propertyGetter ) {
161- addColumn (columnName , Types .VARBINARY , maxLength , 0 , false , propertyGetter );
162- }
163-
164- private void addColumn (String name , int type , boolean isAutoIncrement , Func2 <TEntity , Object > propertyGetter )
165- {
166- // Create the current Column Meta Data:
167- ColumnMetaData columnMetaData = new ColumnMetaData (name , type , 0 , 0 , isAutoIncrement );
168-
169- // Add a new Column with the Meta Data and Property Getter:
170- addColumn (columnMetaData , propertyGetter );
171- }
172-
173- private <TProperty > void addColumn (String name , int type , Func2 <TEntity , TProperty > propertyGetter )
174- {
175- // Create the current Column Meta Data:
176- ColumnMetaData columnMetaData = new ColumnMetaData (name , type );
177-
178- // Add a new Column with the Meta Data and Property Getter:
179- addColumn (columnMetaData , propertyGetter );
180- }
181-
182- private <TProperty > void addColumn (String name , int type , int precision , int scale , boolean isAutoIncrement , Func2 <TEntity , TProperty > propertyGetter )
183- {
184- // Create the current Column Meta Data:
185- ColumnMetaData columnMetaData = new ColumnMetaData (name , type , precision , scale , isAutoIncrement );
186-
187- // Add a new Column with the Meta Data and Property Getter:
188-
189- addColumn (columnMetaData , propertyGetter );
190- }
191-
192- private <TProperty > void addColumn (ColumnMetaData columnMetaData , Func2 <TEntity , TProperty > propertyGetter )
193- {
194- // Add a new Column with the Meta Data and Property Getter:
195- columns .add (new ColumnDefinition (columnMetaData , propertyGetter ));
196- }
19750}
0 commit comments