Implemented initial MongoDB driver
- FireLance DatabaseConnection implementation - Crude test importing from GSMParser to FireLance DB.
This commit is contained in:
+50
-8
@@ -1,4 +1,5 @@
|
||||
using FireLance.Interfaces;
|
||||
using FireLance.Model;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
@@ -6,24 +7,23 @@ namespace DevConsole
|
||||
{
|
||||
class Program
|
||||
{
|
||||
public static GSMParser gsmParser = new GSMParser();
|
||||
public static GSMParser.DatabaseConnection gsmParser = new GSMParser.DatabaseConnection();
|
||||
public static FireLance.DatabaseConnection flcParser = new FireLance.DatabaseConnection();
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
Console.WriteLine($"Connection State: {gsmParser.IsConnectionOpen()}");
|
||||
CommandsMenu();
|
||||
}
|
||||
while (true) { CommandsMenu(); }
|
||||
}
|
||||
|
||||
private static void CommandsMenu()
|
||||
{
|
||||
Console.WriteLine("Please select an option below:\n");
|
||||
Console.WriteLine("A. Database connection");
|
||||
Console.WriteLine("A. GSMParser Database connection");
|
||||
Console.WriteLine("B. Query game by ID");
|
||||
Console.WriteLine("C. Query directory by ID");
|
||||
Console.WriteLine("D. Query game by NAME");
|
||||
Console.WriteLine("E. FireLance Database connection");
|
||||
Console.WriteLine("F. FireLance Import Predefined SaveData Entry");
|
||||
Console.WriteLine("X. Exit application\n");
|
||||
Console.Write(":");
|
||||
|
||||
@@ -44,6 +44,12 @@ namespace DevConsole
|
||||
case ConsoleKey.D:
|
||||
OptionD();
|
||||
break;
|
||||
case ConsoleKey.E:
|
||||
OptionE();
|
||||
break;
|
||||
case ConsoleKey.F:
|
||||
OptionF();
|
||||
break;
|
||||
case ConsoleKey.X:
|
||||
Environment.Exit(0);
|
||||
break;
|
||||
@@ -66,12 +72,14 @@ namespace DevConsole
|
||||
{
|
||||
case ConsoleKey.A:
|
||||
Console.WriteLine("Connect to database");
|
||||
gsmParser.OpenConnection(@"C:\Users\Dunestorm\Projects\FireLance\GSMParser\games.db");
|
||||
gsmParser.OpenConnection(@"C:\Users\Dunestorm\Projects\FireLance\GSMParser\games.db", 0);
|
||||
break;
|
||||
case ConsoleKey.B:
|
||||
gsmParser.CloseCurrentConnection();
|
||||
break;
|
||||
}
|
||||
|
||||
Console.WriteLine($"GSMParser Connection State: {gsmParser.IsConnectionOpen()}\n");
|
||||
}
|
||||
|
||||
private static void OptionB()
|
||||
@@ -100,6 +108,40 @@ namespace DevConsole
|
||||
|
||||
PrintGameEntries(gsmParser.QueryGameByName(r));
|
||||
}
|
||||
|
||||
private static void OptionE()
|
||||
{
|
||||
Console.WriteLine("A. Open DB");
|
||||
Console.WriteLine("B. Close DB");
|
||||
Console.Write(":");
|
||||
|
||||
var userInput = Console.ReadKey();
|
||||
Console.Write("\n\n");
|
||||
|
||||
switch (userInput.Key)
|
||||
{
|
||||
case ConsoleKey.A:
|
||||
Console.WriteLine("Connect to database");
|
||||
flcParser.OpenConnection("sr3", 27017);
|
||||
break;
|
||||
case ConsoleKey.B:
|
||||
flcParser.CloseCurrentConnection();
|
||||
break;
|
||||
}
|
||||
|
||||
Console.WriteLine($"FireLance Connection State: {flcParser.IsConnectionOpen()}\n");
|
||||
}
|
||||
|
||||
private static void OptionF()
|
||||
{
|
||||
Guid guid = Guid.NewGuid();
|
||||
IEnumerable<IDirectoriesModel> dm = gsmParser.QueryDirectoryById("600");
|
||||
foreach (var entry in dm)
|
||||
{
|
||||
entry.Path = guid.ToString();
|
||||
flcParser.InsertRecord("SaveData", entry);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
private static void PrintGameEntries(IEnumerable<IGameEntryModel> gameEntries)
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace FireLance
|
||||
{
|
||||
public class Class1
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using FireLance.Interfaces;
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace FireLance
|
||||
{
|
||||
public class DatabaseConnection : IDatabaseConnection
|
||||
{
|
||||
IMongoDatabase mongoDatabase;
|
||||
|
||||
public void CloseCurrentConnection()
|
||||
{
|
||||
OpenConnection(string.Empty, 0);
|
||||
}
|
||||
|
||||
public bool IsConnectionOpen()
|
||||
{
|
||||
int connectionCnt = 0;
|
||||
|
||||
try { connectionCnt = mongoDatabase.Client.ListDatabases().ToList().Count; }
|
||||
catch {}
|
||||
|
||||
return (connectionCnt > 0) ? true : false;
|
||||
}
|
||||
|
||||
public void OpenConnection(string dbLocation, int port)
|
||||
{
|
||||
var client = new MongoClient($"mongodb://{dbLocation}:{port}");
|
||||
mongoDatabase = client.GetDatabase(dbLocation);
|
||||
}
|
||||
|
||||
public void InsertRecord<T>(string table, T record)
|
||||
{
|
||||
var collection = mongoDatabase.GetCollection<T>(table);
|
||||
collection.InsertOne(record);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,4 +4,8 @@
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.10.4" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
{
|
||||
public interface IDatabaseConnection
|
||||
{
|
||||
public void OpenConnection(string dbLocation);
|
||||
public void OpenConnection(string dbLocation, int port);
|
||||
|
||||
public void CloseCurrentConnection();
|
||||
|
||||
|
||||
@@ -4,12 +4,11 @@ namespace FireLance.Interfaces
|
||||
{
|
||||
public interface ISaveDataModel
|
||||
{
|
||||
string GUID { get; set; }
|
||||
int GameID { get; set; }
|
||||
string SHA256Hash { get; set; }
|
||||
string Description { get; set; }
|
||||
DateTime CreationDate { get; set; }
|
||||
public object SaveData { get; set; }
|
||||
public uint BackupSize { get; set; }
|
||||
object SaveData { get; set; }
|
||||
uint BackupSize { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
using FireLance.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace FireLance.Model
|
||||
{
|
||||
public class DirectoriesModel : IDirectoriesModel
|
||||
{
|
||||
public int id { get; set; }
|
||||
public int GameID { get; set; }
|
||||
public string SpecialPath { get; set; }
|
||||
public string Path { get; set; }
|
||||
public string RegHive { get; set; }
|
||||
public string RegPath { get; set; }
|
||||
public string RegValue { get; set; }
|
||||
public string DefinedFiles { get; set; }
|
||||
public string ExcludedFiles { get; set; }
|
||||
public bool Recurse { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using FireLance.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace FireLance.Model
|
||||
{
|
||||
public class SaveDataModel : ISaveDataModel
|
||||
{
|
||||
public int GameID { get; set; }
|
||||
public string SHA256Hash { get; set; }
|
||||
public string Description { get; set; }
|
||||
public DateTime CreationDate { get; set; }
|
||||
public object SaveData { get; set; }
|
||||
public uint BackupSize { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Dapper;
|
||||
using FireLance.Interfaces;
|
||||
|
||||
namespace GSMParser
|
||||
{
|
||||
public class DatabaseConnection : IDatabaseConnection
|
||||
{
|
||||
private SqliteConnection DBConnection = new SqliteConnection();
|
||||
|
||||
public void OpenConnection(string dbLocation, int port)
|
||||
{
|
||||
if (File.Exists(dbLocation) == false)
|
||||
{
|
||||
throw new FileNotFoundException(dbLocation);
|
||||
}
|
||||
|
||||
DBConnection = new SqliteConnection($"Data Source={dbLocation};Mode=ReadOnly");
|
||||
DBConnection.Open();
|
||||
}
|
||||
|
||||
public void CloseCurrentConnection()
|
||||
{
|
||||
DBConnection.Close();
|
||||
}
|
||||
|
||||
public bool IsConnectionOpen()
|
||||
{
|
||||
return Convert.ToBoolean(DBConnection.State);
|
||||
}
|
||||
|
||||
public IEnumerable<IGameEntryModel> QueryGameById(string id)
|
||||
{
|
||||
return DBConnection.Query<GameEntryModel>(QueryBuilder.GameEntryByGameId(id), new DynamicParameters());
|
||||
}
|
||||
|
||||
public IEnumerable<IDirectoriesModel> QueryDirectoryById(string id)
|
||||
{
|
||||
return DBConnection.Query<DirectoriesModel>(QueryBuilder.DirectoryByGameId(id), new DynamicParameters());
|
||||
}
|
||||
|
||||
public IEnumerable<IGameEntryModel> QueryGameByName(string name)
|
||||
{
|
||||
return DBConnection.Query<GameEntryModel>(QueryBuilder.GameEntryByName(name), new DynamicParameters());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Dapper;
|
||||
using FireLance.Interfaces;
|
||||
|
||||
public class GSMParser : IDatabaseConnection
|
||||
{
|
||||
private SqliteConnection DBConnection = new SqliteConnection();
|
||||
|
||||
public void OpenConnection(string dbLocation)
|
||||
{
|
||||
if (File.Exists(dbLocation) == false)
|
||||
{
|
||||
throw new FileNotFoundException(dbLocation);
|
||||
}
|
||||
|
||||
DBConnection = new SqliteConnection($"Data Source={dbLocation};Mode=ReadOnly");
|
||||
DBConnection.Open();
|
||||
}
|
||||
|
||||
public void CloseCurrentConnection()
|
||||
{
|
||||
DBConnection.Close();
|
||||
}
|
||||
|
||||
public bool IsConnectionOpen()
|
||||
{
|
||||
return Convert.ToBoolean(DBConnection.State);
|
||||
}
|
||||
|
||||
public IEnumerable<IGameEntryModel> QueryGameById(string id)
|
||||
{
|
||||
return DBConnection.Query<GameEntryModel>(QueryBuilder.GameEntryByGameId(id), new DynamicParameters());
|
||||
}
|
||||
|
||||
public IEnumerable<IDirectoriesModel> QueryDirectoryById(string id)
|
||||
{
|
||||
return DBConnection.Query<DirectoriesModel>(QueryBuilder.DirectoryByGameId(id), new DynamicParameters());
|
||||
}
|
||||
|
||||
public IEnumerable<IGameEntryModel> QueryGameByName(string name)
|
||||
{
|
||||
return DBConnection.Query<GameEntryModel>(QueryBuilder.GameEntryByName(name), new DynamicParameters());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user