Library Update

The project now uses the new source generator package for the ServerDatabase package. This now allows code to read a full row at a time while using the type safety from the column definitions.
This commit is contained in:
JacobTech 2023-06-16 14:24:24 -04:00
parent 612ad59547
commit 397d50e062
16 changed files with 125 additions and 98 deletions

View File

@ -1,4 +1,5 @@
using ServerDatabase;
using ServerDatabase.SourceGenerator;
namespace LuskiServer.Classes.TableDef;
@ -13,3 +14,6 @@ public static class Categories
public static TableColumn<long[]> RoleOverides { get; } = new("role_overides") { DefaultValue = Array.Empty<long>() };
public static TableColumn<long[]> UserOverides { get; } = new("member_overides") { DefaultValue = Array.Empty<long>() };
}
[TableRow(typeof(Categories))]
public partial class CategoryRow
{}

View File

@ -2,6 +2,7 @@ using System.Text;
using JacobTechEncryption.Enums;
using LuskiServer.Enums;
using ServerDatabase;
using ServerDatabase.SourceGenerator;
namespace LuskiServer.Classes.TableDef;
@ -28,3 +29,7 @@ public static class Channels
EncoderType.Latin1, EncoderType.BigEndianUnicode
} };
}
[TableRow(typeof(Channels))]
public partial class ChannelRow
{}

View File

@ -1,5 +1,6 @@
using JacobTechEncryption.Enums;
using ServerDatabase;
using ServerDatabase.SourceGenerator;
namespace LuskiServer.Classes.TableDef;
@ -13,4 +14,11 @@ public static class Files
public static TableColumn<byte[]> Data { get; } = new("data");
public static TableColumn<EncryptionType> EncryptionType { get; } = new("encryption_type");
public static TableColumn<EncoderType> EncoderType { get; } = new("encoder_type");
public static TableColumn<EncryptionType> NameEncryptionType { get; } = new("name_encryption_type");
public static TableColumn<EncoderType> NameEncoderType { get; } = new("name_encoder_type");
}
[TableRow(typeof(Files))]
public partial class FileRow
{
}

View File

@ -1,5 +1,6 @@
using LuskiServer.Enums;
using ServerDatabase;
using ServerDatabase.SourceGenerator;
namespace LuskiServer.Classes.TableDef;
@ -9,3 +10,7 @@ public static class Logs
public static TableColumn<LogType> Type { get; } = new("type");
public static TableColumn<string> Message { get; } = new("message");
}
[TableRow(typeof(Logs))]
public partial class LogRow
{}

View File

@ -1,5 +1,6 @@
using JacobTechEncryption.Enums;
using ServerDatabase;
using ServerDatabase.SourceGenerator;
namespace LuskiServer.Classes.TableDef;
@ -14,3 +15,7 @@ public static class Messages
public static TableColumn<EncoderType> EncoderType { get; } = new("encoder_type");
public static TableColumn<long[]> Files { get; } = new("files");
}
[TableRow(typeof(Messages))]
public partial class MessageRow
{}

View File

@ -1,5 +1,6 @@
using LuskiServer.Enums;
using ServerDatabase;
using ServerDatabase.SourceGenerator;
namespace LuskiServer.Classes.TableDef;
@ -12,3 +13,7 @@ public static class Roles
public static TableColumn<ServerPermissions[]> ServerPermissions { get; } = new("server_perms");
public static TableColumn<long[]> MembersList { get; } = new("members_list") {DefaultValue = Array.Empty<long>()};
}
[TableRow(typeof(Roles))]
public partial class RoleRow
{}

View File

@ -1,5 +1,5 @@
using LuskiServer.Enums;
using ServerDatabase;
using ServerDatabase.SourceGenerator;
namespace LuskiServer.Classes.TableDef;
@ -9,3 +9,7 @@ public static class ServerRoleOverides
public static TableColumn<long> RoleID { get; } = new("role_id");
public static TableColumn<string[]> Overides { get; } = new("overides");
}
[TableRow(typeof(ServerRoleOverides))]
public partial class ServerRoleOverideRow
{}

View File

@ -1,4 +1,5 @@
using ServerDatabase;
using ServerDatabase.SourceGenerator;
namespace LuskiServer.Classes.TableDef;
@ -10,3 +11,7 @@ public static class SessionTokens
public static TableColumn<byte[]> AddressFilter { get; } = new("address_filter");
public static TableColumn<DateTime> TimeFilter { get; } = new("date_filter");
}
[TableRow(typeof(SessionTokens))]
public partial class SessionTokenRow
{}

View File

@ -1,5 +1,6 @@
using LuskiServer.Enums;
using ServerDatabase;
using ServerDatabase.SourceGenerator;
namespace LuskiServer.Classes.TableDef;
@ -9,3 +10,7 @@ public static class UserRoleOverides
public static TableColumn<long> UserID { get; } = new("user_id");
public static TableColumn<string[]> Overides { get; } = new("overides");
}
[TableRow(typeof(UserRoleOverides))]
public partial class UserRoleOverideRow
{}

View File

