ReplaceBrackets() public static method

public static ReplaceBrackets ( string expression, ISqlDialect dialect ) : string
expression string
dialect ISqlDialect
return string
Esempio n. 1
0
        public static string Dump(string sql, IDictionary <string, object> parameters, ISqlDialect dialect = null)
        {
            if (parameters == null)
            {
                return(sql);
            }

            var param = parameters.ToList();

            for (var i = 0; i < param.Count; i++)
            {
                var name = param[i].Key;
                if (!name.StartsWith("@"))
                {
                    param[i] = new KeyValuePair <string, object>("@" + name, param[i].Value);
                }
            }

            param.Sort((x, y) => y.Key.Length.CompareTo(x.Key.Length));

            var sb = new StringBuilder(sql);

            foreach (var pair in param)
            {
                sb.Replace(pair.Key, DumpParameterValue(pair.Value, dialect));
            }

            var text = DatabaseCaretReferences.Replace(sb.ToString());

            dialect = dialect ?? SqlSettings.DefaultDialect;
            var openBracket = dialect.OpenQuote;

            if (openBracket != '[')
            {
                text = BracketLocator.ReplaceBrackets(text, dialect);
            }

            var paramPrefix = dialect.ParameterPrefix;

            if (paramPrefix != '@')
            {
                text = ParamPrefixReplacer.Replace(text, paramPrefix);
            }

            return(text);
        }
Esempio n. 2
0
        /// <summary>
        /// Fixes the command text for target dialect by replacing brackets ([]), and parameter prefixes (@).
        /// </summary>
        /// <param name="commandText">The command text.</param>
        /// <param name="dialect">The dialect.</param>
        /// <returns>Fixed query.</returns>
        public static string FixCommandText(string commandText, ISqlDialect dialect)
        {
            commandText = DatabaseCaretReferences.Replace(commandText);

            var openBracket = dialect.OpenQuote;

            if (openBracket != '[')
            {
                commandText = BracketLocator.ReplaceBrackets(commandText, dialect);
            }

            var paramPrefix = dialect.ParameterPrefix;

            if (paramPrefix != '@')
            {
                commandText = ParamPrefixReplacer.Replace(commandText, paramPrefix);
            }

            return(commandText);
        }
Esempio n. 3
0
        /// <summary>
        ///   İstenen bağlantıya bağlı ve verilen komutu içeren yeni bir IDbCommand nesnesi oluşturur.</summary>
        /// <param name="connection">
        ///   IDbCommand nesnesinin oluşturulacağı bağlantı.</param>
        /// <param name="commandText">
        ///   IDbCommand nesnesinin içereceği komut metni. <c>null</c> olabilir.</param>
        /// <returns>
        ///   Yeni IDbCommand nesnesi.</returns>
        public static IDbCommand NewCommand(IDbConnection connection, string commandText)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            IDbCommand command = connection.CreateCommand();

            commandText = DatabaseCaretReferences.Replace(commandText);

            var dialect     = connection.GetDialect();
            var openBracket = dialect.OpenQuote;

            if (openBracket != '[')
            {
                commandText = BracketLocator.ReplaceBrackets(commandText, dialect);
            }

            command.CommandText = commandText;
            return(command);
        }
Esempio n. 4
0
        /// <summary>
        ///   İstenen bağlantıya bağlı ve verilen komutu içeren yeni bir IDbCommand nesnesi oluşturur.</summary>
        /// <param name="connection">
        ///   IDbCommand nesnesinin oluşturulacağı bağlantı.</param>
        /// <param name="commandText">
        ///   IDbCommand nesnesinin içereceği komut metni. <c>null</c> olabilir.</param>
        /// <returns>
        ///   Yeni IDbCommand nesnesi.</returns>
        public static IDbCommand NewCommand(IDbConnection connection, string commandText)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            IDbCommand command = connection.CreateCommand();

            var bindByName = SqlMapper.GetBindByName(command.GetType());

            if (bindByName != null)
            {
                bindByName(command, true);
            }

            commandText = DatabaseCaretReferences.Replace(commandText);

            var dialect     = connection.GetDialect();
            var openBracket = dialect.OpenQuote;

            if (openBracket != '[')
            {
                commandText = BracketLocator.ReplaceBrackets(commandText, dialect);
            }

            var paramPrefix = dialect.ParameterPrefix;

            if (paramPrefix != '@')
            {
                commandText = ParamPrefixReplacer.Replace(commandText, paramPrefix);
            }

            command.CommandText = commandText;
            return(command);
        }