@@ -17,12 +17,27 @@ namespace Coderr.Server.SqlServer.Core.Incidents.Queries
1717 public class FindIncidentsHandler : IQueryHandler < FindIncidents , FindIncidentsResult >
1818 {
1919 private readonly IAdoNetUnitOfWork _uow ;
20+ private string _where = "" ;
21+ private string _joins = "" ;
2022
2123 public FindIncidentsHandler ( IAdoNetUnitOfWork uow )
2224 {
2325 _uow = uow ;
2426 }
2527
28+ private void AppendWhere ( string constraint )
29+ {
30+ if ( _where == "" )
31+ _where = " WHERE " + constraint + "\r \n " ;
32+ else
33+ _where += " AND " + constraint + "\r \n " ;
34+ }
35+
36+ private void AppendJoin ( string clause )
37+ {
38+ _joins += clause + "\r \n " ;
39+ }
40+
2641 public async Task < FindIncidentsResult > HandleAsync ( IMessageContext context , FindIncidents query )
2742 {
2843 using ( var cmd = ( DbCommand ) _uow . CreateCommand ( ) )
@@ -31,17 +46,15 @@ public async Task<FindIncidentsResult> HandleAsync(IMessageContext context, Find
3146 FROM Incidents
3247 JOIN Applications ON (Applications.Id = Incidents.ApplicationId)" ;
3348
34- var startWord = " WHERE " ;
3549 if ( ! string . IsNullOrEmpty ( query . Version ) )
3650 {
3751 var versionId =
3852 _uow . ExecuteScalar ( "SELECT Id FROM ApplicationVersions WHERE Version = @version" ,
3953 new { version = query . Version } ) ;
4054
41- sqlQuery += " JOIN IncidentVersions ON (Incidents.Id = IncidentVersions.IncidentId)" +
42- " WHERE IncidentVersions.VersionId = @versionId";
55+ AppendJoin ( " JOIN IncidentVersions ON (Incidents.Id = IncidentVersions.IncidentId)") ;
56+ AppendWhere ( " IncidentVersions.VersionId = @versionId") ;
4357 cmd . AddParameter ( "versionId" , versionId ) ;
44- startWord = " AND " ;
4558 }
4659 if ( query . Tags != null && query . Tags . Length > 0 )
4760 {
@@ -52,23 +65,22 @@ WHERE TagName IN ({0})
5265 AND IncidentTags.IncidentId=Incidents.Id
5366 GROUP BY IncidentId
5467 HAVING Count(IncidentTags.Id) = {1}
55- ))
56- " ;
68+ ))" ;
5769 var ps = "" ;
5870 for ( int i = 0 ; i < query . Tags . Length ; i ++ )
5971 {
6072 ps += $ "@tag{ i } , ";
6173 cmd . AddParameter ( $ "@tag{ i } ", query . Tags [ i ] ) ;
6274 }
6375
64- sqlQuery += string . Format ( ourSql , ps . Remove ( ps . Length - 2 , 2 ) , query . Tags . Length ) ;
76+ var sql = string . Format ( ourSql , ps . Remove ( ps . Length - 2 , 2 ) , query . Tags . Length ) ;
77+ AppendJoin ( sql ) ;
6578 }
6679
6780 if ( query . EnvironmentIds != null && query . EnvironmentIds . Length > 0 )
6881 {
69- sqlQuery += " JOIN IncidentEnvironments ON (Incidents.Id = IncidentEnvironments.IncidentId)" +
70- $ " WHERE IncidentEnvironments.EnvironmentId IN ({ string . Join ( ", " , query . EnvironmentIds ) } )";
71- startWord = " AND " ;
82+ AppendJoin ( "JOIN IncidentEnvironments ON (Incidents.Id = IncidentEnvironments.IncidentId)" ) ;
83+ AppendWhere ( $ "IncidentEnvironments.EnvironmentId IN ({ string . Join ( ", " , query . EnvironmentIds ) } )") ;
7284 }
7385
7486 if ( ! string . IsNullOrEmpty ( query . ContextCollectionPropertyValue )
@@ -79,7 +91,7 @@ HAVING Count(IncidentTags.Id) = {1}
7991 where += AddContextProperty ( cmd , where , "PropertyName" , "ContextPropertyName" , query . ContextCollectionPropertyName ) ;
8092 where += AddContextProperty ( cmd , where , "Value" , "ContextPropertyValue" , query . ContextCollectionPropertyValue ) ;
8193 if ( where . EndsWith ( " AND " ) )
82- where = where . Remove ( where . Length - 5 , 5 ) ;
94+ where = where . Substring ( 0 , where . Length - 5 ) ;
8395 var ourSql =
8496 $@ "with ContextSearch (IncidentId)
8597 as (
@@ -89,7 +101,8 @@ join ErrorReportCollectionProperties ON (ErrorReports.Id = ErrorReportCollection
89101 WHERE { where }
90102 )
91103" ;
92- sqlQuery = ourSql + sqlQuery + " join ContextSearch ON (Incidents.Id = ContextSearch.IncidentId)\r \n " ;
104+ sqlQuery = ourSql + sqlQuery ;
105+ AppendJoin ( "join ContextSearch ON (Incidents.Id = ContextSearch.IncidentId)" ) ;
93106 }
94107
95108 if ( query . ApplicationIds != null && query . ApplicationIds . Length > 0 )
@@ -104,7 +117,7 @@ join ErrorReportCollectionProperties ON (ErrorReports.Id = ErrorReportCollection
104117 }
105118
106119 var ids = string . Join ( "," , query . ApplicationIds ) ;
107- sqlQuery += $ " { startWord } Applications.Id IN ({ ids } )";
120+ AppendWhere ( $ " Applications.Id IN ({ ids } )") ;
108121 }
109122 else if ( ! context . Principal . IsSysAdmin ( ) )
110123 {
@@ -117,12 +130,12 @@ join ErrorReportCollectionProperties ON (ErrorReports.Id = ErrorReportCollection
117130 return new FindIncidentsResult { Items = new FindIncidentsResultItem [ 0 ] } ;
118131 }
119132
120- sqlQuery += $ " { startWord } Applications.Id IN({ string . Join ( "," , appIds ) } )";
133+ AppendWhere ( $ " Applications.Id IN({ string . Join ( "," , appIds ) } )") ;
121134 }
122135
123136 if ( ! string . IsNullOrWhiteSpace ( query . FreeText ) )
124137 {
125- sqlQuery += @" AND (
138+ AppendWhere ( @" (
126139 Incidents.Id IN
127140 (
128141 SELECT Distinct IncidentId
@@ -131,50 +144,51 @@ WHERE StackTrace LIKE @FreeText
131144 OR ErrorReports.Title LIKE @FreeText
132145 OR ErrorReports.ErrorId LIKE @FreeText
133146 OR Incidents.Description LIKE @FreeText)
134- )" ;
147+ )" ) ;
135148 cmd . AddParameter ( "FreeText" , $ "%{ query . FreeText } %") ;
136149 }
137150
138151
139152
140- sqlQuery += " AND (" ;
153+ _where += " AND (" ;
141154 if ( query . IsIgnored )
142- sqlQuery += $ "State = { ( int ) IncidentState . Ignored } OR ";
155+ _where += $ "State = { ( int ) IncidentState . Ignored } OR ";
143156 if ( query . IsNew )
144- sqlQuery += $ "State = { ( int ) IncidentState . New } OR ";
157+ _where += $ "State = { ( int ) IncidentState . New } OR ";
145158 if ( query . IsClosed )
146- sqlQuery += $ "State = { ( int ) IncidentState . Closed } OR ";
159+ _where += $ "State = { ( int ) IncidentState . Closed } OR ";
147160 if ( query . IsAssigned )
148- sqlQuery += $ "State = { ( int ) IncidentState . Active } OR ";
161+ _where += $ "State = { ( int ) IncidentState . Active } OR ";
149162 if ( query . ReOpened )
150- sqlQuery += "IsReOpened = 1 OR " ;
163+ _where += "IsReOpened = 1 OR " ;
151164
152165
153- if ( sqlQuery . EndsWith ( "OR " ) )
154- sqlQuery = sqlQuery . Remove ( sqlQuery . Length - 4 ) + ") " ;
166+ if ( _where . EndsWith ( "OR " ) )
167+ _where = _where . Remove ( _where . Length - 4 ) + ") " ;
155168 else
156- sqlQuery = sqlQuery . Remove ( sqlQuery . Length - 5 ) ;
169+ _where = _where . Remove ( _where . Length - 5 ) ;
157170
158171 if ( query . MinDate > DateTime . MinValue )
159172 {
160- sqlQuery += " AND Incidents.LastReportAtUtc >= @minDate";
173+ AppendWhere ( " Incidents.LastReportAtUtc >= @minDate") ;
161174 cmd . AddParameter ( "minDate" , query . MinDate ) ;
162175 }
163176 if ( query . MaxDate < DateTime . MaxValue )
164177 {
165- sqlQuery += " AND Incidents.LastReportAtUtc <= @maxDate";
178+ AppendWhere ( " Incidents.LastReportAtUtc <= @maxDate") ;
166179 cmd . AddParameter ( "maxDate" , query . MaxDate ) ;
167180 }
168181
169182 if ( query . AssignedToId > 0 )
170183 {
171- sqlQuery += "AND AssignedToId = @assignedTo";
184+ AppendWhere ( " AssignedToId = @assignedTo") ;
172185 cmd . AddParameter ( "assignedTo" , query . AssignedToId ) ;
173186 }
174187
175188
176189
177190 //count first;
191+ sqlQuery += _joins + _where ;
178192 cmd . CommandText = string . Format ( sqlQuery , "count(Incidents.Id)" ) ;
179193 var count = await cmd . ExecuteScalarAsync ( ) ;
180194
0 commit comments