@@ -1724,53 +1724,57 @@ private void getDestinationMetadata() throws SQLServerException {
1724
1724
SQLServerStatement stmt = null ;
1725
1725
String metaDataQuery = null ;
1726
1726
1727
- try {
1728
- if (null != destinationTableMetadata ) {
1729
- rs = (SQLServerResultSet ) destinationTableMetadata ;
1730
- } else {
1731
- stmt = (SQLServerStatement ) connection .createStatement (ResultSet .TYPE_FORWARD_ONLY ,
1732
- ResultSet .CONCUR_READ_ONLY , connection .getHoldability (), stmtColumnEncriptionSetting );
1727
+ if (null == destColumnMetadata || destColumnMetadata .isEmpty ()) {
1728
+ try {
1729
+ if (null != destinationTableMetadata ) {
1730
+ rs = (SQLServerResultSet ) destinationTableMetadata ;
1731
+ } else {
1732
+ stmt = (SQLServerStatement ) connection .createStatement (ResultSet .TYPE_FORWARD_ONLY ,
1733
+ ResultSet .CONCUR_READ_ONLY , connection .getHoldability (), stmtColumnEncriptionSetting );
1733
1734
1734
- // Get destination metadata
1735
- rs = stmt .executeQueryInternal (
1736
- "sp_executesql N'SET FMTONLY ON SELECT * FROM " + escapedDestinationTableName + " '" );
1737
- }
1735
+ // Get destination metadata
1736
+ rs = stmt .executeQueryInternal (
1737
+ "sp_executesql N'SET FMTONLY ON SELECT * FROM " + escapedDestinationTableName + " '" );
1738
+ }
1738
1739
1739
- destColumnCount = rs .getMetaData ().getColumnCount ();
1740
- destColumnMetadata = new HashMap <>();
1741
- destCekTable = rs .getCekTable ();
1740
+ destColumnCount = rs .getMetaData ().getColumnCount ();
1741
+ destColumnMetadata = new HashMap <>();
1742
+ destCekTable = rs .getCekTable ();
1742
1743
1743
- if (!connection .getServerSupportsColumnEncryption ()) {
1744
- metaDataQuery = "select collation_name from sys.columns where " + "object_id=OBJECT_ID('"
1745
- + escapedDestinationTableName + "') " + "order by column_id ASC" ;
1746
- } else {
1747
- metaDataQuery = "select collation_name, encryption_type from sys.columns where "
1748
- + "object_id=OBJECT_ID('" + escapedDestinationTableName + "') " + "order by column_id ASC" ;
1749
- }
1744
+ if (!connection .getServerSupportsColumnEncryption ()) {
1745
+ metaDataQuery = "select collation_name from sys.columns where " + "object_id=OBJECT_ID('"
1746
+ + escapedDestinationTableName + "') " + "order by column_id ASC" ;
1747
+ } else {
1748
+ metaDataQuery = "select collation_name, encryption_type from sys.columns where "
1749
+ + "object_id=OBJECT_ID('" + escapedDestinationTableName + "') " + "order by column_id ASC" ;
1750
+ }
1750
1751
1751
- try (SQLServerStatement statementMoreMetadata = (SQLServerStatement ) connection .createStatement ();
1752
- SQLServerResultSet rsMoreMetaData = statementMoreMetadata .executeQueryInternal (metaDataQuery )) {
1753
- for (int i = 1 ; i <= destColumnCount ; ++i ) {
1754
- if (rsMoreMetaData .next ()) {
1755
- String bulkCopyEncryptionType = null ;
1756
- if (connection .getServerSupportsColumnEncryption ()) {
1757
- bulkCopyEncryptionType = rsMoreMetaData .getString ("encryption_type" );
1752
+ try (SQLServerStatement statementMoreMetadata = (SQLServerStatement ) connection .createStatement ();
1753
+ SQLServerResultSet rsMoreMetaData = statementMoreMetadata .executeQueryInternal (metaDataQuery )) {
1754
+ for (int i = 1 ; i <= destColumnCount ; ++i ) {
1755
+ if (rsMoreMetaData .next ()) {
1756
+ String bulkCopyEncryptionType = null ;
1757
+ if (connection .getServerSupportsColumnEncryption ()) {
1758
+ bulkCopyEncryptionType = rsMoreMetaData .getString ("encryption_type" );
1759
+ }
1760
+ destColumnMetadata .put (i , new BulkColumnMetaData (rs .getColumn (i ),
1761
+ rsMoreMetaData .getString ("collation_name" ), bulkCopyEncryptionType ));
1762
+ } else {
1763
+ destColumnMetadata .put (i , new BulkColumnMetaData (rs .getColumn (i )));
1758
1764
}
1759
- destColumnMetadata .put (i , new BulkColumnMetaData (rs .getColumn (i ),
1760
- rsMoreMetaData .getString ("collation_name" ), bulkCopyEncryptionType ));
1761
- } else {
1762
- destColumnMetadata .put (i , new BulkColumnMetaData (rs .getColumn (i )));
1763
1765
}
1764
1766
}
1767
+ } catch (SQLException e ) {
1768
+ // Unable to retrieve metadata for destination
1769
+ throw new SQLServerException (SQLServerException .getErrString ("R_unableRetrieveColMeta" ), e );
1770
+ } finally {
1771
+ if (null != rs ) {
1772
+ rs .close ();
1773
+ }
1774
+ if (null != stmt ) {
1775
+ stmt .close ();
1776
+ }
1765
1777
}
1766
- } catch (SQLException e ) {
1767
- // Unable to retrieve metadata for destination
1768
- throw new SQLServerException (SQLServerException .getErrString ("R_unableRetrieveColMeta" ), e );
1769
- } finally {
1770
- if (null != rs )
1771
- rs .close ();
1772
- if (null != stmt )
1773
- stmt .close ();
1774
1778
}
1775
1779
}
1776
1780
@@ -1779,41 +1783,43 @@ private void getDestinationMetadata() throws SQLServerException {
1779
1783
* BulkColumnMetaData object helps to access source metadata from the same place for both ResultSet and File.
1780
1784
*/
1781
1785
private void getSourceMetadata () throws SQLServerException {
1782
- srcColumnMetadata = new HashMap <>();
1783
- int currentColumn ;
1784
- if (null != sourceResultSet ) {
1785
- try {
1786
- srcColumnCount = sourceResultSetMetaData .getColumnCount ();
1787
- for (int i = 1 ; i <= srcColumnCount ; ++i ) {
1788
- srcColumnMetadata .put (i ,
1789
- new BulkColumnMetaData (sourceResultSetMetaData .getColumnName (i ),
1790
- (ResultSetMetaData .columnNoNulls != sourceResultSetMetaData .isNullable (i )),
1791
- sourceResultSetMetaData .getPrecision (i ), sourceResultSetMetaData .getScale (i ),
1792
- sourceResultSetMetaData .getColumnType (i ), null ));
1786
+ if (null == srcColumnMetadata || srcColumnMetadata .isEmpty ()) {
1787
+ srcColumnMetadata = new HashMap <>();
1788
+ int currentColumn ;
1789
+ if (null != sourceResultSet ) {
1790
+ try {
1791
+ srcColumnCount = sourceResultSetMetaData .getColumnCount ();
1792
+ for (int i = 1 ; i <= srcColumnCount ; ++i ) {
1793
+ srcColumnMetadata .put (i ,
1794
+ new BulkColumnMetaData (sourceResultSetMetaData .getColumnName (i ),
1795
+ (ResultSetMetaData .columnNoNulls != sourceResultSetMetaData .isNullable (i )),
1796
+ sourceResultSetMetaData .getPrecision (i ), sourceResultSetMetaData .getScale (i ),
1797
+ sourceResultSetMetaData .getColumnType (i ), null ));
1798
+ }
1799
+ } catch (SQLException e ) {
1800
+ // Unable to retrieve meta data for destination
1801
+ throw new SQLServerException (SQLServerException .getErrString ("R_unableRetrieveColMeta" ), e );
1793
1802
}
1794
- } catch (SQLException e ) {
1795
- // Unable to retrieve meta data for destination
1796
- throw new SQLServerException (SQLServerException .getErrString ("R_unableRetrieveColMeta" ), e );
1797
- }
1798
- } else if (null != serverBulkData ) {
1799
- Set <Integer > columnOrdinals = serverBulkData .getColumnOrdinals ();
1800
- if (null == columnOrdinals || columnOrdinals .isEmpty ()) {
1801
- throw new SQLServerException (SQLServerException .getErrString ("R_unableRetrieveColMeta" ), null );
1802
- } else {
1803
- srcColumnCount = columnOrdinals .size ();
1804
- for (Integer columnOrdinal : columnOrdinals ) {
1805
- currentColumn = columnOrdinal ;
1806
- srcColumnMetadata .put (currentColumn , new BulkColumnMetaData (
1807
- serverBulkData .getColumnName (currentColumn ), true ,
1808
- serverBulkData .getPrecision (currentColumn ), serverBulkData .getScale (currentColumn ),
1809
- serverBulkData .getColumnType (currentColumn ),
1810
- ((serverBulkData instanceof SQLServerBulkCSVFileRecord ) ? ((SQLServerBulkCSVFileRecord ) serverBulkData )
1811
- .getColumnDateTimeFormatter (currentColumn ) : null )));
1803
+ } else if (null != serverBulkData ) {
1804
+ Set <Integer > columnOrdinals = serverBulkData .getColumnOrdinals ();
1805
+ if (null == columnOrdinals || columnOrdinals .isEmpty ()) {
1806
+ throw new SQLServerException (SQLServerException .getErrString ("R_unableRetrieveColMeta" ), null );
1807
+ } else {
1808
+ srcColumnCount = columnOrdinals .size ();
1809
+ for (Integer columnOrdinal : columnOrdinals ) {
1810
+ currentColumn = columnOrdinal ;
1811
+ srcColumnMetadata .put (currentColumn , new BulkColumnMetaData (
1812
+ serverBulkData .getColumnName (currentColumn ), true ,
1813
+ serverBulkData .getPrecision (currentColumn ), serverBulkData .getScale (currentColumn ),
1814
+ serverBulkData .getColumnType (currentColumn ),
1815
+ ((serverBulkData instanceof SQLServerBulkCSVFileRecord ) ? ((SQLServerBulkCSVFileRecord ) serverBulkData )
1816
+ .getColumnDateTimeFormatter (currentColumn ) : null )));
1817
+ }
1812
1818
}
1819
+ } else {
1820
+ // Unable to retrieve meta data for source
1821
+ throw new SQLServerException (SQLServerException .getErrString ("R_unableRetrieveColMeta" ), null );
1813
1822
}
1814
- } else {
1815
- // Unable to retrieve meta data for source
1816
- throw new SQLServerException (SQLServerException .getErrString ("R_unableRetrieveColMeta" ), null );
1817
1823
}
1818
1824
}
1819
1825
0 commit comments