25
25
import static org .lfenergy .compas .scl .data .exception .CompasSclDataServiceErrorCode .*;
26
26
27
27
public class CompasSclDataPostgreSQLRepository implements CompasSclDataRepository {
28
+ private static final String SElECT_METADATA_CLAUSE = "select id, name, major_version, minor_version, patch_version " ;
29
+ private static final String FROM_CLAUSE = " from scl_file " ;
30
+ private static final String DELETE_FROM_CLAUSE = "delete " + FROM_CLAUSE ;
31
+ private static final String WHERE_CLAUSE = " where " ;
32
+ private static final String AND_CLAUSE = " and " ;
33
+ private static final String ORDER_BY_CLAUSE = " order by id, major_version, minor_version, patch_version" ;
34
+
35
+ private static final String FILTER_ON_TYPE = "type = ?" ;
36
+ private static final String FILTER_ON_ID = "id = ?" ;
37
+ private static final String FILTER_ON_VERSION = "major_version = ? and minor_version = ? and patch_version = ? " ;
38
+
28
39
private static final String ID_FIELD = "id" ;
29
40
private static final String MAJOR_VERSION_FIELD = "major_version" ;
30
41
private static final String MINOR_VERSION_FIELD = "minor_version" ;
@@ -41,31 +52,31 @@ public CompasSclDataPostgreSQLRepository(DataSource dataSource) {
41
52
@ Override
42
53
@ Transactional (SUPPORTS )
43
54
public List <Item > list (SclType type ) {
44
- var sql = "select id, name, major_version, minor_version, patch_version" +
45
- " from scl_file outer_scl" +
46
- " where type = ?" +
47
- " and (id, major_version, minor_version, patch_version) in (" +
48
- // Last select the maximum patch version with the major/minor version per id.
49
- " select id, major_version, minor_version, max(patch_version)" +
50
- " from scl_file patch_scl" +
51
- " where patch_scl.type = outer_scl .type" +
52
- " and (id, major_version, minor_version) in (" +
53
- // Next select the maximum minor version with the major version per id.
54
- " select id, major_version, max(minor_version)" +
55
- " from scl_file minor_scl" +
56
- " where minor_scl.type = outer_scl .type" +
57
- " and (id, major_version) in (" +
58
- // First select the maximum major version per id.
59
- " select id, max(major_version)" +
60
- " from scl_file major_scl" +
61
- " where major_scl.type = outer_scl .type" +
62
- " group by id" +
63
- " )" +
64
- " group by id, major_version" +
65
- " )" +
66
- " group by id, major_version, minor_version" +
67
- " )" +
68
- " order by id, major_version, minor_version, patch_version" ;
55
+ var sql = SElECT_METADATA_CLAUSE
56
+ + FROM_CLAUSE
57
+ + WHERE_CLAUSE + FILTER_ON_TYPE
58
+ + " and (id, major_version, minor_version, patch_version) in ("
59
+ // Last select the maximum patch version with the major/minor version per id.
60
+ + " select id, major_version, minor_version, max(patch_version)"
61
+ + " from scl_file patch_scl"
62
+ + " where patch_scl.type = scl_file .type"
63
+ + " and (id, major_version, minor_version) in ("
64
+ // Next select the maximum minor version with the major version per id.
65
+ + " select id, major_version, max(minor_version)"
66
+ + " from scl_file minor_scl"
67
+ + " where minor_scl.type = scl_file .type"
68
+ + " and (id, major_version) in ("
69
+ // First select the maximum major version per id.
70
+ + " select id, max(major_version)"
71
+ + " from scl_file major_scl"
72
+ + " where major_scl.type = scl_file .type"
73
+ + " group by id"
74
+ + " )"
75
+ + " group by id, major_version"
76
+ + " )"
77
+ + " group by id, major_version, minor_version"
78
+ + " )"
79
+ + ORDER_BY_CLAUSE ;
69
80
70
81
var items = new ArrayList <Item >();
71
82
try (var connection = dataSource .getConnection ();
@@ -88,11 +99,11 @@ public List<Item> list(SclType type) {
88
99
@ Override
89
100
@ Transactional (SUPPORTS )
90
101
public List <Item > listVersionsByUUID (SclType type , UUID id ) {
91
- var sql = "select id, name, major_version, minor_version, patch_version"
92
- + " from scl_file"
93
- + " where id = ?"
94
- + " and type = ?"
95
- + " order by major_version, minor_version, patch_version" ;
102
+ var sql = SElECT_METADATA_CLAUSE
103
+ + FROM_CLAUSE
104
+ + WHERE_CLAUSE + FILTER_ON_ID
105
+ + AND_CLAUSE + FILTER_ON_TYPE
106
+ + ORDER_BY_CLAUSE ;
96
107
97
108
var items = new ArrayList <Item >();
98
109
try (var connection = dataSource .getConnection ();
@@ -126,12 +137,11 @@ public String findByUUID(SclType type, UUID id) {
126
137
@ Transactional (SUPPORTS )
127
138
public String findByUUID (SclType type , UUID id , Version version ) {
128
139
var sql = "select scl_data "
129
- + " from scl_file "
130
- + " where id = ?"
131
- + " and type = ?"
132
- + " and major_version = ? "
133
- + " and minor_version = ? "
134
- + " and patch_version = ? " ;
140
+ + FROM_CLAUSE
141
+ + WHERE_CLAUSE + FILTER_ON_ID
142
+ + AND_CLAUSE + FILTER_ON_TYPE
143
+ + AND_CLAUSE + FILTER_ON_VERSION ;
144
+
135
145
try (var connection = dataSource .getConnection ();
136
146
var stmt = connection .prepareStatement (sql )) {
137
147
stmt .setObject (1 , id );
@@ -155,11 +165,12 @@ public String findByUUID(SclType type, UUID id, Version version) {
155
165
@ Override
156
166
@ Transactional (SUPPORTS )
157
167
public SclMetaInfo findMetaInfoByUUID (SclType type , UUID id ) {
158
- var sql = "select id, name, major_version, minor_version, patch_version"
159
- + " from scl_file"
160
- + " where id = ?"
161
- + " and type = ?"
168
+ var sql = SElECT_METADATA_CLAUSE
169
+ + FROM_CLAUSE
170
+ + WHERE_CLAUSE + FILTER_ON_ID
171
+ + AND_CLAUSE + FILTER_ON_TYPE
162
172
+ " order by major_version desc, minor_version desc, patch_version desc" ;
173
+
163
174
try (var connection = dataSource .getConnection ();
164
175
var stmt = connection .prepareStatement (sql )) {
165
176
stmt .setObject (1 , id );
@@ -205,9 +216,10 @@ public void create(SclType type, UUID id, String name, String scl, Version versi
205
216
@ Override
206
217
@ Transactional (REQUIRED )
207
218
public void delete (SclType type , UUID id ) {
208
- var sql = "delete from scl_file "
209
- + " where id = ?"
210
- + " and type = ?" ;
219
+ var sql = DELETE_FROM_CLAUSE
220
+ + WHERE_CLAUSE + FILTER_ON_ID
221
+ + AND_CLAUSE + FILTER_ON_TYPE ;
222
+
211
223
try (var connection = dataSource .getConnection ();
212
224
var stmt = connection .prepareStatement (sql )) {
213
225
stmt .setObject (1 , id );
@@ -221,12 +233,11 @@ public void delete(SclType type, UUID id) {
221
233
@ Override
222
234
@ Transactional (REQUIRED )
223
235
public void delete (SclType type , UUID id , Version version ) {
224
- var sql = "delete from scl_file "
225
- + " where id = ?"
226
- + " and type = ?"
227
- + " and major_version = ? "
228
- + " and minor_version = ? "
229
- + " and patch_version = ? " ;
236
+ var sql = DELETE_FROM_CLAUSE
237
+ + WHERE_CLAUSE + FILTER_ON_ID
238
+ + AND_CLAUSE + FILTER_ON_TYPE
239
+ + AND_CLAUSE + FILTER_ON_VERSION ;
240
+
230
241
try (var connection = dataSource .getConnection ();
231
242
var stmt = connection .prepareStatement (sql )) {
232
243
stmt .setObject (1 , id );
0 commit comments