corretta insert previsioni meteo con prepared statement

This commit is contained in:
2024-04-04 11:04:14 +02:00
parent 27627e2821
commit f3eedecfec

View File

@@ -2,6 +2,8 @@ package it.integry.ems_model.resolver;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.base.Joiner;
import com.microsoft.sqlserver.jdbc.Geography;
import com.microsoft.sqlserver.jdbc.SQLServerException;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.PrecisionModel;
@@ -157,9 +159,7 @@ public class SqlFieldHolder {
return data;
}
};
}
else if (sqlType.equals(Timestamp.class)) {
} else if (sqlType.equals(Timestamp.class)) {
if (dtoType.equals(Date.class))
converter = data -> Date.from(((Timestamp) data).toInstant());
@@ -170,9 +170,7 @@ public class SqlFieldHolder {
else if (dtoType.equals(LocalDate.class))
converter = data -> ((Timestamp) data).toLocalDateTime().toLocalDate();
}
else if (dtoType.equals(LocalDate.class)) {
} else if (dtoType.equals(LocalDate.class)) {
converter = data -> {
try {
return UtilityString.parseLocalDate(data.toString());
@@ -180,9 +178,7 @@ public class SqlFieldHolder {
return data;
}
};
}
else if (dtoType.equals(LatLng.class)) {
} else if (dtoType.equals(LatLng.class)) {
WKTReader readerWkt = new WKTReader((new GeometryFactory(new PrecisionModel(), 4326)));
@@ -795,78 +791,67 @@ public class SqlFieldHolder {
dato = "null";
else
dato = UtilityDB.valueToString(string);
}
else if (obj instanceof Date) {
} else if (obj instanceof Date) {
Date date = (Date) obj;
if (date.equals(EmsRestConstants.DATE_NULL)) {
dato = "null";
} else {
dato = "'" + new SimpleDateFormat(CommonConstants.DATETIME_FORMAT_YMD).format(date) + "'";
}
}
else if (obj instanceof LocalDate) {
} else if (obj instanceof LocalDate) {
LocalDate localDate = (LocalDate) obj;
if (localDate.equals(LocalDate.MIN)) {
dato = "null";
} else {
dato = "'" + CommonConstants.DATE_YMD_DASHED_FORMATTER.format(localDate) + "'";
}
}
else if (obj instanceof LocalTime) {
} else if (obj instanceof LocalTime) {
LocalTime localTime = (LocalTime) obj;
if (localTime.equals(LocalTime.MIN)) {
dato = "null";
} else {
dato = "'" + CommonConstants.TIME_FORMATTER.format(localTime) + "'";
}
}
else if (obj instanceof LocalDateTime) {
} else if (obj instanceof LocalDateTime) {
LocalDateTime localDateTime = (LocalDateTime) obj;
if (localDateTime.equals(LocalDateTime.MIN)) {
dato = "null";
} else {
dato = "'" + CommonConstants.DATETIME_YMD_DASHED_FORMATTER.format(localDateTime) + "'";
}
}
else if (obj instanceof Integer) {
} else if (obj instanceof Integer) {
if (obj.equals(EmsRestConstants.INTEGER_NULL)) {
dato = "null";
} else
dato = obj.toString();
}
else if (obj instanceof BigDecimal) {
} else if (obj instanceof BigDecimal) {
if (obj.equals(EmsRestConstants.BIGDECIMAL_NULL)) {
dato = "null";
} else
dato = obj.toString();
}
else if (obj instanceof LatLng) {
} else if (obj instanceof LatLng) {
LatLng point = (LatLng) obj;
dato = "geography::STGeomFromText('POINT(" + point.getLng() + " " + point.getLat() + ")', 4326)";
}
else if (obj instanceof Boolean) {
} else if (obj instanceof Boolean) {
dato = (Boolean) obj ? "1" : "0";
}
else if (obj instanceof Instant) {
} else if (obj instanceof Instant) {
Instant instant = (Instant) obj;
if (instant.equals(Instant.MIN)) {
dato = "null";
} else {
dato = "'" + CommonConstants.DATE_YMD_DASHED_FORMATTER.format(instant) + "'";
}
}
else if (obj != null) {
} else if (obj != null) {
dato = obj.toString();
}
else {
} else {
dato = "null";
}
return dato;
}
public static Object getSqlValueFieldAsObject(Object obj, boolean removeSpace) {
public static Object getSqlValueFieldAsObject(Object obj, boolean removeSpace) throws SQLServerException {
Object dato = 0;
if (obj != null && obj.getClass().isEnum()) {
@@ -888,8 +873,7 @@ public class SqlFieldHolder {
dato = null;
else
dato = string;
}
else if (obj instanceof Date) {
} else if (obj instanceof Date) {
Date date = (Date) obj;
if (date.equals(EmsRestConstants.DATE_NULL)) {
dato = null;
@@ -897,16 +881,14 @@ public class SqlFieldHolder {
date.setTime((date.getTime() / 1000) * 1000);
dato = date;
}
}
else if (obj instanceof LocalDate) {
} else if (obj instanceof LocalDate) {
LocalDate localDate = (LocalDate) obj;
if (localDate.equals(LocalDate.MIN)) {
dato = null;
} else {
dato = localDate;
}
}
else if (obj instanceof LocalTime) {
} else if (obj instanceof LocalTime) {
LocalTime localTime = (LocalTime) obj;
if (localTime.equals(LocalTime.MIN)) {
dato = null;
@@ -914,8 +896,7 @@ public class SqlFieldHolder {
dato = localTime
.truncatedTo(ChronoUnit.SECONDS);
}
}
else if (obj instanceof LocalDateTime) {
} else if (obj instanceof LocalDateTime) {
LocalDateTime localDateTime = (LocalDateTime) obj;
if (localDateTime.equals(LocalDateTime.MIN)) {
dato = null;
@@ -923,39 +904,31 @@ public class SqlFieldHolder {
dato = localDateTime
.truncatedTo(ChronoUnit.SECONDS);
}
}
else if (obj instanceof Integer) {
} else if (obj instanceof Integer) {
if (obj.equals(EmsRestConstants.INTEGER_NULL)) {
dato = null;
} else
dato = obj;
}
else if (obj instanceof BigDecimal) {
} else if (obj instanceof BigDecimal) {
if (obj.equals(EmsRestConstants.BIGDECIMAL_NULL)) {
dato = null;
} else
dato = obj;
}
else if (obj instanceof LatLng) {
} else if (obj instanceof LatLng) {
LatLng point = (LatLng) obj;
dato = "geography::STGeomFromText('POINT(" + point.getLng() + " " + point.getLat() + ")', 4326)";
}
else if (obj instanceof Boolean) {
dato = Geography.point(point.getLat().doubleValue(), point.getLng().doubleValue(), 4326);
} else if (obj instanceof Boolean) {
dato = obj;
}
else if (obj instanceof Instant) {
} else if (obj instanceof Instant) {
Instant instant = (Instant) obj;
if (instant.equals(Instant.MIN)) {
dato = null;
} else {
dato = instant;
}
}
else if (obj != null) {
} else if (obj != null) {
dato = obj;
}
else {
} else {
dato = null;
}
return dato;