Skip to content

Commit

Permalink
Fixed bulkcopy metadata query to make use of cached data if available (
Browse files Browse the repository at this point in the history
…#2231)

* Fixed repeated bulkcopy metadata query

* Brackets around if statements
  • Loading branch information
tkyc authored Oct 11, 2023
1 parent c3d50c2 commit 5ac4e61
Showing 1 changed file with 77 additions and 71 deletions.
148 changes: 77 additions & 71 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java
Original file line number Diff line number Diff line change
Expand Up @@ -1724,53 +1724,57 @@ 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();
}
}

Expand All @@ -1779,41 +1783,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<Integer> 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<Integer> 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);
}
}

Expand Down

0 comments on commit 5ac4e61

Please sign in to comment.