1313import java .time .OffsetDateTime ;
1414import java .time .OffsetTime ;
1515import java .util .HashMap ;
16+ import java .util .HashSet ;
1617import java .util .Iterator ;
1718import java .util .LinkedHashMap ;
1819import java .util .Map ;
1920import java .util .Map .Entry ;
21+ import java .util .Set ;
2022import java .util .UUID ;
2123
2224public final class SQLServerDataTable {
2325
2426 int rowCount = 0 ;
2527 int columnCount = 0 ;
2628 Map <Integer , SQLServerDataColumn > columnMetadata = null ;
29+ Set <String > columnList = null ;
2730 Map <Integer , Object []> rows = null ;
2831
2932 private String tvpName = null ;
@@ -37,6 +40,7 @@ public final class SQLServerDataTable {
3740 // Name used in CREATE TYPE
3841 public SQLServerDataTable () throws SQLServerException {
3942 columnMetadata = new LinkedHashMap <>();
43+ columnList = new HashSet <>();
4044 rows = new HashMap <>();
4145 }
4246
@@ -75,7 +79,7 @@ public synchronized Iterator<Entry<Integer, Object[]>> getIterator() {
7579 public synchronized void addColumnMetadata (String columnName ,
7680 int sqlType ) throws SQLServerException {
7781 // column names must be unique
78- Util . checkDuplicateColumnName (columnName , columnMetadata );
82+ checkDuplicateColumnName (columnName );
7983 columnMetadata .put (columnCount ++, new SQLServerDataColumn (columnName , sqlType ));
8084 }
8185
@@ -89,10 +93,29 @@ public synchronized void addColumnMetadata(String columnName,
8993 */
9094 public synchronized void addColumnMetadata (SQLServerDataColumn column ) throws SQLServerException {
9195 // column names must be unique
92- Util . checkDuplicateColumnName (column .columnName , columnMetadata );
96+ checkDuplicateColumnName (column .columnName );
9397 columnMetadata .put (columnCount ++, column );
9498 }
9599
100+ /**
101+ * Checks if duplicate columns exists, in O(n) time.
102+ *
103+ * @param columnName
104+ * the name of the column
105+ * @throws SQLServerException
106+ * when a duplicate column exists
107+ */
108+ private void checkDuplicateColumnName (String columnName ) throws SQLServerException {
109+ if (null != columnList ) {
110+ //columnList.add will return false if the same column name already exists
111+ if (!columnList .add (columnName )) {
112+ MessageFormat form = new MessageFormat (SQLServerException .getErrString ("R_TVPDuplicateColumnName" ));
113+ Object [] msgArgs = {columnName };
114+ throw new SQLServerException (null , form .format (msgArgs ), null , 0 , false );
115+ }
116+ }
117+ }
118+
96119 /**
97120 * Adds one row of data to the data table.
98121 *
0 commit comments