﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Couchbase.Linq.QueryGeneration
{
    /// <summary>
    /// Helpers for N1QL query generation
    /// </summary>
    public static class N1QlHelpers
    {

        /// <summary>
        ///     Escapes a N1QL identifier using tick (`) characters
        /// </summary>
        /// <param name="identifier">The identifier to format</param>
        /// <returns>An escaped identifier</returns>
        public static string EscapeIdentifier(string identifier)
        {
            if (identifier == null)
            {
                throw new ArgumentNullException("identifier");
            }

            if (identifier.IndexOf('`') >= 0)
            {
                // This should not occur, and is primarily in place to prevent N1QL injection attacks
                // So it isn't performance critical to perform this replace in the StringBuilder

                identifier = identifier.Replace("`", "``");
            }

            var sb = new StringBuilder(identifier.Length + 2);

            sb.Append('`');
            sb.Append(identifier);
            sb.Append('`');

            return sb.ToString();
        }

    }
}

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Couchbase.Linq.QueryGeneration
{
    /// <summary>
    /// Helpers for N1QL query generation
    /// </summary>
    public static class N1QlHelpers
    {

        /// <summary>
        ///     Escapes a N1QL identifier using tick (`) characters
        /// </summary>
        /// <param name="identifier">The identifier to format</param>
        /// <returns>An escaped identifier</returns>
        public static string EscapeIdentifier(string identifier)
        {
            if (identifier == null)
            {
                throw new ArgumentNullException("identifier");
            }

            if (identifier.IndexOf('`') >= 0)
            {
                // This should not occur, and is primarily in place to prevent N1QL injection attacks
                // So it isn't performance critical to perform this replace in a StringBuilder with the concatenation

                identifier = identifier.Replace("`", "``");
            }

            return string.Concat("`", identifier, "`");
        }

    }
}

Improve N1QL identifier escaping performance
Improve N1QL identifier escaping performance

Modifications
-------------
Changed N1QlHelpers.EscapeIdentifier to using string.Concat instead of a
StringBuilder to wrap identifiers in tick marks.  When repeating the call
to EscapeIdentifier 10,000,000 times, it was approximately 50% slower to
use a StringBuilder for this function.  string.Concat, using strings and
not characters for the ticks, was the fastest alternative found.
