From b4d49559404b93502f9b4f8bfa2aee114486f98d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gild=C3=A9ric=20DERUETTE?= Date: Wed, 22 May 2024 18:17:11 +0200 Subject: [PATCH] =?UTF-8?q?[SQL]=20Traduction=20des=20libell=C3=A9s=20:=20?= =?UTF-8?q?augmentation=20taille=20champ=20&=20renommage=20de=20la=20pk.?= =?UTF-8?q?=20Suppression=20de=20la=20colonne=20locale=20si=20pas=20de=20l?= =?UTF-8?q?angue=20par=20d=C3=A9faut?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Procedural/AbstractSchemaGenerator.cs | 22 ++++++--- samples/generators/pg/src/01_tables.sql | 9 ++-- samples/generators/pg/src/06_resources.sql | 48 +++++++++++++++---- samples/generators/pg/topmodel.config | 2 +- 4 files changed, 58 insertions(+), 23 deletions(-) diff --git a/TopModel.Generator.Sql/Procedural/AbstractSchemaGenerator.cs b/TopModel.Generator.Sql/Procedural/AbstractSchemaGenerator.cs index b641e8ef..fb43cb3a 100644 --- a/TopModel.Generator.Sql/Procedural/AbstractSchemaGenerator.cs +++ b/TopModel.Generator.Sql/Procedural/AbstractSchemaGenerator.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Logging; using TopModel.Core; using TopModel.Generator.Core; +using TopModel.Utils; namespace TopModel.Generator.Sql.Procedural; @@ -432,16 +433,21 @@ private void WriteResourceTableDeclaration(SqlFileWriter writer) writer.WriteLine(" **/"); writer.WriteLine($"create table {_config.ResourcesTableName} ("); writer.WriteLine(1, "RESOURCE_KEY varchar(255),"); - writer.WriteLine(1, "LOCALE varchar(10),"); - writer.WriteLine(1, "LABEL varchar(255),"); - writer.WriteLine(1, "constraint PK_KEY_LOCALE primary key (RESOURCE_KEY, LOCALE)"); + var hasLocale = _translationStore.Translations.Keys.Count > 1 || _translationStore.Translations.Keys.Any(a => a != string.Empty); + if (hasLocale) + { + writer.WriteLine(1, "LOCALE varchar(10),"); + } + + writer.WriteLine(1, "LABEL varchar(4000),"); + writer.WriteLine(1, $"constraint PK_{_config.ResourcesTableName.ToConstantCase()} primary key (RESOURCE_KEY, LOCALE)"); writer.WriteLine($"){BatchSeparator}"); writer.WriteLine("/**"); writer.WriteLine(" * Création de l'index pour " + tableName + " (RESOURCE_KEY, LOCALE)"); writer.WriteLine(" **/"); - writer.WriteLine("create index " + Quote($"IDX_{_config.ResourcesTableName}_RESOURCE_KEY_LOCALE") + " on " + tableName + " ("); - writer.WriteLine("\t" + "RESOURCE_KEY, LOCALE" + " ASC"); + writer.WriteLine("create index " + Quote($"IDX_{_config.ResourcesTableName}_RESOURCE_KEY{(hasLocale ? "_LOCALE" : string.Empty)}") + " on " + tableName + " ("); + writer.WriteLine("\t" + $"RESOURCE_KEY{(hasLocale ? ", LOCALE" : string.Empty)}" + " ASC"); writer.WriteLine($"){BatchSeparator}"); writer.WriteLine(); } @@ -449,15 +455,17 @@ private void WriteResourceTableDeclaration(SqlFileWriter writer) private void WriteResources(SqlFileWriter writer, Class modelClass) { + var hasLocale = _translationStore.Translations.Keys.Count > 1 || _translationStore.Translations.Keys.Any(a => a != string.Empty); if (_config.TranslateProperties == true && modelClass.Properties.OfType().Where(p => p.Label != null).Count() > 0 && modelClass.ModelFile != null) { 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 modelClass.Properties.OfType().Where(p => p.Label != null)) { - writer.WriteLine($@"INSERT INTO {_config.ResourcesTableName}(RESOURCE_KEY, LOCALE, LABEL) VALUES({SingleQuote(property.ResourceKey)}, {(string.IsNullOrEmpty(lang) ? "null" : @$"{SingleQuote(lang)}")}, {SingleQuote(_translationStore.GetTranslation(property, lang))});"); + 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))});"); } } } @@ -470,7 +478,7 @@ private void WriteResources(SqlFileWriter writer, Class modelClass) { foreach (var val in modelClass.Values) { - writer.WriteLine(@$"INSERT INTO {_config.ResourcesTableName}(RESOURCE_KEY, LOCALE, LABEL) VALUES({SingleQuote(val.ResourceKey)}, {(string.IsNullOrEmpty(lang) ? "null" : @$"{SingleQuote(lang)}")}, {SingleQuote(_translationStore.GetTranslation(val, lang))});"); + 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))});"); } } } diff --git a/samples/generators/pg/src/01_tables.sql b/samples/generators/pg/src/01_tables.sql index d548aa6f..600b1e93 100644 --- a/samples/generators/pg/src/01_tables.sql +++ b/samples/generators/pg/src/01_tables.sql @@ -86,14 +86,13 @@ create table UTILISATEUR ( **/ create table TRANSLATION ( RESOURCE_KEY varchar(255), - LOCALE varchar(10), - LABEL varchar(255), - constraint PK_KEY_LOCALE primary key (RESOURCE_KEY, LOCALE) + LABEL varchar(4000), + constraint PK_TRANSLATION primary key (RESOURCE_KEY, LOCALE) ); /** * Création de l'index pour TRANSLATION (RESOURCE_KEY, LOCALE) **/ -create index IDX_TRANSLATION_RESOURCE_KEY_LOCALE on TRANSLATION ( - RESOURCE_KEY, LOCALE ASC +create index IDX_TRANSLATION_RESOURCE_KEY on TRANSLATION ( + RESOURCE_KEY ASC ); diff --git a/samples/generators/pg/src/06_resources.sql b/samples/generators/pg/src/06_resources.sql index aaf92ce2..af230923 100644 --- a/samples/generators/pg/src/06_resources.sql +++ b/samples/generators/pg/src/06_resources.sql @@ -8,18 +8,46 @@ -- Description : Script de création des resources (libellés traduits). -- =========================================================================================== +/** Initialisation des traductions des propriétés de la table DROIT **/ +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.profil.droit.code', 'Droit'); +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.profil.droit.libelle', 'Droit'); + /** Initialisation des traductions des valeurs de la table DROIT **/ -INSERT INTO TRANSLATION(RESOURCE_KEY, LOCALE, LABEL) VALUES('securite.profil.droit.values.Create', null, 'Création'); -INSERT INTO TRANSLATION(RESOURCE_KEY, LOCALE, LABEL) VALUES('securite.profil.droit.values.Read', null, 'Lecture'); -INSERT INTO TRANSLATION(RESOURCE_KEY, LOCALE, LABEL) VALUES('securite.profil.droit.values.Update', null, 'Mise à jour'); -INSERT INTO TRANSLATION(RESOURCE_KEY, LOCALE, LABEL) VALUES('securite.profil.droit.values.Delete', null, 'Suppression'); +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.profil.droit.values.Create', 'Création'); +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.profil.droit.values.Read', 'Lecture'); +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.profil.droit.values.Update', 'Mise à jour'); +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.profil.droit.values.Delete', 'Suppression'); + +/** Initialisation des traductions des propriétés de la table PROFIL **/ +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.profil.profil.id', 'Id technique du profil'); +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.profil.profil.libelle', 'Libellé du profil'); +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.profil.profil.droits', 'Droits'); +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'); + +/** Initialisation des traductions des propriétés de la table TYPE_DROIT **/ +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.profil.typeDroit.code', 'Type de droit'); /** Initialisation des traductions des valeurs de la table TYPE_DROIT **/ -INSERT INTO TRANSLATION(RESOURCE_KEY, LOCALE, LABEL) VALUES('securite.profil.typeDroit.values.Read', null, 'Lecture'); -INSERT INTO TRANSLATION(RESOURCE_KEY, LOCALE, LABEL) VALUES('securite.profil.typeDroit.values.Write', null, 'Ecriture'); -INSERT INTO TRANSLATION(RESOURCE_KEY, LOCALE, LABEL) VALUES('securite.profil.typeDroit.values.Admin', null, 'Administration'); +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.profil.typeDroit.values.Read', 'Lecture'); +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.profil.typeDroit.values.Write', 'Ecriture'); +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.profil.typeDroit.values.Admin', 'Administration'); + +/** Initialisation des traductions des propriétés de la table TYPE_UTILISATEUR **/ +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.utilisateur.typeUtilisateur.code', 'Type d''utilisateur'); /** Initialisation des traductions des valeurs de la table TYPE_UTILISATEUR **/ -INSERT INTO TRANSLATION(RESOURCE_KEY, LOCALE, LABEL) VALUES('securite.utilisateur.typeUtilisateur.values.Admin', null, 'Administrateur'); -INSERT INTO TRANSLATION(RESOURCE_KEY, LOCALE, LABEL) VALUES('securite.utilisateur.typeUtilisateur.values.Gestionnaire', null, 'Gestionnaire'); -INSERT INTO TRANSLATION(RESOURCE_KEY, LOCALE, LABEL) VALUES('securite.utilisateur.typeUtilisateur.values.Client', null, 'Client'); +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.utilisateur.typeUtilisateur.values.Admin', 'Administrateur'); +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.utilisateur.typeUtilisateur.values.Gestionnaire', 'Gestionnaire'); +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.utilisateur.typeUtilisateur.values.Client', 'Client'); + +/** Initialisation des traductions des propriétés de la table UTILISATEUR **/ +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.utilisateur.utilisateur.id', 'Id technique'); +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.utilisateur.utilisateur.prenom', 'Prénom'); +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.utilisateur.utilisateur.email', 'Adresse email'); +INSERT INTO TRANSLATION(RESOURCE_KEY, LABEL) VALUES('securite.utilisateur.utilisateur.dateNaissance', 'Date de naissance'); +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 1e4a3755..6424b2b1 100644 --- a/samples/generators/pg/topmodel.config +++ b/samples/generators/pg/topmodel.config @@ -4,7 +4,7 @@ app: pg modelRoot: ../../model lockFileName: pg.topmodel.lock i18n: - translateProperties: false + translateProperties: true translateReferences: true sql: - tags: