Skip to content

Commit

Permalink
Merge pull request #396 from klee-contrib/issue-pg-resources
Browse files Browse the repository at this point in the history
[SQL] Resource générée en double lorsque l'on utilise des alias de champs dans les entity
  • Loading branch information
gideruette authored Oct 3, 2024
2 parents 93282a4 + 968ec76 commit ff9e1ed
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 33 deletions.
12 changes: 12 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
],
"problemMatcher": "$msCompile"
},
{
"label": "build-sql",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/TopModel.Generator.Sql/TopModel.Generator.Sql.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "build-csharp",
"command": "dotnet",
Expand Down
66 changes: 39 additions & 27 deletions TopModel.Generator.Sql/Procedural/AbstractSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,16 @@ public void GenerateSchemaScript(IEnumerable<Class> classes)
var foreignKeys = classes
.OrderBy(c => c.SqlName)
.Where(c => c.IsPersistent && !c.Abstract && classes.Contains(c))
.SelectMany(classe => WriteTableDeclaration(classe, writerCrebas, writerUk, writerType, writerComment, writerResource, classes.ToList()))
.SelectMany(classe => WriteTableDeclaration(classe, writerCrebas, writerUk, writerType, writerComment, classes.ToList()))
.ToList();

if (writerResource is not null)
{
WriteResources(writerResource, classes
.OrderBy(c => c.SqlName)
.Where(c => c.IsPersistent && !c.Abstract && classes.Contains(c)));
}

if (_config.TranslateProperties == true || _config.TranslateReferences == true)
{
WriteResourceTableDeclaration(writerCrebas);
Expand Down Expand Up @@ -547,32 +554,43 @@ private void WriteResourceTableDeclaration(SqlFileWriter writer)
}
}

