From 0cfc87bd32822e5ba22d8c2b1a26fa60f63ad7fa Mon Sep 17 00:00:00 2001
From: rudb <office@database.ru>
Date: Fri, 19 Jun 2026 15:11:53 +0000
Subject: [PATCH] СУБД RuDB - релиз 2

---
 src/pl/plpgsql/src/pl_comp.c |   77 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 77 insertions(+), 0 deletions(-)

diff --git a/src/pl/plpgsql/src/pl_comp.c b/src/pl/plpgsql/src/pl_comp.c
index 09707d0..c1b810b 100644
--- a/src/pl/plpgsql/src/pl_comp.c
+++ b/src/pl/plpgsql/src/pl_comp.c
@@ -21,11 +21,14 @@
 #include "catalog/namespace.h"
 #include "catalog/pg_proc.h"
 #include "catalog/pg_type.h"
+#include "common/base64.h"
 #include "funcapi.h"
 #include "nodes/makefuncs.h"
 #include "parser/parse_node.h"
 #include "plpgsql.h"
+#include "utils/dbms_warning.h"
 #include "utils/builtins.h"
+#include "utils/coder.h"
 #include "utils/fmgroids.h"
 #include "utils/guc.h"
 #include "utils/lsyscache.h"
@@ -77,6 +80,7 @@
 									 const CachedFunctionHashKey *hashkey,
 									 CachedFunction *cfunc,
 									 bool forValidator);
+static void plpgsql_apply_dbms_warning_mode(PLpgSQL_function *function);
 static void plpgsql_compile_error_callback(void *arg);
 static void add_parameter_name(PLpgSQL_nsitem_type itemtype, int itemno, const char *name);
 static void add_dummy_return(PLpgSQL_function *function);
@@ -141,6 +145,27 @@
 	yyscan_t	yyscanner;
 };
 
+static char*
+unwrap_source(char *wrapped_source, size_t len)
+{
+	char *result = NULL;
+	size_t decoded_source_len = pg_b64_dec_len(len);
+	char* decoded_source = palloc(decoded_source_len + 1);
+
+	int ret = pg_b64_decode(wrapped_source, len, (uint8_t*)decoded_source,
+							decoded_source_len);
+	if (ret == -1)
+	{
+		goto finish;
+	}
+
+	result = pg_kuzn_decode(decoded_source, decoded_source_len);
+
+finish:
+	pfree(decoded_source);
+	return result;
+}
+
 /*
  * This is the slow part of plpgsql_compile().
  *
@@ -201,6 +226,30 @@
 	 */
 	prosrcdatum = SysCacheGetAttrNotNull(PROCOID, procTup, Anum_pg_proc_prosrc);
 	proc_source = TextDatumGetCString(prosrcdatum);
+
+	{
+		char *wrapcomment_begin = strstr(proc_source, "/* WRAPPEDpgSQL ");
+		if (wrapcomment_begin != NULL)
+		{
+			char *unwrapped_source;
+			/* 16 is the len of WRAPPEDpgSQL prefix */
+			char *wrapped_code_start = wrapcomment_begin + 16;
+			char *wrapped_code_end = strstr(wrapped_code_start, " */");
+			if (wrapped_code_end != NULL)
+			{
+				size_t wrapped_code_len = wrapped_code_end - wrapped_code_start;
+				char *wrapped_code = palloc(wrapped_code_len + 1);
+				memcpy(wrapped_code, wrapped_code_start, wrapped_code_len);
+				wrapped_code[wrapped_code_len] = '\0';
+				unwrapped_source = unwrap_source(wrapped_code + 1,
+												 wrapped_code_len - 10);
+				pfree(wrapped_code);
+				pfree(proc_source);
+				proc_source = unwrapped_source;
+			}
+		}
+	}
+
 	scanner = plpgsql_scanner_init(proc_source);
 
 	plpgsql_error_funcname = pstrdup(NameStr(procStruct->proname));
@@ -248,6 +297,8 @@
 	/* only promote extra warnings and errors at CREATE FUNCTION time */
 	function->extra_warnings = forValidator ? plpgsql_extra_warnings : 0;
 	function->extra_errors = forValidator ? plpgsql_extra_errors : 0;
+	if (forValidator)
+		plpgsql_apply_dbms_warning_mode(function);
 
 	if (is_dml_trigger)
 		function->fn_is_trigger = PLPGSQL_DML_TRIGGER;
@@ -720,11 +771,37 @@
 	plpgsql_error_funcname = NULL;
 
 	plpgsql_check_syntax = false;
+	plpgsql_curr_compile = NULL;
 
 	MemoryContextSwitchTo(plpgsql_compile_tmp_cxt);
 	plpgsql_compile_tmp_cxt = NULL;
 }
 
+static void
+plpgsql_apply_dbms_warning_mode(PLpgSQL_function *function)
+{
+	bool is_explicit = false;
+	const DBMSWarningCompilerMode mode = dbms_warning_get_compiler_warning_mode(&is_explicit);
+
+	if (!is_explicit)
+		return;
+
+	function->extra_warnings &= ~PLPGSQL_XCHECK_SHADOWVAR;
+	function->extra_errors &= ~PLPGSQL_XCHECK_SHADOWVAR;
+
+	switch (mode)
+	{
+		case DBMS_WARNING_COMPILER_MODE_DISABLE:
+			break;
+		case DBMS_WARNING_COMPILER_MODE_ENABLE:
+			function->extra_warnings |= PLPGSQL_XCHECK_SHADOWVAR;
+			break;
+		case DBMS_WARNING_COMPILER_MODE_ERROR:
+			function->extra_errors |= PLPGSQL_XCHECK_SHADOWVAR;
+			break;
+	}
+}
+
 /* ----------
  * plpgsql_compile_inline	Make an execution tree for an anonymous code block.
  *

--
Gitblit v1.10.0