Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Bao committed Jun 18, 2024
1 parent 4243e57 commit d85e234
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 86 deletions.
4 changes: 3 additions & 1 deletion src/main/java/edu/ie3/datamodel/io/SqlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import edu.ie3.datamodel.exceptions.ProcessorProviderException;
import edu.ie3.datamodel.io.naming.DatabaseNamingStrategy;
import edu.ie3.datamodel.io.processor.ProcessorProvider;
import edu.ie3.datamodel.models.Entity;
import edu.ie3.datamodel.models.UniqueEntity;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -43,7 +44,7 @@ public static String queryCreateGridTable(String schemaName) {

/** @return query to create a SQL table for an unique entity */
public static String queryCreateTableUniqueEntity(
Class<? extends UniqueEntity> cls, String schemaName)
Class<? extends Entity> cls, String schemaName)
throws EntityProcessorException, ProcessorProviderException {
ProcessorProvider processorProvider = new ProcessorProvider();
DatabaseNamingStrategy namingStrategy = new DatabaseNamingStrategy();
Expand Down Expand Up @@ -103,6 +104,7 @@ public static Map<String, String> columnToSqlDataType() {
map.put("dsm", "bool NOT NULL");
map.put("e_cons_annual", "double precision NOT NULL");
map.put("load_profile", "TEXT NOT NULL");
map.put("controlling_em", "uuid NOT NULL");

map.put("auto_tap", "bool NOT NULL");
map.put("tap_pos", "int NOT NULL");
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/edu/ie3/datamodel/io/connectors/SqlConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,15 @@ public Map<String, String> extractFieldMap(ResultSet rs) {
return insensitiveFieldsToAttributes;
}

public boolean tableExists(Connection connection, String tableName) throws SQLException {
DatabaseMetaData meta = connection.getMetaData();
ResultSet resultSet = meta.getTables(null, null, tableName, new String[] {"TABLE"});
public boolean tableExistsSQL(String tableName) throws SQLException {
PreparedStatement preparedStatement = connection.prepareStatement("SELECT count(*) "
+ "FROM information_schema.tables "
+ "WHERE table_name = ?"
+ "LIMIT 1;");
preparedStatement.setString(1, tableName);

return resultSet.next();
ResultSet resultSet = preparedStatement.executeQuery();
resultSet.next();
return resultSet.getInt(1) != 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static edu.ie3.datamodel.io.naming.EntityPersistenceNamingStrategy.logger;

import edu.ie3.datamodel.io.naming.timeseries.ColumnScheme;
import edu.ie3.datamodel.models.UniqueEntity;
import edu.ie3.datamodel.models.timeseries.TimeSeries;
import edu.ie3.datamodel.models.timeseries.TimeSeriesEntry;
import edu.ie3.datamodel.models.timeseries.individual.IndividualTimeSeries;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Try<LinkedHashMap<String, String>, ProcessorProviderException> handleEntity(T en
.transformF(ProcessorProviderException::new));
}

public <T extends UniqueEntity> Set<LinkedHashMap<String, String>> handleEntities(
public <T extends Entity> Set<LinkedHashMap<String, String>> handleEntities(
List<T> entities) throws ProcessorProviderException {
Set<T> setOfEntities = new HashSet<>(entities);
Set<LinkedHashMap<String, String>> setOfMaps = new HashSet<>();
Expand Down
76 changes: 45 additions & 31 deletions src/main/java/edu/ie3/datamodel/io/sink/SqlSink.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import edu.ie3.datamodel.io.processor.EntityProcessor;
import edu.ie3.datamodel.io.processor.ProcessorProvider;
import edu.ie3.datamodel.io.processor.timeseries.TimeSeriesProcessorKey;
import edu.ie3.datamodel.models.UniqueEntity;
import edu.ie3.datamodel.models.Entity;
import edu.ie3.datamodel.models.Entity;
import edu.ie3.datamodel.models.input.*;
import edu.ie3.datamodel.models.input.connector.*;
import edu.ie3.datamodel.models.input.container.GraphicElements;
Expand Down Expand Up @@ -84,7 +85,7 @@ public void shutdown() {
* executed by a specific {@link EntityProcessor}
* @throws SQLException
*/
public <C extends UniqueEntity> void persistAll(Collection<C> entities, DbGridMetadata identifier)
public <C extends Entity> void persistAll(Collection<C> entities, DbGridMetadata identifier)
throws SQLException {
// Extract nested entities and add them to the set of entities
Set<C> entitiesToAdd = new HashSet<>(entities); // entities to persist
Expand Down Expand Up @@ -141,7 +142,7 @@ public <C extends InputEntity> void persistAllIgnoreNested(
* @param identifier identifier of the grid
* @throws SQLException
*/
public <C extends UniqueEntity> void persist(C entity, DbGridMetadata identifier)
public <C extends Entity> void persist(C entity, DbGridMetadata identifier)
throws SQLException {
if (entity instanceof InputEntity inputEntity) {
persistIncludeNested(inputEntity, identifier);
Expand Down Expand Up @@ -170,7 +171,8 @@ public <C extends InputEntity> void persistIgnoreNested(C entity, DbGridMetadata

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

protected <C extends UniqueEntity> void persistListIncludeNested(
/*
protected <C extends Entity> void persistListIncludeNested(
List<C> entities, Class<C> cls, DbGridMetadata identifier) throws SQLException {
if (NestedEntity.class.isAssignableFrom(cls)) {
entities.forEach(
Expand All @@ -193,14 +195,16 @@ protected <C extends UniqueEntity> void persistListIncludeNested(
}
}
public <C extends UniqueEntity> void persistIncludeNested(C entity, DbGridMetadata identifier)
*/

public <C extends Entity> void persistIncludeNested(C entity, DbGridMetadata identifier)
throws SQLException {
Set<C> entitiesToAdd = new HashSet<>();
entitiesToAdd.add(entity);
persistAll(entitiesToAdd, identifier);
}

private <C extends UniqueEntity> void persistMixedList(
private <C extends Entity> void persistMixedList(
List<C> entities, DbGridMetadata identifier) {
Map<Class<C>, List<C>> entitiesPerClass =
entities.stream().collect(groupingBy(entity -> (Class<C>) entity.getClass()));
Expand All @@ -218,17 +222,17 @@ private <C extends UniqueEntity> void persistMixedList(
});
}

private <C extends UniqueEntity, E extends TimeSeriesEntry<V>, V extends Value> void persistList(
private <C extends Entity, E extends TimeSeriesEntry<V>, V extends Value> void persistList(
List<C> entities, Class<C> cls, DbGridMetadata identifier) throws SQLException {
// Check if there are only elements of the same class
Class<?> firstClass = entities.get(0).getClass();
boolean allSameClass = entities.stream().allMatch(e -> e.getClass() == firstClass);

if (allSameClass) {
if (InputEntity.class.isAssignableFrom(cls)) {
insertListIgnoreNested(entities, cls, identifier);
insertListIgnoreNested(entities, cls, identifier, true);
} else if (ResultEntity.class.isAssignableFrom(cls)) {
insertListIgnoreNested(entities, cls, identifier);
insertListIgnoreNested(entities, cls, identifier, false);
} else if (TimeSeries.class.isAssignableFrom(cls)) {
entities.forEach(
ts -> {
Expand All @@ -254,15 +258,14 @@ private <C extends UniqueEntity, E extends TimeSeriesEntry<V>, V extends Value>
* Writes a list of entities into a sql table. It's necessary that all entities have the same
* class.
*/
private <C extends UniqueEntity> void insertListIgnoreNested(
List<C> entities, Class<C> cls, DbGridMetadata identifier) throws SQLException {
private <C extends Entity> void insertListIgnoreNested(
List<C> entities, Class<C> cls, DbGridMetadata identifier, boolean ignoreConflict) throws SQLException {
try {
String[] headerElements = processorProvider.getHeaderElements(cls);
String query =
basicInsertQueryValuesGrid(
schemaName, databaseNamingStrategy.getEntityName(cls).orElseThrow(), headerElements);
query = query + createInsertQueryBodyIgnoreConflict(entities, headerElements, identifier);
System.out.println(query);
query = query + createInsertQueryBodyIgnoreConflict(entities, headerElements, identifier, ignoreConflict);
connector.executeUpdate(query);
} catch (ProcessorProviderException e) {
log.error("Exception occurred during processor request: ", e);
Expand Down Expand Up @@ -336,15 +339,14 @@ public void persistJointGrid(JointGridContainer jointGridContainer, UUID gridUUI
SystemParticipants systemParticipants = jointGridContainer.getSystemParticipants();
Set<BmInput> bmPlants = systemParticipants.getBmPlants();
Set<ChpInput> chpPlants = systemParticipants.getChpPlants();
Set<EvcsInput> evCS = systemParticipants.getEvCS();
Set<EvcsInput> evCS = systemParticipants.getEvcs();
Set<EvInput> evs = systemParticipants.getEvs();
Set<FixedFeedInInput> fixedFeedIns = systemParticipants.getFixedFeedIns();
Set<HpInput> heatPumps = systemParticipants.getHeatPumps();
Set<LoadInput> loads = systemParticipants.getLoads();
Set<PvInput> pvPlants = systemParticipants.getPvPlants();
Set<StorageInput> storages = systemParticipants.getStorages();
Set<WecInput> wecPlants = systemParticipants.getWecPlants();
Set<EmInput> emSystems = systemParticipants.getEmSystems();

// get graphic elements (just for better readability, we could also just get them directly
// below)
Expand Down Expand Up @@ -384,14 +386,13 @@ public void persistJointGrid(JointGridContainer jointGridContainer, UUID gridUUI
loads,
pvPlants,
storages,
wecPlants,
emSystems)
wecPlants)
.flatMap(Collection::stream)
.map(Extractor::extractOperator)
.flatMap(Optional::stream)
.collect(Collectors.toSet());

List<UniqueEntity> toAdd = new LinkedList<>();
List<Entity> toAdd = new LinkedList<>();
toAdd.addAll(rawGridElements.allEntitiesAsList());
toAdd.addAll(systemParticipants.allEntitiesAsList());
toAdd.addAll(graphicElements.allEntitiesAsList());
Expand All @@ -402,7 +403,7 @@ public void persistJointGrid(JointGridContainer jointGridContainer, UUID gridUUI
persistAll(toAdd, identifier);
}

private <C extends UniqueEntity> void insert(C entity, DbGridMetadata identifier)
private <C extends Entity> void insert(C entity, DbGridMetadata identifier)
throws SQLException {
try {
String[] headerElements = processorProvider.getHeaderElements(entity.getClass());
Expand All @@ -427,16 +428,24 @@ private <C extends UniqueEntity> void insert(C entity, DbGridMetadata identifier
* will be ignored. Conflicts can occur if an entity (e.g. node) already exist. WARNING: It's
* assumed that all entities are from the same class C.
*/
private <C extends UniqueEntity> String createInsertQueryBodyIgnoreConflict(
List<C> entities, String[] headerElements, DbGridMetadata identifier)
private <C extends Entity> String createInsertQueryBodyIgnoreConflict(
List<C> entities, String[] headerElements, DbGridMetadata identifier, boolean ignoreConflict)
throws ProcessorProviderException {
Set<LinkedHashMap<String, String>> entityFieldData = processorProvider.handleEntities(entities);
String queryBody = "";
queryBody =
queryBody
+ entityFieldData.stream()
.map(data -> queryValueLine(sqlEntityFieldData(data), headerElements, identifier))
.collect(Collectors.joining(",\n", "", "\nON CONFLICT (uuid) DO NOTHING;"));
if (ignoreConflict) {
queryBody =
queryBody
+ entityFieldData.stream()
.map(data -> queryValueLine(sqlEntityFieldData(data), headerElements, identifier))
.collect(Collectors.joining(",\n", "", "\nON CONFLICT (uuid) DO NOTHING;"));
} else {
queryBody =
queryBody
+ entityFieldData.stream()
.map(data -> queryValueLine(sqlEntityFieldData(data), headerElements, identifier))
.collect(Collectors.joining(",\n", "", ";\n"));
}
return queryBody;
}

Expand All @@ -445,7 +454,7 @@ private <C extends UniqueEntity> String createInsertQueryBodyIgnoreConflict(
*
* <p>WARNING: It's assumed that all entities are from the same class C.
*/
private <C extends UniqueEntity> String createInsertQueryBody(
private <C extends Entity> String createInsertQueryBody(
List<C> entities, String[] headerElements, DbGridMetadata identifier)
throws ProcessorProviderException {
Set<LinkedHashMap<String, String>> entityFieldData = processorProvider.handleEntities(entities);
Expand All @@ -472,7 +481,7 @@ private String queryValueLine(
}

/** Creates a line with the values of one entity for an insertion query. */
private <C extends UniqueEntity> String queryValueLine(
private <C extends Entity> String queryValueLine(
C entity, String[] headerElements, DbGridMetadata identifier)
throws ProcessorProviderException {
return queryValueLine(
Expand Down Expand Up @@ -510,7 +519,9 @@ private static String basicInsertQuery(String schemaName, String tableName) {
/** Provides the insert, column names, grid identifier and the VALUES statement for a query. */
private String basicInsertQueryValuesGrid(
String schemaName, String tableName, String[] headerElements) {
String[] addParams = {DbGridMetadata.GRID_UUID};
String[] addParams = {
DbGridMetadata.GRID_UUID
};
return basicInsertQuery(schemaName, tableName)
+ " "
+ writeOneLine(StringUtils.camelCaseToSnakeCase(headerElements), addParams)
Expand All @@ -523,7 +534,10 @@ private String basicInsertQueryValuesGrid(
*/
private String basicInsertQueryValuesITS(
String schemaName, String tableName, String[] headerElements) {
String[] addParams = {DbGridMetadata.GRID_UUID, TIME_SERIES};
String[] addParams = {
DbGridMetadata.GRID_UUID,
TIME_SERIES
};
return basicInsertQuery(schemaName, tableName)
+ " "
+ writeOneLine(StringUtils.camelCaseToSnakeCase(headerElements), addParams)
Expand All @@ -545,7 +559,7 @@ private String writeOneLine(String[] entries, String[] addParams) {

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

public void createClassTable(Class<? extends UniqueEntity> cls, String schemaName)
public void createClassTable(Class<? extends Entity> cls, String schemaName)
throws SQLException, ProcessorProviderException, EntityProcessorException {
String query = queryCreateTableUniqueEntity(cls, schemaName);
connector.executeUpdate(query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ protected Stream<Map<String, String>> buildStreamByTableName(String tableName) {
protected Stream<Map<String, String>> executeQuery(String query, AddParams addParams) {
try (PreparedStatement ps = connector.getConnection().prepareStatement(query)) {
addParams.addParams(ps);

ResultSet resultSet = ps.executeQuery();
return connector.extractFieldMaps(resultSet).stream();
} catch (SQLException e) {
Expand All @@ -191,4 +190,10 @@ protected Stream<Map<String, String>> executeQuery(String query, AddParams addPa
protected Stream<Map<String, String>> executeQuery(String query) {
return executeQuery(query, x -> {});
}

public boolean checkExistingTable(String tableName) throws SQLException {
return connector.tableExistsSQL(tableName);
}


}
Loading

0 comments on commit d85e234

Please sign in to comment.