diff --git a/RSND/Core/Database.cs b/RSND/Core/Database.cs index 258eb1c..6c160ae 100644 --- a/RSND/Core/Database.cs +++ b/RSND/Core/Database.cs @@ -1,4 +1,5 @@ using Newtonsoft.Json; +using Pastel; using RSND.Core.DbInternals; using RSND.Core.Querying.Queries; @@ -8,9 +9,16 @@ public class Database { List tables = new(); - public void CreateTable(Table table) + public void CreateTable(Table query) { - tables.Add(table); + var matches = tables.FirstOrDefault(x => x.Name == query.Name); + + if (matches != null) + { + tables.Remove(matches); + } + + tables.Add(query); } private static string GetRowsJson(Row[]? rows) @@ -20,7 +28,7 @@ public class Database public string GetValue(GetQuery? query) { - Console.WriteLine(tables.Count); + Console.WriteLine(tables.Count.ToString().Pastel("#ff0000")); var table = query?.Table; var where = query?.Where; @@ -33,15 +41,16 @@ public class Database var tableToReturn = tables.Find(x => x.Name == table); - var rows = tableToReturn?.Rows?.Where(x => x.Columns.FirstOrDefault(y => y.Data.Key == colName)?.Data.Value == param).ToArray(); + var rows = tableToReturn?.Rows?.Where(x => x.Columns?.FirstOrDefault(y => y.Data.Key == colName)?.Data.Value == param).ToArray(); return GetRowsJson(rows); } - + public static void Loop() { while (true) { + } } } \ No newline at end of file diff --git a/RSND/Core/DbClient.cs b/RSND/Core/DbClient.cs index 5c7e127..115174d 100644 --- a/RSND/Core/DbClient.cs +++ b/RSND/Core/DbClient.cs @@ -1,5 +1,6 @@ using Fleck; using Newtonsoft.Json; +using Pastel; using RSND.Core.DbInternals; using RSND.Core.Querying; using RSND.Core.Querying.Queries; @@ -71,16 +72,18 @@ public class DbClient GetQuery getQuery = JsonConvert.DeserializeObject(query); var queryResult = RsndMain.Db.GetValue(getQuery); _socket.Send(queryResult); - Console.WriteLine($"Yooo: {queryResult}"); + Console.WriteLine($"Sent: {queryResult}".Pastel("#71C562")); break; } case QueryType.CreateTable: { - /*RsndMain.Db.CreateTable(new Table + CreateTableQuery createTableQuery = JsonConvert.DeserializeObject(query); + RsndMain.Db.CreateTable(new Table { - Name = query.Table - });*/ + Name = createTableQuery?.TableName, + Rows = createTableQuery?.Rows + }); break; } diff --git a/RSND/Core/Querying/Queries/CreateTableQuery.cs b/RSND/Core/Querying/Queries/CreateTableQuery.cs new file mode 100644 index 0000000..214317c --- /dev/null +++ b/RSND/Core/Querying/Queries/CreateTableQuery.cs @@ -0,0 +1,15 @@ +using RSND.Core.DbInternals; + +namespace RSND.Core.Querying.Queries; + +public class CreateTableQuery : IQuery +{ + public CreateTableQuery(string tableName, Row[] rows) + { + TableName = tableName; + Rows = rows; + } + + public string TableName { get; set; } + public Row[] Rows; +} \ No newline at end of file diff --git a/RSND/Core/Querying/Queries/GetQuery.cs b/RSND/Core/Querying/Queries/GetQuery.cs index 30bd6bf..ed5ec62 100644 --- a/RSND/Core/Querying/Queries/GetQuery.cs +++ b/RSND/Core/Querying/Queries/GetQuery.cs @@ -1,5 +1,8 @@ namespace RSND.Core.Querying.Queries; +/// +/// The query that gets a value from the database. +/// public class GetQuery : IQuery { public GetQuery(string table, string select, string where) diff --git a/RSND/Core/Querying/QueryHelper.cs b/RSND/Core/Querying/QueryHelper.cs index 0b02239..3722acd 100644 --- a/RSND/Core/Querying/QueryHelper.cs +++ b/RSND/Core/Querying/QueryHelper.cs @@ -13,6 +13,7 @@ public static class QueryHelper return query.Type switch { "GetValue" => QueryType.GetValue, + "CreateTable" => QueryType.CreateTable, _ => null }; } diff --git a/RSND/RSND.csproj b/RSND/RSND.csproj index 3528f15..613d7f8 100644 --- a/RSND/RSND.csproj +++ b/RSND/RSND.csproj @@ -10,6 +10,7 @@ +