Skip to content

Commit 0a0eed3

Browse files
committed
[#464] reduced code duplication for PR
Signed-off-by: David Monichi <[email protected]>
1 parent 2c8d481 commit 0a0eed3

File tree

2 files changed

+35
-71
lines changed

2 files changed

+35
-71
lines changed

repository-postgresql/src/main/java/org/lfenergy/compas/scl/data/repository/postgresql/CompasSclDataPostgreSQLRepository.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ public class CompasSclDataPostgreSQLRepository implements CompasSclDataRepositor
3838

3939
protected final DataSource dataSource;
4040

41+
protected static String DELETE_SCL_FILE_SQL = """
42+
delete from scl_file
43+
where scl_file.id = ?
44+
and scl_file.type = ?
45+
""";
46+
47+
protected static String DELETE_SCL_FILE_SQL_BY_VERSION = """
48+
delete from scl_file
49+
where scl_file.id = ?
50+
and scl_file.type = ?
51+
and scl_file.major_version = ?
52+
and scl_file.minor_version = ?
53+
and scl_file.patch_version = ?
54+
""";
55+
4156
public CompasSclDataPostgreSQLRepository(DataSource dataSource) {
4257
this.dataSource = dataSource;
4358
}
@@ -305,14 +320,9 @@ insert into scl_label(scl_id, major_version, minor_version, patch_version, label
305320
@Override
306321
@Transactional(REQUIRED)
307322
public void delete(SclFileType type, UUID id) {
308-
var sql = """
309-
delete from scl_file
310-
where scl_file.id = ?
311-
and scl_file.type = ?
312-
""";
313323

314324
try (var connection = dataSource.getConnection();
315-
var stmt = connection.prepareStatement(sql)) {
325+
var stmt = connection.prepareStatement(DELETE_SCL_FILE_SQL)) {
316326
stmt.setObject(1, id);
317327
stmt.setString(2, type.name());
318328
stmt.executeUpdate();
@@ -324,17 +334,9 @@ public void delete(SclFileType type, UUID id) {
324334
@Override
325335
@Transactional(REQUIRED)
326336
public void delete(SclFileType type, UUID id, Version version) {
327-
var sql = """
328-
delete from scl_file
329-
where scl_file.id = ?
330-
and scl_file.type = ?
331-
and scl_file.major_version = ?
332-
and scl_file.minor_version = ?
333-
and scl_file.patch_version = ?
334-
""";
335337

336338
try (var connection = dataSource.getConnection();
337-
var stmt = connection.prepareStatement(sql)) {
339+
var stmt = connection.prepareStatement(DELETE_SCL_FILE_SQL_BY_VERSION)) {
338340
stmt.setObject(1, id);
339341
stmt.setString(2, type.name());
340342
stmt.setInt(3, version.getMajorVersion());

repository-postgresql/src/main/java/org/lfenergy/compas/scl/data/repository/postgresql/SoftDeleteCompasSclDataPostgreSQLRepository.java

Lines changed: 18 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,67 +4,29 @@
44

55
package org.lfenergy.compas.scl.data.repository.postgresql;
66

7-
import jakarta.transaction.Transactional;
8-
import org.lfenergy.compas.scl.data.exception.CompasSclDataServiceException;
9-
import org.lfenergy.compas.scl.data.model.*;
10-
import org.lfenergy.compas.scl.extensions.model.SclFileType;
11-
127
import javax.sql.DataSource;
13-
import java.sql.SQLException;
14-
import java.util.UUID;
15-
16-
import static jakarta.transaction.Transactional.TxType.REQUIRED;
17-
import static org.lfenergy.compas.scl.data.exception.CompasSclDataServiceErrorCode.*;
188

199
public class SoftDeleteCompasSclDataPostgreSQLRepository extends CompasSclDataPostgreSQLRepository {
2010

11+
private static final String SOFT_DELETE_SCL_FILE_SQL = """
12+
UPDATE scl_file
13+
SET is_deleted = true
14+
where scl_file.id = ?
15+
and scl_file.type = ?
16+
""";
17+
18+
private static final String SOFT_DELETE_SCL_FILE_SQL_BY_VERSION = """
19+
delete from scl_file
20+
where scl_file.id = ?
21+
and scl_file.type = ?
22+
and scl_file.major_version = ?
23+
and scl_file.minor_version = ?
24+
and scl_file.patch_version = ?
25+
""";
26+
2127
public SoftDeleteCompasSclDataPostgreSQLRepository(DataSource dataSource) {
2228
super(dataSource);
23-
}
24-
25-
@Override
26-
@Transactional(REQUIRED)
27-
public void delete(SclFileType type, UUID id) {
28-
var sql = """
29-
UPDATE scl_file
30-
SET is_deleted = true
31-
where scl_file.id = ?
32-
and scl_file.type = ?
33-
""";
34-
35-
try (var connection = dataSource.getConnection();
36-
var stmt = connection.prepareStatement(sql)) {
37-
stmt.setObject(1, id);
38-
stmt.setString(2, type.name());
39-
stmt.executeUpdate();
40-
} catch (SQLException exp) {
41-
throw new CompasSclDataServiceException(POSTGRES_DELETE_ERROR_CODE, "Error removing SCL from database!", exp);
42-
}
43-
}
44-
45-
@Override
46-
@Transactional(REQUIRED)
47-
public void delete(SclFileType type, UUID id, Version version) {
48-
var sql = """
49-
UPDATE scl_file
50-
SET is_deleted = true
51-
where scl_file.id = ?
52-
and scl_file.type = ?
53-
and scl_file.major_version = ?
54-
and scl_file.minor_version = ?
55-
and scl_file.patch_version = ?
56-
""";
57-
58-
try (var connection = dataSource.getConnection();
59-
var stmt = connection.prepareStatement(sql)) {
60-
stmt.setObject(1, id);
61-
stmt.setString(2, type.name());
62-
stmt.setInt(3, version.getMajorVersion());
63-
stmt.setInt(4, version.getMinorVersion());
64-
stmt.setInt(5, version.getPatchVersion());
65-
stmt.executeUpdate();
66-
} catch (SQLException exp) {
67-
throw new CompasSclDataServiceException(POSTGRES_DELETE_ERROR_CODE, "Error removing SCL (version) from database!", exp);
68-
}
29+
DELETE_SCL_FILE_SQL = SOFT_DELETE_SCL_FILE_SQL;
30+
DELETE_SCL_FILE_SQL_BY_VERSION = SOFT_DELETE_SCL_FILE_SQL_BY_VERSION;
6931
}
7032
}

0 commit comments

Comments
 (0)