return: text
lang: plpgsql
parameters:
  p0:
    type: text
    name: tablename
  p1:
    type: text
    name: field_prefix
src: |
  DECLARE	
  	w_tablename TEXT;
  	w_field_prefix TEXT;
      result TEXT;
      t0 timestamp;
      t1 timestamp;
      t2 timestamp;
      t3 timestamp;
  BEGIN
  	w_tablename = tablename;
  	IF w_tablename = '' THEN
  		w_tablename = 't_codes_emploi';
  	END IF;
  	
  	w_field_prefix = field_prefix;
  	IF w_field_prefix = '' THEN
  		w_field_prefix = 'code_emploi';
  	END IF;
  	
  	RAISE NOTICE 'Traitement de %', w_tablename;
  	t0 = clock_timestamp();
  	EXECUTE 'UPDATE rh.p_profils
  		SET ' || w_field_prefix || '_id = ' || w_tablename || '.oid
  		FROM rh.' || w_tablename || '
  		WHERE ' || w_tablename || '.code_original = p_profils.' || w_field_prefix || '_code_original
  			AND ' || w_field_prefix || '_id IS DISTINCT FROM ' || w_tablename || '.oid;';
  	t1 = clock_timestamp();
  	EXECUTE 'UPDATE rh.p_profils SET ' || w_field_prefix || '_id = 0 WHERE ' || w_field_prefix || '_id IS NULL;';
  	t2 = clock_timestamp();
  	EXECUTE 'UPDATE rh.p_profils
  		SET 
  		' || w_field_prefix || '_code = ' || w_tablename || '.code, 
  		' || w_field_prefix || '_texte = ' || w_tablename || '.texte
  		FROM rh.' || w_tablename || '
  		WHERE ' || w_tablename || '.oid = p_profils.' || w_field_prefix || '_id
  			AND (' || w_field_prefix || '_code IS DISTINCT FROM ' || w_tablename || '.code OR
  			' || w_field_prefix || '_texte IS DISTINCT FROM ' || w_tablename || '.texte);';
  	t3 = clock_timestamp();
  	
  	RAISE NOTICE 'Total % (1 : %; 2 : %; 3 : %)', t3 - t0, t1 - t0, t2 - t1, t3 - t2;	
  	RETURN 'OK';
  END;