Exemplo n.º 1
0
        internal static string ApplyVariables(string sql, IVariables variables, Action <string, string> onReplace = null)
        {
            if (string.IsNullOrWhiteSpace(sql))
            {
                return(sql);
            }

            MatchEvaluator evaluator = match =>
            {
                var name = match.Groups["name"].Value;

                var value = variables.GetValue(name);
                if (value == null)
                {
                    throw new InvalidOperationException("Variable [{0}] not defined.".FormatWith(name));
                }

                onReplace?.Invoke(name, value);
                return(value);
            };

            var result = Regex.Replace(sql, @"\{\{(?'name'\w+)\}\}", evaluator, RegexOptions.Compiled);

            return(Regex.Replace(result, @"\$\((?'name'\w+)\)", evaluator, RegexOptions.Compiled));
        }
Exemplo n.º 2
0
 public string this[string key] => _variables.GetValue(key);