@ -1,5 +1,6 @@
using LuskiServer.Enums;
using ServerDatabase;
using ServerDatabase.SourceGenerator;
namespace LuskiServer.Classes.TableDef;
@ -22,3 +23,7 @@ public class Users
public static TableColumn<string[]> OfflineData { get; } = new("offline_data");
public static TableColumn<string> OffileKey { get; } = new("offline_key") {DefaultValue = string.Empty};
}
[TableRow(typeof(Users))]
public partial class UserRow
{}

View File

@ -1,17 +1,18 @@
using LuskiServer.Classes.TableDef;
using ServerDatabase;
namespace LuskiServer.Classes;
public static class Tables
{
public static Table Users { get; } = new Table("users", null!);
public static Table Roles { get; } = new Table("roles", null!);
public static Table Logs { get; } = new Table("logs", null!);
public static Table Files { get; } = new Table("files", null!);
public static Table Categories { get; } = new Table("categories", null!);
public static Table Channels { get; } = new Table("channels", null!);
public static Table Messages { get; } = new Table("messages", null!);
public static Table ServerRoleOverides { get; } = new Table("role_overides", null!);
public static Table UserRoleOverides { get; } = new Table("user_overides", null!);
public static Table SessionTokens { get; } = new Table("session_tokens", null!);
public static Table<UserRow> Users { get; } = new Table<UserRow>("users", null!);
public static Table<RoleRow> Roles { get; } = new Table<RoleRow>("roles", null!);
public static Table<LogRow> Logs { get; } = new Table<LogRow>("logs", null!);
public static Table<FileRow> Files { get; } = new Table<FileRow>("files", null!);
public static Table<CategoryRow> Categories { get; } = new Table<CategoryRow>("categories", null!);
public static Table<ChannelRow> Channels { get; } = new Table<ChannelRow>("channels", null!);
public static Table<MessageRow> Messages { get; } = new Table<MessageRow>("messages", null!);
public static Table<ServerRoleOverideRow> ServerRoleOverides { get; } = new Table<ServerRoleOverideRow>("role_overides", null!);
public static Table<UserRoleOverideRow> UserRoleOverides { get; } = new Table<UserRoleOverideRow>("user_overides", null!);
public static Table<SessionTokenRow> SessionTokens { get; } = new Table<SessionTokenRow>("session_tokens", null!);
}

View File

@ -1,11 +1,16 @@
using System.Security.Cryptography;
using JacobTechEncryption;
using JacobTechEncryption.Enums;
namespace LuskiServer.Classes.WebTypes;
public class File
{
public string name { get; set; } = default!;
public EncoderType encoder_type { get; set; } = default!;
public EncoderType name_encoder_type { get; set; } = default!;
public EncryptionType encryption_type { get; set; } = default!;
public EncryptionType name_encryption_type { get; set; } = default!;
public string[] data { get; set; } = default!;
public ulong? size { get; set; } = default!;
public long? id { get; set; } = default!;

View File

@ -1,3 +1,5 @@
using JacobTechEncryption.Enums;
namespace LuskiServer.Classes.WebTypes;
public class SocketMessage : HTTPResponse
@ -6,5 +8,7 @@ public class SocketMessage : HTTPResponse
public long user_id { get; set; } = default!;
public long id { get; set; } = default!;
public string content { get; set; } = default!;
public EncryptionType encryption_type { get; set; } = default!;
public EncoderType encoder_type { get; set; } = default!;
public File[] files { get; set; } = default!;
}

View File

