Commit 42822d8c authored by Danilo Trombino's avatar Danilo Trombino
Browse files

[Fix] Date Handler on Realtime notification +

[Fix] DB Column Lenght on Realtime notification
parent 5816f213
......@@ -35,6 +35,17 @@ import org.joda.time.DateTimeZone;
public class PersistedTableHelper {
private static transient Logger logger = Logger.getLogger(PersistedTableHelper.class);
public static void fillColumnSize(Object fieldValue, String fieldMetaName, String fieldMetaTypeName, Map<String, Integer> columnSizes) {
if (fieldMetaTypeName.contains("String")) {
Integer lenValue = (fieldValue == null) ? new Integer("0") : new Integer(fieldValue.toString().length());
Integer prevValue = columnSizes.get(fieldMetaName) == null ? new Integer("0") : columnSizes.get(fieldMetaName);
if (lenValue > prevValue) {
columnSizes.remove(fieldMetaName);
columnSizes.put(fieldMetaName, lenValue);
}
}
}
public static void addField(PreparedStatement insertStatement, int fieldIndex, Object fieldValue, String fieldMetaName, String fieldMetaTypeName,
boolean isfieldMetaFieldTypeMeasure, Map<String, Integer> columnSizes) {
......@@ -77,6 +88,7 @@ public class PersistedTableHelper {
if (lenValue > prevValue) {
columnSizes.remove(fieldMetaName);
columnSizes.put(fieldMetaName, lenValue);
}
if (!(fieldValue instanceof String)) {
logger.debug("An unexpected error occured while extimating field [" + fieldMetaName + "] memory size whose type is equal to ["
......@@ -100,7 +112,20 @@ public class PersistedTableHelper {
insertStatement.setDate(fieldIndex + 1, sqlDate);
}
} else if (fieldMetaTypeName.toLowerCase().contains("timestamp")) {
insertStatement.setTimestamp(fieldIndex + 1, Timestamp.valueOf(fieldValue.toString()));
// if(fieldValue instanceof java.util.Date) {
java.util.Date date = (java.util.Date) fieldValue;
// JDK 8 version
/*
* Instant instant = date.toInstant(); ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()); LocalDate localDate =
* zdt.toLocalDate();
*/
DateTime dateTime = new DateTime(date, DateTimeZone.getDefault());
java.sql.Date sqlDate = new java.sql.Date(dateTime.getMillis());
insertStatement.setTimestamp(fieldIndex + 1, new java.sql.Timestamp(sqlDate.getTime()));
// insertStatement.setTimestamp(fieldIndex + 1, Timestamp.valueOf(fieldValue.toString()));
// }
} else if (fieldMetaTypeName.contains("Time")) {
insertStatement.setTime(fieldIndex + 1, (Time) fieldValue);
} else if (fieldMetaTypeName.contains("Byte")) {
......
......@@ -81,7 +81,7 @@ public class JSONPathDataReader extends AbstractDataReader {
private static final String DEFAULT_TIME_PATTERN = "HH:mm:ss";
private static final String DEFAULT_TIMESTAMP_PATTERN_UNQUOTED = "yyyy-MM-ddTHH:mm:ss.SSSZ";
private static final String DEFAULT_TIMESTAMP_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
private static final String ATTRIBUTES_DIRECTLY = "attributesDirectly";
......@@ -642,6 +642,10 @@ public class JSONPathDataReader extends AbstractDataReader {
if (typeString.toLowerCase().startsWith("time")) {
return DEFAULT_TIME_PATTERN;
}
if (typeString.startsWith("ISO8601")) {
return DEFAULT_TIMESTAMP_PATTERN;
}
Assert.assertUnreachable("type date not recognized: " + typeString);
return null;
......
......@@ -23,6 +23,7 @@ import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
......@@ -316,12 +317,21 @@ public class PersistedTableManager implements IPersistedManager {
IMetaData storeMeta = datastore.getMetaData();
int fieldCount = storeMeta.getFieldCount();
int recordCount = (int) datastore.getRecordsCount();
if (fieldCount == 0 || recordCount == 0) {
return new PreparedStatement[0];
}
//TODO Pre Fill Column Size
try {
fillSizeColumn(connection);
} catch (DataBaseException | SQLException e1) {
logger.error("impossible to prefill column size");
e1.printStackTrace();
}
//---------------
PreparedStatement[] toReturn = new PreparedStatement[recordCount];
StringBuilder insertSB = new StringBuilder("INSERT INTO ");
insertSB.append(getTableName());
insertSB.append(" (");
......@@ -402,6 +412,7 @@ public class PersistedTableManager implements IPersistedManager {
if (this.isRowCountColumIncluded()) {
statement.setLong(1, i + 1);
}
List<Integer> sortedIds = new ArrayList<>();
for (int j = 0; j < record.getFields().size(); j++) {
......@@ -412,6 +423,7 @@ public class PersistedTableManager implements IPersistedManager {
if (!doInsert) {
sortedIds.add(idFieldIndex);
}
for (int j = 0; j < sortedIds.size(); j++) {
try {
......@@ -419,13 +431,28 @@ public class PersistedTableManager implements IPersistedManager {
IFieldMetaData fieldMeta = storeMeta.getFieldMeta(index);
IField field = record.getFieldAt(index);
Object fieldValue = field.getValue();
String fieldMetaName = fieldMeta.getName();
//String fieldMetaName = fieldMeta.getName();
String fieldMetaName = fieldMeta.getAlias() != null ? fieldMeta.getAlias() : fieldMeta.getName();
String fieldMetaTypeName = fieldMeta.getType().toString();
boolean isfieldMetaFieldTypeMeasure = fieldMeta.getFieldType().equals(FieldType.MEASURE);
Map<String,Integer> columnSizeCopy = new HashMap<String,Integer>();
int fieldIndex = isRowCountColumIncluded() ? j + 1 : j;
PersistedTableHelper.addField(statement, fieldIndex, fieldValue, fieldMetaName, fieldMetaTypeName, isfieldMetaFieldTypeMeasure,
getColumnSize());
columnSizeCopy);
//Refresh the columnSize
Integer oldLen = getColumnSize().get(fieldMetaName);
Integer newLen = columnSizeCopy.get(fieldMetaName);
if(oldLen != null && newLen != null) {
if(newLen > oldLen) {
getColumnSize().remove(fieldMetaName);
getColumnSize().put(fieldMetaName, newLen);
Statement ps = connection.createStatement();
String query = "ALTER TABLE " + tableName +" MODIFY COLUMN " + fieldMetaName + " " + getDBFieldTypeFromAlias(datasource, fieldMeta) + " ;";
ps.executeUpdate(query);
}
}
} catch (Exception e) {
throw new RuntimeException("An unexpected error occured while preparing statemenet for record [" + i + "]", e);
}
......@@ -660,6 +687,28 @@ public class PersistedTableManager implements IPersistedManager {
}
return toReturn;
}
private void fillSizeColumn(Connection connection) throws DataBaseException, SQLException {
Set<Object> ids = new HashSet<>();
Map<String,Integer> columnSize= getColumnSize();
StringBuilder selectQuery = new StringBuilder();
selectQuery.append("SELECT * FROM ");
selectQuery.append(getTableName());
try (PreparedStatement preparedStatement = connection.prepareStatement(selectQuery.toString())) {
if (queryTimeout > 0) {
preparedStatement.setQueryTimeout(queryTimeout);
}
try (ResultSet rs = preparedStatement.executeQuery()) {
ResultSetMetaData rsmd = rs.getMetaData();
while (rs.next()) {
int i = 1;
for(i = 1; i <=rsmd.getColumnCount(); i++)
columnSize.put(rsmd.getColumnName(i), rsmd.getColumnDisplaySize(i));
}
}
}
}
private void initializeStatement(PreparedStatement statement, IRecord record, IMetaData storeMeta) {
for (int j = 0; j < record.getFields().size(); j++) {
......@@ -749,6 +798,21 @@ public class PersistedTableManager implements IPersistedManager {
return dataBase.getDataBaseType(type);
}
private String getDBFieldTypeFromAlias(IDataSource dataSource, IFieldMetaData fieldMetaData) throws DataBaseException {
CacheDataBase dataBase = DataBaseFactory.getCacheDataBase(dataSource);
Integer alias = getColumnSize().get(fieldMetaData.getAlias());
if (alias != null) {
dataBase.setVarcharLength(alias);
}
Class type = fieldMetaData.getType();
if (fieldMetaData.getFieldType().equals(FieldType.MEASURE) && type == String.class) {
logger.debug("Column type is string but the field is measure: converting it into a double");
type = Double.class;
}
return dataBase.getDataBaseType(type);
}
private String getCreateTableQuery(IMetaData md, IDataSource dataSource) throws DataBaseException {
String toReturn = null;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment