private SqlString AppendWhere(string commandText, Clauses clauses, SegmentPositions segmentPositions) { if (segmentPositions.WhereIndex > -1 && (clauses & Clauses.Where) == Clauses.Where) { // Remove the WHERE keyword and the subsequent space var startIndex = segmentPositions.WhereIndex + 6; var length = commandText.Length - startIndex; if (segmentPositions.GroupByIndex > -1) { // Remove the space before the GROUP BY keyword length = segmentPositions.GroupByIndex - startIndex; } else if (segmentPositions.OrderByIndex > -1) { // Remove the space before the ORDER BY keyword length = segmentPositions.OrderByIndex - startIndex; } var segment = commandText.Substring(startIndex, length).Trim(); this.Where = segment; } return(this); }
internal static SegmentPositions GetSegmentPositions(string commandText) { var segmentPositions = new SegmentPositions( commandText.IndexOf(" FROM", 0, commandText.Length, StringComparison.OrdinalIgnoreCase), commandText.IndexOf(" WHERE", 0, commandText.Length, StringComparison.OrdinalIgnoreCase), commandText.IndexOf(" GROUP BY", 0, commandText.Length, StringComparison.OrdinalIgnoreCase), commandText.IndexOf(" ORDER BY", 0, commandText.Length, StringComparison.OrdinalIgnoreCase)); return(segmentPositions); }
private void AppendOrderBy(string commandText, Clauses clauses, SegmentPositions segmentPositions) { if (segmentPositions.OrderByIndex > -1 && (clauses & Clauses.OrderBy) == Clauses.OrderBy) { // Remove the ORDER BY keyword and the subsequent space int startIndex = segmentPositions.OrderByIndex + 10; int length = commandText.Length - startIndex; string segment = commandText.Substring(startIndex, length).Trim(); OrderBy = segment; } }
private SqlString AppendOrderBy(string commandText, Clauses clauses, SegmentPositions segmentPositions) { if (segmentPositions.OrderByIndex > -1 && (clauses & Clauses.OrderBy) == Clauses.OrderBy) { // Remove the ORDER BY keyword and the subsequent space var startIndex = segmentPositions.OrderByIndex + 10; var length = commandText.Length - startIndex; var segment = commandText.Substring(startIndex, length).Trim(); this.OrderBy = segment; } return(this); }
private SqlString AppendSelect(string commandText, Clauses clauses, SegmentPositions segmentPositions) { if (segmentPositions.FromIndex > -1 && (clauses & Clauses.Select) == Clauses.Select) { // Remove the SELECT keyword and the subsequent space var startIndex = 7; // Remove the space before the FROM keyword var length = segmentPositions.FromIndex - startIndex; var segment = commandText.Substring(startIndex, length).Trim(); this.Select = segment; } return(this); }
private SqlString AppendGroupBy(string commandText, Clauses clauses, SegmentPositions segmentPositions) { if (segmentPositions.GroupByIndex > -1 && (clauses & Clauses.GroupBy) == Clauses.GroupBy) { // Remove the GROUP BY keyword and the subsequent space int startIndex = segmentPositions.GroupByIndex + 10; int length = commandText.Length - startIndex; if (segmentPositions.OrderByIndex > -1) { // Remove the space before the ORDER BY keyword length = segmentPositions.OrderByIndex - startIndex; } string segment = commandText.Substring(startIndex, length).Trim(); GroupBy = segment; } return(this); }
/// <summary> /// Parses the specified command text into a SqlString instance populating the specified <see cref="Clauses"/> if /// present in the command text. /// </summary> /// <param name="commandText">The SQL command text.</param> /// <param name="clauses">The clauses to include in the SqlString.</param> /// <returns>An SqlString instance populating the specified <see cref="Clauses"/> if present in the command text.</returns> public static SqlString Parse(string commandText, Clauses clauses) { if (string.IsNullOrEmpty(commandText)) { return(new SqlString()); } if (commandText.Contains(Environment.NewLine)) { commandText = commandText.Replace(Environment.NewLine, " "); } var segmentPositions = SegmentPositions.GetSegmentPositions(commandText); var sqlString = new SqlString(); sqlString.AppendSelect(commandText, clauses, segmentPositions) .AppendFrom(commandText, clauses, segmentPositions) .AppendWhere(commandText, clauses, segmentPositions) .AppendGroupBy(commandText, clauses, segmentPositions) .AppendOrderBy(commandText, clauses, segmentPositions); return(sqlString); }
internal static SegmentPositions GetSegmentPositions(string commandText) { var segmentPositions = new SegmentPositions( commandText.IndexOf(" FROM", 0, commandText.Length, StringComparison.OrdinalIgnoreCase), commandText.IndexOf(" WHERE", 0, commandText.Length, StringComparison.OrdinalIgnoreCase), commandText.IndexOf(" GROUP BY", 0, commandText.Length, StringComparison.OrdinalIgnoreCase), commandText.IndexOf(" ORDER BY", 0, commandText.Length, StringComparison.OrdinalIgnoreCase)); return segmentPositions; }
private SqlString AppendWhere(string commandText, Clauses clauses, SegmentPositions segmentPositions) { if (segmentPositions.WhereIndex > -1 && (clauses & Clauses.Where) == Clauses.Where) { // Remove the WHERE keyword and the subsequent space var startIndex = segmentPositions.WhereIndex + 6; var length = commandText.Length - startIndex; if (segmentPositions.GroupByIndex > -1) { // Remove the space before the GROUP BY keyword length = segmentPositions.GroupByIndex - startIndex; } else if (segmentPositions.OrderByIndex > -1) { // Remove the space before the ORDER BY keyword length = segmentPositions.OrderByIndex - startIndex; } var segment = commandText.Substring(startIndex, length).Trim(); this.Where = segment; } return this; }
private SqlString AppendSelect(string commandText, Clauses clauses, SegmentPositions segmentPositions) { if (segmentPositions.FromIndex > -1 && (clauses & Clauses.Select) == Clauses.Select) { // Remove the SELECT keyword and the subsequent space var startIndex = 7; // Remove the space before the FROM keyword var length = segmentPositions.FromIndex - startIndex; var segment = commandText.Substring(startIndex, length).Trim(); this.Select = segment; } return this; }
private SqlString AppendOrderBy(string commandText, Clauses clauses, SegmentPositions segmentPositions) { if (segmentPositions.OrderByIndex > -1 && (clauses & Clauses.OrderBy) == Clauses.OrderBy) { // Remove the ORDER BY keyword and the subsequent space var startIndex = segmentPositions.OrderByIndex + 10; var length = commandText.Length - startIndex; var segment = commandText.Substring(startIndex, length).Trim(); this.OrderBy = segment; } return this; }