Skip to content

Commit

Permalink
Implement checkDuplicateColumnName to check duplicate columns
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbae committed Sep 26, 2017
1 parent 05d0388 commit e77a046
Showing 1 changed file with 25 additions and 2 deletions.
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;
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) {
//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

0 comments on commit e77a046

Please sign in to comment.