@namespace Oqtane.Modules.Admin.Sql @inherits ModuleBase @inject NavigationManager NavigationManager @inject ISystemService SystemService @inject ITenantService TenantService @inject IDatabaseService DatabaseService @inject ISqlService SqlService @inject IStringLocalizer Localizer @inject IStringLocalizer SharedLocalizer @if (_tenants == null) {

@SharedLocalizer["Loading"]

} else {
@if (_connection == "+") {
@if (_databases != null) {
@if (!_showConnectionString) { } else { }
}
@if (!_showConnectionString) { if (_databaseConfigType != null) { @DatabaseConfigComponent } } else {
}
} else { @if (_connection != "-") { @if (!string.IsNullOrEmpty(_tenant)) {
}



@if (_results != null) { @if (_results.Count > 0) {
@foreach (KeyValuePair kvp in _results.First()) { @kvp.Key }
@foreach (KeyValuePair kvp in context) { @kvp.Value }
} else { @Localizer["Return.NoResult"] }

} } }
} @code { private string _connection = "-"; private Dictionary _connections; private List _tenants; private List _databases; private string _name = string.Empty; private string _databasetype = string.Empty; private Type _databaseConfigType; private object _databaseConfig; private RenderFragment DatabaseConfigComponent { get; set; } private bool _showConnectionString = false; private string _tenant = string.Empty; private string _connectionstring = string.Empty; private string _connectionstringtype = "password"; private string _connectionstringtoggle = string.Empty; private string _sql = string.Empty; private List> _results; public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Host; protected override async Task OnInitializedAsync() { try { _connections = await SystemService.GetSystemInfoAsync("connectionstrings"); _tenants = await TenantService.GetTenantsAsync(); _databases = await DatabaseService.GetDatabasesAsync(); _connectionstringtoggle = SharedLocalizer["ShowPassword"]; } catch (Exception ex) { await logger.LogError(ex, "Error Loading Tenants {Error}", ex.Message); AddModuleMessage(ex.Message, MessageType.Error); } } private async void ConnectionChanged(ChangeEventArgs e) { try { _connection = (string)e.Value; if (_connection != "-" && _connection != "+") { _connectionstring = _connections[_connection].ToString(); _tenant = ""; _databasetype = ""; var tenant = _tenants.FirstOrDefault(item => item.DBConnectionString == _connection); if (tenant != null) { _tenant = tenant.Name; _databasetype = _databases.FirstOrDefault(item => item.DBType == tenant.DBType && item.Name != "LocalDB").Name; } } else { if (_databases.Exists(item => item.IsDefault)) { _databasetype = _databases.Find(item => item.IsDefault).Name; } else { _databasetype = "LocalDB"; } _showConnectionString = false; LoadDatabaseConfigComponent(); } StateHasChanged(); } catch (Exception ex) { await logger.LogError(ex, "Error Loading Connection {Connection} {Error}", _connection, ex.Message); AddModuleMessage(ex.Message, MessageType.Error); } } private void DatabaseTypeChanged(ChangeEventArgs eventArgs) { try { _databasetype = (string)eventArgs.Value; _showConnectionString = false; LoadDatabaseConfigComponent(); } catch { AddModuleMessage(Localizer["Error.Database.LoadConfig"], MessageType.Error); } } private void LoadDatabaseConfigComponent() { var database = _databases.SingleOrDefault(d => d.Name == _databasetype); if (database != null) { _databaseConfigType = Type.GetType(database.ControlType); DatabaseConfigComponent = builder => { builder.OpenComponent(0, _databaseConfigType); builder.AddComponentReferenceCapture(1, inst => { _databaseConfig = Convert.ChangeType(inst, _databaseConfigType); }); builder.CloseComponent(); }; } } private void ShowConnectionString() { if (_databaseConfig is IDatabaseConfigControl databaseConfigControl) { _connectionstring = databaseConfigControl.GetConnectionString(); } _showConnectionString = !_showConnectionString; } private async Task Add() { var connectionstring = _connectionstring; if (!_showConnectionString && _databaseConfig is IDatabaseConfigControl databaseConfigControl) { connectionstring = databaseConfigControl.GetConnectionString(); } if (!string.IsNullOrEmpty(_name) && !string.IsNullOrEmpty(connectionstring)) { var settings = new Dictionary(); settings.Add($"{SettingKeys.ConnectionStringsSection}:{_name}", connectionstring); await SystemService.UpdateSystemInfoAsync(settings); _connections = await SystemService.GetSystemInfoAsync("connectionstrings"); _connection = "-"; AddModuleMessage(Localizer["Message.Connection.Added"], MessageType.Success); } else { AddModuleMessage(Localizer["Message.Required.Connection"], MessageType.Warning); } } private void ToggleConnectionString() { if (_connectionstringtype == "password") { _connectionstringtype = "text"; _connectionstringtoggle = SharedLocalizer["HidePassword"]; } else { _connectionstringtype = "password"; _connectionstringtoggle = SharedLocalizer["ShowPassword"]; } } private async Task Execute() { try { if (_databasetype != "-" && !string.IsNullOrEmpty(_sql)) { var dbtype = _databases.FirstOrDefault(item => item.Name == _databasetype).DBType; var sqlquery = new SqlQuery { DBConnectionString = _connection, DBType = dbtype, Query = _sql }; sqlquery = await SqlService.ExecuteQueryAsync(sqlquery); _results = sqlquery.Results; AddModuleMessage(Localizer["Success.QueryExecuted"], MessageType.Success); } else { AddModuleMessage(Localizer["Message.Required.Tenant"], MessageType.Warning); } } catch (Exception ex) { await logger.LogError(ex, "Error Executing SQL Query {SQL} {Error}", _sql, ex.Message); AddModuleMessage(ex.Message, MessageType.Error); } } }