예제 #1
0
        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);
        }
예제 #2
0
            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);
            }
예제 #3
0
        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;
            }
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
        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);
        }
예제 #7
0
        /// <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);
        }
예제 #8
0
            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;
            }
예제 #9
0
        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;
        }
예제 #10
0
        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;
        }
예제 #11
0
        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;
        }