Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add | Implement hashCode() and equals() APIs for SQLServerDataTable and SQLServerDataColumn #1025

Merged
merged 8 commits into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
6 changes: 3 additions & 3 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4596,7 +4596,7 @@ void writeTVPRows(TVP value) throws SQLServerException {
writeBytes(cachedTVPHeaders.array(), 0, ((Buffer) cachedTVPHeaders).position());
}

Object[] rowData = value.getRowData();
List<Object> rowData = value.getRowData();

// ROW
writeByte((byte) TDS.TVP_ROW);
Expand All @@ -4618,8 +4618,8 @@ void writeTVPRows(TVP value) throws SQLServerException {
if (null != rowData) {
// if rowData has value for the current column, retrieve it. If not, current column will stay
// null.
if (rowData.length > currentColumn) {
currentObject = rowData[currentColumn];
if (rowData.size() > currentColumn) {
currentObject = rowData.get(currentColumn);
if (null != currentObject) {
currentColumnStringValue = String.valueOf(currentObject);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package com.microsoft.sqlserver.jdbc;

import java.util.List;

/**
* Provides an interface to create classes that read in data from any source (such as a file) and allow a structured
* type to be sent to SQL Server tables.
Expand Down Expand Up @@ -34,7 +36,7 @@ public interface ISQLServerDataRecord {
*
* @return The data for the row.
*/
public Object[] getRowData();
public List<Object> getRowData();

/**
* Advances to the next data row.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,34 @@ public String getColumnName() {
public int getColumnType() {
return javaSqlType;
}

@Override
public int hashCode() {
int hash = 7;
hash = 31 * hash + javaSqlType;
hash = 31 * hash + precision;
hash = 31 * hash + scale;
hash = 31 * hash + numberOfDigitsIntegerPart;
hash = 31 * hash + (null != columnName ? columnName.hashCode() : 0);
return hash;
}

public boolean equals(Object object) {
cheenamalhotra marked this conversation as resolved.
Show resolved Hide resolved
if (this == object) {
return true;
}

if (null != object && object instanceof SQLServerDataColumn) {
SQLServerDataColumn aSQLServerDataColumn = (SQLServerDataColumn) object;
if (hashCode() == aSQLServerDataColumn.hashCode()) {
// Compare objects to avoid collision
return ((null == columnName && null == aSQLServerDataColumn.columnName
|| columnName.equalsIgnoreCase(aSQLServerDataColumn.columnName))
&& javaSqlType == aSQLServerDataColumn.javaSqlType
&& numberOfDigitsIntegerPart == aSQLServerDataColumn.numberOfDigitsIntegerPart
&& precision == aSQLServerDataColumn.precision && scale == aSQLServerDataColumn.scale);
}
}
return false;
}
}
80 changes: 60 additions & 20 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
Expand All @@ -28,8 +30,7 @@ public final class SQLServerDataTable {
int columnCount = 0;
Map<Integer, SQLServerDataColumn> columnMetadata = null;
Set<String> columnNames = null;
Map<Integer, Object[]> rows = null;

Map<Integer, List<Object>> rows = null;
private String tvpName = null;

/**
Expand All @@ -53,6 +54,7 @@ public synchronized void clear() {
rowCount = 0;
columnCount = 0;
columnMetadata.clear();
columnNames.clear();
rows.clear();
}

Expand All @@ -61,7 +63,7 @@ public synchronized void clear() {
*
* @return an iterator on the rows of the data table.
*/
public synchronized Iterator<Entry<Integer, Object[]>> getIterator() {
public synchronized Iterator<Entry<Integer, List<Object>>> getIterator() {
if ((null != rows) && (null != rows.entrySet())) {
return rows.entrySet().iterator();
}
Expand Down Expand Up @@ -118,7 +120,7 @@ public synchronized void addRow(Object... values) throws SQLServerException {
}

Iterator<Entry<Integer, SQLServerDataColumn>> columnsIterator = columnMetadata.entrySet().iterator();
Object[] rowValues = new Object[columnCount];
List<Object> rowValues = new LinkedList<>();
int currentColumn = 0;
while (columnsIterator.hasNext()) {
Object val = null;
Expand Down Expand Up @@ -153,29 +155,33 @@ public synchronized void addRow(Object... values) throws SQLServerException {
* @throws SQLServerException
* when an error occurs
*/
private void internalAddrow(JDBCType jdbcType, Object val, Object[] rowValues,
private void internalAddrow(JDBCType jdbcType, Object val, List<Object> rowValues,
Map.Entry<Integer, SQLServerDataColumn> pair) throws SQLServerException {

SQLServerDataColumn currentColumnMetadata = pair.getValue();
int key = pair.getKey();
rowValues.add(key, val);

boolean isColumnMetadataUpdated = false;
boolean bValueNull;
int nValueLen;

switch (jdbcType) {
case BIGINT:
rowValues[pair.getKey()] = (null == val) ? null : Long.parseLong(val.toString());
rowValues.set(key, (null == val) ? null : Long.parseLong(val.toString()));
break;

case BIT:
rowValues[pair.getKey()] = (null == val) ? null : Boolean.parseBoolean(val.toString());
rowValues.set(key, (null == val) ? null : Boolean.parseBoolean(val.toString()));;
break;

case INTEGER:
rowValues[pair.getKey()] = (null == val) ? null : Integer.parseInt(val.toString());
rowValues.set(key, (null == val) ? null : Integer.parseInt(val.toString()));
break;

case SMALLINT:
case TINYINT:
rowValues[pair.getKey()] = (null == val) ? null : Short.parseShort(val.toString());
rowValues.set(key, (null == val) ? null : Short.parseShort(val.toString()));
break;

case DECIMAL:
Expand Down Expand Up @@ -208,16 +214,16 @@ private void internalAddrow(JDBCType jdbcType, Object val, Object[] rowValues,
columnMetadata.put(pair.getKey(), currentColumnMetadata);
}
}
rowValues[pair.getKey()] = bd;
rowValues.set(key, bd);
break;

case DOUBLE:
rowValues[pair.getKey()] = (null == val) ? null : Double.parseDouble(val.toString());
rowValues.set(key, (null == val) ? null : Double.parseDouble(val.toString()));
break;

case FLOAT:
case REAL:
rowValues[pair.getKey()] = (null == val) ? null : Float.parseFloat(val.toString());
rowValues.set(key, (null == val) ? null : Float.parseFloat(val.toString()));
break;

case TIMESTAMP_WITH_TIMEZONE:
Expand All @@ -233,18 +239,18 @@ private void internalAddrow(JDBCType jdbcType, Object val, Object[] rowValues,
// DataTypes.SHORT_VARTYPE_MAX_BYTES

if (null == val)
rowValues[pair.getKey()] = null;
rowValues.set(key, null);
// java.sql.Date, java.sql.Time and java.sql.Timestamp are subclass of java.util.Date
else if (val instanceof java.util.Date)
rowValues[pair.getKey()] = val.toString();
rowValues.set(key, val.toString());
else if (val instanceof microsoft.sql.DateTimeOffset)
rowValues[pair.getKey()] = val.toString();
rowValues.set(key, val.toString());
else if (val instanceof OffsetDateTime)
rowValues[pair.getKey()] = val.toString();
rowValues.set(key, val.toString());
else if (val instanceof OffsetTime)
rowValues[pair.getKey()] = val.toString();
rowValues.set(key, val.toString());
else
rowValues[pair.getKey()] = (String) val;
rowValues.set(key, (String) val);
break;

case BINARY:
Expand All @@ -257,7 +263,7 @@ else if (val instanceof OffsetTime)
currentColumnMetadata.precision = nValueLen;
columnMetadata.put(pair.getKey(), currentColumnMetadata);
}
rowValues[pair.getKey()] = (bValueNull) ? null : (byte[]) val;
rowValues.set(key, (bValueNull) ? null : (byte[]) val);

break;

Expand All @@ -277,7 +283,7 @@ else if (val instanceof OffsetTime)
currentColumnMetadata.precision = nValueLen;
columnMetadata.put(pair.getKey(), currentColumnMetadata);
}
rowValues[pair.getKey()] = (bValueNull) ? null : (String) val;
rowValues.set(key, (bValueNull) ? null : (String) val);
break;
case SQL_VARIANT:
JDBCType internalJDBCType;
Expand Down Expand Up @@ -324,4 +330,38 @@ public String getTvpName() {
public void setTvpName(String tvpName) {
this.tvpName = tvpName;
}

@Override
public int hashCode() {
int hash = 7;
hash = 31 * hash + rowCount;
hash = 31 * hash + columnCount;
hash = 31 * hash + (null != columnMetadata ? columnMetadata.hashCode() : 0);
hash = 31 * hash + (null != columnNames ? columnNames.hashCode() : 0);
hash = 31 * hash + (null != rows ? rows.hashCode() : 0);
hash = 31 * hash + (null != tvpName ? tvpName.hashCode() : 0);
return hash;
}

public boolean equals(Object object) {
if (this == object) {
return true;
}

if (null != object && object instanceof SQLServerDataTable) {
SQLServerDataTable aSQLServerDataTable = (SQLServerDataTable) object;
if (hashCode() == aSQLServerDataTable.hashCode()) {

// Compare objects to avoid collision
boolean equalColumnMetadata = columnMetadata.equals(aSQLServerDataTable.columnMetadata);
boolean equalColumnNames = columnNames.equals(aSQLServerDataTable.columnNames);
boolean equalRowData = rows.equals(aSQLServerDataTable.rows);

return (rowCount == aSQLServerDataTable.rowCount && columnCount == aSQLServerDataTable.columnCount
&& tvpName == aSQLServerDataTable.tvpName && equalColumnMetadata && equalColumnNames
&& equalRowData);
}
}
return false;
}
}
14 changes: 8 additions & 6 deletions src/main/java/com/microsoft/sqlserver/jdbc/TVP.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
Expand Down Expand Up @@ -50,7 +52,7 @@ class TVP {
ResultSet sourceResultSet = null;
SQLServerDataTable sourceDataTable = null;
Map<Integer, SQLServerMetaData> columnMetadata = null;
Iterator<Entry<Integer, Object[]>> sourceDataTableRowIterator = null;
Iterator<Entry<Integer, List<Object>>> sourceDataTableRowIterator = null;
ISQLServerDataRecord sourceRecord = null;
TVPType tvpType = null;
Set<String> columnNames = null;
Expand Down Expand Up @@ -109,28 +111,28 @@ boolean isNull() {
return (TVPType.Null == tvpType);
}

Object[] getRowData() throws SQLServerException {
List<Object> getRowData() throws SQLServerException {
if (TVPType.ResultSet == tvpType) {
int colCount = columnMetadata.size();
Object[] rowData = new Object[colCount];
List<Object> rowData = new LinkedList<>();
for (int i = 0; i < colCount; i++) {
try {
/*
* for Time types, getting TimeStamp instead of Time, because this value will be converted to String
* later on. If the value is a time object, the millisecond would be removed.
*/
if (java.sql.Types.TIME == sourceResultSet.getMetaData().getColumnType(i + 1)) {
rowData[i] = sourceResultSet.getTimestamp(i + 1);
rowData.add(i,sourceResultSet.getTimestamp(i + 1));
} else {
rowData[i] = sourceResultSet.getObject(i + 1);
rowData.add(i,sourceResultSet.getObject(i + 1));
}
} catch (SQLException e) {
throw new SQLServerException(SQLServerException.getErrString("R_unableRetrieveSourceData"), e);
}
}
return rowData;
} else if (TVPType.SQLServerDataTable == tvpType) {
Map.Entry<Integer, Object[]> rowPair = sourceDataTableRowIterator.next();
Map.Entry<Integer, List<Object>> rowPair = sourceDataTableRowIterator.next();
return rowPair.getValue();
} else
return sourceRecord.getRowData();
Expand Down
Loading