You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
2.5 KiB
101 lines
2.5 KiB
using Newtonsoft.Json;
|
|
using Pastel;
|
|
using RSND.Core.DbInternals;
|
|
using RSND.Core.Querying.Queries;
|
|
|
|
namespace RSND.Core;
|
|
|
|
public class Database
|
|
{
|
|
private string Name { get; set; }
|
|
|
|
private List<Table> _tables = new();
|
|
|
|
public Database(string name)
|
|
{
|
|
Name = name;
|
|
}
|
|
|
|
public void SetupFiles()
|
|
{
|
|
if (File.Exists($"{Name}.json"))
|
|
{
|
|
Console.WriteLine($"[{Name}] The file exists.");
|
|
var json = File.ReadAllText($"{Name}.json");
|
|
var db = JsonConvert.DeserializeObject<List<Table>>(json);
|
|
|
|
// if this is null, then something is wrong with the file
|
|
if (db != null)
|
|
_tables = db;
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($"[{Name}] The file is getting created.");
|
|
File.Create($"{Name}.json");
|
|
}
|
|
}
|
|
public void Save()
|
|
{
|
|
try
|
|
{
|
|
var json = JsonConvert.SerializeObject(_tables);
|
|
File.WriteAllText($"{Name}.json", json);
|
|
}
|
|
catch
|
|
{
|
|
// slim chance there might be file structure issues.
|
|
// let's attempt to fix that.
|
|
SetupFiles();
|
|
}
|
|
}
|
|
|
|
public void CreateTable(Table query)
|
|
{
|
|
if (_tables.Any(x => x.Name == query.Name))
|
|
{
|
|
_tables.RemoveAll(x => x.Name == query.Name);
|
|
}
|
|
|
|
_tables.Add(query);
|
|
Save();
|
|
}
|
|
|
|
public string GetValue(GetQuery? query)
|
|
{
|
|
var table = query?.TableName;
|
|
|
|
var colName = query?.Key;
|
|
var value = query?.Value;
|
|
|
|
if (query?.Select != "*") return "";
|
|
|
|
var tableToReturn = _tables.Find(x => x.Name == table);
|
|
var rows = tableToReturn?.Rows?.Where(x => x.Columns?.FirstOrDefault(y => y.Name == colName)?.Value == value)
|
|
.ToArray();
|
|
|
|
return JsonConvert.SerializeObject(rows);
|
|
}
|
|
|
|
public void SetValue(SetQuery? query)
|
|
{
|
|
var table = query?.TableName;
|
|
var key = query?.Key;
|
|
var value = query?.Value;
|
|
var newValue = query?.NewValue;
|
|
|
|
var tableToReturn = _tables.Find(x => x.Name == table);
|
|
var row = tableToReturn?.Rows?.FirstOrDefault(x => x.Columns?.FirstOrDefault(y => y.Name == key)?.Value == value);
|
|
var column = row?.Columns?.FirstOrDefault(x => x.Name == key);
|
|
column.Value = newValue;
|
|
|
|
Save();
|
|
}
|
|
|
|
public static void Loop()
|
|
{
|
|
while (true)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|