134 lines
3.8 KiB
C#
134 lines
3.8 KiB
C#
|
|
using System;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
using Erp.SqlQueryManager.Application.Contracts;
|
||
|
|
using Erp.SqlQueryManager.Domain.Entities;
|
||
|
|
using Erp.SqlQueryManager.Domain.Services;
|
||
|
|
using Volo.Abp.Application.Dtos;
|
||
|
|
using Volo.Abp.Application.Services;
|
||
|
|
using Volo.Abp.Domain.Repositories;
|
||
|
|
|
||
|
|
namespace Erp.SqlQueryManager.Application;
|
||
|
|
|
||
|
|
public class SqlViewAppService : CrudAppService<
|
||
|
|
SqlView,
|
||
|
|
SqlViewDto,
|
||
|
|
Guid,
|
||
|
|
PagedAndSortedResultRequestDto,
|
||
|
|
CreateSqlViewDto,
|
||
|
|
UpdateSqlViewDto>, ISqlViewAppService
|
||
|
|
{
|
||
|
|
private readonly ISqlExecutorService _sqlExecutorService;
|
||
|
|
|
||
|
|
public SqlViewAppService(
|
||
|
|
IRepository<SqlView, Guid> repository,
|
||
|
|
ISqlExecutorService sqlExecutorService) : base(repository)
|
||
|
|
{
|
||
|
|
_sqlExecutorService = sqlExecutorService;
|
||
|
|
}
|
||
|
|
|
||
|
|
public override async Task<SqlViewDto> CreateAsync(CreateSqlViewDto input)
|
||
|
|
{
|
||
|
|
var entity = new SqlView(
|
||
|
|
GuidGenerator.Create(),
|
||
|
|
input.ViewName,
|
||
|
|
input.SchemaName ?? "dbo",
|
||
|
|
input.DisplayName,
|
||
|
|
input.ViewDefinition,
|
||
|
|
input.DataSourceCode,
|
||
|
|
CurrentTenant.Id)
|
||
|
|
{
|
||
|
|
Description = input.Description,
|
||
|
|
Category = input.Category,
|
||
|
|
WithSchemaBinding = input.WithSchemaBinding
|
||
|
|
};
|
||
|
|
|
||
|
|
await Repository.InsertAsync(entity);
|
||
|
|
return ObjectMapper.Map<SqlView, SqlViewDto>(entity);
|
||
|
|
}
|
||
|
|
|
||
|
|
public override async Task<SqlViewDto> UpdateAsync(Guid id, UpdateSqlViewDto input)
|
||
|
|
{
|
||
|
|
var entity = await Repository.GetAsync(id);
|
||
|
|
|
||
|
|
entity.DisplayName = input.DisplayName;
|
||
|
|
entity.Description = input.Description;
|
||
|
|
entity.UpdateDefinition(input.ViewDefinition);
|
||
|
|
entity.Category = input.Category;
|
||
|
|
entity.WithSchemaBinding = input.WithSchemaBinding;
|
||
|
|
|
||
|
|
await Repository.UpdateAsync(entity);
|
||
|
|
return ObjectMapper.Map<SqlView, SqlViewDto>(entity);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<SqlQueryExecutionResultDto> DeployAsync(DeployViewDto input)
|
||
|
|
{
|
||
|
|
var view = await Repository.GetAsync(input.Id);
|
||
|
|
|
||
|
|
// Drop if exists and requested
|
||
|
|
if (input.DropIfExists)
|
||
|
|
{
|
||
|
|
await _sqlExecutorService.DropObjectAsync(
|
||
|
|
view.ViewName,
|
||
|
|
"VIEW",
|
||
|
|
view.DataSourceCode,
|
||
|
|
view.SchemaName);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Deploy the view
|
||
|
|
var result = await _sqlExecutorService.DeployViewAsync(
|
||
|
|
view.ViewDefinition,
|
||
|
|
view.DataSourceCode);
|
||
|
|
|
||
|
|
if (result.Success)
|
||
|
|
{
|
||
|
|
view.MarkAsDeployed();
|
||
|
|
await Repository.UpdateAsync(view);
|
||
|
|
}
|
||
|
|
|
||
|
|
return MapExecutionResult(result);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> CheckExistsAsync(Guid id)
|
||
|
|
{
|
||
|
|
var view = await Repository.GetAsync(id);
|
||
|
|
|
||
|
|
return await _sqlExecutorService.CheckObjectExistsAsync(
|
||
|
|
view.ViewName,
|
||
|
|
"VIEW",
|
||
|
|
view.DataSourceCode,
|
||
|
|
view.SchemaName);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<SqlQueryExecutionResultDto> DropAsync(Guid id)
|
||
|
|
{
|
||
|
|
var view = await Repository.GetAsync(id);
|
||
|
|
|
||
|
|
var result = await _sqlExecutorService.DropObjectAsync(
|
||
|
|
view.ViewName,
|
||
|
|
"VIEW",
|
||
|
|
view.DataSourceCode,
|
||
|
|
view.SchemaName);
|
||
|
|
|
||
|
|
if (result.Success)
|
||
|
|
{
|
||
|
|
view.IsDeployed = false;
|
||
|
|
await Repository.UpdateAsync(view);
|
||
|
|
}
|
||
|
|
|
||
|
|
return MapExecutionResult(result);
|
||
|
|
}
|
||
|
|
|
||
|
|
private SqlQueryExecutionResultDto MapExecutionResult(SqlExecutionResult result)
|
||
|
|
{
|
||
|
|
return new SqlQueryExecutionResultDto
|
||
|
|
{
|
||
|
|
Success = result.Success,
|
||
|
|
Message = result.Message,
|
||
|
|
Data = result.Data,
|
||
|
|
RowsAffected = result.RowsAffected,
|
||
|
|
ExecutionTimeMs = result.ExecutionTimeMs,
|
||
|
|
Metadata = result.Metadata
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|