1010import org .lfenergy .compas .scl .data .repository .CompasSclDataRepository ;
1111import org .lfenergy .compas .scl .extensions .model .SclFileType ;
1212
13- import jakarta .enterprise .context .ApplicationScoped ;
14- import jakarta .inject .Inject ;
1513import javax .sql .DataSource ;
1614import jakarta .transaction .Transactional ;
1715import java .sql .Array ;
2725import static jakarta .transaction .Transactional .TxType .SUPPORTS ;
2826import static org .lfenergy .compas .scl .data .exception .CompasSclDataServiceErrorCode .*;
2927
30- @ ApplicationScoped
3128public class CompasSclDataPostgreSQLRepository implements CompasSclDataRepository {
3229 private static final String ID_FIELD = "id" ;
3330 private static final String MAJOR_VERSION_FIELD = "major_version" ;
@@ -39,9 +36,23 @@ public class CompasSclDataPostgreSQLRepository implements CompasSclDataRepositor
3936 private static final String HITEM_WHEN_FIELD = "hitem_when" ;
4037 private static final String HITEM_WHAT_FIELD = "hitem_what" ;
4138
42- private final DataSource dataSource ;
39+ protected final DataSource dataSource ;
40+
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+ """ ;
4355
44- @ Inject
4556 public CompasSclDataPostgreSQLRepository (DataSource dataSource ) {
4657 this .dataSource = dataSource ;
4758 }
@@ -56,6 +67,7 @@ public List<IItem> list(SclFileType type) {
5667 from (select distinct on (scl_file.id) *
5768 from scl_file
5869 where scl_file.type = ?
70+ and scl_file.is_deleted = false
5971 order by scl_file.id
6072 , scl_file.major_version desc
6173 , scl_file.minor_version desc
@@ -115,6 +127,7 @@ left outer join (
115127 and scl_data.patch_version = scl_file.patch_version
116128 where scl_file.id = ?
117129 and scl_file.type = ?
130+ and scl_file.is_deleted = false
118131 order by scl_file.major_version
119132 , scl_file.minor_version
120133 , scl_file.patch_version
@@ -162,6 +175,7 @@ public String findByUUID(SclFileType type, UUID id, Version version) {
162175 and scl_file.major_version = ?
163176 and scl_file.minor_version = ?
164177 and scl_file.patch_version = ?
178+ and scl_file.is_deleted = false
165179 """ ;
166180
167181 try (var connection = dataSource .getConnection ();
@@ -191,6 +205,7 @@ public boolean hasDuplicateSclName(SclFileType type, String name) {
191205 select distinct on (scl_file.id) scl_file.name
192206 from scl_file
193207 where scl_file.type = ?
208+ and scl_file.is_deleted = false
194209 order by scl_file.id
195210 , scl_file.major_version desc
196211 , scl_file.minor_version desc
@@ -221,6 +236,7 @@ public IAbstractItem findMetaInfoByUUID(SclFileType type, UUID id) {
221236 from scl_file
222237 where scl_file.id = ?
223238 and scl_file.type = ?
239+ and scl_file.is_deleted = false
224240 order by scl_file.major_version desc, scl_file.minor_version desc, scl_file.patch_version desc
225241 """ ;
226242
@@ -304,14 +320,9 @@ insert into scl_label(scl_id, major_version, minor_version, patch_version, label
304320 @ Override
305321 @ Transactional (REQUIRED )
306322 public void delete (SclFileType type , UUID id ) {
307- var sql = """
308- delete from scl_file
309- where scl_file.id = ?
310- and scl_file.type = ?
311- """ ;
312323
313324 try (var connection = dataSource .getConnection ();
314- var stmt = connection .prepareStatement (sql )) {
325+ var stmt = connection .prepareStatement (DELETE_SCL_FILE_SQL )) {
315326 stmt .setObject (1 , id );
316327 stmt .setString (2 , type .name ());
317328 stmt .executeUpdate ();
@@ -323,17 +334,9 @@ public void delete(SclFileType type, UUID id) {
323334 @ Override
324335 @ Transactional (REQUIRED )
325336 public void delete (SclFileType type , UUID id , Version version ) {
326- var sql = """
327- delete from scl_file
328- where scl_file.id = ?
329- and scl_file.type = ?
330- and scl_file.major_version = ?
331- and scl_file.minor_version = ?
332- and scl_file.patch_version = ?
333- """ ;
334337
335338 try (var connection = dataSource .getConnection ();
336- var stmt = connection .prepareStatement (sql )) {
339+ var stmt = connection .prepareStatement (DELETE_SCL_FILE_SQL_BY_VERSION )) {
337340 stmt .setObject (1 , id );
338341 stmt .setString (2 , type .name ());
339342 stmt .setInt (3 , version .getMajorVersion ());
0 commit comments