/// <summary> /// Applies quoting to each identifier parameter that needs it, based on the IdentifierQuoteMode, /// and chains the result back into a string. /// </summary> private static string ApplyQuotes(string[] values, IdentifierQuoteMode mode, IdentifierQuoteKind kind) { var builder = new StringBuilder(); for (int i = 0; i < values.Length; i++) { // Do we need to apply quoting to this string? if (mode == IdentifierQuoteMode.All || (mode == IdentifierQuoteMode.KeywordsOnly && IsKeyword(values[i]))) { builder.Append(ApplyQuotes(values[i], kind)); } else { builder.Append(values[i]); } builder.AppendIf(i + 1 < values.Length, "."); } return(builder.ToString()); }
/// <summary> /// Takes an identifier and qoutes it if the name is a reserved keyword. Passing /// a prefixed identifier (ex: "table.column") is valid. /// </summary> /// <param name="value">The attribute name</param> /// <returns></returns> public static string QuoteIdentifier(string value, IdentifierQuoteMode mode, IdentifierQuoteKind kind) { // Lets make this simple and fast! if (mode == IdentifierQuoteMode.None) { return(value); } // Split the value by the period seperator, and determine if any identifiers are a keyword var parts = value.Split('.'); var hasKeyword = mode == IdentifierQuoteMode.All; if (mode == IdentifierQuoteMode.KeywordsOnly) { hasKeyword = (parts.Length > 1) ? ContainsKeyword(parts) : IsKeyword(value); } // Appy the quoting where needed.. if (parts.Length > 1) { switch (mode) { case IdentifierQuoteMode.All: return(ApplyQuotes(parts, mode, kind)); case IdentifierQuoteMode.KeywordsOnly: return((hasKeyword) ? ApplyQuotes(parts, mode, kind) : value); default: return(value); } } else // Non-array { switch (mode) { case IdentifierQuoteMode.All: return(ApplyQuotes(value, kind)); case IdentifierQuoteMode.KeywordsOnly: return((hasKeyword) ? ApplyQuotes(value, kind) : value); default: return(value); } } }