Добавьте файлы проекта.
This commit is contained in:
70
SQLVision.Visualizers/Visualizers/TextVisualizer.cs
Normal file
70
SQLVision.Visualizers/Visualizers/TextVisualizer.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using SQLVision.Core.Enums;
|
||||
using SQLVision.Core.Models;
|
||||
using SQLVision.Visualizers.Interfaces;
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
|
||||
namespace SQLVision.Visualizers.Visualizers;
|
||||
|
||||
public class TextVisualizer : IVisualizer
|
||||
{
|
||||
public FrameworkElement Visualize(DataTable data, OutputDefinition definition)
|
||||
{
|
||||
var textBlock = new TextBlock
|
||||
{
|
||||
Text = ConvertDataTableToText(data),
|
||||
TextWrapping = TextWrapping.Wrap,
|
||||
FontFamily = new Microsoft.UI.Xaml.Media.FontFamily("Consolas"),
|
||||
FontSize = 12,
|
||||
IsTextSelectionEnabled = true
|
||||
};
|
||||
|
||||
return new ScrollViewer
|
||||
{
|
||||
Content = textBlock,
|
||||
VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
|
||||
HorizontalScrollBarVisibility = ScrollBarVisibility.Auto
|
||||
};
|
||||
}
|
||||
|
||||
private string ConvertDataTableToText(DataTable data)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
|
||||
// Заголовки
|
||||
for (int i = 0; i < data.Columns.Count; i++)
|
||||
{
|
||||
sb.Append(data.Columns[i].ColumnName);
|
||||
if (i < data.Columns.Count - 1)
|
||||
sb.Append(" | ");
|
||||
}
|
||||
sb.AppendLine();
|
||||
sb.AppendLine(new string('-', data.Columns.Count * 20));
|
||||
|
||||
// Данные
|
||||
foreach (DataRow row in data.Rows)
|
||||
{
|
||||
for (int i = 0; i < data.Columns.Count; i++)
|
||||
{
|
||||
var value = row[i];
|
||||
var text = value?.ToString() ?? "NULL";
|
||||
|
||||
// Обрезаем слишком длинные значения
|
||||
if (text.Length > 50)
|
||||
text = text.Substring(0, 47) + "...";
|
||||
|
||||
sb.Append(text);
|
||||
|
||||
if (i < data.Columns.Count - 1)
|
||||
sb.Append(" | ");
|
||||
}
|
||||
sb.AppendLine();
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public bool CanVisualize(OutputType type) => type == OutputType.Text;
|
||||
}
|
||||
Reference in New Issue
Block a user