|
8 | 8 | import java.sql.ResultSet;
|
9 | 9 | import java.sql.SQLException;
|
10 | 10 | import java.sql.Statement;
|
| 11 | +import java.sql.Timestamp; |
| 12 | +import java.sql.Types; |
11 | 13 |
|
12 | 14 | import org.junit.jupiter.api.BeforeAll;
|
13 | 15 | import org.junit.jupiter.api.Tag;
|
|
16 | 18 | import org.junit.runner.RunWith;
|
17 | 19 |
|
18 | 20 | import com.microsoft.sqlserver.jdbc.ComparisonUtil;
|
| 21 | +import com.microsoft.sqlserver.jdbc.RandomData; |
| 22 | +import com.microsoft.sqlserver.jdbc.RandomUtil; |
19 | 23 | import com.microsoft.sqlserver.jdbc.SQLServerBulkCopy;
|
| 24 | +import com.microsoft.sqlserver.jdbc.SQLServerBulkCopyOptions; |
20 | 25 | import com.microsoft.sqlserver.jdbc.TestUtils;
|
| 26 | +import com.microsoft.sqlserver.testframework.AbstractSQLGenerator; |
21 | 27 | import com.microsoft.sqlserver.testframework.AbstractTest;
|
22 | 28 | import com.microsoft.sqlserver.testframework.Constants;
|
23 | 29 | import com.microsoft.sqlserver.testframework.DBConnection;
|
24 | 30 | import com.microsoft.sqlserver.testframework.DBStatement;
|
25 | 31 | import com.microsoft.sqlserver.testframework.DBTable;
|
26 | 32 | import com.microsoft.sqlserver.testframework.PrepUtil;
|
27 | 33 |
|
| 34 | +import javax.sql.RowSetMetaData; |
| 35 | +import javax.sql.rowset.CachedRowSet; |
| 36 | +import javax.sql.rowset.RowSetFactory; |
| 37 | +import javax.sql.rowset.RowSetMetaDataImpl; |
| 38 | +import javax.sql.rowset.RowSetProvider; |
| 39 | + |
| 40 | +import java.util.ArrayList; |
| 41 | +import java.util.List; |
| 42 | +import java.util.stream.Collectors; |
| 43 | +import java.util.stream.IntStream; |
| 44 | + |
| 45 | +import static org.junit.Assert.assertEquals; |
| 46 | +import static org.junit.Assert.assertTrue; |
| 47 | + |
28 | 48 |
|
29 | 49 | @RunWith(JUnitPlatform.class)
|
30 | 50 | public class BulkCopyAllTypesTest extends AbstractTest {
|
@@ -98,6 +118,71 @@ private void terminateVariation() throws SQLException {
|
98 | 118 | try (Statement stmt = connection.createStatement()) {
|
99 | 119 | TestUtils.dropTableIfExists(tableSrc.getEscapedTableName(), stmt);
|
100 | 120 | TestUtils.dropTableIfExists(tableDest.getEscapedTableName(), stmt);
|
| 121 | + TestUtils.dropTableIfExists(dateTimeTestTable, stmt); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private static final int DATETIME_COL_COUNT = 2; |
| 126 | + private static final int DATETIME_ROW_COUNT = 1; |
| 127 | + private static final String dateTimeTestTable = |
| 128 | + AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("bulkCopyTimestampTest")); |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testBulkCopyTimestamp() throws SQLException { |
| 132 | + List<Timestamp> timeStamps = new ArrayList<>(); |
| 133 | + try (Connection con = getConnection(); Statement stmt = connection.createStatement()) { |
| 134 | + String colSpec = IntStream.range(1, DATETIME_COL_COUNT + 1).mapToObj(x -> String.format("c%d datetime", x)).collect( |
| 135 | + Collectors.joining(",")); |
| 136 | + String sql1 = String.format("create table %s (%s)", dateTimeTestTable, colSpec); |
| 137 | + stmt.execute(sql1); |
| 138 | + |
| 139 | + RowSetFactory rsf = RowSetProvider.newFactory(); |
| 140 | + CachedRowSet crs = rsf.createCachedRowSet(); |
| 141 | + RowSetMetaData rsmd = new RowSetMetaDataImpl(); |
| 142 | + rsmd.setColumnCount(DATETIME_COL_COUNT); |
| 143 | + |
| 144 | + for (int i = 1; i <= DATETIME_COL_COUNT; i++) { |
| 145 | + rsmd.setColumnName(i, String.format("c%d", i)); |
| 146 | + rsmd.setColumnType(i, Types.TIMESTAMP); |
| 147 | + } |
| 148 | + crs.setMetaData(rsmd); |
| 149 | + |
| 150 | + for (int i = 0; i < DATETIME_COL_COUNT; i++) { |
| 151 | + timeStamps.add(RandomData.generateDatetime(false)); |
| 152 | + } |
| 153 | + |
| 154 | + for (int ri = 0; ri < DATETIME_ROW_COUNT; ri++) { |
| 155 | + crs.moveToInsertRow(); |
| 156 | + |
| 157 | + for (int i = 1; i <= DATETIME_COL_COUNT; i++) { |
| 158 | + crs.updateTimestamp(i, timeStamps.get(i - 1)); |
| 159 | + } |
| 160 | + crs.insertRow(); |
| 161 | + } |
| 162 | + crs.moveToCurrentRow(); |
| 163 | + |
| 164 | + try (SQLServerBulkCopy bcOperation = new SQLServerBulkCopy(con)) { |
| 165 | + SQLServerBulkCopyOptions bcOptions = new SQLServerBulkCopyOptions(); |
| 166 | + bcOptions.setBatchSize(5000); |
| 167 | + bcOperation.setDestinationTableName(dateTimeTestTable); |
| 168 | + bcOperation.setBulkCopyOptions(bcOptions); |
| 169 | + bcOperation.writeToServer(crs); |
| 170 | + } |
| 171 | + |
| 172 | + try (ResultSet rs = stmt.executeQuery("select * from " + dateTimeTestTable)) { |
| 173 | + assertTrue(rs.next()); |
| 174 | + |
| 175 | + for (int i = 1; i <= DATETIME_COL_COUNT; i++) { |
| 176 | + long expectedTimestamp = getTime(timeStamps.get(i - 1)); |
| 177 | + long actualTimestamp = getTime(rs.getTimestamp(i)); |
| 178 | + |
| 179 | + assertEquals(expectedTimestamp, actualTimestamp); |
| 180 | + } |
| 181 | + } |
101 | 182 | }
|
102 | 183 | }
|
| 184 | + |
| 185 | + private static long getTime(Timestamp time) { |
| 186 | + return (3 * time.getTime() + 5) / 10; |
| 187 | + } |
103 | 188 | }
|
0 commit comments