Skip to content

Commit d39156b

Browse files
author
Kusto Build System
committed
Auto-sync from Azure-Kusto-Service
1 parent a180402 commit d39156b

1 file changed

Lines changed: 52 additions & 18 deletions

File tree

src/Kusto.Language/Binder/Binder_Misc.cs

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -658,10 +658,8 @@ public void ApplyDirective(Directive directive, List<Diagnostic> diagnostics = n
658658
switch (directive.Name)
659659
{
660660
case "connect":
661-
ApplyConnectDirective(directive, directive.Token, diagnostics);
662-
break;
663661
case "database":
664-
ApplyDatabaseDirective(directive, directive.Token, diagnostics);
662+
ApplyConnectOrDatabaseDirective(directive, directive.Token, diagnostics);
665663
break;
666664

667665
default:
@@ -674,9 +672,7 @@ public void ApplyDirective(Directive directive, List<Diagnostic> diagnostics = n
674672
}
675673
}
676674

677-
private static char[] databaseSplitChars = new[] { '/' };
678-
679-
private void ApplyDatabaseDirective(Directive directive, SyntaxElement location, List<Diagnostic> diagnostics)
675+
private void ApplyConnectOrDatabaseDirective(Directive directive, SyntaxElement location, List<Diagnostic> diagnostics)
680676
{
681677
if (TryGetDirectiveClusterAndDatabase(directive, out var clusterName, out var databaseName))
682678
{
@@ -690,11 +686,14 @@ private void ApplyDatabaseDirective(Directive directive, SyntaxElement location,
690686
}
691687
}
692688

693-
_currentDatabase = _currentCluster.GetDatabase(databaseName) ?? DatabaseSymbol.Unknown;
694-
695-
if (_currentDatabase == DatabaseSymbol.Unknown && diagnostics != null && location != null)
689+
if (databaseName != null)
696690
{
697-
diagnostics.Add(DiagnosticFacts.GetNameDoesNotReferToAnyKnownDatabase(databaseName).WithLocation(location));
691+
_currentDatabase = _currentCluster.GetDatabase(databaseName) ?? DatabaseSymbol.Unknown;
692+
693+
if (_currentDatabase == DatabaseSymbol.Unknown && diagnostics != null && location != null)
694+
{
695+
diagnostics.Add(DiagnosticFacts.GetNameDoesNotReferToAnyKnownDatabase(databaseName).WithLocation(location));
696+
}
698697
}
699698
}
700699
}
@@ -733,26 +732,61 @@ internal static bool TryGetDirectiveClusterAndDatabase(
733732
}
734733
else if (directive.Name == "connect" && directive.Arguments.Count > 0)
735734
{
736-
var connection = GetDirectiveArgumentStringValue(directive.Arguments[0]);
737-
var info = ConnectionInfo.Parse(connection);
738-
var dataSource = info.DataSource;
739-
KustoFacts.GetHostAndPath(info.DataSource, out var host, out var path);
740-
clusterName = host;
741-
databaseName = path;
742-
return true;
735+
var arg = directive.Arguments[0];
736+
if (arg.Text.StartsWith("cluster"))
737+
{
738+
var cluster = GetStringValueAfterPrefix(arg.Text, "cluster(", 0, out var end);
739+
if (cluster != null)
740+
{
741+
KustoFacts.GetHostAndPath(cluster, out clusterName, out var path);
742+
databaseName = GetStringValueAfterPrefix(arg.Text, "database(", end, out _)
743+
?? path;
744+
return clusterName != null;
745+
}
746+
}
747+
else if (!arg.Text.StartsWith("@"))
748+
{
749+
var connection = GetDirectiveArgumentStringValue(arg);
750+
var info = ConnectionInfo.Parse(connection);
751+
var dataSource = info.DataSource;
752+
KustoFacts.GetHostAndPath(info.DataSource, out var host, out var path);
753+
clusterName = host;
754+
databaseName = path;
755+
return true;
756+
}
743757
}
744758

745759
return false;
746760
}
747761

762+
internal static string GetStringValueAfterPrefix(string text, string prefix, int start, out int end)
763+
{
764+
var index = text.IndexOf(prefix, start);
765+
if (index >= 0)
766+
{
767+
var wsLen = Parsing.TokenParser.ScanWhitespace(text, index + prefix.Length);
768+
var stringStart = index + prefix.Length + wsLen;
769+
if (Parsing.TokenParser.ScanStringLiteral(text, stringStart) is int len
770+
&& len > 0)
771+
{
772+
var stringLiteral = text.Substring(stringStart, len);
773+
end = index + len;
774+
return KustoFacts.GetStringLiteralValue(stringLiteral);
775+
}
776+
}
777+
end = start;
778+
return null;
779+
}
780+
748781
internal static string GetDirectiveArgumentStringValue(Editor.ClientDirectiveArgument argument)
749782
{
750783
return argument.Value as string ?? "";
751784
}
752785

753786
private void ApplyConnectDirective(Directive directive, SyntaxElement location, List<Diagnostic> diagnostics)
754787
{
755-
ApplyDatabaseDirective(directive, location, diagnostics);
788+
// uses same logic as #database directive
789+
ApplyConnectOrDatabaseDirective(directive, location, diagnostics);
756790
}
757791

758792
#endregion

0 commit comments

Comments
 (0)