From 6fa6c1dd1554e9cba4edb6ab9b7b22c8eecbf64a Mon Sep 17 00:00:00 2001 From: Terry Chow Date: Fri, 6 Oct 2023 15:18:22 -0700 Subject: [PATCH 1/2] Fixed repeated bulkcopy metadata query --- .../sqlserver/jdbc/SQLServerBulkCopy.java | 146 +++++++++--------- 1 file changed, 75 insertions(+), 71 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java index 48160ee70..30ba07c70 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java @@ -1724,53 +1724,55 @@ private void getDestinationMetadata() throws SQLServerException { SQLServerStatement stmt = null; String metaDataQuery = null; - try { - if (null != destinationTableMetadata) { - rs = (SQLServerResultSet) destinationTableMetadata; - } else { - stmt = (SQLServerStatement) connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, - ResultSet.CONCUR_READ_ONLY, connection.getHoldability(), stmtColumnEncriptionSetting); + if (null == destColumnMetadata || destColumnMetadata.isEmpty()) { + try { + if (null != destinationTableMetadata) { + rs = (SQLServerResultSet) destinationTableMetadata; + } else { + stmt = (SQLServerStatement) connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, + ResultSet.CONCUR_READ_ONLY, connection.getHoldability(), stmtColumnEncriptionSetting); - // Get destination metadata - rs = stmt.executeQueryInternal( - "sp_executesql N'SET FMTONLY ON SELECT * FROM " + escapedDestinationTableName + " '"); - } + // Get destination metadata + rs = stmt.executeQueryInternal( + "sp_executesql N'SET FMTONLY ON SELECT * FROM " + escapedDestinationTableName + " '"); + } - destColumnCount = rs.getMetaData().getColumnCount(); - destColumnMetadata = new HashMap<>(); - destCekTable = rs.getCekTable(); + destColumnCount = rs.getMetaData().getColumnCount(); + destColumnMetadata = new HashMap<>(); + destCekTable = rs.getCekTable(); - if (!connection.getServerSupportsColumnEncryption()) { - metaDataQuery = "select collation_name from sys.columns where " + "object_id=OBJECT_ID('" - + escapedDestinationTableName + "') " + "order by column_id ASC"; - } else { - metaDataQuery = "select collation_name, encryption_type from sys.columns where " - + "object_id=OBJECT_ID('" + escapedDestinationTableName + "') " + "order by column_id ASC"; - } + if (!connection.getServerSupportsColumnEncryption()) { + metaDataQuery = "select collation_name from sys.columns where " + "object_id=OBJECT_ID('" + + escapedDestinationTableName + "') " + "order by column_id ASC"; + } else { + metaDataQuery = "select collation_name, encryption_type from sys.columns where " + + "object_id=OBJECT_ID('" + escapedDestinationTableName + "') " + "order by column_id ASC"; + } - try (SQLServerStatement statementMoreMetadata = (SQLServerStatement) connection.createStatement(); - SQLServerResultSet rsMoreMetaData = statementMoreMetadata.executeQueryInternal(metaDataQuery)) { - for (int i = 1; i <= destColumnCount; ++i) { - if (rsMoreMetaData.next()) { - String bulkCopyEncryptionType = null; - if (connection.getServerSupportsColumnEncryption()) { - bulkCopyEncryptionType = rsMoreMetaData.getString("encryption_type"); + try (SQLServerStatement statementMoreMetadata = (SQLServerStatement) connection.createStatement(); + SQLServerResultSet rsMoreMetaData = statementMoreMetadata.executeQueryInternal(metaDataQuery)) { + for (int i = 1; i <= destColumnCount; ++i) { + if (rsMoreMetaData.next()) { + String bulkCopyEncryptionType = null; + if (connection.getServerSupportsColumnEncryption()) { + bulkCopyEncryptionType = rsMoreMetaData.getString("encryption_type"); + } + destColumnMetadata.put(i, new BulkColumnMetaData(rs.getColumn(i), + rsMoreMetaData.getString("collation_name"), bulkCopyEncryptionType)); + } else { + destColumnMetadata.put(i, new BulkColumnMetaData(rs.getColumn(i))); } - destColumnMetadata.put(i, new BulkColumnMetaData(rs.getColumn(i), - rsMoreMetaData.getString("collation_name"), bulkCopyEncryptionType)); - } else { - destColumnMetadata.put(i, new BulkColumnMetaData(rs.getColumn(i))); } } + } catch (SQLException e) { + // Unable to retrieve metadata for destination + throw new SQLServerException(SQLServerException.getErrString("R_unableRetrieveColMeta"), e); + } finally { + if (null != rs) + rs.close(); + if (null != stmt) + stmt.close(); } - } catch (SQLException e) { - // Unable to retrieve metadata for destination - throw new SQLServerException(SQLServerException.getErrString("R_unableRetrieveColMeta"), e); - } finally { - if (null != rs) - rs.close(); - if (null != stmt) - stmt.close(); } } @@ -1779,41 +1781,43 @@ private void getDestinationMetadata() throws SQLServerException { * BulkColumnMetaData object helps to access source metadata from the same place for both ResultSet and File. */ private void getSourceMetadata() throws SQLServerException { - srcColumnMetadata = new HashMap<>(); - int currentColumn; - if (null != sourceResultSet) { - try { - srcColumnCount = sourceResultSetMetaData.getColumnCount(); - for (int i = 1; i <= srcColumnCount; ++i) { - srcColumnMetadata.put(i, - new BulkColumnMetaData(sourceResultSetMetaData.getColumnName(i), - (ResultSetMetaData.columnNoNulls != sourceResultSetMetaData.isNullable(i)), - sourceResultSetMetaData.getPrecision(i), sourceResultSetMetaData.getScale(i), - sourceResultSetMetaData.getColumnType(i), null)); + if (null == srcColumnMetadata || srcColumnMetadata.isEmpty()) { + srcColumnMetadata = new HashMap<>(); + int currentColumn; + if (null != sourceResultSet) { + try { + srcColumnCount = sourceResultSetMetaData.getColumnCount(); + for (int i = 1; i <= srcColumnCount; ++i) { + srcColumnMetadata.put(i, + new BulkColumnMetaData(sourceResultSetMetaData.getColumnName(i), + (ResultSetMetaData.columnNoNulls != sourceResultSetMetaData.isNullable(i)), + sourceResultSetMetaData.getPrecision(i), sourceResultSetMetaData.getScale(i), + sourceResultSetMetaData.getColumnType(i), null)); + } + } catch (SQLException e) { + // Unable to retrieve meta data for destination + throw new SQLServerException(SQLServerException.getErrString("R_unableRetrieveColMeta"), e); } - } catch (SQLException e) { - // Unable to retrieve meta data for destination - throw new SQLServerException(SQLServerException.getErrString("R_unableRetrieveColMeta"), e); - } - } else if (null != serverBulkData) { - Set columnOrdinals = serverBulkData.getColumnOrdinals(); - if (null == columnOrdinals || columnOrdinals.isEmpty()) { - throw new SQLServerException(SQLServerException.getErrString("R_unableRetrieveColMeta"), null); - } else { - srcColumnCount = columnOrdinals.size(); - for (Integer columnOrdinal : columnOrdinals) { - currentColumn = columnOrdinal; - srcColumnMetadata.put(currentColumn, new BulkColumnMetaData( - serverBulkData.getColumnName(currentColumn), true, - serverBulkData.getPrecision(currentColumn), serverBulkData.getScale(currentColumn), - serverBulkData.getColumnType(currentColumn), - ((serverBulkData instanceof SQLServerBulkCSVFileRecord) ? ((SQLServerBulkCSVFileRecord) serverBulkData) - .getColumnDateTimeFormatter(currentColumn) : null))); + } else if (null != serverBulkData) { + Set columnOrdinals = serverBulkData.getColumnOrdinals(); + if (null == columnOrdinals || columnOrdinals.isEmpty()) { + throw new SQLServerException(SQLServerException.getErrString("R_unableRetrieveColMeta"), null); + } else { + srcColumnCount = columnOrdinals.size(); + for (Integer columnOrdinal : columnOrdinals) { + currentColumn = columnOrdinal; + srcColumnMetadata.put(currentColumn, new BulkColumnMetaData( + serverBulkData.getColumnName(currentColumn), true, + serverBulkData.getPrecision(currentColumn), serverBulkData.getScale(currentColumn), + serverBulkData.getColumnType(currentColumn), + ((serverBulkData instanceof SQLServerBulkCSVFileRecord) ? ((SQLServerBulkCSVFileRecord) serverBulkData) + .getColumnDateTimeFormatter(currentColumn) : null))); + } } + } else { + // Unable to retrieve meta data for source + throw new SQLServerException(SQLServerException.getErrString("R_unableRetrieveColMeta"), null); } - } else { - // Unable to retrieve meta data for source - throw new SQLServerException(SQLServerException.getErrString("R_unableRetrieveColMeta"), null); } } From 2084e80aee10e83c03b0e7638bf9d3396bd25d89 Mon Sep 17 00:00:00 2001 From: Terry Chow Date: Tue, 10 Oct 2023 17:06:05 -0700 Subject: [PATCH 2/2] Brackets around if statements --- .../com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java index 30ba07c70..c1e459283 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java @@ -1768,10 +1768,12 @@ private void getDestinationMetadata() throws SQLServerException { // Unable to retrieve metadata for destination throw new SQLServerException(SQLServerException.getErrString("R_unableRetrieveColMeta"), e); } finally { - if (null != rs) + if (null != rs) { rs.close(); - if (null != stmt) + } + if (null != stmt) { stmt.close(); + } } } }