Skip to content

Commit

Permalink
Correct regex to match only full words
Browse files Browse the repository at this point in the history
  • Loading branch information
Joakim Hansson committed Oct 2, 2017
1 parent 24c7118 commit 0d12aba
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions InfluxData.Net.InfluxDb/Helpers/QueryHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand All @@ -49,4 +49,4 @@ public static string BuildParameterizedQuery(string query, object param)
return query;
}
}
}
}

0 comments on commit 0d12aba

Please sign in to comment.