Skip to content

Commit

Permalink
Add csv to create table
Browse files Browse the repository at this point in the history
  • Loading branch information
nh43de committed Mar 13, 2023
1 parent ea584fd commit 03cdb18
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 0 deletions.
53 changes: 53 additions & 0 deletions DataToolChain.Ui/CsvToCreateTable/CsvToCreateTable.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<Window x:Class="DataToolChain.CsvToCreateTable"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataToolChain"
mc:Ignorable="d"
Title="CSV To SQL Create Table" Height="650" Width="850"
d:DataContext="{d:DesignInstance Type=local:CsvToCreateTableViewModel, IsDesignTimeCreatable=True}"
>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>


<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label VerticalContentAlignment="Center" Margin="5">Input Files:</Label>
<DataGrid Grid.Column="1" Margin="5" VerticalContentAlignment="Center" Height="200" ItemsSource="{Binding Path=FilePaths, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<StackPanel Grid.Column="2" Orientation="Horizontal">
<Button Margin="5,5,5,0" Content="..." HorizontalAlignment="Stretch" VerticalAlignment="Top" Width="25" Click="ButtonBrowse_OnClick" Height="25"/>
<Button Margin="5" Content="Go" HorizontalAlignment="Stretch" VerticalAlignment="Top" Height="25" Width="86" Click="ButtonGoClick"/>
</StackPanel>
</Grid>


<StackPanel Background="Beige" Grid.Row="1" Margin="5">
<StackPanel Orientation="Horizontal">
<Label Margin="5" VerticalContentAlignment="Center">Output Table Name:</Label>
<TextBox HorizontalAlignment="Left" VerticalContentAlignment="Center" Margin="5" Height="24" TextWrapping="Wrap" Text="{Binding Path=TableName, UpdateSourceTrigger=PropertyChanged}" Width="409" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Margin="5">Delimiter:</Label>
<RadioButton Margin="5" IsChecked="{Binding Path=UseTabDelimiter}" GroupName="delimiter" VerticalContentAlignment="Center">Tab</RadioButton>
<RadioButton Margin="5" VerticalContentAlignment="Center" IsChecked="{Binding Path=UseCommaDelimiter}" GroupName="delimiter">Comma</RadioButton>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Margin="5">Header offset rows:</Label>
<TextBox HorizontalAlignment="Left" VerticalContentAlignment="Center" Margin="5" Height="24" TextWrapping="Wrap" Text="{Binding Path=HeaderOffsetRows, UpdateSourceTrigger=PropertyChanged}" Width="65" />
<Label Margin="5"></Label>
</StackPanel>
</StackPanel>

<TextBox Grid.Row="2" Margin="5" TextWrapping="Wrap" Text="{Binding Path=OutputText, Mode=TwoWay}" AcceptsReturn="True" IsUndoEnabled="False" VerticalScrollBarVisibility="Auto" BorderThickness="2" BorderBrush="#FF193EAE" />
</Grid>
</Window>
124 changes: 124 additions & 0 deletions DataToolChain.Ui/CsvToCreateTable/CsvToCreateTable.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using DataToolChain.Ui.Extensions;
using Microsoft.Win32;

namespace DataToolChain
{
public class FilePathObject
{
public string FilePath { get; set; }
}

public class CsvToCreateTableViewModel : INotifyPropertyChanged
{
private string _outputText;
private ObservableCollection<FilePathObject> _filePaths = new ObservableCollection<FilePathObject>();
private string _tableName;

public string OutputText
{
get { return _outputText; }
set
{
_outputText = value;
OnPropertyChanged();
}
}

public ObservableCollection<FilePathObject> FilePaths
{
get { return _filePaths; }
set
{
_filePaths = value;
OnPropertyChanged();
}
}

public string TableName
{
get { return _tableName; }
set
{
_tableName = value;
OnPropertyChanged();
}
}

public bool UseTabDelimiter { get; set; } = true;
public bool UseCommaDelimiter { get; set; }
public int HeaderOffsetRows { get; set; } = 1;

public void Go()
{
var filePaths = _filePaths.Select(p => p.FilePath).ToArray();
var tableName = TableName;
var csvDelimiter = UseTabDelimiter ? '\t' : ',';
var headerOffsetRows = HeaderOffsetRows;

try
{
OutputText = TableSqlUploadHelpers.GetTableSql(filePaths, tableName, csvDelimiter, headerOffsetRows);
}
catch (Exception ex)
{
OutputText = ex.ConcatenateInners();
}
}


public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class CsvToCreateTable : Window
{
public CsvToCreateTableViewModel _viewModel { get; set; } = new CsvToCreateTableViewModel();


public
CsvToCreateTable()
{
InitializeComponent();

DataContext = _viewModel;
}

private void ButtonGoClick(object sender, RoutedEventArgs e)
{
try
{
_viewModel.Go();
}
catch (Exception ex)
{
ex.DisplayInners();
}
}

private void ButtonBrowse_OnClick(object sender, RoutedEventArgs e)
{
var d = new OpenFileDialog();
d.Multiselect = true;

if (d.ShowDialog() == true)
{
_viewModel.FilePaths.AddRange(d.FileNames.Select(p => new FilePathObject(){FilePath = p}));
_viewModel.TableName = Path.GetFileNameWithoutExtension(d.FileName);
}
}
}
}

0 comments on commit 03cdb18

Please sign in to comment.