This repository has been archived on 2025-05-14. You can view files and clone it, but cannot push or open issues or pull requests.
Shaun Walker 02fde9cec3
rolled back change creating an Infrastructure.Interfaces namespace, modified IModule interface to be strongly typed (#343)
* upgrade to .NET Core 3.2 Preview 3 and fixes for issues created by #314

* Components based on Bootstrap4 for Sections and  TabStrip to increase productivity and promote uniformity in Module UIs

* rolled back change creating an Infrastructure.Interfaces namespace, modified IModule interface to be strongly typed
2020-04-05 14:39:08 -04:00

66 lines
2.3 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Oqtane.Models;
using System.Collections.Generic;
using Oqtane.Shared;
using Oqtane.Infrastructure;
using Oqtane.Repository;
using Oqtane.Enums;
using System.Data.SqlClient;
using System.Data;
using System.Dynamic;
using Newtonsoft.Json;
using System;
namespace Oqtane.Controllers
{
[Route("{site}/api/[controller]")]
public class SqlController : Controller
{
private readonly ITenantRepository _tenants;
private readonly ISqlRepository _sql;
private readonly ILogManager _logger;
public SqlController(ITenantRepository tenants, ISqlRepository sql, ILogManager logger)
{
_tenants = tenants;
_sql = sql;
_logger = logger;
}
// POST: api/<controller>
[HttpPost]
[Authorize(Roles = Constants.HostRole)]
public SqlQuery Post([FromBody] SqlQuery sqlquery)
{
var results = new List<Dictionary<string, string>>();
Dictionary<string, string> row;
Tenant tenant = _tenants.GetTenant(sqlquery.TenantId);
try
{
foreach (string query in sqlquery.Query.Split("GO", StringSplitOptions.RemoveEmptyEntries))
{
SqlDataReader dr = _sql.ExecuteReader(tenant, query);
_logger.Log(LogLevel.Information, this, LogFunction.Other, "Sql Query {Query} Executed on Tenant {TenantId}", query, sqlquery.TenantId);
while (dr.Read())
{
row = new Dictionary<string, string>();
for (var field = 0; field < dr.FieldCount; field++)
{
row[dr.GetName(field)] = dr.IsDBNull(field) ? "" : dr.GetValue(field).ToString();
}
results.Add(row);
}
}
}
catch (Exception ex)
{
_logger.Log(LogLevel.Error, this, LogFunction.Other, "Sql Query {Query} Executed on Tenant {TenantId} Results In An Error {Error}", sqlquery.Query, sqlquery.TenantId, ex.Message);
}
sqlquery.Results = results;
return sqlquery;
}
}
}