Skip to content

Commit

Permalink
Fix update API - allow using floating-point types (#3929)
Browse files Browse the repository at this point in the history
* fix update API

Signed-off-by: Iliyan Velichkov <[email protected]>

* format the code

Signed-off-by: Iliyan Velichkov <[email protected]>

---------

Signed-off-by: Iliyan Velichkov <[email protected]>
  • Loading branch information
iliyan-velichkov authored May 16, 2024
1 parent 2d2c185 commit 78cb3f2
Showing 1 changed file with 46 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,23 @@
*/
package org.eclipse.dirigible.components.api.db;

import java.io.ByteArrayInputStream;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.Set;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.eclipse.dirigible.commons.api.helpers.BytesHelper;
import org.eclipse.dirigible.commons.api.helpers.GsonHelper;
import org.eclipse.dirigible.components.database.NamedParameterStatement;
import org.eclipse.dirigible.database.sql.DataTypeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.io.ByteArrayInputStream;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.Set;

/**
* The Class ParametersSetter.
Expand Down Expand Up @@ -138,11 +135,25 @@ private static void setJsonPrimitiveParam(PreparedStatement preparedStatement, i
private static void setNumber(PreparedStatement preparedStatement, int paramIndex, JsonElement parameterElement) throws SQLException {
boolean isNumberParameterSet = false;
int numberIndex = paramIndex;
try {
preparedStatement.setInt(numberIndex, parameterElement.getAsInt());
isNumberParameterSet = true;
} catch (SQLException | ClassCastException e) {
// Do nothing

double doubleValue = parameterElement.getAsDouble();
boolean floatingPointValue = doubleValue % 1 != 0;
if (floatingPointValue) {
try {
preparedStatement.setDouble(numberIndex, doubleValue);
return;
} catch (SQLException | ClassCastException e) {
// Do nothing
}
}

if (!isNumberParameterSet) {
try {
preparedStatement.setInt(numberIndex, parameterElement.getAsInt());
isNumberParameterSet = true;
} catch (SQLException | ClassCastException e) {
// Do nothing
}
}

if (!isNumberParameterSet) {
Expand All @@ -153,6 +164,7 @@ private static void setNumber(PreparedStatement preparedStatement, int paramInde
// Do nothing
}
}

if (!isNumberParameterSet) {
try {
preparedStatement.setLong(numberIndex, parameterElement.getAsLong());
Expand All @@ -161,6 +173,7 @@ private static void setNumber(PreparedStatement preparedStatement, int paramInde
// Do nothing
}
}

if (!isNumberParameterSet) {
try {
preparedStatement.setBigDecimal(numberIndex, parameterElement.getAsBigDecimal());
Expand All @@ -169,6 +182,7 @@ private static void setNumber(PreparedStatement preparedStatement, int paramInde
// Do nothing
}
}

if (!isNumberParameterSet) {
preparedStatement.setObject(numberIndex, parameterElement.getAsNumber()
.toString());
Expand Down Expand Up @@ -266,6 +280,7 @@ void setParam(JsonElement sourceParam, String paramName, NamedParameterStatement
throws SQLException;
}


/**
* The Class BaseParamSetter.
*/
Expand All @@ -282,6 +297,7 @@ protected void throwWrongValue(JsonElement sourceParam, String dataType) {
}
}


/**
* The Class TextParamSetter.
*/
Expand Down Expand Up @@ -342,6 +358,7 @@ public void setParam(JsonElement sourceParam, String paramName, NamedParameterSt
}
}


/**
* The Class DateParamSetter.
*/
Expand Down Expand Up @@ -437,6 +454,7 @@ public void setParam(JsonElement sourceParam, String paramName, NamedParameterSt
}
}


/**
* The Class TimestampParamSetter.
*/
Expand Down Expand Up @@ -557,6 +575,7 @@ private long getTime(String timestampString) {
}
}


/**
* The Class TimeParamSetter.
*/
Expand Down Expand Up @@ -654,6 +673,7 @@ public void setParam(JsonElement sourceParam, String paramName, NamedParameterSt
}
}


/**
* The Class IntegerParamSetter.
*/
Expand Down Expand Up @@ -729,6 +749,7 @@ public void setParam(JsonElement sourceParam, String paramName, NamedParameterSt
}
}


/**
* The Class TinyIntParamSetter.
*/
Expand Down Expand Up @@ -804,6 +825,7 @@ public void setParam(JsonElement sourceParam, String paramName, NamedParameterSt
}
}


/**
* The Class SmallIntParamSetter.
*/
Expand Down Expand Up @@ -879,6 +901,7 @@ public void setParam(JsonElement sourceParam, String paramName, NamedParameterSt
}
}


/**
* The Class BigIntParamSetter.
*/
Expand Down Expand Up @@ -956,6 +979,7 @@ public void setParam(JsonElement sourceParam, String paramName, NamedParameterSt
}
}


/**
* The Class RealParamSetter.
*/
Expand Down Expand Up @@ -1033,6 +1057,7 @@ public void setParam(JsonElement sourceParam, String paramName, NamedParameterSt
}
}


/**
* The Class DoubleParamSetter.
*/
Expand Down Expand Up @@ -1110,6 +1135,7 @@ public void setParam(JsonElement sourceParam, String paramName, NamedParameterSt
}
}


/**
* The Class BooleanParamSetter.
*/
Expand Down Expand Up @@ -1185,6 +1211,7 @@ public void setParam(JsonElement sourceParam, String paramName, NamedParameterSt
}
}


/**
* The Class BlobParamSetter.
*/
Expand Down

0 comments on commit 78cb3f2

Please sign in to comment.