From 0d12aba7ca977ca5a8f080ff9d228d1ee5927f84 Mon Sep 17 00:00:00 2001 From: Joakim Hansson Date: Mon, 2 Oct 2017 20:26:57 +0200 Subject: [PATCH] Correct regex to match only full words --- .../Helpers/QueryHelpers.cs | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/InfluxData.Net.InfluxDb/Helpers/QueryHelpers.cs b/InfluxData.Net.InfluxDb/Helpers/QueryHelpers.cs index cff5c8d..f51ed79 100644 --- a/InfluxData.Net.InfluxDb/Helpers/QueryHelpers.cs +++ b/InfluxData.Net.InfluxDb/Helpers/QueryHelpers.cs @@ -10,36 +10,36 @@ public static class QueryHelpers { public static string BuildParameterizedQuery(string query, object param) { - var paramRegex = "@([A-Za-z0-9åäöÅÄÖ'_-]+)"; - - var matches = Regex.Matches(query, paramRegex); - Type t = param.GetType(); PropertyInfo[] pi = t.GetProperties(); - foreach(Match match in matches) - { - if (!pi.Any(x => match.Groups[0].Value.Contains(x.Name))) - throw new ArgumentException($"Missing parameter value for {match.Groups[0].Value}"); - } foreach (var propertyInfo in pi) { + var regex = $@"@{propertyInfo.Name}(?!\w)"; + + if(!Regex.IsMatch(query, regex) && Nullable.GetUnderlyingType(propertyInfo.GetType()) != null) + throw new ArgumentException($"Missing parameter identifier for @{propertyInfo.Name}"); + var paramValue = propertyInfo.GetValue(param); + if (paramValue == null) + continue; + var paramType = paramValue.GetType(); - if(!paramType.IsPrimitive && paramType != typeof(String)) + if (!paramType.IsPrimitive && paramType != typeof(String) && paramType != typeof(DateTime)) throw new NotSupportedException($"The type {paramType.Name} is not a supported query parameter type."); var sanitizedParamValue = paramValue; - if(paramType == typeof(String)) { + if (paramType == typeof(String)) + { sanitizedParamValue = ((string)sanitizedParamValue).Sanitize(); } - while (Regex.IsMatch(query, $"@{propertyInfo.Name}")) + while (Regex.IsMatch(query, regex)) { - var match = Regex.Match(query, $"@{propertyInfo.Name}"); + var match = Regex.Match(query, regex); query = query.Remove(match.Index, match.Length); query = query.Insert(match.Index, $"{sanitizedParamValue}"); @@ -49,4 +49,4 @@ public static string BuildParameterizedQuery(string query, object param) return query; } } -} \ No newline at end of file +}