Add ability to create tables
This commit is contained in:
parent
9b31bd61a3
commit
1700f01e40
|
@ -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<Table> 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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<GetQuery>(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<CreateTableQuery>(query);
|
||||
RsndMain.Db.CreateTable(new Table
|
||||
{
|
||||
Name = query.Table
|
||||
});*/
|
||||
Name = createTableQuery?.TableName,
|
||||
Rows = createTableQuery?.Rows
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -1,5 +1,8 @@
|
|||
namespace RSND.Core.Querying.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// The query that gets a value from the database.
|
||||
/// </summary>
|
||||
public class GetQuery : IQuery
|
||||
{
|
||||
public GetQuery(string table, string select, string where)
|
||||
|
|
|
@ -13,6 +13,7 @@ public static class QueryHelper
|
|||
return query.Type switch
|
||||
{
|
||||
"GetValue" => QueryType.GetValue,
|
||||
"CreateTable" => QueryType.CreateTable,
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
<ItemGroup>
|
||||
<PackageReference Include="Fleck" Version="1.2.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.2-beta1" />
|
||||
<PackageReference Include="Pastel" Version="3.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
Loading…
Reference in New Issue