CryptoCalc/DBInteraction.cs

53 lines
1.6 KiB
C#

using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Diagnostics;
using System.Linq;
namespace CryptoCalc
{
public class DBInteraction
{
public static void SaveData<T>(ref T data) where T : DBClasses
{
if (data is null)
{
throw new ArgumentNullException(nameof(data));
}
using IDbConnection cnn = new SQLiteConnection(Util.GetConnectionString(data.DBTableName));
cnn.Execute($"insert into {data.DBTableName} {data.DBSaveDataString}", data);
Debug.WriteLine($"Saved {data.DBTableName} DB data");
}
public class GenericDB<T> where T : DBClasses
{
public IEnumerable<T> LoadAllData(string tableName)
{
if (tableName is null)
{
throw new ArgumentNullException(nameof(tableName));
}
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));
return _connection.Query<T>($"{query}", new DynamicParameters());
}
}
}
}