-
Notifications
You must be signed in to change notification settings - Fork 806
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial try at reusing existing entities while generating code by gen…
…erating ForeignKey(typeof(SomeRow)) instead of ForeignKey("SomeTable", "SomeId")
- Loading branch information
1 parent
cc295d7
commit ce87569
Showing
13 changed files
with
185 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/Serenity.Net.CodeGenerator/Models/Metadata/ApplicationMetadata.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using Serenity.CodeGeneration; | ||
|
||
namespace Serenity.CodeGenerator; | ||
|
||
public class ApplicationMetadata : IApplicationMetadata | ||
{ | ||
private class Scanner : ServerTypingsGenerator | ||
{ | ||
public List<TypeDefinition> RowTypes { get; } = new(); | ||
|
||
public Scanner(IGeneratorFileSystem fileSystem, params string[] assemblyLocations) | ||
: base(fileSystem, assemblyLocations) | ||
{ | ||
} | ||
|
||
protected override void GenerateCodeFor(TypeDefinition type) | ||
{ | ||
if (TypingsUtils.IsSubclassOf(type, "Serenity.Data", "Row") || | ||
TypingsUtils.IsSubclassOf(type, "Serenity.Data", "Row`1")) | ||
{ | ||
RowTypes.Add(type); | ||
} | ||
} | ||
|
||
protected override void HandleMemberType(TypeReference memberType, string codeNamespace, bool module) | ||
{ | ||
} | ||
} | ||
|
||
private readonly Scanner scanner; | ||
|
||
public ApplicationMetadata(IGeneratorFileSystem fileSystem, params string[] assemblyLocations) | ||
{ | ||
scanner = new Scanner(fileSystem, assemblyLocations); | ||
scanner.Run(); | ||
} | ||
|
||
string DefaultSchema { get; set; } | ||
|
||
private string ParseSchemaAndName(string objectName, out string schema) | ||
{ | ||
if (objectName is null) | ||
throw new ArgumentNullException(nameof(objectName)); | ||
|
||
schema = DefaultSchema; | ||
var parts = objectName.Split('.'); | ||
if (parts.Length <= 0 || parts.Length > 2) | ||
return null; | ||
|
||
if (parts.Length == 1) | ||
return SqlSyntax.Unquote(parts[0]); | ||
|
||
schema = SqlSyntax.Unquote(parts[0]); | ||
return SqlSyntax.Unquote(parts[1]); | ||
} | ||
|
||
private string NormalizeTablename(string objectName) | ||
{ | ||
var table = ParseSchemaAndName(objectName, out var schema); | ||
return table + "." + schema; | ||
} | ||
|
||
private bool IsEqualIgnoreCase(string objectName1, string objectName2) | ||
{ | ||
return !string.IsNullOrEmpty(objectName1) && | ||
!string.IsNullOrEmpty(objectName2) && | ||
string.Equals(NormalizeTablename(objectName1), | ||
NormalizeTablename(objectName2), StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
public IRowMetadata GetRowByTablename(string tablename) | ||
{ | ||
foreach (var type in scanner.RowTypes) | ||
{ | ||
var attr = type.GetAttributes().FirstOrDefault(x => | ||
x.AttributeType().Name == "TableNameAttribute" && | ||
x.AttributeType().NamespaceOf() == "Serenity.Data.Mapping"); | ||
|
||
if (attr != null) | ||
{ | ||
if (IsEqualIgnoreCase(tablename, attr?.ConstructorArguments?.FirstOrDefault().Value as string)) | ||
return new RowMetadata(type); | ||
|
||
continue; | ||
} | ||
|
||
var name = type.Name; | ||
if (name.EndsWith("Row", StringComparison.Ordinal)) | ||
name = name[..^3]; | ||
|
||
if (IsEqualIgnoreCase(tablename, name)) | ||
return new RowMetadata(type); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private class RowMetadata : IRowMetadata | ||
{ | ||
private readonly TypeDefinition type; | ||
|
||
public RowMetadata(TypeDefinition type) | ||
{ | ||
this.type = type ?? throw new ArgumentNullException(nameof(type)); | ||
} | ||
|
||
public string Namespace => type.NamespaceOf(); | ||
|
||
public string ClassName => type.Name; | ||
|
||
public string FullName => type.FullNameOf(); | ||
|
||
public IRowPropertyMetadata GetTableField(string columnName) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/Serenity.Net.CodeGenerator/Models/Metadata/IApplicationMetadata.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Serenity.CodeGenerator; | ||
|
||
public interface IApplicationMetadata | ||
{ | ||
IRowMetadata GetRowByTablename(string tablename); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Serenity.Net.CodeGenerator/Models/Metadata/IClassMetadata.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Serenity.CodeGenerator; | ||
|
||
public interface IClassMetadata | ||
{ | ||
string Namespace { get; } | ||
string ClassName { get; } | ||
string FullName => string.IsNullOrEmpty(Namespace) ? ClassName : Namespace + "." + ClassName; | ||
} |
6 changes: 6 additions & 0 deletions
6
src/Serenity.Net.CodeGenerator/Models/Metadata/IPropertyMetadata.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Serenity.CodeGenerator; | ||
|
||
public interface IPropertyMetadata | ||
{ | ||
string PropertyName { get; } | ||
} |
6 changes: 6 additions & 0 deletions
6
src/Serenity.Net.CodeGenerator/Models/Metadata/IRowMetadata.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Serenity.CodeGenerator; | ||
|
||
public interface IRowMetadata : IClassMetadata | ||
{ | ||
IRowPropertyMetadata GetTableField(string columnName); | ||
} |
5 changes: 5 additions & 0 deletions
5
src/Serenity.Net.CodeGenerator/Models/Metadata/IRowPropertyMetadata.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace Serenity.CodeGenerator; | ||
|
||
public interface IRowPropertyMetadata : IPropertyMetadata | ||
{ | ||
} |
4 changes: 2 additions & 2 deletions
4
...y.Net.CodeGenerator/Models/TypeNameRef.cs → ...ity.Net.CodeGenerator/Models/TypeOfRef.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters