/// <summary> /// Performs the actual quoting of the indentifier. Passing a prefixed indentifier /// (ex: "table.column") is NOT valid, and should be passed to the /// <see cref="ApplyQuotes(string[], IdentifierQuoteMode, IdentifierQuoteKind))"/> /// method instead. /// </summary> private static string ApplyQuotes(string value, IdentifierQuoteKind kind) { // Don't escape wildcard if (value == "*") { return(value); } // Apply quotes to value var chars = EscapeChars[kind]; return($"{chars[0]}{value}{chars[1]}"); }
/// <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); } } }
/// <summary> /// Creates a new instance of <see cref="WhereStatement"/> using the quoting settings /// from the supplied SQLiteContext /// </summary> public WhereStatement(SQLiteContext context) : this() { AttributeQuoteMode = context.IdentifierQuoteMode; AttributeQuoteKind = context.IdentifierQuoteKind; }