Skip to content

Commit 4590e57

Browse files
author
Artur Zakirov
committed
Added pgv_exists(package) function
1 parent 95c2c2b commit 4590e57

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ Function | Returns | Description
145145
Function | Returns | Description
146146
-------- | ------- | -----------
147147
`pgv_exists(package text, name text)` | `bool` | Returns **true** if package and variable exists.
148+
`pgv_exists(package text)` | `bool` | Returns **true** if package exists.
148149
`pgv_remove(package text, name text)` | `void` | Removes the variable with the corresponding name. Required package and variable must exists, otherwise the error will be raised.
149150
`pgv_remove(package text)` | `void` | Removes the package and all package variables with the corresponding name. Required package must exists, otherwise the error will be raised.
150151
`pgv_free()` | `void` | Removes all packages and variables.

expected/pg_variables.out

+30
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ SELECT pgv_exists('vars', 'int1');
6464
t
6565
(1 row)
6666

67+
SELECT pgv_exists('vars2');
68+
pgv_exists
69+
------------
70+
f
71+
(1 row)
72+
73+
SELECT pgv_exists('vars');
74+
pgv_exists
75+
------------
76+
t
77+
(1 row)
78+
6779
SELECT pgv_set_int('vars', 'intNULL', NULL);
6880
pgv_set_int
6981
-------------
@@ -669,6 +681,12 @@ SELECT pgv_remove('vars', 'int1');
669681

670682
SELECT pgv_get_int('vars', 'int1');
671683
ERROR: unrecognized variable "int1"
684+
SELECT pgv_exists('vars');
685+
pgv_exists
686+
------------
687+
t
688+
(1 row)
689+
672690
SELECT pgv_remove('vars2');
673691
pgv_remove
674692
------------
@@ -677,6 +695,12 @@ SELECT pgv_remove('vars2');
677695

678696
SELECT pgv_get_jsonb('vars2', 'j1');
679697
ERROR: unrecognized package "vars2"
698+
SELECT pgv_exists('vars2');
699+
pgv_exists
700+
------------
701+
f
702+
(1 row)
703+
680704
SELECT * FROM pgv_list() order by package, name;
681705
package | name
682706
---------+----------
@@ -707,6 +731,12 @@ SELECT pgv_free();
707731

708732
(1 row)
709733

734+
SELECT pgv_exists('vars');
735+
pgv_exists
736+
------------
737+
f
738+
(1 row)
739+
710740
SELECT * FROM pgv_list() order by package, name;
711741
package | name
712742
---------+------

pg_variables--1.0.sql

+5
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ RETURNS bool
113113
AS 'MODULE_PATHNAME', 'variable_exists'
114114
LANGUAGE C VOLATILE;
115115

116+
CREATE FUNCTION pgv_exists(package text)
117+
RETURNS bool
118+
AS 'MODULE_PATHNAME', 'package_exists'
119+
LANGUAGE C VOLATILE;
120+
116121
CREATE FUNCTION pgv_remove(package text, name text)
117122
RETURNS void
118123
AS 'MODULE_PATHNAME', 'remove_variable'

pg_variables.c

+23
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ PG_FUNCTION_INFO_V1(variable_select_by_values);
5252

5353
/* Functions to work with packages */
5454
PG_FUNCTION_INFO_V1(variable_exists);
55+
PG_FUNCTION_INFO_V1(package_exists);
5556
PG_FUNCTION_INFO_V1(remove_variable);
5657
PG_FUNCTION_INFO_V1(remove_package);
5758
PG_FUNCTION_INFO_V1(remove_packages);
@@ -956,6 +957,28 @@ variable_exists(PG_FUNCTION_ARGS)
956957
PG_RETURN_BOOL(found);
957958
}
958959

960+
/*
961+
* Check if package exists.
962+
*/
963+
Datum
964+
package_exists(PG_FUNCTION_ARGS)
965+
{
966+
text *package_name;
967+
bool res;
968+
969+
if (PG_ARGISNULL(0))
970+
ereport(ERROR,
971+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
972+
errmsg("package name can not be NULL")));
973+
974+
package_name = PG_GETARG_TEXT_PP(0);
975+
976+
res = getPackageByName(package_name, false, false) != NULL;
977+
978+
PG_FREE_IF_COPY(package_name, 0);
979+
PG_RETURN_BOOL(res);
980+
}
981+
959982
/*
960983
* Remove variable from package by name.
961984
*/

sql/pg_variables.sql

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ SELECT pgv_get_int('vars', 'int3');
1616
SELECT pgv_get_int('vars', 'int3', false);
1717
SELECT pgv_exists('vars', 'int3');
1818
SELECT pgv_exists('vars', 'int1');
19+
SELECT pgv_exists('vars2');
20+
SELECT pgv_exists('vars');
1921

2022
SELECT pgv_set_int('vars', 'intNULL', NULL);
2123
SELECT pgv_get_int('vars', 'intNULL');
@@ -176,12 +178,15 @@ SELECT * FROM pgv_list() order by package, name;
176178
SELECT pgv_remove('vars', 'int3');
177179
SELECT pgv_remove('vars', 'int1');
178180
SELECT pgv_get_int('vars', 'int1');
181+
SELECT pgv_exists('vars');
179182

180183
SELECT pgv_remove('vars2');
181184
SELECT pgv_get_jsonb('vars2', 'j1');
185+
SELECT pgv_exists('vars2');
182186

183187
SELECT * FROM pgv_list() order by package, name;
184188

185189
SELECT pgv_free();
190+
SELECT pgv_exists('vars');
186191

187192
SELECT * FROM pgv_list() order by package, name;

0 commit comments

Comments
 (0)