Загрузка данных


CREATE OR REPLACE FUNCTION smev.f_smev_data_json_build(param_var smev.ctx_var[], call_stack json[], sid text)
 RETURNS text[]
 LANGUAGE plpgsql
AS $function$
	declare
		metadata text[];
		x smev.ctx_var;
		extracted_id_var_value text;
		step_number int;
		plus_sign_position int;
		subst_var_name text;
		spec_template text;
		i_count int;
	begin
		i_count:= 1;
		spec_template:= 'STEP=';
		plus_sign_position:= 0;
		foreach x in array param_var
		loop
			-- Variable value processing:
			extracted_id_var_value:= trim(x.value);
		
			 --RAISE notice 'Exctracted default id_var_value is %', extracted_id_var_value;
		
			-- Processing special template: 'step=' exists in extracted value	
			if upper(left(extracted_id_var_value, 5)) = spec_template then
				
				 --RAISE notice 'In template eq ';			
				-- position of '+' example: 'step=3+id'
				plus_sign_position:= position('+' in extracted_id_var_value);
				
				 --RAISE notice 'Position + %', plus_sign_position;
			
				-- Variable Name for extracted value (default the same as x.name):
				subst_var_name:= x.name;
				if plus_sign_position > 0 then
					-- Process for 'step=3+id' example. Name of the variable is indicated there  
					if char_length(extracted_id_var_value) > plus_sign_position then
						subst_var_name:= substring(extracted_id_var_value, plus_sign_position + 1);
						 --RAISE notice 'Subst var name %', subst_var_name;
						step_number:= (substring(extracted_id_var_value, char_length(spec_template) + 1, plus_sign_position - (char_length(spec_template)+1)))::int;
						 --RAISE notice 'Step number %', step_number;
					else
						perform robot.log_message('работа', 'Неправильный формат значения переменной '|| extracted_id_var_value , sid);
						RAISE EXCEPTION 'Wrong variable value format %', extracted_id_var_value;
						return metadata;
					end if;					
				else
					-- Process for 'step=3' example. without varable name (name is equal x.name and does not need to indicate it there) 
					step_number:= (substring(extracted_id_var_value, char_length(spec_template) + 1))::int;
					RAISE notice 'Step number without var name %', step_number;
				end if;
			
				extracted_id_var_value:= (call_stack[step_number]->>subst_var_name);	
				-- RAISE notice 'Extracted id var value %', extracted_id_var_value;		
				if extracted_id_var_value is null then
					RAISE notice 'Exctracted id_var_value is null';
					perform robot.log_message('работа', 'неопределённое значение параметра '|| subst_var_name , sid);
					return metadata;
				else
					-- RAISE notice 'Exctracted id_var_value is %', extracted_id_var_value;
				end if;
			
			end if;
			
			 RAISE notice 'Out template eq %, %', x.name, extracted_id_var_value ;
			-- Build the output array (pair varname, varvalue) the first - variable name , the second - variable value:		
			metadata[i_count]:= x.name; -- encode(x.name::bytea, 'escape');
			metadata[i_count+1]:= extracted_id_var_value; --encode(extracted_id_var_value::bytea, 'escape'); 
			i_count:= i_count + 2;				
		
		end loop;
		return metadata;
	END;
$function$