Skip to content

Commit

Permalink
First Push
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikarsoft committed Jan 30, 2023
1 parent dfa35f0 commit aec78a7
Show file tree
Hide file tree
Showing 59 changed files with 7,945 additions and 0 deletions.
Binary file not shown.
Empty file.
53 changes: 53 additions & 0 deletions BlackHole/Attributes/ColumnAttributes/ForeignKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

namespace BlackHole.Attributes.ColumnAttributes
{
/// <summary>
/// Sets Foreign Key for this Column
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class ForeignKey : Attribute
{
public string TableName { get; set; }
public string Column { get; set; }
public string IsNullable { get; set; }
public string CascadeInfo { get; set; }

/// <summary>
/// This Overload of the Constructor Sets by Default the corresponding column
/// on the Primary Table as Id. You Can choose the Primary Table and
/// if the Foreign Key is Nullable
/// </summary>
/// <param name="table"></param>
/// <param name="isNullable"></param>
public ForeignKey(Type table,bool isNullable)
{
TableName = table.Name;
Column = "Id";

if (isNullable)
{
IsNullable = "NULL";
CascadeInfo = "on delete set null";
}
else
{
IsNullable = "NOT NULL";
CascadeInfo = "on delete cascade";
}
}

/// <summary>
/// This Overload of the Constructor Sets by Default the corresponding column
/// on the Primary Table as Id and makes the Foreign Key Column Nullable.
/// You Can choose the Primary Table
/// </summary>
/// <param name="table"></param>
public ForeignKey(Type table)
{
TableName = table.Name;
Column = "Id";
IsNullable = "NULL";
CascadeInfo = "on delete set null";
}
}
}
16 changes: 16 additions & 0 deletions BlackHole/Attributes/ColumnAttributes/NotNullable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace BlackHole.Attributes.ColumnAttributes
{
[AttributeUsage(AttributeTargets.Property)]
public class NotNullable : Attribute
{
public string IsNotNull = "NOT NULL";

/// <summary>
/// It turns the property to a Non Nullable Column in the Table.
/// </summary>
public NotNullable()
{

}
}
}
8 changes: 8 additions & 0 deletions BlackHole/Attributes/ColumnAttributes/PrimaryKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace BlackHole.Attributes.ColumnAttributes
{
[AttributeUsage(AttributeTargets.Property)]
internal class PrimaryKey : Attribute
{
public bool IsPrimaryKey = true;
}
}
14 changes: 14 additions & 0 deletions BlackHole/Attributes/ColumnAttributes/VarCharSize.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

namespace BlackHole.Attributes.ColumnAttributes
{
[AttributeUsage(AttributeTargets.Property)]
public class VarCharSize : Attribute
{
public int Charlength { get; set; }

public VarCharSize(int Characters)
{
Charlength = Characters;
}
}
}
20 changes: 20 additions & 0 deletions BlackHole/Attributes/EcryptionAttributes/EncryptProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@


namespace BlackHole.Attributes.EcryptionAttributes
{
[AttributeUsage(AttributeTargets.Property)]
public class EncryptProperty : Attribute
{
private bool decryptOnSelect { get; set; }

public EncryptProperty()
{
decryptOnSelect = false;
}

public EncryptProperty(bool decryptOnSelect)
{
this.decryptOnSelect = decryptOnSelect;
}
}
}
10 changes: 10 additions & 0 deletions BlackHole/Attributes/EntityAttributes/UseActivator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

namespace BlackHole.Attributes.EntityAttributes
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]

public class UseActivator : Attribute
{
public bool useActivator = true;
}
}
19 changes: 19 additions & 0 deletions BlackHole/BlackHole.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.123" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.4" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.0.1" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="6.0.4" />
<PackageReference Include="MySql.Data" Version="8.0.29" />
<PackageReference Include="Npgsql" Version="6.0.4" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.1" />
</ItemGroup>

</Project>
203 changes: 203 additions & 0 deletions BlackHole/Data/BHDataCascade.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
using BlackHole.Entities;
using BlackHole.Interfaces;
using BlackHole.Statics;
using Dapper;
using System.Data;

