-
Notifications
You must be signed in to change notification settings - Fork 391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add do-nothing SQL kernel to the builtin kernels in order to enable SQL language coloring #1105
Changes from all commits
77cf340
a0533aa
57984a0
d3d436e
e1c5000
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using FluentAssertions; | ||
using System.Threading.Tasks; | ||
using Microsoft.DotNet.Interactive.Commands; | ||
using Microsoft.DotNet.Interactive.Events; | ||
using Microsoft.DotNet.Interactive.Tests.Utility; | ||
using Xunit; | ||
|
||
namespace Microsoft.DotNet.Interactive.Tests | ||
{ | ||
public class SqlKernelTests | ||
{ | ||
[Fact] | ||
public async Task sql_kernel_emits_help_message_without_sql_server_extension_installed() | ||
{ | ||
using var kernel = new CompositeKernel | ||
{ | ||
new SqlKernel() | ||
}; | ||
|
||
using var events = kernel.KernelEvents.ToSubscribedList(); | ||
|
||
var query = "select * from sys.databases"; | ||
await kernel.SendAsync(new SubmitCode($"#!sql\n\n{query}")); | ||
|
||
var displayValue = events.Should() | ||
.ContainSingle<DisplayedValueProduced>() | ||
.Which; | ||
|
||
var message = (string)displayValue.Value; | ||
|
||
message.Should().NotContain(query); | ||
|
||
// Should contain instructions for how to install SqlServer extension package | ||
message.Should().Contain("#r Microsoft.DotNet.Interactive.SqlServer"); | ||
|
||
// Should contain instructions for how to get help message for MSSQL kernel | ||
message.Should().Contain("#!connect mssql -h"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,29 @@ public static Kernel FindKernel(this Kernel kernel, string name) | |
}; | ||
} | ||
|
||
public static Kernel FindKernel(this Kernel kernel, Func<Kernel, bool> selector) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should return It would be more straightforward to use |
||
{ | ||
var root = kernel | ||
.RecurseWhileNotNull(k => k switch | ||
{ | ||
{ } kb => kb.ParentKernel, | ||
_ => null | ||
}) | ||
.LastOrDefault(); | ||
|
||
return root switch | ||
{ | ||
_ when selector(kernel) => kernel, | ||
CompositeKernel c => | ||
c.Directives | ||
.OfType<ChooseKernelDirective>() | ||
.Where(d => selector(d.Kernel)) | ||
.Select(d => d.Kernel) | ||
.SingleOrDefault(), | ||
_ => null | ||
}; | ||
} | ||
|
||
public static Task<KernelCommandResult> SendAsync( | ||
this Kernel kernel, | ||
KernelCommand command) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.DotNet.Interactive.Commands; | ||
|
||
namespace Microsoft.DotNet.Interactive | ||
{ | ||
/* This kernel is used as a placeholder for the MSSQL kernel in order to enable SQL language coloring | ||
* in the editor. Language grammars can only be defined for fixed kernel names, but MSSQL subkernels | ||
* are user-defined via the #!connect magic command. So, this kernel is specified in addition to the | ||
* user-defined kernel as a kind of "styling" kernel. | ||
*/ | ||
public class SqlKernel : | ||
Kernel, | ||
IKernelCommandHandler<SubmitCode> | ||
{ | ||
public const string DefaultKernelName = "sql"; | ||
|
||
public SqlKernel() : base(DefaultKernelName) | ||
{ | ||
} | ||
|
||
public Task HandleAsync(SubmitCode command, KernelInvocationContext context) | ||
{ | ||
var sqlKernel = this.FindKernel(kernel => kernel.Language?.ToUpper() == "SQL"); | ||
if (sqlKernel == null) | ||
{ | ||
context.Display(@" | ||
A SQL kernel is not currently defined. | ||
|
||
SQL kernels are provided as part of the Microsoft.DotNet.Interactive.SqlServer package, which can be installed by using the following nuget command: | ||
#r Microsoft.DotNet.Interactive.SqlServer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
Once installed, you can find more info about creating a SQL kernel and running queries by running the following help command: | ||
#!connect mssql -h | ||
"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the message is emitted as |
||
} | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"scopeName": "source.dotnet-interactive.sql", | ||
"patterns": [ | ||
{ | ||
"include": "source.dotnet-interactive.magic-commands" | ||
}, | ||
{ | ||
"include": "source.dotnet-interactive" | ||
}, | ||
{ | ||
"include": "source.sql" | ||
} | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be
"T-SQL"
or"MSSQL"
or something more specific.