Extract sqlite conntection to AppDbContext
This commit is contained in:
@ -6,6 +6,8 @@ using System.IO;
|
||||
using System;
|
||||
using System.Windows;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Elwig.Helpers {
|
||||
public class AppDbContext : DbContext {
|
||||
@ -60,7 +62,20 @@ namespace Elwig.Helpers {
|
||||
}
|
||||
SavedLastWriteTime = LastWriteTime;
|
||||
SavedChanges += OnSavedChanges;
|
||||
}
|
||||
|
||||
public static SqliteConnection Connect() {
|
||||
var cnx = new SqliteConnection(ConnectionString);
|
||||
cnx.CreateFunction<string, string?, bool?>("REGEXP", (pattern, value) => value == null ? null : Regex.Match(value, pattern).Success, true);
|
||||
cnx.Open();
|
||||
return cnx;
|
||||
}
|
||||
|
||||
public static async Task<SqliteConnection> ConnectAsync() {
|
||||
var cnx = new SqliteConnection(ConnectionString);
|
||||
cnx.CreateFunction<string, string?, bool?>("REGEXP", (pattern, value) => value == null ? null : Regex.Match(value, pattern).Success, true);
|
||||
await cnx.OpenAsync();
|
||||
return cnx;
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
|
||||
|
@ -1,9 +1,8 @@
|
||||
using Elwig.Helpers;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
@ -14,26 +13,24 @@ namespace Elwig.Windows {
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void QueryButton_Click(object sender, RoutedEventArgs evt) {
|
||||
private async void QueryButton_Click(object sender, RoutedEventArgs evt) {
|
||||
try {
|
||||
ExecuteQuery(QueryInput.Text);
|
||||
await ExecuteQuery(QueryInput.Text);
|
||||
} catch (Exception e) {
|
||||
MessageBox.Show(e.Message, "Fehler beim Ausführen", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private void ExecuteQuery(string sqlQuery) {
|
||||
private async Task ExecuteQuery(string sqlQuery) {
|
||||
var rows = new List<object[]>();
|
||||
IList<DbColumn> header;
|
||||
|
||||
using (var cnx = new SqliteConnection(AppDbContext.ConnectionString)) {
|
||||
cnx.CreateFunction<string, string?, bool?>("REGEXP", (pattern, value) => value == null ? null : Regex.Match(value, pattern).Success, true);
|
||||
cnx.Open();
|
||||
using (var cnx = await AppDbContext.ConnectAsync()) {
|
||||
var cmd = cnx.CreateCommand();
|
||||
cmd.CommandText = sqlQuery;
|
||||
using var reader = cmd.ExecuteReader();
|
||||
header = reader.GetColumnSchema();
|
||||
while (reader.Read()) {
|
||||
using var reader = await cmd.ExecuteReaderAsync();
|
||||
header = await reader.GetColumnSchemaAsync();
|
||||
while (await reader.ReadAsync()) {
|
||||
var values = new object[reader.FieldCount];
|
||||
reader.GetValues(values);
|
||||
rows.Add(values);
|
||||
|
Reference in New Issue
Block a user