/// <summary> /// Parses a given query and tries to determine the names of the fields to be retrieved. Those field names are then returned as a string array. /// </summary> /// <param name="QueryString">An initialized string value containing the command string to parse for field names.</param> /// <param name="option">A value from the QueryFieldParseOption enumeration which defines how to parse the query.</param> /// <returns>Returns a string array containing one element for each field names parse from the command string.</returns> public static string[] GetFields(string QueryString, QueryFieldParseOption option) { if (string.IsNullOrEmpty(QueryString)) { return(null); } // Get a string value containing only the field names from the query string qryFields = ""; if (QueryString.ToUpper().IndexOf("DISTINCT") < 0) { qryFields = QueryString.Substring(QueryString.ToUpper().IndexOf("SELECT") + 7, QueryString.ToUpper().IndexOf("FROM") - (QueryString.ToUpper().IndexOf("SELECT") + 7)); } else { qryFields = QueryString.Substring(QueryString.ToUpper().IndexOf("DISTINCT") + 9, QueryString.ToUpper().IndexOf("FROM") - (QueryString.ToUpper().IndexOf("DISTINCT") + 9)); } // Now, parse the fields into a string array. string[] fields = qryFields.Split(','); // Create an string array to hold the 'groomed' field names. string[] foundFields = new string[fields.Length]; for (int i = 0; i < fields.Length; i++) { string fieldName = ""; if (fields[i].ToUpper().IndexOf(" AS ") < 0) { fieldName = fields[i]; } else { switch (option) { case QueryFieldParseOption.None: fieldName = fields[i]; break; case QueryFieldParseOption.DestinationName: fieldName = fields[i].Substring(fields[i].ToUpper().IndexOf(" AS ") + 4); break; case QueryFieldParseOption.OriginalName: fieldName = fields[i].Substring(0, fields[i].ToUpper().IndexOf(" AS ")); break; } } foundFields[i] = fieldName.Trim(); } // And, finally, return the completed array of field names. return(foundFields); }
/// <summary> /// Parses a given query and tries to determine the names of the fields to be retrieved. Those field names are then returned as a string array. /// </summary> /// <param name="QueryString">An initialized string value containing the command string to parse for field names.</param> /// <param name="option">A value from the QueryFieldParseOption enumeration which defines how to parse the query.</param> /// <returns>Returns a string array containing one element for each field names parse from the command string.</returns> public static string[] GetFields(string QueryString, QueryFieldParseOption option) { if (string.IsNullOrEmpty(QueryString)) return null; // Get a string value containing only the field names from the query string qryFields = ""; if (QueryString.ToUpper().IndexOf("DISTINCT") < 0) qryFields = QueryString.Substring(QueryString.ToUpper().IndexOf("SELECT") + 7, QueryString.ToUpper().IndexOf("FROM") - (QueryString.ToUpper().IndexOf("SELECT") + 7)); else qryFields = QueryString.Substring(QueryString.ToUpper().IndexOf("DISTINCT") + 9, QueryString.ToUpper().IndexOf("FROM") - (QueryString.ToUpper().IndexOf("DISTINCT") + 9)); // Now, parse the fields into a string array. string[] fields = qryFields.Split(','); // Create an string array to hold the 'groomed' field names. string[] foundFields = new string[fields.Length]; for (int i = 0; i < fields.Length; i++) { string fieldName = ""; if (fields[i].ToUpper().IndexOf(" AS ") < 0) fieldName = fields[i]; else switch (option) { case QueryFieldParseOption.None: fieldName = fields[i]; break; case QueryFieldParseOption.DestinationName: fieldName = fields[i].Substring(fields[i].ToUpper().IndexOf(" AS ") + 4); break; case QueryFieldParseOption.OriginalName: fieldName = fields[i].Substring(0, fields[i].ToUpper().IndexOf(" AS ")); break; } foundFields[i] = fieldName.Trim(); } // And, finally, return the completed array of field names. return foundFields; }