PostgreSQL - Problemas con variables en funciones

 
Vista:
sin imagen de perfil
Val: 1
Ha disminuido su posición en 4 puestos en PostgreSQL (en relación al último mes)
Gráfica de PostgreSQL

Problemas con variables en funciones

Publicado por Manuel (1 intervención) el 22/01/2020 13:29:48
Buenos días, he heredado una aplicación hecha en node.js y postgres, dado que yo soy de php y mysql estoy teniendo problemas con las funciones de postgres y sus variables. Pongo una función y os pregunto:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
-- Function: informacion.actualizarespuestas(informacion.campo_respuestas[], integer, integer, integer)
 
-- DROP FUNCTION informacion.actualizarespuestas(informacion.campo_respuestas[], integer, integer, integer);
 
CREATE OR REPLACE FUNCTION informacion.actualizarespuestas(
    respuestas informacion.campo_respuestas[],
    id_revision integer,
    id_categoria integer,
    id_agrup integer)
  RETURNS SETOF informacion.agrupacion_respuestas AS
$BODY$declare
	id_agrupacion integer;
	respuesta informacion.campo_respuestas;
	campo record;
	referencia record;
	id_emplazamiento integer;
	aux_n numeric;
	aux_f timestamp without time zone;
	aux_other character varying;
	tipo_dato record;
begin
	foreach respuesta in array respuestas loop
		select * into campo from informacion.campo where id = respuesta.campo;
		if campo.tipo_dato = 8 then
			select * into referencia from informacion.referencia where id = campo.id_referencia;
			if referencia.id_plantilla is null then
				select * into tipo_dato from tiposdatos.index where id = referencia.tipodato;
				case tipo_dato.tipoprimario when 'numeric' then
						aux_n := cast(respuesta.respuesta as numeric);
						raise notice 'hola';
						if referencia.nombretabla = 'emplazamiento' then
							execute 'update visorschema.emplazamiento set '||referencia.columna||' = $1 where id = (select emplazamiento from visorschema.revision where id = '||id_revision||')' using aux_n;
						else
							execute 'update visorschema.revision set '||referencia.columna||' = $1 where id = '||id_revision||'' using aux_n;
						end if;
					when 'timestamp without time zone' then
						aux_f := cast(respuesta.respuesta as timestamp without time zone);
						if referencia.nombretabla = 'emplazamiento' then
							execute 'update visorschema.emplazamiento set '||referencia.columna||' = $1 where id = (select emplazamiento from visorschema.revision where id = '||id_revision||')' using aux_f;
						else
							execute 'update visorschema.revision set '||referencia.columna||' = $1 where id = '||id_revision||'' using aux_f;
						end if;
					else
						aux_other := respuesta.respuesta;
						if referencia.nombretabla = 'emplazamiento' then
							execute 'update visorschema.emplazamiento set '||referencia.columna||' = $1 where id = (select emplazamiento from visorschema.revision where id = '||id_revision||')' using aux_other;
						else
							execute 'update visorschema.revision set '||referencia.columna||' = $1 where id = '||id_revision||'' using aux_other;
						end if;
				end case;
 
			else
				update informacion.respuestas set valor = respuesta.respuesta where id = (select a.id from informacion.respuestas as a left outer join informacion.agrupacion_respuestas as b on (a.id_agrupacion = b.id) where b.categoria = cast(referencia.nombretabla as integer) and a.campo = cast(referencia.columna as integer));
			end if;
		else
			update informacion.respuestas as a set valor = respuesta.respuesta where a.id_agrupacion = id_agrup and a.campo = campo.id;
		end if;
	end loop;
	return query select * from informacion.agrupacion_respuestas where id = id_agrup;
end $BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100
  ROWS 1000;
ALTER FUNCTION informacion.actualizarespuestas(informacion.campo_respuestas[], integer, integer, integer)
  OWNER TO postgres;

Basicamente entiendo lo que hace. lo que no se es que significa esto
1
informacion.campo_respuestas[]
, pues hay una tabla campo y otra respuesta, y esto debiera traer datos de ambas, pero no se donde ni como se hace esa asignación. La llamada a la función desde node.js es:

1
2
3
4
5
6
7
db.one('select * from informacion.crearespuestas($1::informacion.campo_respuestas[], $2, $3)', [array, idRevision, idCategoria])
        .then(data => {
            fn(null, data);
        })
        .catch(err => {
            fn(err);
        });

Espero haberme explicado, gracias por responder

Cordial saludo
Manuel Ruiz-Falcó
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder