140 lines
5.4 KiB
C#
140 lines
5.4 KiB
C#
using DocumentFormat.OpenXml.Drawing.Charts;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using NPOI.SS.Formula.Functions;
|
|
using Sarand.BusinessMgr.Net.Standard.Base.Bus.Control;
|
|
using Sarand.BusinessMgr.Web.Control.Consoles;
|
|
using Sarand.BusinessMgr.Web.Control.Models;
|
|
using Sarand.BusinessMgr.Web.Control.Models.Instance;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Dbas;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace Sarand.BusinessMgr.Web.Control.Controllers.Instance
|
|
{
|
|
//=======================================================================
|
|
partial class CoreDatabaseController
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Definitions.
|
|
|
|
// Core Database Table list data.
|
|
private class TableListData : AjaxResponse_Data.ItemData_Base
|
|
{
|
|
public String SchemaName { get; set; }
|
|
public String TableName { get; set; }
|
|
public long? ColumnCount { get; set; }
|
|
public long? IndexCount { get; set; }
|
|
public long? RowCount { get; set; }
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Get Core Database Table list ajax request.
|
|
//-------------------------------------------------------------------
|
|
private class AjaxResponse_CoreDatabaseTableList : AjaxResponse_Data
|
|
{
|
|
// Construction.
|
|
public AjaxResponse_CoreDatabaseTableList(SbmModel_Control model) : base(model) { }
|
|
// CoreDatabase list.
|
|
public List<TableListData> Tables { get; } = new List<TableListData>();
|
|
}
|
|
//-------------------------------------------------------------------
|
|
[HttpPost]
|
|
[Route("Instance/CoreDatabase/Ajax/GetTableList")]
|
|
[Route("Instance/CoreDatabase/Ajax/GetTables")]
|
|
public IActionResult Ajax_GetTableList(Model_CoreDatabase model)
|
|
{
|
|
String messageTitle = Localiser["label.database"];
|
|
AjaxResponse_CoreDatabaseTableList responseData = new AjaxResponse_CoreDatabaseTableList(model);
|
|
|
|
// Validate session and referrer.
|
|
if (!ValidReferer) return Forbid();
|
|
if (!ValidUserSession) return GetAjaxResult_InvalidSession(responseData);
|
|
RefreshModel(model);
|
|
|
|
// Record disposable items as they are created so all can be disposed when needed.
|
|
using (DisposableCollection<SacsDisposable> disposables = new DisposableCollection<SacsDisposable>())
|
|
{
|
|
|
|
// Validate Repository.
|
|
SessionData_Control.Profile.Filter.InstanceContext = model.Filter.Context;
|
|
if (StringUtil.IsBlank(model.Filter.Context))
|
|
{
|
|
SaLogger.Warn($"{SysUtil.Fn} Instance Context not supplied.");
|
|
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.system"]));
|
|
}
|
|
SbmcRepository repository = ControlInstanceMgr.GetRepositoryByContext(model.Filter.Context);
|
|
disposables.Add(repository);
|
|
if (repository == null)
|
|
{
|
|
SaLogger.Warn($"{SysUtil.Fn} Repository does not exist. Context: {model.Filter.Context}.");
|
|
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.system"]));
|
|
}
|
|
|
|
// Repository Console.
|
|
SbmCoreInstanceConsole_Control instanceConsole = new SbmCoreInstanceConsole_Control(SessionData_Control, repository);
|
|
disposables.Add(instanceConsole);
|
|
|
|
// Columns.
|
|
String colSchemaName = "SchemaName";
|
|
String colTableName = "TableName";
|
|
String colColumnCount = "ColumnCount";
|
|
String colIndexCount = "IndexCount";
|
|
String colRowCount = "RowCount";
|
|
|
|
// Table filter.
|
|
List<String> filter = new List<string>();
|
|
if (!StringUtil.IsBlank(model.Filter.Search))
|
|
filter.Add($"tbl.name Like '%{DbQuery.FormatForLike(model.Filter.Search)}%'");
|
|
if (!filter.Any()) filter.Add("1=1");
|
|
|
|
// Construction query.
|
|
List<String> sql = new List<string>()
|
|
{
|
|
$"Select Top 2000",
|
|
$"[{colSchemaName}]=sch.name, [{colTableName}]=tbl.name,",
|
|
$"[{colColumnCount}]=(Select Count(*) From sys.columns col Where col.object_id=tbl.object_id),",
|
|
$"[{colIndexCount}]=(Select Count(*) From sys.indexes idx Where idx.object_id=tbl.object_id),",
|
|
$"[{colRowCount}]=(Select Sum(ptn.rows) From sys.partitions ptn Where ptn.object_id=tbl.object_id And ptn.index_id<2)",
|
|
$"From sys.tables tbl",
|
|
$"Left Join sys.schemas sch On sch.schema_id=tbl.schema_id",
|
|
$"Where tbl.type='U' And {filter.Concat(" And ","(",")")}",
|
|
$"Order By tbl.name, sch.name;"
|
|
};
|
|
|
|
// Query items.
|
|
using (DbResultSet resultSet = instanceConsole.Database_Core.ExecuteQuery(String.Join("\n", sql)))
|
|
{
|
|
while (resultSet != null && resultSet.HasRows && resultSet.NextRecord())
|
|
{
|
|
NamedValue.Map values = resultSet.GetRecordValues();
|
|
TableListData tableData = new TableListData()
|
|
{
|
|
SchemaName = values.GetString(colSchemaName),
|
|
TableName = values.GetString(colTableName),
|
|
ColumnCount = values.GetLong(colColumnCount),
|
|
IndexCount = values.GetLong(colIndexCount),
|
|
RowCount = values.GetLong(colRowCount),
|
|
};
|
|
responseData.Tables.Add(tableData);
|
|
}
|
|
}
|
|
if (model.Paging.IsPaged)
|
|
model.Paging.ItemCount = (ushort)responseData.Tables.Count;
|
|
}
|
|
|
|
// Complete response.
|
|
responseData.SetPaging(model);
|
|
responseData.Success = true;
|
|
return Json(responseData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|