private static void ExtractColumnOrAliasNames(SqlString select, out List <SqlString> columnsOrAliases, out Dictionary <SqlString, SqlString> aliasToColumn) { columnsOrAliases = new List <SqlString>(); aliasToColumn = new Dictionary <SqlString, SqlString>(); IList <string> tokens = new QuotedAndParenthesisStringTokenizer(select.ToString()).GetTokens(); int index = 0; while (index < tokens.Count) { string token = tokens[index]; index += 1; if ("select".Equals(token, StringComparison.InvariantCultureIgnoreCase)) { continue; } if ("distinct".Equals(token, StringComparison.InvariantCultureIgnoreCase)) { continue; } if ("," == token) { continue; } if ("from".Equals(token, StringComparison.InvariantCultureIgnoreCase)) { break; } //handle composite expressions like 2 * 4 as foo while (index < tokens.Count && "as".Equals(tokens[index], StringComparison.InvariantCultureIgnoreCase) == false && "," != tokens[index]) { token = token + " " + tokens[index]; index += 1; } string alias = token; bool isFunctionCallOrQuotedString = token.Contains("'") || token.Contains("("); // this is heuristic guess, if the expression contains ' or (, it is probably // not appropriate to just slice parts off of it if (isFunctionCallOrQuotedString == false) { int dot = token.IndexOf('.'); if (dot != -1) { alias = token.Substring(dot + 1); } } // notice! we are checking here the existence of "as" "alias", two // tokens from the current one if (index + 1 < tokens.Count && "as".Equals(tokens[index], StringComparison.InvariantCultureIgnoreCase)) { alias = tokens[index + 1]; index += 2; //skip the "as" and the alias \ } columnsOrAliases.Add(new SqlString(alias)); aliasToColumn[SqlString.Parse(alias)] = SqlString.Parse(token); } }
private static void ExtractColumnOrAliasNames(SqlString select, out List <SqlString> columnsOrAliases, out Dictionary <SqlString, SqlString> aliasToColumn) { columnsOrAliases = new List <SqlString>(); aliasToColumn = new Dictionary <SqlString, SqlString>(); IList <SqlString> tokens = new QuotedAndParenthesisStringTokenizer(select).GetTokens(); int index = 0; while (index < tokens.Count) { SqlString token = tokens[index]; int nextTokenIndex = index += 1; if (token.StartsWithCaseInsensitive("select")) { continue; } if (token.StartsWithCaseInsensitive("distinct")) { continue; } if (token.StartsWithCaseInsensitive(",")) { continue; } if (token.StartsWithCaseInsensitive("from")) { break; } // handle composite expressions like "2 * 4 as foo" while ((nextTokenIndex < tokens.Count) && (tokens[nextTokenIndex].StartsWithCaseInsensitive("as") == false && tokens[nextTokenIndex].StartsWithCaseInsensitive(",") == false)) { SqlString nextToken = tokens[nextTokenIndex]; token = token.Append(nextToken); nextTokenIndex = index += 1; } // if there is no alias, the token and the alias will be the same SqlString alias = token; bool isFunctionCallOrQuotedString = token.IndexOfCaseInsensitive("'") >= 0 || token.IndexOfCaseInsensitive("(") >= 0; // this is heuristic guess, if the expression contains ' or (, it is probably // not appropriate to just slice parts off of it if (isFunctionCallOrQuotedString == false) { // its a simple column reference, so lets set the alias to the // column name minus the table qualifier if it exists int dot = token.IndexOfCaseInsensitive("."); if (dot != -1) { alias = token.Substring(dot + 1); } } // notice! we are checking here the existence of "as" "alias", two // tokens from the current one if (nextTokenIndex + 1 < tokens.Count) { SqlString nextToken = tokens[nextTokenIndex]; if (nextToken.IndexOfCaseInsensitive("as") >= 0) { SqlString tokenAfterNext = tokens[nextTokenIndex + 1]; alias = tokenAfterNext; index += 2; //skip the "as" and the alias } } columnsOrAliases.Add(alias); aliasToColumn[alias] = token; } }