Skip to content
Snippets Groups Projects
Commit e7e3263c authored by maxlastglance's avatar maxlastglance
Browse files

update timestamp table on delete

parent eaef815e
No related tags found
No related merge requests found
...@@ -797,7 +797,9 @@ public interface MariaDbMapper { ...@@ -797,7 +797,9 @@ public interface MariaDbMapper {
} }
statement.append("`").append(pkColumn).append("` = ").append(valueStr); statement.append("`").append(pkColumn).append("` = ").append(valueStr);
} }
statement.append(";");
// Append ORDER BY clause to sort by ROW_START descending and limit the result to the most recent row.
statement.append(" ORDER BY `ROW_START` DESC LIMIT 1;");
log.trace("mapped select system timestamps query: {}", statement); log.trace("mapped select system timestamps query: {}", statement);
return statement.toString(); return statement.toString();
......
...@@ -253,14 +253,69 @@ public class TableServiceMariaDbImpl extends DataConnector implements TableServi ...@@ -253,14 +253,69 @@ public class TableServiceMariaDbImpl extends DataConnector implements TableServi
final int[] idx = new int[]{1}; final int[] idx = new int[]{1};
final PreparedStatement statement = connection.prepareStatement(mariaDbMapper.tupleToRawDeleteQuery(table, data)); final PreparedStatement statement = connection.prepareStatement(mariaDbMapper.tupleToRawDeleteQuery(table, data));
for (String column : data.getKeys().keySet()) { for (String column : data.getKeys().keySet()) {
mariaDbMapper.prepareStatementWithColumnTypeObject(statement, mariaDbMapper.prepareStatementWithColumnTypeObject(
getColumnType(table.getColumns(), column), idx[0], column, data.getKeys().get(column)); statement,
getColumnType(table.getColumns(), column),
idx[0],
column,
data.getKeys().get(column));
idx[0]++; idx[0]++;
} }
final long start = System.currentTimeMillis(); final long start = System.currentTimeMillis();
statement.executeUpdate(); statement.executeUpdate();
log.trace("executed statement in {} ms", System.currentTimeMillis() - start); log.trace("executed statement in {} ms", System.currentTimeMillis() - start);
connection.commit(); connection.commit();
String selectQuery = mariaDbMapper.tupleToRawSelectSystemTimestampsQuery(
table, TupleDto.builder().data(data.getKeys()).build());
try (PreparedStatement selectStmt = connection.prepareStatement(selectQuery);
ResultSet rs = selectStmt.executeQuery()) {
if (rs.next()) {
// Retrieve the new primary key values from the updated row (if needed).
Map<String, Object> pkData = new HashMap<>();
for (PrimaryKeyDto pk : table.getConstraints().getPrimaryKey()) {
pkData.put(pk.getColumn().getInternalName(),
rs.getObject(pk.getColumn().getInternalName()));
}
Timestamp tsAdd = rs.getTimestamp("ROW_START");
Timestamp tsDelete = rs.getTimestamp("ROW_END");
// Build the update query for the _timestamps table.
String updateTsQuery = mariaDbMapper.tupleToRawUpdateTimestampQuery(
table, TupleUpdateDto.builder().data(data.getKeys()).build(), tsAdd, tsDelete);
try (PreparedStatement ps = connection.prepareStatement(updateTsQuery)) {
int parameterIndex = 1;
// SET clause: new timestamp values.
ps.setTimestamp(parameterIndex++, tsAdd);
ps.setTimestamp(parameterIndex++, tsDelete);
// SET clause: new primary key values (assumed to be provided in data.getKeys())
for (PrimaryKeyDto pk : table.getConstraints().getPrimaryKey()) {
Object newPkValue = data.getKeys().get(pk.getColumn().getInternalName());
ps.setObject(parameterIndex++, newPkValue);
}
// WHERE clause: bind the database id.
String databaseId = table.getDatabase().getInternalName();
ps.setString(parameterIndex++, databaseId);
// WHERE clause: bind the old primary key values.
for (PrimaryKeyDto pk : table.getConstraints().getPrimaryKey()) {
log.info("Setting new primary key value: {}", pk.getColumn().getInternalName());
Object oldPkValue = data.getKeys().get(pk.getColumn().getInternalName());
log.info("Old primary key value: {}", oldPkValue);
ps.setObject(parameterIndex++, oldPkValue);
}
int rowsUpdated = ps.executeUpdate();
} catch (SQLException e) {
log.error("Error updating timestamp table", e);
throw new RuntimeException("Error updating timestamp table", e);
}
} else {
log.warn("No row found for updated PK; timestamps not updated.");
}
} // End of try-with-resources for selectStmt and rs.
} catch (SQLException e) { } catch (SQLException e) {
connection.rollback(); connection.rollback();
log.error("Failed to delete tuple: {}", e.getMessage()); log.error("Failed to delete tuple: {}", e.getMessage());
...@@ -271,6 +326,7 @@ public class TableServiceMariaDbImpl extends DataConnector implements TableServi ...@@ -271,6 +326,7 @@ public class TableServiceMariaDbImpl extends DataConnector implements TableServi
log.info("Deleted tuple(s) from table: {}.{}", table.getDatabase(), table.getInternalName()); log.info("Deleted tuple(s) from table: {}.{}", table.getDatabase(), table.getInternalName());
} }
@Override @Override
public void createTuple(TableDto table, TupleDto data) throws SQLException, QueryMalformedException, public void createTuple(TableDto table, TupleDto data) throws SQLException, QueryMalformedException,
TableMalformedException, StorageUnavailableException, StorageNotFoundException { TableMalformedException, StorageUnavailableException, StorageNotFoundException {
...@@ -426,9 +482,7 @@ public class TableServiceMariaDbImpl extends DataConnector implements TableServi ...@@ -426,9 +482,7 @@ public class TableServiceMariaDbImpl extends DataConnector implements TableServi
// SET clause: new primary key values (assumed to be provided in data.getData()) // SET clause: new primary key values (assumed to be provided in data.getData())
for (PrimaryKeyDto pk : table.getConstraints().getPrimaryKey()) { for (PrimaryKeyDto pk : table.getConstraints().getPrimaryKey()) {
log.info("Setting new primary key value: {}", pk.getColumn().getInternalName());
Object newPkValue = data.getData().get(pk.getColumn().getInternalName()); Object newPkValue = data.getData().get(pk.getColumn().getInternalName());
log.info("New primary key value: {}", newPkValue);
ps.setObject(parameterIndex++, newPkValue); ps.setObject(parameterIndex++, newPkValue);
} }
...@@ -443,20 +497,7 @@ public class TableServiceMariaDbImpl extends DataConnector implements TableServi ...@@ -443,20 +497,7 @@ public class TableServiceMariaDbImpl extends DataConnector implements TableServi
log.info("Old primary key value: {}", oldPkValue); log.info("Old primary key value: {}", oldPkValue);
ps.setObject(parameterIndex++, oldPkValue); ps.setObject(parameterIndex++, oldPkValue);
} }
log.info("SQL Query: " + updateTsQuery);
log.info("Bound parameters: tsAdd={}, tsDelete={}, newPKValues={}, databaseId={}, oldPKValues={}",
tsAdd, tsDelete,
table.getConstraints().getPrimaryKey().stream()
.map(pk -> data.getData().get(pk.getColumn().getInternalName()))
.collect(Collectors.toList()),
databaseId,
table.getConstraints().getPrimaryKey().stream()
.map(pk -> data.getKeys().get(pk.getColumn().getInternalName()))
.collect(Collectors.toList()));
int rowsUpdated = ps.executeUpdate(); int rowsUpdated = ps.executeUpdate();
log.info("Updated {} row(s) in {}_timestamps table.", rowsUpdated, table.getInternalName());
} }
} else { } else {
log.warn("No row found for updated PK; timestamps not updated."); log.warn("No row found for updated PK; timestamps not updated.");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment