PostgreSQL - Generar Funciones 3

   
Vista:
Imágen de perfil de outrera

Generar Funciones 3

Publicado por outrera (35 intervenciones) el 01/04/2014 15:41:18
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
--
-- TOC entry 512 (class 1255 OID 26072)
-- Name: psp_obtener_encabezado(); Type: FUNCTION; Schema: utils; Owner: postgres
--
 
CREATE FUNCTION psp_obtener_encabezado() RETURNS character varying
    LANGUAGE plpgsql
    AS $$DECLARE
	oFunctionBody character varying;
BEGIN
 
	oFunctionBody :='';
 
	oFunctionBody := oFunctionBody || ' RETURNS refcursor AS  ' || CHR(36) || CHR(36) ;
 
	--Declaraciones de Variables
	oFunctionBody := oFunctionBody || ' DECLARE ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' registro_error  refcursor;       ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' v_usuario 	character(30); ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' v_clase   	character(2);  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' v_error   	character(3);  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' v_mensaje     	TEXT;          ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' v_contexto 	TEXT;          ' || CHR(13) || CHR(10) ;
 
	--Comienzo del Begin
	oFunctionBody := oFunctionBody || '  BEGIN ' || CHR(13) || CHR(10)  ;
 
	-- Inicializar cursor
	oFunctionBody := oFunctionBody || CHR(9) || ' registro_error:=' || CHR(39) || 're' || CHR(39) || ';' || CHR(13) || CHR(10) ;
 
 
	return oFunctionBody;
 
END;$$;
 
 
ALTER FUNCTION utils.psp_obtener_encabezado() OWNER TO postgres;
 
--
-- TOC entry 514 (class 1255 OID 26074)
-- Name: psp_obtener_encabezado2(); Type: FUNCTION; Schema: utils; Owner: postgres
--
 
CREATE FUNCTION psp_obtener_encabezado2() RETURNS character varying
    LANGUAGE plpgsql
    AS $$DECLARE
	oFunctionBody character varying;
BEGIN
 
	oFunctionBody :='';
 
	oFunctionBody := oFunctionBody || ' RETURNS SETOF refcursor AS  ' || CHR(36) || CHR(36) ;
 
	--Declaraciones de Variables
	oFunctionBody := oFunctionBody || ' DECLARE ' || CHR(13) || CHR(10) ;
 
	oFunctionBody := oFunctionBody || CHR(9) || ' registro_salida refcursor;       ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' registro_error  refcursor;       ' || CHR(13) || CHR(10) ;
 
	oFunctionBody := oFunctionBody || CHR(9) || ' v_usuario 	character(30); ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' v_clase   	character(2);  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' v_error   	character(3);  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' v_mensaje     	TEXT;          ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' v_contexto 	TEXT;          ' || CHR(13) || CHR(10) ;
 
	--Comienzo del Begin
	oFunctionBody := oFunctionBody || '  BEGIN ' || CHR(13) || CHR(10)  ;
 
	-- Inicializar cursor
	oFunctionBody := oFunctionBody || CHR(9) || ' registro_error:=' || CHR(39) || 're' || CHR(39) || ';' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' registro_salida:=' || CHR(39) || 'rs' || CHR(39) || ';' || CHR(13) || CHR(10) ;
 
	--Comando cusor y select
	oFunctionBody := oFunctionBody || CHR(9) || ' OPEN registro_salida FOR ' || CHR(13) || CHR(10) ;
 
 
	return oFunctionBody;
 
END;$$;
 
 
ALTER FUNCTION utils.psp_obtener_encabezado2() OWNER TO postgres;
 
--
-- TOC entry 562 (class 1255 OID 25530)
-- Name: psp_obtener_nombre_pk(character varying, character varying); Type: FUNCTION; Schema: utils; Owner: postgres
--
 
CREATE FUNCTION psp_obtener_nombre_pk(p_schemaname character varying, p_tablename character varying) RETURNS character varying
    LANGUAGE plpgsql
    AS $$
