Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;

public final class SQLServerDataTable {

int rowCount = 0;
int columnCount = 0;
Map<Integer, SQLServerDataColumn> columnMetadata = null;
Set<String> columnList = null;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think naming this columNames would be clearer

Map<Integer, Object[]> rows = null;

private String tvpName = null;
Expand All @@ -37,6 +40,7 @@ public final class SQLServerDataTable {
// Name used in CREATE TYPE
public SQLServerDataTable() throws SQLServerException {
columnMetadata = new LinkedHashMap<>();
columnList = new HashSet<>();
rows = new HashMap<>();
}

Expand Down Expand Up @@ -75,7 +79,7 @@ public synchronized Iterator<Entry<Integer, Object[]>> getIterator() {
public synchronized void addColumnMetadata(String columnName,
int sqlType) throws SQLServerException {
// column names must be unique
Util.checkDuplicateColumnName(columnName, columnMetadata);
checkDuplicateColumnName(columnName);
columnMetadata.put(columnCount++, new SQLServerDataColumn(columnName, sqlType));
}

Expand All @@ -89,10 +93,29 @@ public synchronized void addColumnMetadata(String columnName,
*/
public synchronized void addColumnMetadata(SQLServerDataColumn column) throws SQLServerException {
// column names must be unique
Util.checkDuplicateColumnName(column.columnName, columnMetadata);
checkDuplicateColumnName(column.columnName);
columnMetadata.put(columnCount++, column);
}

/**
* Checks if duplicate columns exists, in O(n) time.
*
* @param columnName
* the name of the column
* @throws SQLServerException
* when a duplicate column exists
*/
private void checkDuplicateColumnName(String columnName) throws SQLServerException {
if (null != columnList) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather an NPE than silently fail to check dupes if someone messes up null handling.

Better IMO would be defining the local variable like Set<String> columnNames = new HashSet<>(); and omitting this check, but I don't know if that'd violate the project's coding style.

//columnList.add will return false if the same column name already exists
if (!columnList.add(columnName)) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_TVPDuplicateColumnName"));
Object[] msgArgs = {columnName};
throw new SQLServerException(null, form.format(msgArgs), null, 0, false);
}
}
}

/**
* Adds one row of data to the data table.
*
Expand Down