Generating DB and Tables on startup
This commit is contained in:
parent
7f59ecf7a2
commit
3d201103ae
BIN
CryptoCalc.db
BIN
CryptoCalc.db
Binary file not shown.
44
DBClasses.cs
44
DBClasses.cs
|
@ -6,16 +6,17 @@ namespace CryptoCalc
|
||||||
|
|
||||||
public interface IDBClasses
|
public interface IDBClasses
|
||||||
{
|
{
|
||||||
string DBName { get; }
|
static string DBName { get; }
|
||||||
string DBTableName { get; }
|
string DBTableName { get; }
|
||||||
string DBVariables { get; set; }
|
string DBVariables { get; set; }
|
||||||
string DBSaveDataString { get; }
|
string DBSaveDataString { get; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class DBClasses : IDBClasses
|
public class DBClasses : IDBClasses
|
||||||
{
|
{
|
||||||
#region Publics
|
#region Publics
|
||||||
public string DBName => "CryptoCalc";
|
public static string DBName => "CryptoCalc";
|
||||||
public virtual string DBTableName { get; }
|
public virtual string DBTableName { get; }
|
||||||
public virtual string DBVariables { get; set; }
|
public virtual string DBVariables { get; set; }
|
||||||
public string DBSaveDataString => $"({Regex.Replace(DBVariables, "@", "")}) values ({DBVariables})";
|
public string DBSaveDataString => $"({Regex.Replace(DBVariables, "@", "")}) values ({DBVariables})";
|
||||||
|
@ -59,6 +60,7 @@ namespace CryptoCalc
|
||||||
public override string DBVariables { get => base.DBVariables; set => base.DBVariables = value; }
|
public override string DBVariables { get => base.DBVariables; set => base.DBVariables = value; }
|
||||||
public override string DBTableName => "Transactions";
|
public override string DBTableName => "Transactions";
|
||||||
|
|
||||||
|
public int WalletID { get; set; }
|
||||||
public string Currency { get; set; }
|
public string Currency { get; set; }
|
||||||
public float Amount { get; set; }
|
public float Amount { get; set; }
|
||||||
public string TransactionType { get; set; }
|
public string TransactionType { get; set; }
|
||||||
|
@ -107,7 +109,8 @@ namespace CryptoCalc
|
||||||
private void SetDBStrings()
|
private void SetDBStrings()
|
||||||
{
|
{
|
||||||
DBVariables =
|
DBVariables =
|
||||||
$"@{nameof(UnixTime)},"
|
$"@{nameof(WalletID)},"
|
||||||
|
+ $"@{nameof(UnixTime)},"
|
||||||
+ $"@{nameof(Currency)},"
|
+ $"@{nameof(Currency)},"
|
||||||
+ $"@{nameof(Amount)},"
|
+ $"@{nameof(Amount)},"
|
||||||
+ $"@{nameof(TransactionType)},"
|
+ $"@{nameof(TransactionType)},"
|
||||||
|
@ -119,6 +122,25 @@ namespace CryptoCalc
|
||||||
|
|
||||||
public string FullInfo => $"{ Index } { GetLocalTimeFromUnixTime(UnixTime) } { Currency } { Amount } { TransactionType }";
|
public string FullInfo => $"{ Index } { GetLocalTimeFromUnixTime(UnixTime) } { Currency } { Amount } { TransactionType }";
|
||||||
|
|
||||||
|
public string CreateTable(string walletTableName)
|
||||||
|
{
|
||||||
|
return
|
||||||
|
$"CREATE TABLE IF NOT EXISTS \"{DBTableName}\" ("
|
||||||
|
+ $"\"{nameof(Index)}\" INTEGER NOT NULL UNIQUE,"
|
||||||
|
+ $"\"{nameof(WalletID)}\" INTEGER NOT NULL,"
|
||||||
|
+ $"\"{nameof(UnixTime)}\" INTEGER NOT NULL,"
|
||||||
|
+ $"\"{nameof(Currency)}\" TEXT NOT NULL,"
|
||||||
|
+ $"\"{nameof(Amount)}\" REAL NOT NULL,"
|
||||||
|
+ $"\"{nameof(TransactionType)}\" TEXT NOT NULL,"
|
||||||
|
+ $"\"{nameof(FeeCurrency)}\" TEXT,"
|
||||||
|
+ $"\"{nameof(FeeAmount)}\" REAL,"
|
||||||
|
+ $"\"{nameof(Platform)}\" TEXT,"
|
||||||
|
+ $"\"{nameof(Note)}\" TEXT,"
|
||||||
|
+ $"PRIMARY KEY(\"{nameof(Index)}\" AUTOINCREMENT),"
|
||||||
|
+ $"FOREIGN KEY(\"{nameof(WalletID)}\") REFERENCES \"{walletTableName}\"(\"{nameof(Index)}\")"
|
||||||
|
+ $"); ";
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,6 +198,22 @@ namespace CryptoCalc
|
||||||
|
|
||||||
//public string FullInfo => $"{ Index.ToString() } { DateTimeString } { Currency } { Amount } { TransactionType }";
|
//public string FullInfo => $"{ Index.ToString() } { DateTimeString } { Currency } { Amount } { TransactionType }";
|
||||||
|
|
||||||
|
public string CreateTable()
|
||||||
|
{
|
||||||
|
return
|
||||||
|
$"CREATE TABLE IF NOT EXISTS \"{DBTableName}\" ("
|
||||||
|
+ $"\"{nameof(Index)}\" INTEGER NOT NULL UNIQUE,"
|
||||||
|
+ $"\"{nameof(UnixTime)}\" INTEGER NOT NULL,"
|
||||||
|
+ $"\"{nameof(UnixTimeCreated)}\" INTEGER NOT NULL,"
|
||||||
|
+ $"\"{nameof(Platform)}\" TEXT,"
|
||||||
|
+ $"\"{nameof(Name)}\" TEXT,"
|
||||||
|
+ $"\"{nameof(Currency)}\" TEXT NOT NULL,"
|
||||||
|
+ $"\"{nameof(Balance)}\" REAL NOT NULL,"
|
||||||
|
+ $"\"{nameof(DefaultWallet)}\" INTEGER,"
|
||||||
|
+ $"\"{nameof(Note)}\" TEXT,"
|
||||||
|
+ $"PRIMARY KEY(\"{nameof(Index)}\" AUTOINCREMENT)"
|
||||||
|
+ $"); ";
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,12 +4,17 @@ using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Data.SQLite;
|
using System.Data.SQLite;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace CryptoCalc
|
namespace CryptoCalc
|
||||||
{
|
{
|
||||||
public class DBInteraction<T> where T : DBClasses
|
public class DBInteraction<T> where T : DBClasses
|
||||||
{
|
{
|
||||||
|
|
||||||
|
public void CreateDB()
|
||||||
|
{
|
||||||
|
SQLiteConnection.CreateFile(DBClasses.DBName);
|
||||||
|
}
|
||||||
|
|
||||||
public void SaveData(T data)
|
public void SaveData(T data)
|
||||||
{
|
{
|
||||||
if (data is null)
|
if (data is null)
|
||||||
|
@ -17,10 +22,10 @@ namespace CryptoCalc
|
||||||
throw new ArgumentNullException(nameof(data), "No data passed to SQL");
|
throw new ArgumentNullException(nameof(data), "No data passed to SQL");
|
||||||
}
|
}
|
||||||
|
|
||||||
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(data.DBName));
|
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(DBClasses.DBName));
|
||||||
string query = $"insert into {data.DBTableName} {data.DBSaveDataString}";
|
string query = $"insert into {data.DBTableName} {data.DBSaveDataString}";
|
||||||
_connection.Execute(query, data);
|
_connection.Execute(query, data);
|
||||||
Debug.WriteLine($"Saved data - DB: {data.DBName} - Table: {data.DBTableName} - Query: {query}");
|
Debug.WriteLine($"Saved data - DB: {DBClasses.DBName} - Table: {data.DBTableName} - Query: {query}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ExecuteQuery(T data, string query)
|
public void ExecuteQuery(T data, string query)
|
||||||
|
@ -30,9 +35,9 @@ namespace CryptoCalc
|
||||||
throw new ArgumentNullException(nameof(query), "No query entered");
|
throw new ArgumentNullException(nameof(query), "No query entered");
|
||||||
}
|
}
|
||||||
|
|
||||||
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(data.DBName));
|
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(DBClasses.DBName));
|
||||||
_connection.Execute($"{query}");
|
_connection.Execute($"{query}");
|
||||||
Debug.WriteLine($"Executed query - DB: {data.DBName} - Query: {query}");
|
Debug.WriteLine($"Executed query - DB: {DBClasses.DBName} - Query: {query}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<T> ReturnQuery(T data, string query)
|
public IEnumerable<T> ReturnQuery(T data, string query)
|
||||||
|
@ -42,9 +47,9 @@ namespace CryptoCalc
|
||||||
throw new ArgumentNullException(nameof(data), "DBclass is null");
|
throw new ArgumentNullException(nameof(data), "DBclass is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(data.DBName));
|
using IDbConnection _connection = new SQLiteConnection(Util.GetConnectionString(DBClasses.DBName));
|
||||||
IEnumerable<T> output = _connection.Query<T>($"{query}", new DynamicParameters());
|
IEnumerable<T> output = _connection.Query<T>($"{query}", new DynamicParameters());
|
||||||
Debug.WriteLine($"Executed query - DB: {data.DBName} - Query: {query}");
|
Debug.WriteLine($"Executed query - DB: {DBClasses.DBName} - Query: {query}");
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
<ListBox x:Name="transactionsFoundListBox" Margin="27,66,331,10"/>
|
<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" LostFocus="currencyText_LostFocus"/>
|
<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"/>
|
<Button x:Name="searchButton" Content="Search" HorizontalAlignment="Left" Margin="169,20,0,0" VerticalAlignment="Top" Click="searchButton_Click" Height="25" Width="55"/>
|
||||||
|
<ListBox x:Name="listBox" Margin="518,234,37,450"/>
|
||||||
</Grid>
|
</Grid>
|
||||||
</TabItem>
|
</TabItem>
|
||||||
<TabItem Header="Overview">
|
<TabItem Header="Overview">
|
||||||
|
|
|
@ -9,6 +9,7 @@ namespace CryptoCalc
|
||||||
{
|
{
|
||||||
private readonly Random rand = new();
|
private readonly Random rand = new();
|
||||||
|
|
||||||
|
private readonly DBInteraction<DBClasses> DB = new();
|
||||||
private readonly DBInteraction<Transaction> transactionDB = new();
|
private readonly DBInteraction<Transaction> transactionDB = new();
|
||||||
private readonly DBInteraction<Wallet> walletDB = new();
|
private readonly DBInteraction<Wallet> walletDB = new();
|
||||||
|
|
||||||
|
@ -18,6 +19,13 @@ namespace CryptoCalc
|
||||||
public MainWindow()
|
public MainWindow()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
|
//Create the DB file if none exist
|
||||||
|
DB.CreateDB();
|
||||||
|
|
||||||
|
//Create tables in DB if none exist
|
||||||
|
transactionDB.ExecuteQuery(genericT, genericT.CreateTable(genericW.DBTableName));
|
||||||
|
walletDB.ExecuteQuery(genericW, genericW.CreateTable());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveButton_Click(object sender, RoutedEventArgs e)
|
private void saveButton_Click(object sender, RoutedEventArgs e)
|
||||||
|
@ -57,6 +65,7 @@ namespace CryptoCalc
|
||||||
private Transaction DummyTransaction()
|
private Transaction DummyTransaction()
|
||||||
{
|
{
|
||||||
Transaction t = new();
|
Transaction t = new();
|
||||||
|
t.WalletID = 1;
|
||||||
t.SaveUnixTimeNow();
|
t.SaveUnixTimeNow();
|
||||||
t.Currency = "SOL";
|
t.Currency = "SOL";
|
||||||
t.Amount = Convert.ToSingle(30 * rand.NextDouble());
|
t.Amount = Convert.ToSingle(30 * rand.NextDouble());
|
||||||
|
|
Loading…
Reference in New Issue