DECLARE
	oPkDef REFCURSOR ;
	oRecord record;
 
	v_ConstraintName character varying;
 
BEGIN
	v_ConstraintName:='';
 
	SELECT constraint_name
	FROM information_schema.table_constraints
	WHERE table_schema = p_schemaName AND table_name = p_tableName AND constraint_type='PRIMARY KEY'
	INTO v_ConstraintName ;
 
	IF NOT FOUND THEN
		RAISE EXCEPTION 'No posee campos claves ';
		RETURN NULL;
	END IF;
 
 
 
	RETURN v_ConstraintName;
 
END;
$$;
 
 
ALTER FUNCTION utils.psp_obtener_nombre_pk(p_schemaname character varying, p_tablename character varying) OWNER TO postgres;
 
--
-- TOC entry 515 (class 1255 OID 26075)
-- Name: psp_obtener_pies(); Type: FUNCTION; Schema: utils; Owner: postgres
--
 
CREATE FUNCTION psp_obtener_pies() RETURNS character varying
    LANGUAGE plpgsql
    AS $$DECLARE
	oFunctionBody character varying;
BEGIN
 
	oFunctionBody :='';
 
	oFunctionBody := oFunctionBody || CHR(9) || ' --  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' IF NOT FOUND THEN  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 	OPEN  registro_error FOR  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 	      SELECT o_error_codigo,o_error_mensaje  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 	      FROM acceso.psp_busca_mensaje(' || chr(39) || 'DB0002' || chr(39) || ',p_id_idioma);  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 	RETURN registro_error;  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || '  END IF;  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' --  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' OPEN  registro_error FOR   ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 	    SELECT o_error_codigo,o_error_mensaje  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 	    FROM acceso.psp_busca_mensaje(' || chr(39) || '00000'|| chr(39) || ',p_id_idioma);  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' RETURN  registro_error;  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' --  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' EXCEPTION   ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 	  WHEN others THEN   ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 		GET STACKED DIAGNOSTICS	 ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 		v_contexto	= PG_EXCEPTION_CONTEXT;  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 		v_usuario	= current_user;  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 		v_clase		= substring(SQLSTATE from 1 for 2);  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 		v_error		= substring(SQLSTATE from 3 for 3);  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 		v_mensaje	= SQLERRM;  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' -- ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 		OPEN  registro_error FOR  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 		      SELECT o_error_codigo,o_error_mensaje  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 		      FROM acceso.psp_busca_mensaje(SQLSTATE,p_id_idioma);  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' --RAISE NOTICE ' || chr(39) || 'Error en postgres %' || chr(39) || ', SQLERRM;  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' --  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 		      perform public.psp_bitacora_bd(v_usuario, v_clase, v_error, v_contexto, v_mensaje);  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' --  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' RETURN registro_error;  ' || CHR(13) || CHR(10) ;
 
	oFunctionBody := oFunctionBody || ' END; ' || CHR(36)||CHR(36) ||' LANGUAGE PLPGSQL ' ;
 
 
	return oFunctionBody;
 
END;$$;
 
 
ALTER FUNCTION utils.psp_obtener_pies() OWNER TO postgres;
 
--
-- TOC entry 526 (class 1255 OID 26076)
-- Name: psp_obtener_pies2(); Type: FUNCTION; Schema: utils; Owner: postgres
--
 
CREATE FUNCTION psp_obtener_pies2() RETURNS character varying
    LANGUAGE plpgsql
    AS $$DECLARE
	oFunctionBody character varying;
BEGIN
 
	oFunctionBody :='';
 
	oFunctionBody := oFunctionBody || CHR(9) || ' MOVE FIRST IN registro_salida; ' || CHR(13) || CHR(10) ;
 
	oFunctionBody := oFunctionBody || CHR(9) || ' --  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' IF NOT FOUND THEN  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 	OPEN  registro_error FOR  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 	      SELECT o_error_codigo,o_error_mensaje  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 	      FROM acceso.psp_busca_mensaje(' || chr(39) || 'DB0004' || chr(39) || ',p_id_idioma);  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || '  END IF;  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' --  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' OPEN  registro_error FOR   ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 	    SELECT o_error_codigo,o_error_mensaje  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 	    FROM acceso.psp_busca_mensaje(' || chr(39) || '00000'|| chr(39) || ',p_id_idioma);  ' || CHR(13) || CHR(10) ;
 
	oFunctionBody := oFunctionBody || CHR(9) || ' --  ' || CHR(13) || CHR(10) ;
 
 
	oFunctionBody := oFunctionBody || CHR(9) || ' MOVE BACKWARD ALL IN registro_salida; ' || CHR(13) || CHR(10) ;
 
	oFunctionBody := oFunctionBody || CHR(9) || ' RETURN NEXT registro_salida;  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' RETURN NEXT registro_error;  ' || CHR(13) || CHR(10) ;
 
	oFunctionBody := oFunctionBody || CHR(9) || ' RETURN;  ' || CHR(13) || CHR(10) ;
 
 
	oFunctionBody := oFunctionBody || CHR(9) || ' --  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' EXCEPTION   ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 	  WHEN others THEN   ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 		GET STACKED DIAGNOSTICS	 ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 		v_contexto	= PG_EXCEPTION_CONTEXT;  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 		v_usuario	= current_user;  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 		v_clase		= substring(SQLSTATE from 1 for 2);  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 		v_error		= substring(SQLSTATE from 3 for 3);  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 		v_mensaje	= SQLERRM;  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' -- ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 		OPEN  registro_error FOR  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 		      SELECT o_error_codigo,o_error_mensaje  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 		      FROM acceso.psp_busca_mensaje(SQLSTATE,p_id_idioma);  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' --RAISE NOTICE ' || chr(39) || 'Error en postgres %' || chr(39) || ', SQLERRM;  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' --  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' 		      perform public.psp_bitacora_bd(v_usuario, v_clase, v_error, v_contexto, v_mensaje);  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' --  ' || CHR(13) || CHR(10) ;
	oFunctionBody := oFunctionBody || CHR(9) || ' RETURN NEXT registro_error;  ' || CHR(13) || CHR(10) ;
 
	oFunctionBody := oFunctionBody || ' END; ' || CHR(36)||CHR(36) ||' LANGUAGE PLPGSQL ' ;
 
 
	return oFunctionBody;
 
END;$$;
 
 
ALTER FUNCTION utils.psp_obtener_pies2() OWNER TO postgres;
 
--
-- TOC entry 607 (class 1255 OID 24835)
-- Name: psp_obtener_pk(character varying, character varying); Type: FUNCTION; Schema: utils; Owner: postgres
--
 
CREATE FUNCTION psp_obtener_pk(p_schemaname character varying, p_tablename character varying) RETURNS character varying
    LANGUAGE plpgsql
    AS $$
DECLARE
       v_keyColumnName varchar  ;
       v_ConstraintName varchar ;
BEGIN
     SELECT constraint_name
     FROM information_schema.table_constraints
     WHERE table_schema = p_schemaName AND table_name = p_tableName AND constraint_type='PRIMARY KEY'
     INTO v_ConstraintName ;
     /**
     * At this point we have the constraint name and we will look for the column that maps to the primary key contraint name
     */
     IF NOT FOUND THEN
        RAISE EXCEPTION 'Missing Primary Key for selected schema/table '; --||p_schemaName||'.'||p_tableName) ;
     ELSE
          SELECT column_name
          FROM information_schema.constraint_column_usage
          WHERE table_schema = p_schemaName AND table_name = p_tableName AND constraint_name = v_ConstraintName
          INTO v_keyColumnName ;
     END IF ;
 
     RETURN v_keyColumnName ;
END ;
$$;
 
 
ALTER FUNCTION utils.psp_obtener_pk(p_schemaname character varying, p_tablename character varying) OWNER TO postgres;
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
1
Responder