ALTER TABLE ardoc.tb_contenant ADD ts_search tsvector__@@__

CREATE OR REPLACE FUNCTION ardoc.set_ts_search_contenant(_idcontenant integer) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE rec record;
BEGIN
	UPDATE ardoc.tb_contenant g SET ts_search=to_tsvector('french', concat_ws(' ', unaccent(g1.code), unaccent(g1.libelle), 
	unaccent(s.code), unaccent(s.libelle), unaccent(r.code), unaccent(r.libelle),   unaccent(g1.statut), unaccent(g1.description)))
	FROM ardoc.tb_contenant g1
	LEFT JOIN tb_securite s ON g1.idsecurite=s.id
	LEFT JOIN ardoc.tb_rayonnage r ON g1.idrayonnage=r.id
	WHERE g.id=_idcontenant AND g1.id=g.id;
END;
$$__@@__


CREATE FUNCTION ardoc.set_all_ts_search_contenant() RETURNS void
    LANGUAGE plpgsql
    AS $$
DECLARE 
	rec RECORD; 
	rec1 RECORD;
BEGIN
	--Search all archives
	FOR rec IN SELECT id FROM ardoc.tb_contenant 
	LOOP
	    SELECT ardoc.set_ts_search_contenant(rec.id) INTO rec1;
	END LOOP;
END;
$$__@@__

SELECT ardoc.set_all_ts_search_contenant() __@@__

INSERT INTO tb_colonne (id, libelle, nom, entite, import_only, table_referenced, column_referenced, description)
VALUES (77, 'Contenant parent', 'idcontenant', 'ardoc.tb_contenant', 1, 'ardoc.tb_contenant', 'code', 'Code du contenant parent')
ON CONFLICT (id) DO UPDATE SET libelle = 'Contenant parent', nom = 'idcontenant', entite='ardoc.tb_contenant', import_only=1, table_referenced='ardoc.tb_contenant', column_referenced='code', description='Code du contenant parent'__@@__

INSERT INTO public.tb_colonne (libelle, nom, entite, import_only, description) VALUES ('Numéro de tablette', 'num_tablette', 'ardoc.tb_contenant', 1, 'Numéro de la tablette du rayonnage')__@@__