Skip to content

Commit 0d2cad2

Browse files
authored
Support special characters in external att names in CosmosDB (#837)
1 parent c3f00d8 commit 0d2cad2

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

dotnet/src/dotnetcore/DynService/Cosmos/CosmosDBConnection.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,13 @@ private void CreateCosmosQuery(CosmosDBQuery query,ServiceCursorDef cursorDef, I
461461
IEnumerable<string> projection = query.Projection;
462462
string element;
463463
string projectionList = string.Empty;
464+
string regexSpecialChars = "[^A-Za-z0-9]";
464465
foreach (string key in projection)
465466
{
466-
element = $"{TABLE_ALIAS}.{key}";
467+
if (Regex.IsMatch(key, regexSpecialChars))
468+
element = $"{TABLE_ALIAS}[\"{key}\"]";
469+
else
470+
element = $"{TABLE_ALIAS}.{key}";
467471
if (!string.IsNullOrEmpty(projectionList))
468472
projectionList = $"{element},{projectionList}";
469473
else
@@ -550,7 +554,10 @@ private void CreateCosmosQuery(CosmosDBQuery query,ServiceCursorDef cursorDef, I
550554
foreach (string d in projection)
551555
{
552556
string wholeWordPattern = String.Format(@"\b{0}\b", d);
553-
filterProcess = Regex.Replace(filterProcess, wholeWordPattern, $"{TABLE_ALIAS}.{d}");
557+
if (Regex.IsMatch(wholeWordPattern, regexSpecialChars))
558+
filterProcess = Regex.Replace(filterProcess, wholeWordPattern, $"{TABLE_ALIAS}[\"{d}\"]");
559+
else
560+
filterProcess = Regex.Replace(filterProcess, wholeWordPattern, $"{TABLE_ALIAS}.{d}");
554561
}
555562
keyFilterQ = new string[] { filterProcess };
556563
allFiltersQuery = allFiltersQuery.Concat(keyFilterQ);
@@ -563,7 +570,10 @@ private void CreateCosmosQuery(CosmosDBQuery query,ServiceCursorDef cursorDef, I
563570

564571
foreach (string orderAtt in query.OrderBys)
565572
{
566-
expression = orderAtt.StartsWith("(") ? $"{TABLE_ALIAS}.{orderAtt.Remove(orderAtt.Length-1,1).Remove(0,1)} DESC" : $"{TABLE_ALIAS}.{orderAtt} ASC";
573+
if (!Regex.IsMatch(orderAtt, regexSpecialChars))
574+
expression = orderAtt.StartsWith("(") ? $"{TABLE_ALIAS}.{orderAtt.Remove(orderAtt.Length-1,1).Remove(0,1)} DESC" : $"{TABLE_ALIAS}.{orderAtt} ASC";
575+
else
576+
expression = orderAtt.StartsWith("(") ? $"{TABLE_ALIAS}[\"{orderAtt.Remove(orderAtt.Length - 1, 1).Remove(0, 1)}\"] DESC" : $"{TABLE_ALIAS}[\"{orderAtt}\"] ASC";
567577
orderExpressionList = orderExpressionList.Concat(new string[] { expression });
568578
}
569579

0 commit comments

Comments
 (0)