From 8e777e93305e70f68649fbabdd2aa5dcbf01ced6 Mon Sep 17 00:00:00 2001 From: Stedd Date: Sat, 12 Feb 2022 15:49:32 +0100 Subject: [PATCH 1/2] Merged DBs to one BD --- App.config | 4 +--- DBClasses.cs | 7 +++++-- DBInteraction.cs | 20 ++++++++++---------- MainWindow.xaml.cs | 4 ++-- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/App.config b/App.config index 924c79d..e124558 100644 --- a/App.config +++ b/App.config @@ -1,8 +1,6 @@  - - - + \ No newline at end of file diff --git a/DBClasses.cs b/DBClasses.cs index ccb3af4..88e1ff8 100644 --- a/DBClasses.cs +++ b/DBClasses.cs @@ -6,21 +6,24 @@ namespace CryptoCalc public interface IDBClasses { - string DBVariables { get; set; } + string DBName { get; } string DBTableName { get; } + string DBVariables { get; set; } string DBSaveDataString { get; } } public class DBClasses : IDBClasses { #region Publics - public virtual string DBVariables { get; set; } + public string DBName => "CryptoCalc"; public virtual string DBTableName { get; } + public virtual string DBVariables { get; set; } public string DBSaveDataString => $"({Regex.Replace(DBVariables, "@", "")}) values ({DBVariables})"; public int Index { get; set; } public long UnixTime { get; set; } + #endregion #region Functions diff --git a/DBInteraction.cs b/DBInteraction.cs index 3a6eac9..572918b 100644 --- a/DBInteraction.cs +++ b/DBInteraction.cs @@ -17,7 +17,7 @@ namespace CryptoCalc throw new ArgumentNullException(nameof(data)); } - using IDbConnection cnn = new SQLiteConnection(Util.GetConnectionString(data.DBTableName)); + using IDbConnection cnn = new SQLiteConnection(Util.GetConnectionString(data.DBName)); cnn.Execute($"insert into {data.DBTableName} {data.DBSaveDataString}", data); Debug.WriteLine($"Saved {data.DBTableName} DB data"); } @@ -26,25 +26,25 @@ namespace CryptoCalc public class GenericDB where T : DBClasses { - public IEnumerable LoadAllData(string tableName) + public IEnumerable LoadAllData(T data) { - if (tableName is null) + if (data.DBTableName is null) { - throw new ArgumentNullException(nameof(tableName)); + throw new ArgumentNullException(nameof(data.DBTableName)); } - using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(tableName)); - return _connection.Query($"select * from {tableName}", new DynamicParameters()); + using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(data.DBName)); + return _connection.Query($"select * from {data.DBTableName}", new DynamicParameters()); } - public IEnumerable QueryData(string tableName, string query) + public IEnumerable QueryData(T data, string query) { - if (tableName is null) + if (data.DBTableName is null) { - throw new ArgumentNullException(nameof(query)); + throw new ArgumentNullException(nameof(data.DBTableName)); } - using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(tableName)); + using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(data.DBTableName)); return _connection.Query($"{query}", new DynamicParameters()); } } diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 26d9f68..15d1ebc 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -36,7 +36,7 @@ namespace CryptoCalc private void readButton_click(object sender, RoutedEventArgs e) { - IEnumerable transactions = transactionDB.LoadAllData(genericT.DBTableName); + IEnumerable transactions = transactionDB.LoadAllData(genericT); foreach (Transaction t in transactions) { Debug.WriteLine($"{t.GetLocalTimeFromUnixTime(t.UnixTime)} *** {t.Currency} - {t.Amount}"); @@ -45,7 +45,7 @@ namespace CryptoCalc private void searchButton_Click(object sender, RoutedEventArgs e) { string query = $"SELECT * from {genericT.DBTableName} WHERE {nameof(genericT.Currency)}=\'{currencyText.Text}\'"; - IEnumerable transactions = transactionDB.QueryData(genericT.DBTableName, query); + IEnumerable transactions = transactionDB.QueryData(genericT, query); transactionsFoundListBox.Items.Clear(); foreach (Transaction x in transactions) { From 4007190be380ddb8715d2e3c2a29acc7c0af3a2c Mon Sep 17 00:00:00 2001 From: Stedd Date: Sat, 12 Feb 2022 16:09:37 +0100 Subject: [PATCH 2/2] SQL interaction update Using only Query method + text input cleanup --- DBInteraction.cs | 20 ++++---------------- MainWindow.xaml | 4 ++-- MainWindow.xaml.cs | 20 +++++++++++++++----- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/DBInteraction.cs b/DBInteraction.cs index 572918b..4d27355 100644 --- a/DBInteraction.cs +++ b/DBInteraction.cs @@ -14,7 +14,7 @@ namespace CryptoCalc { if (data is null) { - throw new ArgumentNullException(nameof(data)); + throw new ArgumentNullException(nameof(data), "No data passed to SQL"); } using IDbConnection cnn = new SQLiteConnection(Util.GetConnectionString(data.DBName)); @@ -25,26 +25,14 @@ namespace CryptoCalc public class GenericDB where T : DBClasses { - - public IEnumerable LoadAllData(T data) + public IEnumerable QueryData(T data, string query) { - if (data.DBTableName is null) + if (data is null) { - throw new ArgumentNullException(nameof(data.DBTableName)); + throw new ArgumentNullException(nameof(data), "DBclass is null"); } using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(data.DBName)); - return _connection.Query($"select * from {data.DBTableName}", new DynamicParameters()); - } - - public IEnumerable QueryData(T data, string query) - { - if (data.DBTableName is null) - { - throw new ArgumentNullException(nameof(data.DBTableName)); - } - - using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(data.DBTableName)); return _connection.Query($"{query}", new DynamicParameters()); } } diff --git a/MainWindow.xaml b/MainWindow.xaml index a369759..02c105a 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -14,7 +14,7 @@