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.

87 lines
2.1 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 (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)
{
Console.WriteLine(_tables.Count.ToString().Pastel("#ff0000"));
var table = query?.Table;
var where = query?.Where;
var split = where?.Split("|");
var colName = split?[0];
var condition = split?[1];
var value = split?[2];
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 static void Loop()
{
while (true)
{
}
}
}