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


CREATE OR REPLACE FUNCTION smev.f_smev_data_json_build2(param_var smev.ctx_var[], call_stack json[], cycle_counter integer, 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; 
		--next vars for cycling 
		current_cycle_j json; 
		items_j int; 
		inext int; 
		ijson jsonb; 
	begin 
		i_count:= 1; 
		spec_template:= 'STEP='; 
		plus_sign_position:= 0;
		raise notice 'CALL_STACK %', call_stack; 
		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; 
				
				if (subst_var_name = 'start_b' or subst_var_name = 'end_b') then 
					current_cycle_j := call_stack[step_number];
					raise notice 'vars %, %, %', cycle_counter, step_number, subst_var_name;
					
					
					select jsonb_array_length((current_cycle_j::jsonb)->'list') into items_j;
					
					
					ijson:= (current_cycle_j::jsonb)->'list'->(cycle_counter - 1); 
					raise notice 'ijson= %', ijson::text;
					extracted_id_var_value:= ijson->>subst_var_name::text;
					raise notice 'extracted1= %', extracted_id_var_value::text;
				else 
					extracted_id_var_value:= (call_stack[step_number]->>subst_var_name);
					raise notice 'extracted2= %', extracted_id_var_value::text;
				end if;
			 
				/*if (cycle_counter is not null and cycle_counter = 0) then 
					extracted_id_var_value:= (call_stack[step_number]->>subst_var_name); 
				else  
					current_cycle_j := call_stack[step_number];
					raise notice 'vars %, %, %', cycle_counter, step_number, subst_var_name;
					
					 
					select jsonb_array_length((current_cycle_j::jsonb)->'list') into items_j; 
					
					raise notice 'vars2 %', ((current_cycle_j::jsonb)->'list'->cycle_counter)::text; 
					ijson:= (current_cycle_j::jsonb)->'list'->(cycle_counter - 1);  
					raise notice 'ijson= %', ijson::text; 
					extracted_id_var_value:= ijson->>subst_var_name::text;	 
					 
				end if;*/ 
				 
				-- 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$