@@ -19,7 +19,6 @@ package repository
19
19
import (
20
20
"fmt"
21
21
"github.com/devtron-labs/devtron/api/bean"
22
- "github.com/devtron-labs/devtron/internal/sql/repository/helper"
23
22
"github.com/go-pg/pg"
24
23
)
25
24
@@ -28,7 +27,7 @@ const EmptyLikeRegex = "%%"
28
27
func BuildQueryForParentTypeCIOrWebhook (listingFilterOpts bean.ArtifactsListFilterOptions ) (string , []interface {}) {
29
28
commonPaginatedQueryPart , commonPaginatedQueryParams := " cia.image LIKE ?" , []interface {}{listingFilterOpts .SearchString }
30
29
orderByClause := " ORDER BY cia.id DESC"
31
- limitOffsetQueryPart , limitOffsetQueryParams := fmt . Sprintf ( " LIMIT ? OFFSET ?" ) , []interface {}{listingFilterOpts .Limit , listingFilterOpts .Offset }
30
+ limitOffsetQueryPart , limitOffsetQueryParams := " LIMIT ? OFFSET ?" , []interface {}{listingFilterOpts .Limit , listingFilterOpts .Offset }
32
31
finalQuery := ""
33
32
var finalQueryParams []interface {}
34
33
var remainingQueryParams []interface {}
@@ -71,82 +70,123 @@ func BuildQueryForParentTypeCIOrWebhook(listingFilterOpts bean.ArtifactsListFilt
71
70
return finalQuery , finalQueryParams
72
71
}
73
72
74
- func BuildQueryForArtifactsForCdStage (listingFilterOptions bean.ArtifactsListFilterOptions ) string {
73
+ func BuildQueryForArtifactsForCdStage (listingFilterOptions bean.ArtifactsListFilterOptions ) ( string , [] interface {}) {
75
74
// expected result -> will fetch all successfully deployed artifacts ar parent stage plus its own stage. Along with this it will
76
75
// also fetch all artifacts generated by plugin at pre_cd or post_cd process (will use data_source in ci artifact table for this)
77
76
78
77
if listingFilterOptions .UseCdStageQueryV2 {
79
78
return buildQueryForArtifactsForCdStageV2 (listingFilterOptions )
80
79
}
81
80
81
+ var queryParams []interface {}
82
+
82
83
commonQuery := " from ci_artifact LEFT JOIN cd_workflow ON ci_artifact.id = cd_workflow.ci_artifact_id" +
83
84
" LEFT JOIN cd_workflow_runner ON cd_workflow_runner.cd_workflow_id=cd_workflow.id " +
84
85
" Where (((cd_workflow_runner.id in (select MAX(cd_workflow_runner.id) OVER (PARTITION BY cd_workflow.ci_artifact_id) FROM cd_workflow_runner inner join cd_workflow on cd_workflow.id=cd_workflow_runner.cd_workflow_id))" +
85
- " AND ((cd_workflow.pipeline_id= %v and cd_workflow_runner.workflow_type = '%v' ) OR (cd_workflow.pipeline_id = %v AND cd_workflow_runner.workflow_type = '%v' AND cd_workflow_runner.status IN ('Healthy','Succeeded') )))" +
86
- " OR (ci_artifact.component_id = %v and ci_artifact.data_source= '%v' ))" +
87
- " AND (ci_artifact.image LIKE '%v' )"
86
+ " AND ((cd_workflow.pipeline_id = ? and cd_workflow_runner.workflow_type = ?) OR (cd_workflow.pipeline_id = ? AND cd_workflow_runner.workflow_type = ? AND cd_workflow_runner.status IN ('Healthy','Succeeded') )))" +
87
+ " OR (ci_artifact.component_id = ? and ci_artifact.data_source = ?))" +
88
+ " AND (ci_artifact.image LIKE ?)"
89
+
90
+ queryParams = append (queryParams ,
91
+ listingFilterOptions .PipelineId ,
92
+ listingFilterOptions .StageType ,
93
+ listingFilterOptions .ParentId ,
94
+ listingFilterOptions .ParentStageType ,
95
+ listingFilterOptions .ParentId ,
96
+ listingFilterOptions .PluginStage ,
97
+ listingFilterOptions .SearchString )
88
98
89
- commonQuery = fmt .Sprintf (commonQuery , listingFilterOptions .PipelineId , listingFilterOptions .StageType , listingFilterOptions .ParentId , listingFilterOptions .ParentStageType , listingFilterOptions .ParentId , listingFilterOptions .PluginStage , listingFilterOptions .SearchString )
90
99
if len (listingFilterOptions .ExcludeArtifactIds ) > 0 {
91
- commonQuery = commonQuery + fmt .Sprintf (" AND ( ci_artifact.id NOT IN (%v))" , helper .GetCommaSepratedString (listingFilterOptions .ExcludeArtifactIds ))
100
+ commonQuery += " AND ci_artifact.id NOT IN (?)"
101
+ queryParams = append (queryParams , pg .In (listingFilterOptions .ExcludeArtifactIds ))
92
102
}
93
103
94
104
totalCountQuery := "SELECT COUNT(DISTINCT ci_artifact.id) as total_count " + commonQuery
95
- selectQuery := fmt .Sprintf ("SELECT DISTINCT(ci_artifact.id) , (%v) " , totalCountQuery )
96
- //GroupByQuery := " GROUP BY cia.id "
97
- limitOffSetQuery := fmt .Sprintf (" order by ci_artifact.id desc LIMIT %v OFFSET %v" , listingFilterOptions .Limit , listingFilterOptions .Offset )
105
+ selectQuery := "SELECT DISTINCT(ci_artifact.id), (" + totalCountQuery + ") "
106
+ limitOffSetQuery := " order by ci_artifact.id desc LIMIT ? OFFSET ?"
107
+
108
+ // Duplicate queryParams for the subquery
109
+ finalQueryParams := append (queryParams , queryParams ... )
110
+ finalQueryParams = append (finalQueryParams , listingFilterOptions .Limit , listingFilterOptions .Offset )
98
111
99
- //finalQuery := selectQuery + commonQuery + GroupByQuery + limitOffSetQuery
100
112
finalQuery := selectQuery + commonQuery + limitOffSetQuery
101
- return finalQuery
113
+ return finalQuery , finalQueryParams
102
114
}
103
115
104
- func buildQueryForArtifactsForCdStageV2 (listingFilterOptions bean.ArtifactsListFilterOptions ) string {
105
- whereCondition := fmt .Sprintf (" WHERE (id IN (" +
106
- " SELECT DISTINCT(cd_workflow.ci_artifact_id) as ci_artifact_id " +
107
- " FROM cd_workflow_runner" +
108
- " INNER JOIN cd_workflow ON cd_workflow.id = cd_workflow_runner.cd_workflow_id " +
109
- " AND (cd_workflow.pipeline_id = %d OR cd_workflow.pipeline_id = %d)" +
110
- " WHERE (" +
111
- " (cd_workflow.pipeline_id = %d AND cd_workflow_runner.workflow_type = '%s')" +
112
- " OR" +
113
- " (cd_workflow.pipeline_id = %d" +
114
- " AND cd_workflow_runner.workflow_type = '%s'" +
115
- " AND cd_workflow_runner.status IN ('Healthy','Succeeded')" +
116
- " )" +
117
- " ) ) " , listingFilterOptions .PipelineId , listingFilterOptions .ParentId , listingFilterOptions .PipelineId , listingFilterOptions .StageType , listingFilterOptions .ParentId , listingFilterOptions .ParentStageType )
118
-
119
- whereCondition = fmt .Sprintf (" %s OR (ci_artifact.component_id = %d AND ci_artifact.data_source= '%s' ))" , whereCondition , listingFilterOptions .ParentId , listingFilterOptions .PluginStage )
116
+ func buildQueryForArtifactsForCdStageV2 (listingFilterOptions bean.ArtifactsListFilterOptions ) (string , []interface {}) {
117
+ var queryParams []interface {}
118
+
119
+ whereCondition := " WHERE (id IN (" +
120
+ " SELECT DISTINCT(cd_workflow.ci_artifact_id) as ci_artifact_id " +
121
+ " FROM cd_workflow_runner" +
122
+ " INNER JOIN cd_workflow ON cd_workflow.id = cd_workflow_runner.cd_workflow_id " +
123
+ " AND (cd_workflow.pipeline_id = ? OR cd_workflow.pipeline_id = ?)" +
124
+ " WHERE (" +
125
+ " (cd_workflow.pipeline_id = ? AND cd_workflow_runner.workflow_type = ?)" +
126
+ " OR" +
127
+ " (cd_workflow.pipeline_id = ?" +
128
+ " AND cd_workflow_runner.workflow_type = ?" +
129
+ " AND cd_workflow_runner.status IN ('Healthy','Succeeded')" +
130
+ " )" +
131
+ " ) ) "
132
+
133
+ queryParams = append (queryParams ,
134
+ listingFilterOptions .PipelineId ,
135
+ listingFilterOptions .ParentId ,
136
+ listingFilterOptions .PipelineId ,
137
+ listingFilterOptions .StageType ,
138
+ listingFilterOptions .ParentId ,
139
+ listingFilterOptions .ParentStageType )
140
+
141
+ whereCondition += " OR (ci_artifact.component_id = ? AND ci_artifact.data_source = ?))"
142
+ queryParams = append (queryParams , listingFilterOptions .ParentId , listingFilterOptions .PluginStage )
143
+
120
144
if listingFilterOptions .SearchString != EmptyLikeRegex {
121
- whereCondition = whereCondition + fmt .Sprintf (" AND ci_artifact.image LIKE '%s' " , listingFilterOptions .SearchString )
145
+ whereCondition += " AND ci_artifact.image LIKE ?"
146
+ queryParams = append (queryParams , listingFilterOptions .SearchString )
122
147
}
148
+
123
149
if len (listingFilterOptions .ExcludeArtifactIds ) > 0 {
124
- whereCondition = whereCondition + fmt .Sprintf (" AND ( ci_artifact.id NOT IN (%s))" , helper .GetCommaSepratedString (listingFilterOptions .ExcludeArtifactIds ))
150
+ whereCondition += " AND ci_artifact.id NOT IN (?)"
151
+ queryParams = append (queryParams , pg .In (listingFilterOptions .ExcludeArtifactIds ))
125
152
}
126
153
127
- selectQuery := fmt .Sprintf (" SELECT ci_artifact.* ,COUNT(id) OVER() AS total_count " +
128
- " FROM ci_artifact" )
129
- ordeyByAndPaginated := fmt .Sprintf (" ORDER BY id DESC LIMIT %d OFFSET %d " , listingFilterOptions .Limit , listingFilterOptions .Offset )
130
- finalQuery := selectQuery + whereCondition + ordeyByAndPaginated
131
- return finalQuery
154
+ selectQuery := " SELECT ci_artifact.*, COUNT(id) OVER() AS total_count FROM ci_artifact"
155
+ orderByAndPaginated := " ORDER BY id DESC LIMIT ? OFFSET ?"
156
+ queryParams = append (queryParams , listingFilterOptions .Limit , listingFilterOptions .Offset )
157
+
158
+ finalQuery := selectQuery + whereCondition + orderByAndPaginated
159
+ return finalQuery , queryParams
132
160
}
133
161
134
- func BuildQueryForArtifactsForRollback (listingFilterOptions bean.ArtifactsListFilterOptions ) string {
162
+ func BuildQueryForArtifactsForRollback (listingFilterOptions bean.ArtifactsListFilterOptions ) (string , []interface {}) {
163
+ var queryParams []interface {}
164
+
135
165
commonQuery := " FROM cd_workflow_runner cdwr " +
136
166
" INNER JOIN cd_workflow cdw ON cdw.id=cdwr.cd_workflow_id " +
137
167
" INNER JOIN ci_artifact cia ON cia.id=cdw.ci_artifact_id " +
138
- " WHERE cdw.pipeline_id=%v AND cdwr.workflow_type = '%v' "
168
+ " WHERE cdw.pipeline_id = ? AND cdwr.workflow_type = ?"
169
+
170
+ queryParams = append (queryParams , listingFilterOptions .PipelineId , listingFilterOptions .StageType )
139
171
140
- commonQuery = fmt .Sprintf (commonQuery , listingFilterOptions .PipelineId , listingFilterOptions .StageType )
141
172
if listingFilterOptions .SearchString != EmptyLikeRegex {
142
- commonQuery += fmt .Sprintf (" AND cia.image LIKE '%v' " , listingFilterOptions .SearchString )
173
+ commonQuery += " AND cia.image LIKE ?"
174
+ queryParams = append (queryParams , listingFilterOptions .SearchString )
143
175
}
176
+
144
177
if len (listingFilterOptions .ExcludeWfrIds ) > 0 {
145
- commonQuery = fmt .Sprintf (" %s AND cdwr.id NOT IN (%s)" , commonQuery , helper .GetCommaSepratedString (listingFilterOptions .ExcludeWfrIds ))
178
+ commonQuery += " AND cdwr.id NOT IN (?)"
179
+ queryParams = append (queryParams , pg .In (listingFilterOptions .ExcludeWfrIds ))
146
180
}
181
+
147
182
totalCountQuery := " SELECT COUNT(cia.id) as total_count " + commonQuery
148
183
orderByQuery := " ORDER BY cdwr.id DESC "
149
- limitOffsetQuery := fmt .Sprintf ("LIMIT %v OFFSET %v" , listingFilterOptions .Limit , listingFilterOptions .Offset )
150
- finalQuery := fmt .Sprintf (" SELECT cdwr.id as cd_workflow_runner_id,cdwr.triggered_by,cdwr.started_on,cia.*,(%s) " , totalCountQuery ) + commonQuery + orderByQuery + limitOffsetQuery
151
- return finalQuery
184
+ limitOffsetQuery := " LIMIT ? OFFSET ?"
185
+
186
+ // Duplicate queryParams for the subquery
187
+ finalQueryParams := append (queryParams , queryParams ... )
188
+ finalQueryParams = append (finalQueryParams , listingFilterOptions .Limit , listingFilterOptions .Offset )
189
+
190
+ finalQuery := " SELECT cdwr.id as cd_workflow_runner_id,cdwr.triggered_by,cdwr.started_on,cia.*,(" + totalCountQuery + ") " + commonQuery + orderByQuery + limitOffsetQuery
191
+ return finalQuery , finalQueryParams
152
192
}
0 commit comments