return: text
lang: plpgsql
parameters:
  p0:
    type: text
    name: i_column_name
  p1:
    type: text
    name: i_option
src: |
  DECLARE	
  	to_column_name text;
  	listeString text;
  	listeCode text;
  	d int;
  	f int;
  	l int;
  BEGIN
  	to_column_name = i_column_name;
  	IF (i_option  = 'MONTH') THEN
  		IF to_column_name NOT ILIKE 'DISTINCT%' THEN
  			to_column_name = 'CASE WHEN COLUMNCHECKSEQUENCE THEN ' || to_column_name || ' ELSE NULL END';
  		ELSE
  			to_column_name = 'DISTINCT CASE WHEN COLUMNCHECKSEQUENCE THEN ' || substr(to_column_name,9) || ' ELSE NULL END';
  		END IF;
  	END IF;
  	d = strpos(to_column_name,'[LISTE:');
  	IF (d > 0) THEN
  		f = strpos(to_column_name,']');
  		IF (f > 0) THEN
  			l = f - d + 1;
  			listeString = substr(to_column_name, d, l);
  			listeCode = substr(listeString, 8, length(listeString) - 8);
  			to_column_name = replace(to_column_name, listeString, '(SELECT to_id FROM rh.t_listes JOIN rh.t_listes_contenu ON t_listes.oid = t_listes_contenu.liste_id WHERE t_listes.code = ''' || listeCode || ''')');
  		END IF;
  	END IF;
  	d = strpos(to_column_name,'[LISTE:');
  	IF (d > 0) THEN
  		f = strpos(to_column_name,']');
  		IF (f > 0) THEN
  			l = f - d + 1;
  			listeString = substr(to_column_name, d, l);
  			listeCode = substr(listeString, 8, length(listeString) - 8);
  			to_column_name = replace(to_column_name, listeString, '(SELECT to_id FROM rh.t_listes JOIN rh.t_listes_contenu ON t_listes.oid = t_listes_contenu.liste_id WHERE t_listes.code = ''' || listeCode || ''')');
  		END IF;
  	END IF;
  	d = strpos(to_column_name,'[LISTE:');
  	IF (d > 0) THEN
  		f = strpos(to_column_name,']');
  		IF (f > 0) THEN
  			l = f - d + 1;
  			listeString = substr(to_column_name, d, l);
  			listeCode = substr(listeString, 8, length(listeString) - 8);
  			to_column_name = replace(to_column_name, listeString, '(SELECT to_id FROM rh.t_listes JOIN rh.t_listes_contenu ON t_listes.oid = t_listes_contenu.liste_id WHERE t_listes.code = ''' || listeCode || ''')');
  		END IF;
  	END IF;
  	d = strpos(to_column_name,'[LISTE:');
  	IF (d > 0) THEN
  		f = strpos(to_column_name,']');
  		IF (f > 0) THEN
  			l = f - d + 1;
  			listeString = substr(to_column_name, d, l);
  			listeCode = substr(listeString, 8, length(listeString) - 8);
  			to_column_name = replace(to_column_name, listeString, '(SELECT to_id FROM rh.t_listes JOIN rh.t_listes_contenu ON t_listes.oid = t_listes_contenu.liste_id WHERE t_listes.code = ''' || listeCode || ''')');
  		END IF;
  	END IF;
  	d = strpos(to_column_name,'[LISTE:');
  	IF (d > 0) THEN
  		f = strpos(to_column_name,']');
  		IF (f > 0) THEN
  			l = f - d + 1;
  			listeString = substr(to_column_name, d, l);
  			listeCode = substr(listeString, 8, length(listeString) - 8);
  			to_column_name = replace(to_column_name, listeString, '(SELECT to_id FROM rh.t_listes JOIN rh.t_listes_contenu ON t_listes.oid = t_listes_contenu.liste_id WHERE t_listes.code = ''' || listeCode || ''')');
  		END IF;
  	END IF;
  	d = strpos(to_column_name,'[CLASSE:');
  	IF (d > 0) THEN
  		f = strpos(to_column_name,']');
  		IF (f > 0) THEN
  			l = f - d + 1;
  			listeString = substr(to_column_name, d, l);
  			listeCode = substr(listeString, 9, length(listeString) - 9);
  			to_column_name = replace(to_column_name, listeString, '(SELECT to_id FROM rh.t_classes JOIN rh.t_classes_sections ON classe_id = t_classes.oid JOIN rh.t_classes_sections_elements ON section_id = t_classes_sections.oid WHERE t_classes.code || '':'' || t_classes_sections.code = ''' || listeCode || ''')');
  		END IF;
  	END IF;
  	d = strpos(to_column_name,'[CLASSE:');
  	IF (d > 0) THEN
  		f = strpos(to_column_name,']');
  		IF (f > 0) THEN
  			l = f - d + 1;
  			listeString = substr(to_column_name, d, l);
  			listeCode = substr(listeString, 9, length(listeString) - 9);
  			to_column_name = replace(to_column_name, listeString, '(SELECT to_id FROM rh.t_classes JOIN rh.t_classes_sections ON classe_id = t_classes.oid JOIN rh.t_classes_sections_elements ON section_id = t_classes_sections.oid WHERE t_classes.code || '':'' || t_classes_sections.code = ''' || listeCode || ''')');
  		END IF;
  	END IF;
  	d = strpos(to_column_name,'[CLASSE:');
  	IF (d > 0) THEN
  		f = strpos(to_column_name,']');
  		IF (f > 0) THEN
  			l = f - d + 1;
  			listeString = substr(to_column_name, d, l);
  			listeCode = substr(listeString, 9, length(listeString) - 9);
  			to_column_name = replace(to_column_name, listeString, '(SELECT to_id FROM rh.t_classes JOIN rh.t_classes_sections ON classe_id = t_classes.oid JOIN rh.t_classes_sections_elements ON section_id = t_classes_sections.oid WHERE t_classes.code || '':'' || t_classes_sections.code = ''' || listeCode || ''')');
  		END IF;
  	END IF;
  		
  	RETURN to_column_name;
  END;