|
4 | 4 |
|
5 | 5 | package org.lfenergy.compas.scl.data.repository.postgresql; |
6 | 6 |
|
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 | | - |
12 | 7 | 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.*; |
18 | 8 |
|
19 | 9 | public class SoftDeleteCompasSclDataPostgreSQLRepository extends CompasSclDataPostgreSQLRepository { |
20 | 10 |
|
| 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 | + |
21 | 27 | public SoftDeleteCompasSclDataPostgreSQLRepository(DataSource dataSource) { |
22 | 28 | 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; |
69 | 31 | } |
70 | 32 | } |
0 commit comments