Skip to content

Commit

Permalink
Improved integer value check
Browse files Browse the repository at this point in the history
  • Loading branch information
hudeany committed Feb 25, 2024
1 parent 877e9de commit 7f553d7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/de/soderer/dbimport/DbImport.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
// TODO: Invalid null in not null column => error message
// TODO: Missing mapping for not null column => error message
// TODO: Check for string too large
// TODO: Check for int too large
// TODO: Errorhandling on createConnection (dbconnection error detection and help messages in errors)
// TODO: Dateien blob import export
public class DbImport extends UpdateableConsoleApplication implements WorkerParentDual {
Expand Down
56 changes: 49 additions & 7 deletions src/de/soderer/dbimport/DbImportWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -1081,21 +1081,53 @@ protected Closeable validateAndSetParameter(final PreparedStatement preparedStat
final double value = Double.parseDouble(valueString);
preparedStatement.setDouble(columnIndex, value);
batchValueItem.add(value);
} else {
final int value = Integer.parseInt(valueString);
} else if (simpleDataType == SimpleDataType.Integer) {
int value;
try {
value = Integer.parseInt(valueString);
} catch (@SuppressWarnings("unused") final NumberFormatException e) {
throw new DbImportException("Numeric value is invalid for integer: " + valueString);
}
preparedStatement.setInt(columnIndex, value);
batchValueItem.add(value);
} else if (simpleDataType == SimpleDataType.BigInteger) {
long value;
try {
value = Long.parseLong(valueString);
} catch (@SuppressWarnings("unused") final NumberFormatException e) {
throw new DbImportException("Numeric value is invalid for big integer: " + valueString);
}
preparedStatement.setLong(columnIndex, value);
batchValueItem.add(value);
} else {
throw new DbImportException("Numeric value is invalid for " + simpleDataType.name() + " data column: " + valueString);
}
} else if (",".equals(formatInfo)) {
valueString = valueString.replace(".", "").replace(",", ".");
if (valueString.contains(".")) {
final double value = Double.parseDouble(valueString);
preparedStatement.setDouble(columnIndex, value);
batchValueItem.add(value);
} else {
final int value = Integer.parseInt(valueString);
} else if (simpleDataType == SimpleDataType.Integer) {
int value;
try {
value = Integer.parseInt(valueString);
} catch (@SuppressWarnings("unused") final NumberFormatException e) {
throw new DbImportException("Numeric value is invalid for integer: " + valueString);
}
preparedStatement.setInt(columnIndex, value);
batchValueItem.add(value);
} else if (simpleDataType == SimpleDataType.BigInteger) {
long value;
try {
value = Long.parseLong(valueString);
} catch (@SuppressWarnings("unused") final NumberFormatException e) {
throw new DbImportException("Numeric value is invalid for big integer: " + valueString);
}
preparedStatement.setLong(columnIndex, value);
batchValueItem.add(value);
} else {
throw new DbImportException("Numeric value is invalid for " + simpleDataType.name() + " data column: " + valueString);
}
} else if ("file".equalsIgnoreCase(formatInfo)) {
if (!new File(valueString).exists()) {
Expand Down Expand Up @@ -1352,8 +1384,13 @@ protected Closeable validateAndSetParameter(final PreparedStatement preparedStat
preparedStatement.setDouble(columnIndex, value);
batchValueItem.add(value);
} else {
final int value = Integer.parseInt(valueString);
preparedStatement.setInt(columnIndex, value);
long value;
try {
value = Long.parseLong(valueString);
} catch (@SuppressWarnings("unused") final NumberFormatException e) {
throw new DbImportException("Numeric value is invalid for integer: " + valueString);
}
preparedStatement.setLong(columnIndex, value);
batchValueItem.add(value);
}
} else if (simpleDataType == SimpleDataType.BigInteger) {
Expand All @@ -1363,7 +1400,12 @@ protected Closeable validateAndSetParameter(final PreparedStatement preparedStat
preparedStatement.setDouble(columnIndex, value);
batchValueItem.add(value);
} else {
final long value = Long.parseLong(valueString);
long value;
try {
value = Long.parseLong(valueString);
} catch (@SuppressWarnings("unused") final NumberFormatException e) {
throw new DbImportException("Value is invalid for big integer: " + valueString);
}
preparedStatement.setLong(columnIndex, value);
batchValueItem.add(value);
}
Expand Down

0 comments on commit 7f553d7

Please sign in to comment.