public string BuildRelationshipExpression(string fieldName, string relationshipOperator, string value) { var operatorToken = TranslateRelationshipOpertor(relationshipOperator); var manipulatedValue = value.Trim(); if (operatorToken == "$regex") { var regexPattern = ParseHelpers.HasQuotes(value) ? Regex.Escape(ParseHelpers.StripQuotes(value)).Replace(@"\", @"\\").Replace("%", ".*").Replace("_", ".") : value; manipulatedValue = $"'^{regexPattern}$', $options: 'i'"; } else if (operatorToken.InSet("$in", "$nin")) { if (manipulatedValue.StartsWith("(") && manipulatedValue.EndsWith(")")) { manipulatedValue = $"[{manipulatedValue.Substring(1, manipulatedValue.Length - 2)}]"; } } else if (DateTime.TryParse(ParseHelpers.StripQuotes(value), out var parsedTimestamp)) { manipulatedValue = $"ISODate('{parsedTimestamp:yyyy-MM-ddTHH:mm:ssZ}')"; } else if (value.ToLowerInvariant().InSet("null", "true", "false")) { manipulatedValue = manipulatedValue.ToLowerInvariant(); } return($"{{ \"{fieldName}\" : {{ {operatorToken} : {manipulatedValue} }} }}"); }
private bool TryMatchUnaryOperator(string expressionValue, out string unaryExpression) { unaryExpression = null; var leftPattern = BuildFieldNameRegexPattern(); var aggregatedUnaryOperators = DataApiSqlOperators.UnaryOperators.Aggregate((a, b) => a + "|" + b); var combinedPattern = $"^{leftPattern}\\s*({aggregatedUnaryOperators})$"; var regex = Regex.Match(expressionValue, combinedPattern, RegexOptions.IgnoreCase); if (!regex.Success) { return(false); } var unaryOperator = regex.Groups[2].Value; var fieldName = ParseHelpers.StripQuotes(regex.Groups[1].Value); unaryExpression = syntaxBuilder.BuildUnaryExpression(fieldName, unaryOperator.Trim()); return(true); }