--freemarker template
<#include "SQL_lingua_franca"/>
<#include "SuperX_general"/>
<#include "RPTA-Makros"/>

<sqlvars>
<sqlvar name="rpta_column_layouts" type="hashsequence">
select uniquename,
caption,
resultset_uniquename as rpta_resultset,
replace(whereclause,'''','''''') as whereclause,
description,
sortclause,
is_virtual
from tmp_etl_rpta_column_layout;
</sqlvar>
<sqlvar name="rpta_columns" type="hashsequence">
select C.uniquename,
null::varchar(255) as caption,
C.caption as caption_der_spalte,
L.caption as caption_in_ergebnistabelle,
C.srcfieldname,
C.targetfieldname,
T.uniquename as column_type,
replace(C.col_function,'''','''''') as col_function,
L.is_visible,
L.visible_size,
C.is_aggregate,
null::varchar(255) as description,
L.format_code,
C.description as description_der_spalte,
L.description as description_in_ergebnistabelle
from tmp_rpta_column C, tmp_rpta_column2layout L, rpta_column_type T
where L.column_id=C.tid
and T.tid=C.column_type
</sqlvar>
</sqlvars>


<@rpta_column_layout_fuellen_multi />
drop TABLE if exists tmp_rpta_column;
drop TABLE if exists tmp_rpta_column_layout;
drop TABLE if exists tmp_etl_rpta_column_layout;

<#macro rpta_column_layout_fuellen_multi>
CREATE temp TABLE tmp_rpta_column
(
   uniquename       varchar(255)   NOT NULL,
   caption          varchar(255),
   caption_in_ergebnistabelle         varchar(255),
   srcfieldname     varchar(255),
   targetfieldname  varchar(255),
   column_type      integer,
   col_function     text,
   is_visible       smallint,
   visible_size     smallint,
   is_aggregate     smallint,
   resultset_id     integer,
   sortnr integer,
   description TEXT,
   description_in_ergebnistabelle TEXT,
   format_code_uniquename varchar(255),
   format_code_id integer
);

CREATE temp TABLE tmp_rpta_column_layout
(
    uniquename    varchar(255)   NOT NULL,
   caption       varchar(255),
   resultset_id  integer,
   whereclause text,
   sortclause text,
   description text,
   is_virtual smallint default 1
);
<#foreach rpta_column_layout in rpta_column_layouts>

delete from tmp_rpta_column_layout;
delete from tmp_rpta_column;


insert into tmp_rpta_column_layout(
resultset_id,
uniquename,
caption,
whereclause,
<#if rpta_column_layout.sortclause?has_content>
sortclause,
</#if>
<#if rpta_column_layout.is_virtual?has_content>
  is_virtual,
</#if>
  description
)
select tid,
'${rpta_column_layout.uniquename}',
'${rpta_column_layout.caption}',
'${rpta_column_layout.whereclause}',
<#if rpta_column_layout.sortclause?has_content>
'${rpta_column_layout.sortclause}',
</#if>
<#if rpta_column_layout.is_virtual?has_content>
'${rpta_column_layout.is_virtual}',
</#if>
'${rpta_column_layout.description}'
FROM rpta_resultset
where uniquename='${rpta_column_layout.rpta_resultset}';


<#assign sortnr=0 />
<#foreach column in rpta_columns>
<#assign sortnr=sortnr +1 />

INSERT INTO tmp_rpta_column
(resultset_id,
  uniquename,
  caption,
  caption_in_ergebnistabelle,
  srcfieldname,
  targetfieldname,
  column_type,
  col_function,
  is_visible,
  visible_size,
  is_aggregate,
  sortnr,
  description,
  description_in_ergebnistabelle,
  format_code_uniquename
  )
select R.tid,
'${column.uniquename}',
<#if column.caption?exists>
'${column.caption}',
<#else>
'${column.caption_der_spalte}',
</#if>
<#if column.caption_in_ergebnistabelle?exists>
'${column.caption_in_ergebnistabelle}',
<#else>
'${column.caption}',
</#if>
'${column.srcfieldname}',
<#if !column.targetfieldname?exists || column.targetfieldname=="">'${column.srcfieldname}' <#else>'${column.targetfieldname}' </#if>,
T.tid as column_type,
<#if column.col_function?exists>'${column.col_function}'<#else>null::varchar  </#if> as col_function,
${column.is_visible} as is_visible,
<#if column.visible_size?exists && column.visible_size?string !="" > ${column.visible_size} <#else>null::integer </#if> as visible_size,
${column.is_aggregate} as is_aggregate,
${sortnr*10} as sortnr,
<#if column.description?exists>'${column.description}'<#elseif column.description_der_spalte?exists>'${column.description_der_spalte}'<#else>null::varchar  </#if>,
<#if column.description_in_ergebnistabelle?exists>'${column.description_in_ergebnistabelle}'<#elseif column.description?exists>'${column.description}'<#else>null::varchar  </#if>,
<#if column.format_code?exists>'${column.format_code}'<#else>null::varchar </#if>
FROM rpta_resultset R, rpta_column_type T
where R.uniquename='${rpta_column_layout.rpta_resultset}'
and T.uniquename='${column.column_type}';

</#foreach>


update tmp_rpta_column set format_code_id=C.tid
from rpta_format_code C
where C.uniquename=tmp_rpta_column.format_code_uniquename
and format_code_uniquename is not null;


select * into temp tmp_rpta_column2layout2
from rpta_column2layout
where layout_id in (select L.tid
from rpta_column_layout L,rpta_resultset R
where R.tid=L.resultset_id 
and R.uniquename='${rpta_column_layout.rpta_resultset}'
and L.uniquename='${rpta_column_layout.uniquename}'
)
; 
  
select uniquename into temp tmp_rpta_column_layout_target
from rpta_column_layout
where resultset_id in (select tid
FROM rpta_resultset
where uniquename='${rpta_column_layout.rpta_resultset}')
and resultset_id in (select tid
FROM rpta_resultset
where uniquename='${rpta_column_layout.rpta_resultset}')
;
--falls neu
insert into rpta_column_layout
(uniquename,
       caption,
       resultset_id,
       whereclause,
       sortclause,
  description,
  is_virtual)
 select uniquename,
       caption,
       resultset_id,
       whereclause,
       <#if rpta_column_layout.sortclause?has_content>
       sortclause,
       <#else>
       null::text as sortclause,
       </#if>
  description,
    is_virtual
FROM tmp_rpta_column_layout T
where not exists (select T2.uniquename from tmp_rpta_column_layout_target T2
where T2.uniquename=T.uniquename);

--falls geändert, uniquename muss bleiben
update rpta_column_layout
set (caption, resultset_id, whereclause,sortclause, description,  is_virtual)
= ( select distinct caption, resultset_id,
       whereclause,
       <#if rpta_column_layout.sortclause?has_content>
       sortclause,
       <#else>
       null::text as sortclause,
       </#if>
  description,
    is_virtual
FROM tmp_rpta_column_layout T
where T.uniquename=rpta_column_layout.uniquename)
where rpta_column_layout.uniquename in (select uniquename from tmp_rpta_column_layout);

drop table tmp_rpta_column_layout_target;

delete from rpta_column2layout
where layout_id in (select L.tid
from rpta_column_layout L,rpta_resultset R
where R.tid=L.resultset_id 
and R.uniquename='${rpta_column_layout.rpta_resultset}'
and L.uniquename='${rpta_column_layout.uniquename}'
)
; 

select * into temp tmp_rpta_column2 
from tmp_rpta_column T 
where (resultset_id,uniquename) not in
(select resultset_id,uniquename from rpta_column);

INSERT INTO rpta_column
(resultset_id,
  uniquename,
  caption,
  srcfieldname,
  targetfieldname,
  column_type,
  col_function,
  is_aggregate,
  description,
  custom
  )
  select 
resultset_id,
  uniquename,
  caption,
  srcfieldname,
  targetfieldname,
  column_type,
  col_function,
  is_aggregate,
  description,
  0 as custom
from tmp_rpta_column2; 

--evtl. neuen Satz einfügen, dann alle updaten

update rpta_column set (  caption,
  srcfieldname,
  targetfieldname,
  column_type,
  col_function,
  is_aggregate,
  description)
  = (select distinct  caption,
  srcfieldname,
  targetfieldname,
  column_type,
  col_function,
  is_aggregate,
  description
  from tmp_rpta_column T
  where T.resultset_id=rpta_column.resultset_id
  and T.uniquename=rpta_column.uniquename)
  where custom=0
  and  (resultset_id,uniquename) in
(select T.resultset_id,T.uniquename
from tmp_rpta_column T)
;

drop table tmp_rpta_column2;

insert into rpta_column2layout(column_id,
       layout_id,
       sortnr,
       is_visible,
       visible_size,
       caption,
  description,
  format_code_id)
select C.tid as column_id,
       L.tid as layout_id,
       T.sortnr,
       T.is_visible,
       T.visible_size,
       T.caption_in_ergebnistabelle,
  T.description_in_ergebnistabelle,
  T.format_code_id
FROM rpta_column C,  rpta_column_layout L, tmp_rpta_column T
where C.uniquename=T.uniquename
and C.resultset_id=T.resultset_id
and L.uniquename='${rpta_column_layout.uniquename}'
and L.resultset_id=T.resultset_id
 ;
drop TABLE tmp_rpta_column2layout2;

</#foreach>

drop table tmp_rpta_column;

</#macro>
