Merge branch 'Use-1DB-multiple-tables'

This commit is contained in:
Stedd 2022-02-12 16:09:54 +01:00
commit e15d5ed3af
5 changed files with 30 additions and 31 deletions

View File

@ -1,8 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="RawData" connectionString="Data Source = ./data.db;version=3" providerName="System.Data.SqlClient"/>
<add name="Transactions" connectionString="Data Source = ./transactions.db;version=3" providerName="System.Data.SqlClient"/>
<add name="Wallets" connectionString="Data Source = ./wallets.db;version=3" providerName="System.Data.SqlClient"/>
<add name="CryptoCalc" connectionString="Data Source = ./CryptoCalc.db;version=3" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>

View File

@ -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

View File

@ -14,10 +14,10 @@ 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.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");
}
@ -25,26 +25,14 @@ namespace CryptoCalc
public class GenericDB<T> where T : DBClasses
{
public IEnumerable<T> LoadAllData(string tableName)
public IEnumerable<T> QueryData(T data, string query)
{
if (tableName is null)
if (data is null)
{
throw new ArgumentNullException(nameof(tableName));
throw new ArgumentNullException(nameof(data), "DBclass is null");
}
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(tableName));
return _connection.Query<T>($"select * from {tableName}", new DynamicParameters());
}
public IEnumerable<T> QueryData(string tableName, string query)
{
if (tableName is null)
{
throw new ArgumentNullException(nameof(query));
}
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(tableName));
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(data.DBName));
return _connection.Query<T>($"{query}", new DynamicParameters());
}
}

View File

@ -14,7 +14,7 @@
<Button x:Name="saveWalletButton" Content="Save Random Wallet" Margin="0,120,37,0" Click="saveWalletButton_Click" FontSize="20" HorizontalAlignment="Right" Width="239" Height="38" VerticalAlignment="Top"/>
<Button x:Name="readButton" Content="PrintToConsole" Margin="0,53,37,0" Click="readButton_click" FontSize="20" Height="36" VerticalAlignment="Top" HorizontalAlignment="Right" Width="239"/>
<Button x:Name="saveButton_fromInput" Content="Save Data" Margin="0,0,37,10" VerticalAlignment="Bottom" Height="39" Click="saveButtonFromInput_Click" FontSize="20" HorizontalAlignment="Right" Width="239"/>
<TextBox x:Name="inputCurrency" Margin="0,0,60,125" TextWrapping="Wrap" Text="BTC" Height="27" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="120"/>
<TextBox x:Name="inputCurrency" Margin="0,0,60,125" TextWrapping="Wrap" Text="BTC" Height="27" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="120" LostFocus="inputCurrency_LostFocus"/>
<Label x:Name="label" Content="Currency" HorizontalAlignment="Right" Margin="0,0,185,125" Width="69" Height="27" VerticalAlignment="Bottom"/>
<TextBox x:Name="inputAmount" Margin="0,0,60,94" TextWrapping="Wrap" Text="1,3141592" LostFocus="inputAmount_LostFocus" Height="27" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="120"/>
<Label x:Name="label_Copy" Content="Amount" HorizontalAlignment="Right" Margin="0,0,185,94" Width="69" Height="27" VerticalAlignment="Bottom"/>
@ -22,7 +22,7 @@
<Label x:Name="label_Copy1" Content="Type" HorizontalAlignment="Right" Margin="0,0,185,62" Width="69" Height="27" VerticalAlignment="Bottom"/>
<Label x:Name="currencyLablel" Content="Currency" HorizontalAlignment="Left" Margin="27,16,0,0" VerticalAlignment="Top" Height="32"/>
<ListBox x:Name="transactionsFoundListBox" Margin="27,66,331,10"/>
<TextBox x:Name="currencyText" HorizontalAlignment="Left" Margin="89,21,0,0" Text="SOL" TextWrapping="Wrap" VerticalAlignment="Top" Width="75" Height="22"/>
<TextBox x:Name="currencyText" HorizontalAlignment="Left" Margin="89,21,0,0" Text="SOL" TextWrapping="Wrap" VerticalAlignment="Top" Width="75" Height="22" LostFocus="currencyText_LostFocus"/>
<Button x:Name="searchButton" Content="Search" HorizontalAlignment="Left" Margin="169,20,0,0" VerticalAlignment="Top" Click="searchButton_Click" Height="25" Width="55"/>
</Grid>
</TabItem>

View File

@ -36,7 +36,8 @@ namespace CryptoCalc
private void readButton_click(object sender, RoutedEventArgs e)
{
IEnumerable <Transaction> transactions = transactionDB.LoadAllData(genericT.DBTableName);
string query = $"select * from {genericT.DBTableName}";
IEnumerable <Transaction> transactions = transactionDB.QueryData(genericT, query);
foreach (Transaction t in transactions)
{
Debug.WriteLine($"{t.GetLocalTimeFromUnixTime(t.UnixTime)} *** {t.Currency} - {t.Amount}");
@ -45,7 +46,7 @@ namespace CryptoCalc
private void searchButton_Click(object sender, RoutedEventArgs e)
{
string query = $"SELECT * from {genericT.DBTableName} WHERE {nameof(genericT.Currency)}=\'{currencyText.Text}\'";
IEnumerable<Transaction> transactions = transactionDB.QueryData(genericT.DBTableName, query);
IEnumerable<Transaction> transactions = transactionDB.QueryData(genericT, query);
transactionsFoundListBox.Items.Clear();
foreach (Transaction x in transactions)
{
@ -71,11 +72,11 @@ namespace CryptoCalc
w.SaveUnixTimeNow();
w.UnixTimeCreated = w.GetUnixTime(new DateTime(rand.Next(2011, DateTime.Now.Year), rand.Next(1,12), rand.Next(1, 28)));
w.Platform = "Ledger";
w.Name = "TestWallet";
w.Currency = "SOL";
w.Name = "DefaultBTCWallet";
w.Currency = "BTC";
w.Balance = Convert.ToSingle(30 * rand.NextDouble());
w.DefaultWallet = 0;
w.Note = "Test";
w.DefaultWallet = 1;
w.Note = "Main BTC Wallet";
return w;
}
@ -85,5 +86,14 @@ namespace CryptoCalc
inputAmount.Text = inputAmount.Text.Replace(".", ",");
}
private void currencyText_LostFocus(object sender, RoutedEventArgs e)
{
currencyText.Text = currencyText.Text.ToUpper();
}
private void inputCurrency_LostFocus(object sender, RoutedEventArgs e)
{
inputCurrency.Text = inputCurrency.Text.ToUpper();
}
}
}