forked from dotnet/interactive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteKernel.cs
More file actions
135 lines (111 loc) · 4.27 KB
/
Copy pathSQLiteKernel.cs
File metadata and controls
135 lines (111 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// 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.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Html;
using Microsoft.Data.Sqlite;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Formatting.TabularData;
using Enumerable = System.Linq.Enumerable;
namespace Microsoft.DotNet.Interactive.SQLite;
public class SQLiteKernel :
Kernel,
IKernelCommandHandler<SubmitCode>
{
private readonly string _connectionString;
private IEnumerable<IEnumerable<IEnumerable<(string name, object value)>>> _tables;
public SQLiteKernel(string name, string connectionString) : base(name)
{
KernelInfo.LanguageName = "SQLite";
KernelInfo.Description ="""
Query a SQLite database
""";
_connectionString = connectionString;
}
private DbConnection OpenConnection()
{
return new SqliteConnection(_connectionString);
}
async Task IKernelCommandHandler<SubmitCode>.HandleAsync(
SubmitCode submitCode,
KernelInvocationContext context)
{
await using var connection = OpenConnection();
if (connection.State is not ConnectionState.Open)
{
await connection.OpenAsync();
}
await using var dbCommand = connection.CreateCommand();
dbCommand.CommandText = submitCode.Code;
_tables = Execute(dbCommand);
foreach (var table in _tables)
{
var tabularDataResource = table.ToTabularDataResource();
var explorer = DataExplorer.CreateDefault(tabularDataResource);
context.Display(explorer);
}
}
private IEnumerable<IEnumerable<IEnumerable<(string name, object value)>>> Execute(IDbCommand command)
{
using var reader = command.ExecuteReader();
do
{
var values = new object[reader.FieldCount];
var names = Enumerable.Range(0, reader.FieldCount).Select(reader.GetName).ToArray();
ResolveColumnNameClashes(names);
// holds the result of a single statement within the query
var table = new List<(string, object)[]>();
while (reader.Read())
{
reader.GetValues(values);
var row = new (string, object)[values.Length];
for (var i = 0; i < values.Length; i++)
{
row[i] = (names[i], values[i]);
}
table.Add(row);
}
yield return table;
} while (reader.NextResult());
void ResolveColumnNameClashes(string[] names)
{
var nameCounts = new Dictionary<string, int>(capacity: names.Length);
for (var i1 = 0; i1 < names.Length; i1++)
{
var columnName = names[i1];
if (nameCounts.TryGetValue(columnName, out var count))
{
nameCounts[columnName] = ++count;
names[i1] = columnName + $" ({count})";
}
else
{
nameCounts[columnName] = 1;
}
}
}
}
public static void AddSQLiteKernelConnectorTo(CompositeKernel kernel)
{
kernel.AddConnectDirective(new ConnectSQLiteDirective());
KernelInvocationContext.Current?.Display(
new HtmlString(@"<details><summary>Query SQLite databases.</summary>
<p>This extension adds support for connecting to SQLite databases using the <code>#!connect sqlite</code> magic command. For more information, run a cell using the <code>#!sql</code> magic command.</p>
</details>"),
"text/html");
}
public static void AddSQLiteKernelConnectorToCurrentRoot()
{
if (KernelInvocationContext.Current is { } context &&
context.HandlingKernel.RootKernel is CompositeKernel root)
{
SQLiteKernel.AddSQLiteKernelConnectorTo(root);
}
}
}
public class SqlRow : Dictionary<string, object>
{
}