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

Replace for and while loops with foeach loops #421

Merged
merged 1 commit into from Aug 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 9 additions & 12 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Expand Up @@ -2473,7 +2473,7 @@ private void findSocketUsingJavaNIO(InetAddress[] inetAddrs,
try {
selector = Selector.open();

for (int i = 0; i < inetAddrs.length; i++) {
for (InetAddress inetAddr : inetAddrs) {
SocketChannel sChannel = SocketChannel.open();
socketChannels.add(sChannel);

Expand All @@ -2484,10 +2484,10 @@ private void findSocketUsingJavaNIO(InetAddress[] inetAddrs,
int ops = SelectionKey.OP_CONNECT;
SelectionKey key = sChannel.register(selector, ops);

sChannel.connect(new InetSocketAddress(inetAddrs[i], portNumber));
sChannel.connect(new InetSocketAddress(inetAddr, portNumber));

if (logger.isLoggable(Level.FINER))
logger.finer(this.toString() + " initiated connection to address: " + inetAddrs[i] + ", portNumber: " + portNumber);
logger.finer(this.toString() + " initiated connection to address: " + inetAddr + ", portNumber: " + portNumber);
}

long timerNow = System.currentTimeMillis();
Expand Down Expand Up @@ -5035,13 +5035,11 @@ void writeTVPColumnMetaData(TVP value) throws SQLServerException {
writeShort((short) value.getTVPColumnCount());

Map<Integer, SQLServerMetaData> columnMetadata = value.getColumnMetadata();
Iterator<Entry<Integer, SQLServerMetaData>> columnsIterator = columnMetadata.entrySet().iterator();
/*
* TypeColumnMetaData = UserType Flags TYPE_INFO ColName ;
*/

while (columnsIterator.hasNext()) {
Map.Entry<Integer, SQLServerMetaData> pair = columnsIterator.next();
for (Entry<Integer, SQLServerMetaData> pair : columnMetadata.entrySet()) {
JDBCType jdbcType = JDBCType.of(pair.getValue().javaSqlType);
boolean useServerDefault = pair.getValue().useServerDefault;
// ULONG ; UserType of column
Expand Down Expand Up @@ -5114,13 +5112,12 @@ void writeTVPColumnMetaData(TVP value) throws SQLServerException {
writeByte(TDSType.NVARCHAR.byteValue());
isShortValue = (2L * pair.getValue().precision) <= DataTypes.SHORT_VARTYPE_MAX_BYTES;
// Use PLP encoding on Yukon and later with long values
if (!isShortValue) // PLP
if (!isShortValue) // PLP
{
// Handle Yukon v*max type header here.
writeShort((short) 0xFFFF);
con.getDatabaseCollation().writeCollation(this);
}
else // non PLP
} else // non PLP
{
writeShort((short) DataTypes.SHORT_VARTYPE_MAX_BYTES);
con.getDatabaseCollation().writeCollation(this);
Expand All @@ -5134,16 +5131,16 @@ void writeTVPColumnMetaData(TVP value) throws SQLServerException {
writeByte(TDSType.BIGVARBINARY.byteValue());
isShortValue = pair.getValue().precision <= DataTypes.SHORT_VARTYPE_MAX_BYTES;
// Use PLP encoding on Yukon and later with long values
if (!isShortValue) // PLP
if (!isShortValue) // PLP
// Handle Yukon v*max type header here.
writeShort((short) 0xFFFF);
else // non PLP
else // non PLP
writeShort((short) DataTypes.SHORT_VARTYPE_MAX_BYTES);
break;
case SQL_VARIANT:
writeByte(TDSType.SQL_VARIANT.byteValue());
writeInt(TDS.SQL_VARIANT_LENGTH);// write length of sql variant 8009

break;

default:
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/com/microsoft/sqlserver/jdbc/KerbCallback.java
Expand Up @@ -42,18 +42,15 @@ public String getUsernameRequested() {

@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (int i = 0; i < callbacks.length; i++) {
Callback callback = callbacks[i];
for (Callback callback : callbacks) {
if (callback instanceof NameCallback) {
usernameRequested = getAnyOf(callback, con.activeConnectionProperties, "user", SQLServerDriverStringProperty.USER.name());
((NameCallback) callback).setName(usernameRequested);
}
else if (callback instanceof PasswordCallback) {
} else if (callback instanceof PasswordCallback) {
String password = getAnyOf(callback, con.activeConnectionProperties, "password", SQLServerDriverStringProperty.PASSWORD.name());
((PasswordCallback) callbacks[i]).setPassword(password.toCharArray());
((PasswordCallback) callback).setPassword(password.toCharArray());

}
else {
} else {
throw new UnsupportedCallbackException(callback, "Unrecognized Callback type: " + callback.getClass());
}
}
Expand Down
Expand Up @@ -16,6 +16,7 @@
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Types;
import java.text.DecimalFormat;
import java.text.MessageFormat;
import java.time.OffsetDateTime;
Expand Down Expand Up @@ -525,9 +526,7 @@ public Object[] getRowData() throws SQLServerException {
// Cannot go directly from String[] to Object[] and expect it to act as an array.
Object[] dataRow = new Object[data.length];

Iterator<Entry<Integer, ColumnMetadata>> it = columnMetadata.entrySet().iterator();
while (it.hasNext()) {
Entry<Integer, ColumnMetadata> pair = it.next();
for (Entry<Integer, ColumnMetadata> pair : columnMetadata.entrySet()) {
ColumnMetadata cm = pair.getValue();

// Reading a column not available in csv
Expand Down Expand Up @@ -556,69 +555,67 @@ public Object[] getRowData() throws SQLServerException {
* Both BCP and BULK INSERT considers double quotes as part of the data and throws error if any data (say "10") is to be
* inserted into an numeric column. Our implementation does the same.
*/
case java.sql.Types.INTEGER: {
case Types.INTEGER: {
// Formatter to remove the decimal part as SQL Server floors the decimal in integer types
DecimalFormat decimalFormatter = new DecimalFormat("#");
String formatedfInput = decimalFormatter.format(Double.parseDouble(data[pair.getKey() - 1]));
dataRow[pair.getKey() - 1] = Integer.valueOf(formatedfInput);
break;
}

case java.sql.Types.TINYINT:
case java.sql.Types.SMALLINT: {
case Types.TINYINT:
case Types.SMALLINT: {
// Formatter to remove the decimal part as SQL Server floors the decimal in integer types
DecimalFormat decimalFormatter = new DecimalFormat("#");
String formatedfInput = decimalFormatter.format(Double.parseDouble(data[pair.getKey() - 1]));
dataRow[pair.getKey() - 1] = Short.valueOf(formatedfInput);
break;
}

case java.sql.Types.BIGINT: {
case Types.BIGINT: {
BigDecimal bd = new BigDecimal(data[pair.getKey() - 1].trim());
try {
dataRow[pair.getKey() - 1] = bd.setScale(0, BigDecimal.ROUND_DOWN).longValueExact();
}
catch (ArithmeticException ex) {
} catch (ArithmeticException ex) {
String value = "'" + data[pair.getKey() - 1] + "'";
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_errorConvertingValue"));
throw new SQLServerException(form.format(new Object[] {value, JDBCType.of(cm.columnType)}), null, 0, ex);
throw new SQLServerException(form.format(new Object[]{value, JDBCType.of(cm.columnType)}), null, 0, ex);
}
break;
}

case java.sql.Types.DECIMAL:
case java.sql.Types.NUMERIC: {
case Types.DECIMAL:
case Types.NUMERIC: {
BigDecimal bd = new BigDecimal(data[pair.getKey() - 1].trim());
dataRow[pair.getKey() - 1] = bd.setScale(cm.scale, RoundingMode.HALF_UP);
break;
}

case java.sql.Types.BIT: {
case Types.BIT: {
// "true" => 1, "false" => 0
// Any non-zero value (integer/double) => 1, 0/0.0 => 0
try {
dataRow[pair.getKey() - 1] = (0 == Double.parseDouble(data[pair.getKey() - 1])) ? Boolean.FALSE : Boolean.TRUE;
}
catch (NumberFormatException e) {
} catch (NumberFormatException e) {
dataRow[pair.getKey() - 1] = Boolean.parseBoolean(data[pair.getKey() - 1]);
}
break;
}

case java.sql.Types.REAL: {
case Types.REAL: {
dataRow[pair.getKey() - 1] = Float.parseFloat(data[pair.getKey() - 1]);
break;
}

case java.sql.Types.DOUBLE: {
case Types.DOUBLE: {
dataRow[pair.getKey() - 1] = Double.parseDouble(data[pair.getKey() - 1]);
break;
}

case java.sql.Types.BINARY:
case java.sql.Types.VARBINARY:
case java.sql.Types.LONGVARBINARY:
case java.sql.Types.BLOB: {
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
case Types.BLOB: {
/*
* For binary data, the value in file may or may not have the '0x' prefix. We will try to match our implementation with
* 'BULK INSERT' except that we will allow 0x prefix whereas 'BULK INSERT' command does not allow 0x prefix. A BULK INSERT
Expand All @@ -630,14 +627,13 @@ public Object[] getRowData() throws SQLServerException {
String binData = data[pair.getKey() - 1].trim();
if (binData.startsWith("0x") || binData.startsWith("0X")) {
dataRow[pair.getKey() - 1] = binData.substring(2);
}
else {
} else {
dataRow[pair.getKey() - 1] = binData;
}
break;
}

case 2013: // java.sql.Types.TIME_WITH_TIMEZONE
case 2013: // java.sql.Types.TIME_WITH_TIMEZONE
{
DriverJDBCVersion.checkSupportsJDBC42();
OffsetTime offsetTimeValue;
Expand Down Expand Up @@ -671,19 +667,19 @@ else if (dateTimeFormatter != null)
break;
}

case java.sql.Types.NULL: {
case Types.NULL: {
dataRow[pair.getKey() - 1] = null;
break;
}

case java.sql.Types.DATE:
case java.sql.Types.CHAR:
case java.sql.Types.NCHAR:
case java.sql.Types.VARCHAR:
case java.sql.Types.NVARCHAR:
case java.sql.Types.LONGVARCHAR:
case java.sql.Types.LONGNVARCHAR:
case java.sql.Types.CLOB:
case Types.DATE:
case Types.CHAR:
case Types.NCHAR:
case Types.VARCHAR:
case Types.NVARCHAR:
case Types.LONGVARCHAR:
case Types.LONGNVARCHAR:
case Types.CLOB:
default: {
// The string is copied as is.
/*
Expand All @@ -702,13 +698,11 @@ else if (dateTimeFormatter != null)
break;
}
}
}
catch (IllegalArgumentException e) {
} catch (IllegalArgumentException e) {
String value = "'" + data[pair.getKey() - 1] + "'";
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_errorConvertingValue"));
throw new SQLServerException(form.format(new Object[] {value, JDBCType.of(cm.columnType)}), null, 0, e);
}
catch (ArrayIndexOutOfBoundsException e) {
throw new SQLServerException(form.format(new Object[]{value, JDBCType.of(cm.columnType)}), null, 0, e);
} catch (ArrayIndexOutOfBoundsException e) {
throw new SQLServerException(SQLServerException.getErrString("R_CSVDataSchemaMismatch"), e);
}

Expand Down
29 changes: 13 additions & 16 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java
Expand Up @@ -1818,9 +1818,8 @@ else if (null != sourceBulkRecord) {
}
else {
srcColumnCount = columnOrdinals.size();
Iterator<Integer> columnsIterator = columnOrdinals.iterator();
while (columnsIterator.hasNext()) {
currentColumn = columnsIterator.next();
for (Integer columnOrdinal : columnOrdinals) {
currentColumn = columnOrdinal;
srcColumnMetadata.put(currentColumn,
new BulkColumnMetaData(sourceBulkRecord.getColumnName(currentColumn), true, sourceBulkRecord.getPrecision(currentColumn),
sourceBulkRecord.getScale(currentColumn), sourceBulkRecord.getColumnType(currentColumn),
Expand Down Expand Up @@ -1944,9 +1943,7 @@ else if (0 > cm.destinationColumnOrdinal || destColumnCount < cm.destinationColu
}
else {
Set<Integer> columnOrdinals = sourceBulkRecord.getColumnOrdinals();
Iterator<Integer> columnsIterator = columnOrdinals.iterator();
while (columnsIterator.hasNext()) {
int currentColumn = columnsIterator.next();
for (Integer currentColumn : columnOrdinals) {
if (sourceBulkRecord.getColumnName(currentColumn).equals(cm.sourceColumnName)) {
foundColumn = true;
cm.sourceColumnOrdinal = currentColumn;
Expand Down Expand Up @@ -3535,13 +3532,13 @@ private boolean writeBatchData(TDSWriter tdsWriter,
if (null != sourceResultSet) {
// Loop for each destination column. The mappings is a many to one mapping
// where multiple source columns can be mapped to one destination column.
for (int i = 0; i < mappingColumnCount; ++i) {
writeColumn(tdsWriter, columnMappings.get(i).sourceColumnOrdinal, columnMappings.get(i).destinationColumnOrdinal, null // cell
// value is
// retrieved
// inside
// writeRowData()
// method.
for (ColumnMapping columnMapping : columnMappings) {
writeColumn(tdsWriter, columnMapping.sourceColumnOrdinal, columnMapping.destinationColumnOrdinal, null // cell
// value is
// retrieved
// inside
// writeRowData()
// method.
);
}
}
Expand All @@ -3558,11 +3555,11 @@ private boolean writeBatchData(TDSWriter tdsWriter,
throw new SQLServerException(SQLServerException.getErrString("R_unableRetrieveSourceData"), ex);
}

for (int i = 0; i < mappingColumnCount; ++i) {
for (ColumnMapping columnMapping : columnMappings) {
// If the SQLServerBulkCSVRecord does not have metadata for columns, it returns strings in the object array.
// COnvert the strings using destination table types.
writeColumn(tdsWriter, columnMappings.get(i).sourceColumnOrdinal, columnMappings.get(i).destinationColumnOrdinal,
rowObjects[columnMappings.get(i).sourceColumnOrdinal - 1]);
writeColumn(tdsWriter, columnMapping.sourceColumnOrdinal, columnMapping.destinationColumnOrdinal,
rowObjects[columnMapping.sourceColumnOrdinal - 1]);
}
}
row++;
Expand Down