|
|
@@ -1,5 +1,7 @@
|
|
|
+using System.Collections.ObjectModel;
|
|
|
using System.Data.SQLite;
|
|
|
using System.IO;
|
|
|
+using System.Reflection;
|
|
|
using System.Windows.Threading;
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
using CommunityToolkit.Mvvm.Input;
|
|
|
@@ -9,15 +11,16 @@ namespace DbReload;
|
|
|
|
|
|
public partial class MainViewModel : ObservableObject
|
|
|
{
|
|
|
- [ObservableProperty] private string _sqLiteFile = "";
|
|
|
- [ObservableProperty] private string[] _logFiles = [];
|
|
|
+ [ObservableProperty] private string _sqLiteFile = "C:\\Development\\data\\_test\\test.dbs";
|
|
|
+ [ObservableProperty] private string[] _logFiles = Directory.GetFiles("C:\\Windows\\System32\\config\\systemprofile\\AppData\\Roaming\\PRSServer\\PRSDatabase", "*.sql");
|
|
|
[ObservableProperty] private string _progress = "";
|
|
|
+ [ObservableProperty] private List<string> _log = new();
|
|
|
|
|
|
[RelayCommand]
|
|
|
private void CreateSQLiteFile()
|
|
|
{
|
|
|
OpenFileDialog dlg = new OpenFileDialog();
|
|
|
- dlg.Filter = "SQLite Files (*.db)|*.db";
|
|
|
+ dlg.Filter = "SQLite Files (*.db,*.dbs)|*.db;*.dbs";
|
|
|
if (dlg.ShowDialog() == true)
|
|
|
SqLiteFile = dlg.FileName;
|
|
|
}
|
|
|
@@ -31,13 +34,14 @@ public partial class MainViewModel : ObservableObject
|
|
|
if (dlg.ShowDialog() == true)
|
|
|
LogFiles = dlg.FileNames;
|
|
|
}
|
|
|
-
|
|
|
- private SQLiteConnection _connection;
|
|
|
|
|
|
[RelayCommand]
|
|
|
private async Task Go()
|
|
|
{
|
|
|
Progress = "Opening Database File";
|
|
|
+
|
|
|
+ Log.Clear();
|
|
|
+
|
|
|
var sb = new SQLiteConnectionStringBuilder();
|
|
|
sb.DataSource = SqLiteFile;
|
|
|
sb.Version = 3;
|
|
|
@@ -46,40 +50,54 @@ public partial class MainViewModel : ObservableObject
|
|
|
|
|
|
var conn = sb.ToString();
|
|
|
|
|
|
- _connection = new SQLiteConnection(conn);
|
|
|
- _connection.BusyTimeout = Convert.ToInt32(TimeSpan.FromMinutes(2).TotalMilliseconds);
|
|
|
+ using var connection = new SQLiteConnection(conn);
|
|
|
+ connection.BusyTimeout = Convert.ToInt32(TimeSpan.FromMinutes(2).TotalMilliseconds);
|
|
|
+ connection.Update += _connection_Update;
|
|
|
+ connection.Trace += _connection_Trace;
|
|
|
|
|
|
- _connection.Open();
|
|
|
- var command = new SQLiteCommand(_connection);
|
|
|
+ connection.Open();
|
|
|
+ using var command = new SQLiteCommand(connection);
|
|
|
var ordered = LogFiles.OrderBy(x => x);
|
|
|
foreach (var logfile in ordered)
|
|
|
{
|
|
|
Progress = $"Opening {Path.GetFileName(logfile)}";
|
|
|
- var text = await File.ReadAllTextAsync(logfile);
|
|
|
- //var statements = text.Split(";\r\n");
|
|
|
- //int i = 0;
|
|
|
- //foreach (var line in statements)
|
|
|
- //{
|
|
|
- // await Dispatcher.CurrentDispatcher.BeginInvoke(() =>
|
|
|
- // {
|
|
|
- // Progress = "Processing {Path.GetFileName(logfile)} ({i}/{lines.Length})";
|
|
|
- // });
|
|
|
+ var text = await File.ReadAllTextAsync(logfile).ConfigureAwait(false);
|
|
|
+ var statements = SqlTokenizer.SplitStatements(text);
|
|
|
+ int i = 0;
|
|
|
+ foreach (var line in statements)
|
|
|
+ {
|
|
|
+ Progress = $"Processing {Path.GetFileName(logfile)} ({i}/{statements.Count})";
|
|
|
|
|
|
- command.CommandText = text;
|
|
|
+ command.CommandText = line;
|
|
|
try
|
|
|
{
|
|
|
- command.ExecuteNonQuery();
|
|
|
+ await command.ExecuteNonQueryAsync();
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
+ Log.Add($"{Path.GetFileName(logfile)}: \"{e.Message}\" in \"{line}\"");
|
|
|
Console.WriteLine(e);
|
|
|
//throw;
|
|
|
}
|
|
|
|
|
|
- // i++;
|
|
|
- //}
|
|
|
+ i++;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ var logFile = Path.Combine(
|
|
|
+ Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location) ?? "",
|
|
|
+ "log.txt");
|
|
|
+ await File.AppendAllLinesAsync(logFile, new string[] { "", $"Reload at {DateTime.Now:yyyy-MM-dd hh\\:mm\\:ss}" }.Concat(Log));
|
|
|
+
|
|
|
+ Progress = "Done";
|
|
|
+ }
|
|
|
+
|
|
|
+ private void _connection_Update(object sender, UpdateEventArgs e)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ private void _connection_Trace(object sender, TraceEventArgs e)
|
|
|
+ {
|
|
|
+ Console.WriteLine(e.Statement);
|
|
|
}
|
|
|
-
|
|
|
}
|