@@ -20,24 +20,28 @@ import (
20
20
"fmt"
21
21
"github.com/devtron-labs/devtron/api/bean"
22
22
"github.com/devtron-labs/devtron/internal/sql/repository/helper"
23
+ "github.com/go-pg/pg"
23
24
)
24
25
25
26
const EmptyLikeRegex = "%%"
26
27
27
- func BuildQueryForParentTypeCIOrWebhook (listingFilterOpts bean.ArtifactsListFilterOptions ) string {
28
- commonPaginatedQueryPart := fmt . Sprintf ( " cia.image LIKE '%v' " , listingFilterOpts .SearchString )
28
+ func BuildQueryForParentTypeCIOrWebhook (listingFilterOpts bean.ArtifactsListFilterOptions ) ( string , [] interface {}) {
29
+ commonPaginatedQueryPart , commonPaginatedQueryParams := " cia.image LIKE ? " , [] interface {}{ listingFilterOpts .SearchString }
29
30
orderByClause := " ORDER BY cia.id DESC"
30
- limitOffsetQueryPart := fmt .Sprintf (" LIMIT %v OFFSET %v" , listingFilterOpts .Limit , listingFilterOpts .Offset )
31
+ limitOffsetQueryPart , limitOffsetQueryParams := fmt .Sprintf (" LIMIT ? OFFSET ?" ), [] interface {}{ listingFilterOpts .Limit , listingFilterOpts .Offset }
31
32
finalQuery := ""
33
+ var finalQueryParams []interface {}
34
+ var remainingQueryParams []interface {}
32
35
if listingFilterOpts .ParentStageType == bean .CI_WORKFLOW_TYPE {
33
36
selectQuery := " SELECT cia.* "
34
37
remainingQuery := " FROM ci_artifact cia" +
35
38
" INNER JOIN ci_pipeline cp ON (cp.id=cia.pipeline_id or (cp.id=cia.component_id and cia.data_source='post_ci' ) )" +
36
- " INNER JOIN pipeline p ON (p.ci_pipeline_id = cp.id and p.id=%v )" +
39
+ " INNER JOIN pipeline p ON (p.ci_pipeline_id = cp.id and p.id=? )" +
37
40
" WHERE "
38
- remainingQuery = fmt . Sprintf ( remainingQuery , listingFilterOpts .PipelineId )
41
+ remainingQueryParams = [] interface {}{ listingFilterOpts .PipelineId }
39
42
if len (listingFilterOpts .ExcludeArtifactIds ) > 0 {
40
- remainingQuery += fmt .Sprintf ("cia.id NOT IN (%s) AND " , helper .GetCommaSepratedString (listingFilterOpts .ExcludeArtifactIds ))
43
+ remainingQuery += "cia.id NOT IN (?) AND "
44
+ remainingQueryParams = append (remainingQueryParams , pg .In (listingFilterOpts .ExcludeArtifactIds ))
41
45
}
42
46
43
47
countQuery := " SELECT count(cia.id) as total_count"
@@ -47,19 +51,24 @@ func BuildQueryForParentTypeCIOrWebhook(listingFilterOpts bean.ArtifactsListFilt
47
51
} else if listingFilterOpts .ParentStageType == bean .WEBHOOK_WORKFLOW_TYPE {
48
52
selectQuery := " SELECT cia.* "
49
53
remainingQuery := " FROM ci_artifact cia " +
50
- " WHERE cia.external_ci_pipeline_id = %v AND "
51
- remainingQuery = fmt . Sprintf ( remainingQuery , listingFilterOpts .ParentId )
54
+ " WHERE cia.external_ci_pipeline_id = ? AND "
55
+ remainingQueryParams = [] interface {}{ listingFilterOpts .ParentId }
52
56
if len (listingFilterOpts .ExcludeArtifactIds ) > 0 {
53
- remainingQuery += fmt .Sprintf ("cia.id NOT IN (%s) AND " , helper .GetCommaSepratedString (listingFilterOpts .ExcludeArtifactIds ))
57
+ remainingQuery += "cia.id NOT IN (?) AND "
58
+ remainingQueryParams = append (remainingQueryParams , pg .In (listingFilterOpts .ExcludeArtifactIds ))
54
59
}
55
60
56
61
countQuery := " SELECT count(cia.id) as total_count"
57
62
totalCountQuery := countQuery + remainingQuery + commonPaginatedQueryPart
58
63
selectQuery = fmt .Sprintf ("%s,(%s) " , selectQuery , totalCountQuery )
59
64
finalQuery = selectQuery + remainingQuery + commonPaginatedQueryPart + orderByClause + limitOffsetQueryPart
60
-
61
65
}
62
- return finalQuery
66
+ finalQueryParams = append (finalQueryParams , remainingQueryParams ... )
67
+ finalQueryParams = append (finalQueryParams , commonPaginatedQueryParams ... )
68
+ finalQueryParams = append (finalQueryParams , remainingQueryParams ... )
69
+ finalQueryParams = append (finalQueryParams , commonPaginatedQueryParams ... )
70
+ finalQueryParams = append (finalQueryParams , limitOffsetQueryParams ... )
71
+ return finalQuery , finalQueryParams
63
72
}
64
73
65
74
func BuildQueryForArtifactsForCdStage (listingFilterOptions bean.ArtifactsListFilterOptions ) string {
0 commit comments