namespace BlackHole.Data
{
internal class BHDataCascade : IBHDataCascade
{
private IBHDatabaseSelector _multiDatabaseSelector;
private bool isMyShit { get; set; } = false;

public BHDataCascade(IBHDatabaseSelector multiDatabaseSelector)
{
_multiDatabaseSelector = multiDatabaseSelector;
isMyShit = _multiDatabaseSelector.GetMyShit();
}

async void IBHDataCascade.CascadeTable(string table , List<int> Ids)
{
if (Ids.Count > 0 && Ids[0] != 0)
{
bool successfull = true;
bool startDigging = false;
int diggingIndex = 0;
int safetyLimiter = 0;

List<CascadeTree> cascadeTree = new List<CascadeTree>();

CascadeTree cascadeRoot = CascadeRoot(table, Ids);
cascadeTree.Add(cascadeRoot);

if (cascadeRoot.NonNullableCount > 0)
{
startDigging = true;
}

while (startDigging)
{
if(diggingIndex >= 0)
{
if (cascadeTree[diggingIndex].CurrentBranch < cascadeTree[diggingIndex].NonNullableCount )
{
DataConstraints currentConstraint = cascadeTree[diggingIndex].NonNullableConstraints[cascadeTree[diggingIndex].CurrentBranch];
cascadeTree.Add(CascadeBranch(currentConstraint.TABLE_NAME, currentConstraint.COLUMN_NAME, cascadeTree[diggingIndex].AffectedIds));
cascadeTree[diggingIndex].CurrentBranch++;
diggingIndex = cascadeTree.Count - 1;
}
else
{
diggingIndex--;
}
}
else
{
startDigging = false;
}

if(safetyLimiter > 100)
{
startDigging = false;
successfull = false;
}

safetyLimiter++;
}

if (successfull)
{
await ExecuteCascade(cascadeTree, table, Ids);
}
}
}

private async Task ExecuteCascade(List<CascadeTree> cascadeTree , string table , List<int> Ids)
{
string NullableCommands = string.Empty;
string NotNullableCommands = string.Empty;
string BaseCommand = $"delete from {MyShit(table)} {WhereIds(MyShit("Id"), Ids)};";

int branches = cascadeTree.Count;

for(int i = 0; i < branches; i++)
{
NullableCommands += cascadeTree[branches - i - 1].NullableCascade;
NotNullableCommands += cascadeTree[branches - i - 1].NonNullableCascade;
}

try
{
using (IDbConnection connection = _multiDatabaseSelector.GetConnection())
{
await connection.ExecuteAsync(NullableCommands + NotNullableCommands + BaseCommand);
}
}
catch(Exception ex)
{
Console.Write(ex.ToString());
}
}

private CascadeTree CascadeBranch(string TableName, string columnName, List<int> ParentIds)
{
CascadeTree cascadeTree = new CascadeTree
{
CurrentBranch = 0,
NonNullableCount = 0,
};

if (ParentIds.Count > 0)
{
List<DataConstraints> constraints = CascadeRelations.dataConstrains.Where(x => x.REFERENCED_TABLE_NAME == TableName).ToList();

using (IDbConnection connection = _multiDatabaseSelector.GetConnection())
{
string idsCommand = $"select {MyShit("Id")} from {MyShit(TableName)} {WhereIds(MyShit(columnName), ParentIds)}";
cascadeTree.AffectedIds = connection.Query<int>(idsCommand).ToList();
}

if(cascadeTree.AffectedIds.Count > 0)
{
foreach (DataConstraints constraint in constraints.Where(x => x.IS_NULLABLE == "YES"))
{
string? column = MyShit(constraint.COLUMN_NAME);
string command = $"update {MyShit(constraint.TABLE_NAME)} set {column}=null {WhereIds(column, cascadeTree.AffectedIds)};";
cascadeTree.NullableCascade += command;
}

cascadeTree.NonNullableConstraints = constraints.Where(x => x.IS_NULLABLE == "NO").ToList();
cascadeTree.NonNullableCount = cascadeTree.NonNullableConstraints.Count;

foreach (DataConstraints constraint in cascadeTree.NonNullableConstraints)
{
string? column = MyShit(constraint.COLUMN_NAME);
string command = $"delete from {MyShit(constraint.TABLE_NAME)} {WhereIds(column, cascadeTree.AffectedIds)};";
cascadeTree.NonNullableCascade += command;
}
}
}

return cascadeTree;
}

private CascadeTree CascadeRoot(string TableName, List<int> AffectedIds)
{
CascadeTree cascadeTree = new CascadeTree
{
CurrentBranch = 0,
NonNullableCount = 0,
AffectedIds = AffectedIds
};

if (cascadeTree.AffectedIds.Count > 0)
{
List<DataConstraints> constraints = CascadeRelations.dataConstrains.Where(x => x.REFERENCED_TABLE_NAME == TableName).ToList();

foreach (DataConstraints constraint in constraints.Where(x => x.IS_NULLABLE == "YES"))
{
string? column = MyShit(constraint.COLUMN_NAME);
string command = $"update {MyShit(constraint.TABLE_NAME)} set {column}=null {WhereIds(column, cascadeTree.AffectedIds)};";
cascadeTree.NullableCascade += command;
}

cascadeTree.NonNullableConstraints = constraints.Where(x => x.IS_NULLABLE == "NO").ToList();
cascadeTree.NonNullableCount = cascadeTree.NonNullableConstraints.Count;

foreach (DataConstraints constraint in cascadeTree.NonNullableConstraints)
{
string? column = MyShit(constraint.COLUMN_NAME);
string command = $"delete from {MyShit(constraint.TABLE_NAME)} {WhereIds(column, cascadeTree.AffectedIds)};";
cascadeTree.NonNullableCascade += command;
}
}

return cascadeTree;
}

public string WhereIds(string? column, List<int> Ids)
{
string whereCommand = "where";

foreach (int Id in Ids)
{
whereCommand += $" {column}={Id} or";
}

return whereCommand.Substring(0, whereCommand.Length - 3);
}

string? MyShit(string? propName)
{
string? result = propName;

if (!isMyShit)
{
result = $@"""{propName}""";
}

return result;
}
}
}
Loading

0 comments on commit aec78a7

Please sign in to comment.