Skip to content

Commit

Permalink
Refactoring the logic for checking duplicate column into Util class a…
Browse files Browse the repository at this point in the history
…nd adding a test case for this
  • Loading branch information
peterbae committed Oct 6, 2017
1 parent 8c0ebf2 commit adf10ea
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 47 deletions.
20 changes: 2 additions & 18 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,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
checkDuplicateColumnName(columnName);
Util.checkDuplicateColumnName(columnName, columnNames);
columnMetadata.put(columnCount++, new SQLServerDataColumn(columnName, sqlType));
}

Expand All @@ -93,26 +93,10 @@ public synchronized void addColumnMetadata(String columnName,
*/
public synchronized void addColumnMetadata(SQLServerDataColumn column) throws SQLServerException {
// column names must be unique
checkDuplicateColumnName(column.columnName);
Util.checkDuplicateColumnName(column.columnName, columnNames);
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 {
//columnList.add will return false if the same column name already exists
if (!columnNames.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
7 changes: 1 addition & 6 deletions src/main/java/com/microsoft/sqlserver/jdbc/TVP.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,7 @@ void populateMetadataFromDataRecord() throws SQLServerException {
throw new SQLServerException(SQLServerException.getErrString("R_TVPEmptyMetadata"), null);
}
for (int i = 0; i < sourceRecord.getColumnCount(); i++) {
//columnList.add will return false if the same column name already exists
if (!columnNames.add(sourceRecord.getColumnMetaData(i + 1).columnName)) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_TVPDuplicateColumnName"));
Object[] msgArgs = {sourceRecord.getColumnMetaData(i + 1).columnName};
throw new SQLServerException(null, form.format(msgArgs), null, 0, false);
}
Util.checkDuplicateColumnName(sourceRecord.getColumnMetaData(i + 1).columnName, columnNames);

// Make a copy here as we do not want to change user's metadata.
SQLServerMetaData metaData = new SQLServerMetaData(sourceRecord.getColumnMetaData(i + 1));
Expand Down
39 changes: 16 additions & 23 deletions src/main/java/com/microsoft/sqlserver/jdbc/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.LogManager;
Expand Down Expand Up @@ -557,32 +558,24 @@ static String escapeSQLId(String inID) {
outID.append(']');
return outID.toString();
}

/*

/**
* Checks if duplicate columns exists, in O(n) time.
*
* @param columnName
* the name of the column
* @throws SQLServerException
* when a duplicate column exists
*/
static void checkDuplicateColumnName(String columnName,
Map<Integer, ?> columnMetadata) throws SQLServerException {
if (columnMetadata.get(0) instanceof SQLServerMetaData) {
for (Entry<Integer, ?> entry : columnMetadata.entrySet()) {
SQLServerMetaData value = (SQLServerMetaData) entry.getValue();
if (value.columnName.equals(columnName)) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_TVPDuplicateColumnName"));
Object[] msgArgs = {columnName};
throw new SQLServerException(null, form.format(msgArgs), null, 0, false);
}
}
}
else if (columnMetadata.get(0) instanceof SQLServerDataColumn) {
for (Entry<Integer, ?> entry : columnMetadata.entrySet()) {
SQLServerDataColumn value = (SQLServerDataColumn) entry.getValue();
if (value.columnName.equals(columnName)) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_TVPDuplicateColumnName"));
Object[] msgArgs = {columnName};
throw new SQLServerException(null, form.format(msgArgs), null, 0, false);
}
}
Set<String> columnNames) throws SQLServerException {
//columnList.add will return false if the same column name already exists
if (!columnNames.add(columnName)) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_TVPDuplicateColumnName"));
Object[] msgArgs = {columnName};
throw new SQLServerException(null, form.format(msgArgs), null, 0, false);
}
}
*/

/**
* Reads a UNICODE string from byte buffer at offset (up to byteLength).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,23 @@ public void testIntStoredProcedure() throws SQLServerException {
Cstatement.close();
}
}

/**
* Test for allowing duplicate columns
*
* @throws SQLServerException
*/
@Test
public void testDuplicateColumn() throws SQLServerException {
tvp = new SQLServerDataTable();
tvp.addColumnMetadata("c1", microsoft.sql.Types.SQL_VARIANT);
tvp.addColumnMetadata("c2", microsoft.sql.Types.SQL_VARIANT);
try {
tvp.addColumnMetadata("c2", microsoft.sql.Types.SQL_VARIANT);
} catch (SQLServerException e) {
assertEquals(e.getMessage(), "A column name c2 already belongs to this SQLServerDataTable.");
}
}

private static String[] createNumericValues() {
Boolean C1_BIT;
Expand Down

0 comments on commit adf10ea

Please sign in to comment.