@ -1,6 +1,4 @@
using Asp.Versioning;
using JacobTechEncryption;
using JacobTechEncryption.Enums;
using LuskiServer.Classes;
using LuskiServer.Classes.TableDef;
using LuskiServer.Classes.WebTypes;
@ -54,88 +52,53 @@ public class SocketBulkMessageController : ControllerBase
}
}
SocketBulkMessage all = new();
///StartDatabse(MessagesToDownload, Channel_Id, MostRecentID, sign, out SocketMessage[]? mmm);
SocketMessage[]? mmm = null;
if (MostRecentID != null)
{
var con2 = Luski.Database.CreateConnection();
con2.Open();
var cmd2 = con2.CreateCommand();
Console.WriteLine($"SELECT context,author_id,message_id,files FROM messages WHERE ts {sign} '{new Luski.Snowflake(long.Parse(MostRecentID)).Timestamp}' AND channel_id = '{Channel_Id}' ORDER BY ts DESC LIMIT {MessagesToDownload};");
cmd2.CommandText = $"SELECT context,author_id,message_id,files FROM messages WHERE ts {sign} '{new Luski.Snowflake(long.Parse(MostRecentID)).Timestamp}' AND channel_id = '{Channel_Id}' ORDER BY ts DESC LIMIT {MessagesToDownload};";
var reader2 = cmd2.ExecuteReader();
//Loop(Messages, Channel_Id, out mmm, reader);
long current = 0;
var mmmm = new List<SocketMessage>();
while (reader2.Read())
MessageRow[] rawmsgs = Tables.Messages.ReadRows((uint)MessagesToDownload,
new Order() { Assending = false, Type = Messages.TimeStamp.Name },
Messages.TimeStamp.CreateParameter(new Luski.Snowflake(long.Parse(MostRecentID)).Timestamp, sign),
Messages.ChannelID.CreateParameter(Channel_Id));
List<SocketMessage> mmmm = new List<SocketMessage>();
foreach (MessageRow Row in rawmsgs)
{
current++;
if (current <= MessagesToDownload)
SocketMessage temp = new()
{
SocketMessage Current = new();
//ReadRow(Channel_Id, reader, ref mmm);
Current.channel_id = Channel_Id;
int i2 = 0;
while (i2 < reader2.FieldCount)
{
//ReadCollume(reader.GetValue(i), ref msg, reader.GetName(i));
string name = reader2.GetName(i2);
object val = reader2.GetValue(i2);
if (name == "context") Current.content = Convert.ToBase64String((byte[])val);
else if (name == "author_id") Current.user_id = long.Parse(val.ToString());
else if (name == "message_id") Current.id = long.Parse(val.ToString());
else if (name == "files")
{
if (val is not DBNull)
{
if ((long[]?)val is not null && ((long[])val).Length > 0)
content = Convert.ToBase64String(Row.Context),
encoder_type = Row.EncoderType,
encryption_type = Row.EncryptionType,
channel_id = Row.ChannelID,
user_id = Row.AuthorID,
id = Row.ID,
files = Array.Empty<Classes.WebTypes.File>()
};
if (Row.Files.Any())
{
List<Classes.WebTypes.File> list = new();
foreach (long b in (long[])val)
foreach (long b in Row.Files)
{
var con = Luski.Database.CreateConnection();
con.Open();
var cmd = con.CreateCommand();
cmd.CommandText = $"SELECT name,size FROM files WHERE id = '{b}';";
var reader = cmd.ExecuteReader();
int i3 = 0;
FileRow fileraw = Tables.Files.ReadRow(Files.ID.CreateParameter(b));
Classes.WebTypes.File file = new()
{
data = null!,
id = b
id = b,
name = Convert.ToBase64String(fileraw.Name),
size = ulong.Parse(fileraw.Size.ToString()),
name_encoder_type = fileraw.NameEncoderType,
name_encryption_type = fileraw.NameEncryptionType,
encoder_type = fileraw.EncoderType,
encryption_type = fileraw.EncryptionType
};
while (reader.Read())
{
while (i3 < reader.FieldCount)
{
//ReadFileCollume(reader.GetValue(i), ref file, reader.GetName(i));
string name2 = reader2.GetName(i3);
object val2 = reader2.GetValue(i3);
if (name2 == "name") file.name = Convert.ToBase64String((byte[])val2);
else if (name2 == "size") file.size = ulong.Parse(val2.ToString());
else;
i3++;
}
if (i3 >= reader.FieldCount) break;
list.Add(file);
}
list.Add(file);
con.Close();
temp.files = list.ToArray();
}
Current.files = list.ToArray();
mmmm.Add(temp);
}
}
}
i2++;
}
if (mmm == null) break;
mmmm.Add(Current);
}
else break;
}
mmm = mmmm.ToArray();
con2.Close();
}
if (mmm is not null) all.messages = mmm;
else all.messages = Array.Empty<SocketMessage>();

View File

@ -4,6 +4,7 @@
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IncludeBuildOutput>true</IncludeBuildOutput>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
@ -22,7 +23,8 @@
<PackageReference Include="JacobTechEncryption" Version="1.0.1" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.3" />
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
<PackageReference Include="ServerDatabase" Version="2.5.8" />
<PackageReference Include="ServerDatabase" Version="2.7.5" />
<PackageReference Include="ServerDatabase.SourceGenerator" Version="1.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
</ItemGroup>

View File

@ -9,6 +9,7 @@ using LuskiServer.Enums;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using ServerDatabase;
using ServerDatabase.Utils;
using Swashbuckle.AspNetCore.SwaggerGen;
[assembly: Microsoft.AspNetCore.Mvc.ApiController]
@ -26,19 +27,19 @@ try
Luski.Database.ExecuteNonQuery($"CREATE DATABASE {Luski.Config.Database} WITH OWNER = {Luski.Config.Username} ENCODING = 'UTF8' CONNECTION LIMIT = -1 IS_TEMPLATE = False;");
Console.WriteLine("Database Created");
}
catch (Exception e)
catch
{
Console.WriteLine("Database Found");
}
foreach (PropertyInfo prop in typeof(Tables).GetProperties())
{
Table table = (Table)prop.GetValue(null)!;
ITable table = (ITable)prop.GetValue(null)!;
table.DatabaseHandler = Luski.Database;
Type t = Type.GetType($"LuskiServer.Classes.TableDef.{prop.Name}")!;
foreach(PropertyInfo prop2 in t.GetProperties())
{
table.WithColumn((ITableColumn)prop2.GetValue(null)!);
table.AddColumn((ITableColumn)prop2.GetValue(null)!, prop2.Name);
}
Luski.Database.Tables.Add(table);
}