1212import java .util .TreeSet ;
1313import java .util .regex .Matcher ;
1414import java .util .regex .Pattern ;
15- import java .util .stream .Collectors ;
16-
17- import com .google .common .collect .Streams ;
1815
1916import fr .insee .arc .core .dataobjects .ArcPreparedStatementBuilder ;
2017import fr .insee .arc .core .dataobjects .ColumnEnum ;
2118import fr .insee .arc .core .dataobjects .ViewEnum ;
2219import fr .insee .arc .core .service .global .bo .JeuDeRegle ;
23- import fr .insee .arc .core .service .global .dao .TableOperations ;
24- import fr .insee .arc .core .service .global .thread .ThreadTemporaryTable ;
2520import fr .insee .arc .core .service .p5mapping .bo .TableMapping ;
2621import fr .insee .arc .core .service .p5mapping .bo .VariableMapping ;
2722import fr .insee .arc .core .service .p5mapping .bo .rules .RegleMappingClePrimaire ;
@@ -82,11 +77,8 @@ public class MappingQueries implements IConstanteCaractere, IConstanteNumerique
8277 private Set <VariableMapping > ensembleVariableMapping ;
8378 private SortedSet <Integer > ensembleGroupes ;
8479
85- private static final String IDENTIFIANT_TRIGGER = "id$tg" ;
8680 private static final String PREP_UNION_TABLE = "pu" ;
8781
88-
89- private Map <String , String > insertionTriggers = new HashMap <String , String >();
9082
9183 private Map <TableMapping , Set <String >> ensembleRubriqueIdentifianteTable ;
9284 /*
@@ -105,7 +97,6 @@ public class MappingQueries implements IConstanteCaractere, IConstanteNumerique
10597 */
10698 private String nomTableModVariableMetier ;
10799 private String nomTableSource ;
108- private String nomViewSource ;
109100 private String nomTableRegleMapping ;
110101 private String tableLienIdentifiants ;
111102
@@ -278,32 +269,6 @@ private void construireListeRubriqueParTable() {
278269 this .ensembleRubriqueIdentifianteTable .put (table , s );
279270 }
280271 }
281-
282- private void initializeTrigger (StringBuilder returned ) throws ArcException {
283- returned .append ("\n CREATE OR REPLACE FUNCTION pg_temp.pu() RETURNS trigger AS $$ BEGIN " );
284- }
285-
286- private void finalizeTrigger (StringBuilder returned ) throws ArcException {
287-
288- // important to return NULL not to fill memory with pointer
289- returned .append ("\n RETURN NULL; END; $$ LANGUAGE plpgsql;" );
290-
291- // create a view on a empty image of the controle phase output table
292- // it could avoid some nasty bugs if the instead of clause fails
293- // as in postgres, insert into view triggers an insert on the master table
294- // and we do not want to modify the data from the controle phase output table
295- String tableTrigger = FormatSQL .temporaryTableName (ThreadTemporaryTable .TABLE_MAPPING_DATA_TEMP );
296- this .nomViewSource = FormatSQL .temporaryTableName (ThreadTemporaryTable .VIEW_MAPPING_DATA_TEMP );
297-
298- returned .append (TableOperations .createTableTravail (nomTableSource , tableTrigger , null , true ));
299- returned .append (TableOperations .createViewImage (tableTrigger , this .nomViewSource ));
300-
301- // apply trigger to view
302- returned .append ("\n CREATE TRIGGER pu" );
303- returned .append ("\n INSTEAD OF INSERT ON " +this .nomViewSource );
304- returned .append ("\n FOR EACH ROW EXECUTE FUNCTION pg_temp.pu();" );
305- }
306-
307272
308273 /**
309274 * Construit les objets de type {@link TableMapping} et {@link VariableMapping}
@@ -395,8 +360,9 @@ public String construireTableLienIdentifiants() throws ArcException {
395360 * @return a map with the model table as key and the query that inserts data as value
396361 * @throws ArcException
397362 */
398- public void getQueriesThatInsertDataIntoTemporaryModelTable (String aNomFichier , StringBuilder requete ) throws ArcException {
363+ public StringBuilder getQueriesThatInsertDataIntoTemporaryModelTable (String aNomFichier ) throws ArcException {
399364
365+ StringBuilder requete = new StringBuilder ();
400366 for (TableMapping table : this .ensembleTableMapping ) {
401367
402368 requete .append (ModeRequete .NESTLOOP_OFF );
@@ -412,45 +378,9 @@ public void getQueriesThatInsertDataIntoTemporaryModelTable(String aNomFichier,
412378 requete .append (ModeRequete .NESTLOOP_ON );
413379
414380 }
415-
416-
417- // the view trigger is made to split the main table from controle phase in several little tables that contains only the useful columns
418- // it is a huge optimization on large file as the full scan is only made once
419- StringBuilder requeteTrigger = new StringBuilder ();
420- createTriggerTables (requeteTrigger );
421- initializeTrigger (requeteTrigger );
422- defineInsertionTrigger (requeteTrigger );
423- finalizeTrigger (requeteTrigger );
424- useTrigger (requeteTrigger );
425-
426- requete .insert (0 , requeteTrigger );
427-
428- }
429-
430- private void useTrigger (StringBuilder returned ) {
431- returned .append ("\n INSERT INTO " ).append (this .nomViewSource ).append (" SELECT * FROM " ).append (this .nomTableSource ).append (";" );
381+ return requete ;
432382 }
433383
434- private void defineInsertionTrigger (StringBuilder returned ) {
435- insertionTriggers .entrySet ().stream ().forEach (t ->
436- {
437- returned .append ("\n INSERT INTO " ).append (t .getKey ());
438- returned .append ("\n SELECT " ).append (t .getValue ()).append ("\n FROM " ).append ("(SELECT NEW.*) e;" );
439- }
440- );
441- }
442-
443- private void createTriggerTables (StringBuilder returned ) {
444-
445- insertionTriggers .entrySet ().stream ().forEach (t ->
446- {
447- returned .append ("\n CREATE TEMPORARY TABLE " ).append (t .getKey ()).append (FormatSQL .WITH_NO_VACUUM );
448- returned .append ("\n AS SELECT " ).append (t .getValue ()).append ("\n FROM " ).append (this .nomTableSource );
449- returned .append ("\n LIMIT 0;" );
450- }
451- );
452- }
453-
454384 /**
455385 * Delete empty records from model tables
456386 * @param aNomFichier
@@ -474,18 +404,7 @@ public String deleteEmptyRecords(String aNomFichier) throws ArcException {
474404 return requeteGlobale .toString ().replace (TOKEN_ID_SOURCE , aNomFichier );
475405
476406 }
477-
478- /**
479- * Parse expression to identify what source column are used in
480- * @param expressionSQLCalculMapping
481- * @return
482- */
483- private String computeSourceColumnsUsedInExpression (String expressionSQLCalculMapping )
484- {
485- return Streams .concat (regleMappingFactory .getEnsembleNomRubriqueExistante ().stream ()
486- , regleMappingFactory .getEnsembleIdentifiantRubriqueExistante ().stream ())
487- .filter (t -> expressionSQLCalculMapping .toLowerCase ().contains (t )).collect (Collectors .joining ("," ));
488- }
407+
489408
490409 private StringBuilder insererTablePrepUnionNew (StringBuilder returned , TableMapping table ,
491410 Map <String , String > reglesIdentifiantes ) throws ArcException {
@@ -504,16 +423,14 @@ private StringBuilder insererTablePrepUnionNew(StringBuilder returned, TableMapp
504423 returned .append ("\n FROM (" );
505424 returned .append ("\n SELECT " ).append (expressionSQLCalculMapping );
506425 returned .append ("\n FROM " ).append (this .tableLienIdentifiants ).append (" d " );
507- returned .append ("\n ," ).append (idTableGroupe ( table , groupe ) ).append (" e " );
426+ returned .append ("\n ," ).append (this . nomTableSource ).append (" e " );
508427 returned .append ("\n WHERE e.id=d.id_table " );
509428 returned .append ("\n AND " ).append (idTableGroupe (table ,groupe ));
510429 returned .append ("\n ) a " );
511430 returned .append ("\n GROUP BY " );
512431 table .sqlListeVariables (returned );
513432 returned .append (";" );
514433
515- insertionTriggers .put (idTableGroupe (table ,groupe ),computeSourceColumnsUsedInExpression (expressionSQLCalculMapping ));
516-
517434 }
518435 }
519436
@@ -528,7 +445,7 @@ private StringBuilder insererTablePrepUnionNew(StringBuilder returned, TableMapp
528445 returned .append ("\n FROM (" );
529446 returned .append ("\n SELECT " ).append (expressionSQLCalculMapping );
530447 returned .append ("\n FROM " ).append (this .tableLienIdentifiants ).append (" d " );
531- returned .append ("\n ," ).append (idTableNGroupe ( table ) ).append (" e " );
448+ returned .append ("\n ," ).append (this . nomTableSource ).append (" e " );
532449 returned .append ("\n WHERE e.id=d.id_table " );
533450 returned .append ("\n AND " ).append (idTableNGroupe (table ));
534451 // if table has group, records selected by group must be excluded
@@ -537,9 +454,6 @@ private StringBuilder insererTablePrepUnionNew(StringBuilder returned, TableMapp
537454 returned .append ("\n GROUP BY " );
538455 table .sqlListeVariables (returned );
539456 returned .append (";" );
540-
541- insertionTriggers .put (idTableNGroupe (table ),computeSourceColumnsUsedInExpression (expressionSQLCalculMapping ));
542-
543457 }
544458 return returned ;
545459 }
@@ -770,48 +684,37 @@ private StringBuilder construireTableIdentifiantsFichierCourant(StringBuilder re
770684 tableGroups .keySet ().stream ().forEach (k -> blocSelect .append (",\n " ).append (k ));
771685 tableNonGroup .keySet ().stream ().forEach (k -> blocSelect .append (",\n " ).append (k ));
772686
773- blocSelect .append ("\n FROM " ).append (idTableNGroupe (IDENTIFIANT_TRIGGER )).append (";" );
774- //blocSelect.append("\n FROM ttt;");
687+ blocSelect .append ("\n FROM ttt;" );
775688
776689 // bloc 2 : on met les variables non groupe et les variables subalternes
777690 // identifiantes de groupes
778691 alreadyAdded = new HashSet <>();
779- StringBuilder blocTrigger =new StringBuilder ();
780-
781- blocTrigger .append (ColumnEnum .ID );
782-
692+ StringBuilder blocWith =new StringBuilder ();
693+ blocWith .append ("\n WITH ttt as materialized ( SELECT id" );
783694 for (String nomVariable : reglesIdentifiantes .keySet ()) {
784695 String expressionVariable = reglesIdentifiantes .get (nomVariable );
785696 if (nomsVariablesGroupe .get (nomVariable ) == null ) {
786- blocTrigger .append ("\n ," ).append (expressionVariable ).append (" AS " ).append (nomVariable );
787- } else
788- {
789- String nomVariableReworked = linkedIds .get (nomsVariablesGroupe .get (nomVariable )).replaceAll (",id\\ _[^,]+" , "" );
790- if (!alreadyAdded
791- .contains (nomVariableReworked )) {
792- blocTrigger .append ("\n " ).append (nomVariableReworked );
793- alreadyAdded .add (nomVariableReworked );
794- }
697+ blocWith .append ("\n ," + expressionVariable + " AS " + nomVariable );
698+ } else if (!alreadyAdded
699+ .contains (linkedIds .get (nomsVariablesGroupe .get (nomVariable )).replaceAll (",id\\ _[^,]+" , "" ))) {
700+ blocWith .append ("\n "
701+ + linkedIds .get (nomsVariablesGroupe .get (nomVariable )).replaceAll (",id\\ _[^,]+" , "" ) + " " );
702+ alreadyAdded .add (linkedIds .get (nomsVariablesGroupe .get (nomVariable )).replaceAll (",id\\ _[^,]+" , "" ));
795703 }
796704 }
797-
798- tableGroups .keySet ().stream ().forEach (k -> {
799- blocTrigger .append ("\n , " )
705+
706+ tableGroups .keySet ().stream ().forEach (k -> blocWith .append ("\n , " )
800707 .append (checkIsNotNull (tableGroups .get (k )))
801- .append (" as " ).append (k );
802- }
708+ .append (" as " ).append (k )
803709 );
804- tableNonGroup .keySet ().stream ().forEach (k ->
805- blocTrigger .append ("\n ," )
710+ tableNonGroup .keySet ().stream ().forEach (k -> blocWith .append ("\n ," )
806711 .append (checkIsNotNull (tableNonGroup .get (k )))
807712 .append (" as " ).append (k )
808713 );
809714
810- insertionTriggers .put (idTableNGroupe (IDENTIFIANT_TRIGGER ),blocTrigger .toString ());
811-
812- returned
813- //.append(blocWith)
814- .append (blocSelect );
715+ blocWith .append ("\n FROM " + this .nomTableSource + " ) " );
716+
717+ returned .append (blocWith ).append (blocSelect );
815718
816719 return returned ;
817720 }
0 commit comments