Skip to content

Commit

Permalink
Proposal to fix typing
Browse files Browse the repository at this point in the history
Signed-off-by: BOUHOURS Antoine <[email protected]>
  • Loading branch information
antoinebhs committed Mar 27, 2024
1 parent 2afb69d commit e92d1b5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@
import javax.sql.DataSource;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.sql.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Instant;
import java.util.*;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -698,7 +700,7 @@ public <T extends IdentifiableAttributes & Contained> void updateIdentifiables(U
try (var connection = dataSource.getConnection()) {
for (List<Resource<T>> subResources : Lists.partition(resources, BATCH_SIZE)) {
List<Object> values = new ArrayList<>(4 + tableMapping.getColumnsMapping().size());
try (PreparedStatement preparedStmt = connection.prepareStatement(QueryCatalog.buildMultiRowsUpdateIdentifiableQuery(tableMapping.getTable(), tableMapping.getColumnsMapping().keySet(), columnToAddToWhereClause, subResources.size()))) {
try (PreparedStatement preparedStmt = connection.prepareStatement(QueryCatalog.buildMultiRowsUpdateIdentifiableQuery(tableMapping.getTable(), tableMapping.getColumnsMapping(), columnToAddToWhereClause, subResources.size()))) {
for (Resource<T> resource : subResources) {
T attributes = resource.getAttributes();
for (var e : tableMapping.getColumnsMapping().entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import com.powsybl.network.store.model.Resource;

import java.util.*;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static com.powsybl.network.store.server.Mappings.*;
Expand Down Expand Up @@ -178,7 +180,8 @@ public static String buildUpdateIdentifiableQuery(String tableName, Collection<S
return query.toString();
}

public static String buildMultiRowsUpdateIdentifiableQuery(String tableName, Set<String> columns, String columnToAddToWhereClause, int rowsNumber) {
public static String buildMultiRowsUpdateIdentifiableQuery(String tableName, Map<String, ColumnMapping> columnMapping, String columnToAddToWhereClause, int rowsNumber) {
Set<String> columns = columnMapping.keySet();
StringBuilder query = new StringBuilder("update ")
.append(tableName + " as T1 \n")
.append(" set \n");
Expand All @@ -198,17 +201,29 @@ public static String buildMultiRowsUpdateIdentifiableQuery(String tableName, Set
query.append(" FROM (VALUES \n");
for (int i = 0; i < rowsNumber; i++) {
query.append("(");
query.append("?, ");
query.append("?, ");
query.append("?, ");
it = columns.iterator();
while (it.hasNext()) {
it.next();
query.append("?");
if (it.hasNext()) {
query.append(", ");
String column = it.next();
if (!column.equalsIgnoreCase(columnToAddToWhereClause)) {
Class<?> columnType = columnMapping.get(column).getClassR();
if (columnType == Double.class) {
query.append("?::double precision");
} else if (columnType == Boolean.class) {
query.append("?::boolean");
} else {
query.append("?");
}
if (it.hasNext()) {
query.append(", ");
}
}
}
query.append(", ?");
query.append(", ?");
query.append(", ?");
if (columnToAddToWhereClause != null) {
query.append(", ?");
}
query.append(")");
if (i < rowsNumber - 1) {
query.append(",\n");
Expand Down Expand Up @@ -242,6 +257,43 @@ public static String buildMultiRowsUpdateIdentifiableQuery(String tableName, Set
}
query.append(";");
return query.toString();
// return "update generator as T1 \n" +
// " set \n" +
// "name = T2.name, \n" +
// "bus = T2.bus, \n" +
// "connectableBus = T2.connectableBus, \n" +
// "minP = T2.minP, \n" +
// "maxP = T2.maxP, \n" +
// "energySource = T2.energySource, \n" +
// "p = T2.p, \n" +
// "q = T2.q, \n" +
// "fictitious = T2.fictitious, \n" +
// "voltageRegulatorOn = T2.voltageRegulatorOn, \n" +
// "targetP = T2.targetP, \n" +
// "targetQ = T2.targetQ, \n" +
// "targetV = T2.targetV, \n" +
// "ratedS = T2.ratedS, \n" +
// "minQ = T2.minQ, \n" +
// "maxQ = T2.maxQ, \n" +
// "activePowerControl = T2.activePowerControl, \n" +
// "regulatingTerminal = T2.regulatingTerminal, \n" +
// "coordinatedReactiveControl = T2.coordinatedReactiveControl, \n" +
// "remoteReactivePowerControl = T2.remoteReactivePowerControl, \n" +
// "entsoeCategory = T2.entsoeCategory, \n" +
// "node = T2.node, \n" +
// "properties = T2.properties, \n" +
// "aliasByType = T2.aliasByType, \n" +
// "aliasesWithoutType = T2.aliasesWithoutType, \n" +
// "position = T2.position, \n" +
// "generatorStartup = T2.generatorStartup, \n" +
// "generatorShortCircuit = T2.generatorShortCircuit\n" +
// " FROM (VALUES \n" +
// "(NULL, NULL, NULL, -20.0, 200.0, '\"OTHER\"', 20.0, -1.0560280084609985, 'FALSE'::boolean, 'TRUE'::boolean, -20.0, 1.0560014247894287, 238.71322631835938, 'NaN'::double precision, null::double precision, null::double precision, NULL, NULL, NULL, NULL, NULL, 0, '{\"genreCvg\":\"FICTIF\"}', NULL, NULL, '{\"label\":\".AUBAT 1\",\"order\":20,\"direction\":\"TOP\"}', NULL, NULL, '57260bed-ca26-49a8-932b-1cd2cc31c6b7'::uuid, 2, '.AUBAT 1', '.AUBA 6') )\n" +
// " AS T2( name, bus, connectableBus, minP, maxP, energySource, p, q, fictitious, voltageRegulatorOn, targetP, targetQ, targetV, ratedS, minQ, maxQ, activePowerControl, regulatingTerminal, coordinatedReactiveControl, remoteReactivePowerControl, entsoeCategory, node, properties, aliasByType, aliasesWithoutType, position, generatorStartup, generatorShortCircuit, networkUuid, variantNum, id, voltageLevelId )\n" +
// " where T2.networkUuid::text = T1.networkUuid::text and \n" +
// " T2.variantNum = T1.variantNum and\n" +
// " T2.id = T1.id and\n" +
// " T2.voltageLevelId = T1.voltageLevelId;";
}

public static String buildUpdateInjectionSvQuery(String tableName) {
Expand Down

0 comments on commit e92d1b5

Please sign in to comment.