Skip to content

Commit

Permalink
[SQL] Traduction des libellés : augmentation taille champ & renommage…
Browse files Browse the repository at this point in the history
… de la pk. Suppression de la colonne locale si pas de langue par défaut
  • Loading branch information
gideruette committed May 22, 2024
1 parent 61a1293 commit b4d4955
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 23 deletions.
22 changes: 15 additions & 7 deletions TopModel.Generator.Sql/Procedural/AbstractSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.Logging;
using TopModel.Core;
using TopModel.Generator.Core;
using TopModel.Utils;

namespace TopModel.Generator.Sql.Procedural;

Expand Down Expand Up @@ -432,32 +433,39 @@ 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();
}
}

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<IFieldProperty>().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<IFieldProperty>().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))});");
}
}
}
Expand All @@ -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))});");
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions samples/generators/pg/src/01_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

48 changes: 38 additions & 10 deletions samples/generators/pg/src/06_resources.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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');
2 changes: 1 addition & 1 deletion samples/generators/pg/topmodel.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ app: pg
modelRoot: ../../model
lockFileName: pg.topmodel.lock
i18n:
translateProperties: false
translateProperties: true
translateReferences: true
sql:
- tags:
Expand Down

0 comments on commit b4d4955

Please sign in to comment.