private void WriteResources(SqlFileWriter writer, Class modelClass)
private void WriteResources(SqlFileWriter writer, IEnumerable<Class> classes)
{
var hasLocale = _translationStore.Translations.Keys.Count > 1 || _translationStore.Translations.Keys.Any(a => a != string.Empty);
if (_config.TranslateProperties == true && modelClass.Properties.Where(p => p.Label != null).Count() > 0 && modelClass.ModelFile != null)
var propertiesMap = classes
.Where(c => c != null && c.Properties != null)
.SelectMany(c => c.Properties)
.Where(p => p.ResourceProperty.Parent.Namespace.Module != null && p.Label != null && p.ResourceProperty != null && p.Class != null)
.DistinctBy(property => property.ResourceKey).GroupBy(property => property.Class).ToDictionary(g => g.Key, g => g.Select(t => t));
foreach (var modelClass in propertiesMap.Keys)
{
writer.WriteLine();
writer.WriteLine("/**\t\tInitialisation des traductions des propriétés de la table " + modelClass.SqlName + "\t\t**/");

foreach (var lang in _translationStore.Translations.Keys)
if (propertiesMap.TryGetValue(modelClass, out var properties))
{
foreach (var property in modelClass.Properties.Where(p => p.Label != null))
var hasLocale = _translationStore.Translations.Keys.Count > 1 || _translationStore.Translations.Keys.Any(a => a != string.Empty);
if (_config.TranslateProperties == true && properties.Where(p => p.Label != null).Count() > 0 && modelClass.ModelFile != null)
{
writer.WriteLine($@"INSERT INTO {_config.ResourcesTableName}(RESOURCE_KEY{(hasLocale ? ", LOCALE" : string.Empty)}, LABEL) VALUES({SingleQuote(property.ResourceKey)}{(string.IsNullOrEmpty(lang) ? string.Empty : @$", {SingleQuote(lang)}")}, {SingleQuote(_translationStore.GetTranslation(property, lang))});");
writer.WriteLine();
writer.WriteLine("/**\t\tInitialisation des traductions des propriétés de la table " + modelClass.SqlName + "\t\t**/");

foreach (var lang in _translationStore.Translations.Keys)
{
foreach (var property in properties.Where(p => p.Label != null).DistinctBy(property => property.ResourceKey))
{
writer.WriteLine($@"INSERT INTO {_config.ResourcesTableName}(RESOURCE_KEY{(hasLocale ? ", LOCALE" : string.Empty)}, LABEL) VALUES({SingleQuote(property.ResourceKey)}{(string.IsNullOrEmpty(lang) ? string.Empty : @$", {SingleQuote(lang)}")}, {SingleQuote(_translationStore.GetTranslation(property, lang))});");
}
}
}
}
}

if (modelClass.DefaultProperty != null && modelClass.Values.Count() > 0 && _config.TranslateReferences == true)
{
writer.WriteLine();
writer.WriteLine("/**\t\tInitialisation des traductions des valeurs de la table " + modelClass.SqlName + "\t\t**/");
foreach (var lang in _translationStore.Translations.Keys)
{
foreach (var val in modelClass.Values)
if (modelClass.DefaultProperty != null && modelClass.Values.Count() > 0 && _config.TranslateReferences == true)
{
writer.WriteLine(@$"INSERT INTO {_config.ResourcesTableName}(RESOURCE_KEY{(hasLocale ? ", LOCALE" : string.Empty)}, LABEL) VALUES({SingleQuote(val.ResourceKey)}{(string.IsNullOrEmpty(lang) ? string.Empty : @$", {SingleQuote(lang)}")}, {SingleQuote(_translationStore.GetTranslation(val, lang))});");
writer.WriteLine();
writer.WriteLine("/**\t\tInitialisation des traductions des valeurs de la table " + modelClass.SqlName + "\t\t**/");
foreach (var lang in _translationStore.Translations.Keys)
{
foreach (var val in modelClass.Values)
{
writer.WriteLine(@$"INSERT INTO {_config.ResourcesTableName}(RESOURCE_KEY{(hasLocale ? ", LOCALE" : string.Empty)}, LABEL) VALUES({SingleQuote(val.ResourceKey)}{(string.IsNullOrEmpty(lang) ? string.Empty : @$", {SingleQuote(lang)}")}, {SingleQuote(_translationStore.GetTranslation(val, lang))});");
}
}
}
}
}
Expand All @@ -598,9 +616,8 @@ private void WriteSequence(Class classe, SqlFileWriter writerCrebas, string tabl
/// <param name="writerUk">Flux d'écriture Unique Key.</param>
/// <param name="writerType">Flux d'écritures des types.</param>
/// <param name="writerComment">Flux d'écritures des commentaires.</param>
/// <param name="writerResources">Flux d'écritures des ressources.</param>
/// <returns>Liste des propriétés étrangères persistentes.</returns>
private IEnumerable<AssociationProperty> WriteTableDeclaration(Class classe, SqlFileWriter writerCrebas, SqlFileWriter? writerUk, SqlFileWriter? writerType, SqlFileWriter? writerComment, SqlFileWriter? writerResources, IList<Class> availableClasses)
private IEnumerable<AssociationProperty> WriteTableDeclaration(Class classe, SqlFileWriter writerCrebas, SqlFileWriter? writerUk, SqlFileWriter? writerType, SqlFileWriter? writerComment, IList<Class> availableClasses)
{
var fkPropertiesList = new List<AssociationProperty>();

Expand Down Expand Up @@ -710,11 +727,6 @@ private IEnumerable<AssociationProperty> WriteTableDeclaration(Class classe, Sql
WriteComments(writerComment, classe, tableName, properties);
}

if (writerResources is not null)
{
WriteResources(writerResources, classe);
}

writerCrebas.WriteLine();

if (isContainsInsertKey)
Expand Down
2 changes: 0 additions & 2 deletions samples/generators/pg/src/06_resources.sql
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,3 @@ INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.utilisateur.utilis
INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.utilisateur.utilisateur.adresse', 'Adresse');
INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.utilisateur.utilisateur.profilId', 'Profil');
INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.utilisateur.utilisateur.typeUtilisateurCode', 'Type d''utilisateur');
INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('common.entityListeners.dateCreation', 'Date de création');
INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('common.entityListeners.dateModification', 'Date de modification');
4 changes: 3 additions & 1 deletion samples/generators/pg/topmodel.config
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ sql:
increment: 50
start: 1000
mode: sequence
resourcesTableName: TRANSLATION
resourcesTableName: TRANSLATION
translateProperties: true
translateReferences: true
2 changes: 1 addition & 1 deletion samples/model/Securite/Profil/02_Entities.tmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module: Securite.Profil
tags:
- back
uses:
- Securite/Profil/01_References
- meta/decorators
- Securite/Profil/01_References

---
class:
Expand Down
2 changes: 1 addition & 1 deletion samples/model/pg.topmodel.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

version: 2.1.2
custom:
../../../TopModel.Generator.Sql: a7d6d70714c8ee38aeac8e9447ed12fd
../../../TopModel.Generator.Sql: 6c49105633a94d33dc5f26d6fece3a43
generatedFiles:
- ../generators/pg/src/01_tables.sql
- ../generators/pg/src/02_fk_indexes.sql
Expand Down
2 changes: 1 addition & 1 deletion samples/model/ssdt.topmodel.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

version: 2.1.2
custom:
../../../TopModel.Generator.Sql: a7d6d70714c8ee38aeac8e9447ed12fd
../../../TopModel.Generator.Sql: 6c49105633a94d33dc5f26d6fece3a43
generatedFiles:
- ../generators/ssdt/src/init/DROIT.insert.sql
- ../generators/ssdt/src/init/main.sql
Expand Down

0 comments on commit ff9e1ed

Please sign in to comment.