-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryEngine.cs
More file actions
37 lines (28 loc) · 1.52 KB
/
Copy pathQueryEngine.cs
File metadata and controls
37 lines (28 loc) · 1.52 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
using System.Data.Common;
using DataFuse.Adapters.Abstraction;
namespace DataFuse.Adapters.SQL;
public class QueryEngine : IQueryEngine
{
private readonly SQLConfiguration sqlConfiguration;
public QueryEngine(SQLConfiguration sqlConfiguration)
{
Guard.ThrowIfNull(sqlConfiguration?.ConnectionSettings?.ProviderName,
"SQL Configuration is required with connection settings. Provider name is missing.");
Guard.ThrowIfNull(sqlConfiguration?.ConnectionSettings?.ConnectionString,
"SQL Configuration is required with connection settings. Connection string is missing.");
this.sqlConfiguration = sqlConfiguration!;
}
public bool CanExecute(IQuery query) => query is ISQLQuery;
public async Task<IQueryResult> Execute(IQuery query)
{
var factory = DbProviderFactories.GetFactory(sqlConfiguration.ConnectionSettings.ProviderName)
?? throw new InvalidOperationException($"Provider: {sqlConfiguration.ConnectionSettings.ProviderName} is not supported. Please register entry in DbProviderFactories ");
var connection = factory.CreateConnection()
?? throw new InvalidOperationException($"Failed to create connection with Provider: {sqlConfiguration.ConnectionSettings.ProviderName}. Please check the connection settings.");
await using (connection)
{
connection.ConnectionString = sqlConfiguration.ConnectionSettings.ConnectionString;
return await ((ISQLQuery)query).Run(connection);
}
}
}