Skip to content

Commit

Permalink
modifications supp
Browse files Browse the repository at this point in the history
  • Loading branch information
barnuri committed Nov 27, 2024
1 parent 90278fb commit 2c9f957
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 26 deletions.
80 changes: 66 additions & 14 deletions CSharp.OpenSource.LinqToKql/ORMGen/ORMGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CSharp.OpenSource.LinqToKql.Extensions;
using CSharp.OpenSource.LinqToKql.Models;
using CSharp.OpenSource.LinqToKql.Provider;
using System.Text.RegularExpressions;

namespace CSharp.OpenSource.LinqToKql.ORMGen;

Expand Down Expand Up @@ -125,15 +126,12 @@ protected virtual async Task<ORMGenaratedModel> GenerateFunctionModelAsync(ShowF
var kqlParams = string.Join(", ", function.ParametersItems.Select(x => $"{{{x.Name}.GetKQLValue()}}"));

var usings = new List<string> { "System" };
var lines = new List<string>
{
$"public partial class {typeName}",
$"{{",
};
var modelDeclarationLines = GetModelDeclarationLines(typeName, function.Name, dbConfig.DatabaseName);
var lines = new List<string>(modelDeclarationLines);
var functionColumns = await GetFunctionSchemaAsync(function, dbConfig);
foreach (var column in functionColumns)
{
lines.Add($"{TAB}public virtual {DataTypeTranslate(column.DataType)} {column.ColumnName} {{ get; set; }}");
GenerateProperty(function.Name, dbConfig, lines, column.ColumnName, column.DataType);
}
lines.Add($"}}");
var fileContent = WrapContentWithNamespaceAndUsing(
Expand All @@ -157,6 +155,54 @@ protected virtual async Task<ORMGenaratedModel> GenerateFunctionModelAsync(ShowF
};
}

private List<string> GetModelDeclarationLines(string typeName, string name, string databaseName)
{
var modification = Config.TableOrFunctionModifications
.Where(x => x.DatabasePatterns.Count == 0 || x.DatabasePatterns.Any(p => Match(p, databaseName)))
.Where(x => x.TableOrFunctionPatterns.Count == 0 || x.TableOrFunctionPatterns.Any(p => Match(p, name)))
.FirstOrDefault();
var res = new List<string>();
var declaration = $"public partial class {typeName}";
if (modification == null)
{
res.Add(declaration);
res.Add("{");
return res;
}
if (!string.IsNullOrEmpty(modification.ClassAttributes))
{
res.Add(modification.ClassAttributes);
}
if (!string.IsNullOrEmpty(modification.ClassInherit))
{
declaration += $" : {modification.ClassInherit}";
}
res.Add(declaration);
res.Add("{");
res.AddRange(modification.BodyExtraLines);
return res;
}

private void GenerateProperty(string tableOrFunctionName, ORMGeneratorDatabaseConfig dbConfig, List<string> lines, string columnType, string columnName)
{
var modification = Config.ColumnModifications
.Where(x => x.DatabasePatterns.Count == 0 || x.DatabasePatterns.Any(p => Match(p, dbConfig.DatabaseName)))
.Where(x => x.TableOrFunctionPatterns.Count == 0 || x.TableOrFunctionPatterns.Any(p => Match(p, tableOrFunctionName)))
.Where(x => x.ColumnNamePatterns.Count == 0 || x.ColumnNamePatterns.Any(p => Match(p, columnName)))
.FirstOrDefault();
if (modification?.Exclude == true)
{
return;
}
var attributes = modification?.ColumnAttributes ?? "";
if (attributes.Length > 0)
{
lines.Add($"{TAB}{attributes}");
}
columnType = modification?.NewColumnType ?? DataTypeTranslate(columnType);
lines.Add($"{TAB}public virtual {columnType} {columnName} {{ get; set; }}");
}

private async Task<List<GetSchemaResult>> GetFunctionSchemaAsync(ShowFunctionsResult function, ORMGeneratorDatabaseConfig dbConfig)
{
return await DbContext.CreateQuery<GetSchemaResult>($"{function.Name}({string.Join(", ", function.ParametersItems.Select(x => DefaultValue(x.Type)))})", dbConfig.DatabaseName)
Expand All @@ -169,14 +215,11 @@ protected virtual async Task<ORMGenaratedModel> GenerateTableModelAsync(ORMGener
{
var typeName = GetModelName(table.Name);
var usings = new List<string> { "System" };
var lines = new List<string>
{
$"public partial class {typeName}",
$"{{",
};
var modelDeclarationLines = GetModelDeclarationLines(typeName, table.Name, dbConfig.DatabaseName);
var lines = new List<string>(modelDeclarationLines);
foreach (var column in table.Columns)
{
lines.Add($"{TAB}public virtual {DataTypeTranslate(column.ColumnType)} {column.ColumnName} {{ get; set; }}");
GenerateProperty(table.Name, dbConfig, lines, column.ColumnType, column.ColumnName);
}
lines.Add($"}}");
var fileContent = WrapContentWithNamespaceAndUsing(
Expand Down Expand Up @@ -245,11 +288,11 @@ protected virtual List<T> ApplyFilters<T>(List<T> list, Func<T, string> valueGet
{
return list.FindAll(table =>
{
if (filters.Any(f => !f.Exclude && f.Match(valueGetter(table))))
if (filters.Any(f => !f.Exclude && Match(valueGetter(table), f.Pattern)))
{
return true;
}
if (filters.Any(f => f.Exclude && f.Match(valueGetter(table))))
if (filters.Any(f => f.Exclude && Match(valueGetter(table), f.Pattern)))
{
return false;
}
Expand Down Expand Up @@ -339,4 +382,13 @@ public string GetModelName(string name)
_modelsNames[name]++;
return finalName;
}

public bool Match(string value, string pattern)
{
// Escape the pattern and replace '*' and '?' with regex equivalents
string regexPattern = "^" + Regex.Escape(pattern)
.Replace("\\*", ".*")
.Replace("\\?", ".") + "$";
return Regex.IsMatch(value, regexPattern);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace CSharp.OpenSource.LinqToKql.ORMGen;

public class ORMGeneratorColumnModification
{
public List<string> DatabasePatterns { get; set; }
public List<string> TableOrFunctionPatterns { get; set; }
public List<string> ColumnNamePatterns { get; set; }
public string NewColumnType { get; set; }
public string ColumnAttributes { get; set; }
public bool Exclude { get; set; }
}
2 changes: 2 additions & 0 deletions CSharp.OpenSource.LinqToKql/ORMGen/ORMGeneratorConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace CSharp.OpenSource.LinqToKql.ORMGen;
public class ORMGeneratorConfig
{
public ORMGeneratorFilterConfig Filters { get; set; } = new();
public List<ORMGeneratorColumnModification> ColumnModifications { get; set; } = new();
public List<ORMGeneratorTableOrFunctionModification> TableOrFunctionModifications { get; set; } = new();
public string Namespace { get; set; }
public List<ORMGeneratorDatabaseConfig> DatabaseConfigs { get; set; }
public bool CreateDbContext { get; set; } = true;
Expand Down
13 changes: 1 addition & 12 deletions CSharp.OpenSource.LinqToKql/ORMGen/ORMGeneratorFilter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Text.RegularExpressions;

namespace CSharp.OpenSource.LinqToKql.ORMGen;
namespace CSharp.OpenSource.LinqToKql.ORMGen;

public class ORMGeneratorFilter
{
Expand All @@ -9,13 +7,4 @@ public class ORMGeneratorFilter
/// </summary>
public string Pattern { get; set; }
public bool Exclude { get; set; }

public bool Match(string value)
{
// Escape the pattern and replace '*' and '?' with regex equivalents
string regexPattern = "^" + Regex.Escape(Pattern)
.Replace("\\*", ".*")
.Replace("\\?", ".") + "$";
return Regex.IsMatch(value, regexPattern);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

namespace CSharp.OpenSource.LinqToKql.ORMGen;

public class ORMGeneratorTableOrFunctionModification
{
public List<string> DatabasePatterns { get; set; }
public List<string> TableOrFunctionPatterns { get; set; }
public string ClassAttributes { get; set; }
public string ClassInherit { get; set; }
public List<string> BodyExtraLines { get; set; } = new();
}

0 comments on commit 2c9f957

Please sign in to comment.