diff --git a/.vscode/tasks.json b/.vscode/tasks.json index f0cdefc0..542c35ac 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -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", diff --git a/TopModel.Generator.Sql/Procedural/AbstractSchemaGenerator.cs b/TopModel.Generator.Sql/Procedural/AbstractSchemaGenerator.cs index 5c3e4c6f..27649bf1 100644 --- a/TopModel.Generator.Sql/Procedural/AbstractSchemaGenerator.cs +++ b/TopModel.Generator.Sql/Procedural/AbstractSchemaGenerator.cs @@ -181,9 +181,16 @@ public void GenerateSchemaScript(IEnumerable 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); @@ -547,32 +554,43 @@ private void WriteResourceTableDeclaration(SqlFileWriter writer) } } - private void WriteResources(SqlFileWriter writer, Class modelClass) + private void WriteResources(SqlFileWriter writer, IEnumerable 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))});"); + } + } } } } @@ -598,9 +616,8 @@ private void WriteSequence(Class classe, SqlFileWriter writerCrebas, string tabl /// Flux d'écriture Unique Key. /// Flux d'écritures des types. /// Flux d'écritures des commentaires. - /// Flux d'écritures des ressources. /// Liste des propriétés étrangères persistentes. - private IEnumerable WriteTableDeclaration(Class classe, SqlFileWriter writerCrebas, SqlFileWriter? writerUk, SqlFileWriter? writerType, SqlFileWriter? writerComment, SqlFileWriter? writerResources, IList availableClasses) + private IEnumerable WriteTableDeclaration(Class classe, SqlFileWriter writerCrebas, SqlFileWriter? writerUk, SqlFileWriter? writerType, SqlFileWriter? writerComment, IList availableClasses) { var fkPropertiesList = new List(); @@ -710,11 +727,6 @@ private IEnumerable WriteTableDeclaration(Class classe, Sql WriteComments(writerComment, classe, tableName, properties); } - if (writerResources is not null) - { - WriteResources(writerResources, classe); - } - writerCrebas.WriteLine(); if (isContainsInsertKey) diff --git a/samples/generators/pg/src/06_resources.sql b/samples/generators/pg/src/06_resources.sql index af230923..20799a55 100644 --- a/samples/generators/pg/src/06_resources.sql +++ b/samples/generators/pg/src/06_resources.sql @@ -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'); diff --git a/samples/generators/pg/topmodel.config b/samples/generators/pg/topmodel.config index 33d9866c..cb8c997d 100644 --- a/samples/generators/pg/topmodel.config +++ b/samples/generators/pg/topmodel.config @@ -24,4 +24,6 @@ sql: increment: 50 start: 1000 mode: sequence - resourcesTableName: TRANSLATION \ No newline at end of file + resourcesTableName: TRANSLATION + translateProperties: true + translateReferences: true \ No newline at end of file diff --git a/samples/model/Securite/Profil/02_Entities.tmd b/samples/model/Securite/Profil/02_Entities.tmd index 9a9ed539..b0cde0f9 100644 --- a/samples/model/Securite/Profil/02_Entities.tmd +++ b/samples/model/Securite/Profil/02_Entities.tmd @@ -3,8 +3,8 @@ module: Securite.Profil tags: - back uses: - - Securite/Profil/01_References - meta/decorators + - Securite/Profil/01_References --- class: diff --git a/samples/model/pg.topmodel.lock b/samples/model/pg.topmodel.lock index e5c32de5..0cadab1c 100644 --- a/samples/model/pg.topmodel.lock +++ b/samples/model/pg.topmodel.lock @@ -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 diff --git a/samples/model/ssdt.topmodel.lock b/samples/model/ssdt.topmodel.lock index 425a97ec..963823c9 100644 --- a/samples/model/ssdt.topmodel.lock +++ b/samples/model/ssdt.topmodel.lock @@ -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