Using the VS2012 code analyzer i see the following code has a pretty low Maintainability Index(40) but i cant seem to think of a better way to design it. Does anyone have suggestions on cleaning this up?
This code is part of a search screen for a web app I'm developing. They have several text fields they can use as well as some select lists. Is there any way to simplify this or make it cleaner? I'm more interested in cleaning the VB up but if someone notices my sql has issues, im open for that too =-)
Edit: As some commentators correctly pointed out, this code doesn't sanitize the input. Most of the time I use AddInParameter but since this is a bit more dynamic, i decided to sanitize in the BLL before even calling this function. I figured it would help keep this DAL code cleaner if i did it in the BLL instead
Shared Function SearchSession(searchCriteria As SessionSearchCriteria) As List(Of Sessions_BO)
Dim Result As New List(Of Sessions_BO)
Dim DatabaseObject As Database = DatabaseFactory.CreateDatabase(SQLUtils.GetConnectionString)
Dim SqlString = <![CDATA[
SELECT DISTINCT
Sessions.sessionId, Sessions.copiedSessionId, Sessions.sessionTitle, Sessions.initialDate,
Sessions.sessionDescription
FROM Sessions LEFT OUTER JOIN
CurriculumSessionLink ON Sessions.sessionId = CurriculumSessionLink.sessionId LEFT OUTER JOIN
CurriculaHeader ON CurriculumSessionLink.curriculumId = CurriculaHeader.curriculumId LEFT OUTER JOIN
TargetLearnerCurriculum ON CurriculaHeader.curriculumId = TargetLearnerCurriculum.curriculumId LEFT OUTER JOIN
CurriculumKeywords ON CurriculaHeader.curriculumId = CurriculumKeywords.curriculumId LEFT OUTER JOIN
SessionAuthors ON Sessions.sessionId = SessionAuthors.sessionId LEFT OUTER JOIN
CurriculumKeywords AS CK ON CurriculaHeader.curriculumId = CK.curriculumId LEFT OUTER JOIN
SessionTypeSessions ON Sessions.sessionId = SessionTypeSessions.sessionId
]]>.Value
If (Not String.IsNullOrEmpty(searchCriteria.SearchSessionTitle)) _
Or (Not String.IsNullOrEmpty(searchCriteria.SearchSessionDateFrom)) _
Or (Not String.IsNullOrEmpty(searchCriteria.SearchSessionDateTo)) _
Or (searchCriteria.SessionType <> -1) _
Or (searchCriteria.SessionAuthor <> -1) _
Or (Not searchCriteria.SessionSearchKeywords Is Nothing) _
Or (Not searchCriteria.TargetLearners Is Nothing) _
Or (searchCriteria.SessionCurriculum <> -1) Then
Dim whereStr As String = BuildWhereClause(searchCriteria)
SqlString &= If(whereStr.Length > 0, " WHERE " & whereStr, "")
End If
Try
Using DatabaseCommand = DatabaseObject.GetSqlStringCommand(SqlString)
Using DataReader As IDataReader = DatabaseObject.ExecuteReader(DatabaseCommand)
While DataReader.Read()
Result.Add(FillSessions_BO(DataReader))
End While
End Using
End Using
Catch ex As Exception
Dim err As New ErrorLog_BO
err.ErrorText = ex.Message
End Try
Return Result
End Function
Shared Function BuildWhereClause(searchCriteria As SessionSearchCriteria) As String
Dim whereStr As New StringBuilder()
If Not String.IsNullOrEmpty(searchCriteria.SearchSessionTitle) Then
whereStr.Append(" ( Sessions.sessionTitle LIKE '%" & searchCriteria.SearchSessionTitle & "%') ")
End If
If searchCriteria.SessionAuthor <> -1 Then
If whereStr.Length > 0 Then
whereStr.Append(" AND ")
End If
whereStr.Append(" (SessionAuthors.authorId = " & searchCriteria.SessionAuthor & ") ")
End If
If searchCriteria.SessionType <> -1 Then
If whereStr.Length > 0 Then
whereStr.Append(" AND ")
End If
whereStr.Append(" (SessionTypeSessions.sessionTypeId = " & searchCriteria.SessionType & ") ")
End If
If searchCriteria.SessionCurriculum <> -1 Then
If whereStr.Length > 0 Then
whereStr.Append(" AND ")
End If
whereStr.Append(" (CurriculumSessionLink.curriculumId = " & searchCriteria.SessionCurriculum & " ) ")
End If
If Not String.IsNullOrEmpty(searchCriteria.SearchSessionDateFrom) And Not String.IsNullOrEmpty(searchCriteria.SearchSessionDateTo) Then
If whereStr.Length > 0 Then
whereStr.Append(" AND ")
End If
whereStr.Append("( initialDate BETWEEN '" & searchCriteria.SearchSessionDateFrom & "' AND '" & searchCriteria.SearchSessionDateTo & "')")
End If
If Not searchCriteria.SessionSearchKeywords Is Nothing Then
If whereStr.Length > 0 Then
whereStr.Append(" AND ")
End If
whereStr.Append("(")
For i As Integer = 0 To searchCriteria.SessionSearchKeywords.Count - 1
whereStr.Append(" CurriculumKeywords.keywordId = " & searchCriteria.SessionSearchKeywords(i) & " ")
If i < searchCriteria.SessionSearchKeywords.Count - 1 Then
whereStr.Append(" OR ")
End If
Next
whereStr.Append(")")
End If
If Not searchCriteria.TargetLearners Is Nothing Then
If whereStr.Length > 0 Then
whereStr.Append(" AND ")
End If
whereStr.Append("(")
For i As Integer = 0 To searchCriteria.TargetLearners.Count - 1
whereStr.Append(" TargetLearnerCurriculum.targetLearnerId = " & searchCriteria.TargetLearners(i) & " ")
If i < searchCriteria.TargetLearners.Count - 1 Then
whereStr.Append(" OR ")
End If
Next
whereStr.Append(")")
End If
Return whereStr.ToString()
End Function
"Robert'); DROP TABLE Students;--"
, what do I get? \$\endgroup\$