static void logErrorMessage(ParsingException exception, SqlSelectStatement sqlStatement) { if (exception is ParameterParsingException) { ParameterParsingException paramException = sqlStatement.SqlStatementParsingException as ParameterParsingException; if (exception != null) { using (StreamWriter streamWriter = errorFile.AppendText()) { streamWriter.WriteLine(getLogErrorMessage(paramException.Message, paramException.ActualSql, paramException.ParameterSection)); } } } else { using (StreamWriter streamWriter = errorFile.AppendText()) { streamWriter.WriteLine(getLogErrorMessage(sqlStatement.SqlStatementParsingException.Message, exception.ActualSql, string.Empty)); } } }
// ( attempts to set the parameter and values in an organized list of key value pairs ) internal virtual void SetParameterKeyValuePairs() { int paramStartIndex = 0; string sqlSection = string.Empty; string parameterSection = string.Empty; this.parameters = new List<SqlParamKeyValuePair>(); try { // ( parameters are found after the first found semi colon (;) ) paramStartIndex = Sql.IndexOf(";") + 1; parameterSection = Sql.Substring(paramStartIndex, Sql.Length - paramStartIndex).Trim(); if (!string.IsNullOrWhiteSpace(parameterSection)) { sqlSection = Sql.Substring(0, paramStartIndex + 1).Replace(";", string.Empty).Trim(); var parameters = parameterSection.SplitByWord(new string[] { "select scope_identity()" }, new string[] { "=", ",", ";" }); if (parameters.Length > 1) { // ( parse parameters ) for (int ix = 0; ix < parameters.Length; ix += 2) { string key = parameters[ix].Trim(); if ((ix + 1) < parameters.Length) { string dataType = string.Empty; // ( get the value of the parameter ) string value = parameters[ix + 1].Split('[')[0].Replace(";", string.Empty).Trim(); // ( covers parameter values where string values contain pipes (|) ) if (value.Contains("|")) { // ( continue looping until no comma found ) while (ix + 1 < parameters.Length - 1) { ix++; value = value + "," + parameters[ix + 1]; } } // ( signals that a parameter data type has been found ) if (parameters[ix + 1].Contains("[")) { dataType = parameters[ix + 1].Split(new string[] { "[", "]" }, StringSplitOptions.RemoveEmptyEntries)[1].Split(new string[] { "Type:", "(" }, StringSplitOptions.RemoveEmptyEntries)[0].Trim(); } // ( signals that a parameter key has been found ) if (key.StartsWith("@")) { Parameters.Add(new SqlParamKeyValuePair() { Key = key, Value = value, DataType = dataType }); } else break; // ( were not interested in anything after this for this statement) } else break; // ( were not interested in anything after this for this statement) } } else { // ( abnormal parameter length ) SqlStatementParsingException = new ParameterParsingException("Exception parsing parameters.", sqlSection, parameterSection); } } } catch (Exception exception) { var sqlStatementParsingException = new ParameterParsingException("Unhandled exception parsing parameters", sqlSection, parameterSection, exception); SqlStatementParsingException = sqlStatementParsingException; throw sqlStatementParsingException; } }
// ( validates the sql before constructing its parts ) private bool IsValidateSql() { var openbraceMatches = Regex.Matches(Sql, @"\("); var closebraceMatches = Regex.Matches(Sql, @"\)"); if (openbraceMatches.Count > 0 && openbraceMatches.Count != closebraceMatches.Count) { SqlStatementParsingException = new ParsingException("There was an error parsing this SQL statement. (Enclosing braces do not match).", Sql); } var fromMatches = Regex.Matches(Sql, @"\bfrom\b", RegexOptions.IgnoreCase); if (fromMatches.Count <= 0) { SqlStatementParsingException = new ParsingException("The SQL select does not contain a 'From' clause.", Sql); } return (SqlStatementParsingException == null); }