Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add graph_exists function (#1958) #1965

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions age--1.5.0--y.y.y.sql
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,7 @@ CREATE FUNCTION ag_catalog.age_graph_stats(agtype)
PARALLEL SAFE
AS 'MODULE_PATHNAME';

CREATE FUNCTION ag_catalog.graph_exists(graph_name name)
RETURNS agtype
LANGUAGE c
AS 'MODULE_PATHNAME', 'age_graph_exists';
64 changes: 64 additions & 0 deletions regress/expected/catalog.out
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,70 @@ SELECT create_vlabel(NULL, NULL);
ERROR: graph name must not be NULL
SELECT create_elabel(NULL, NULL);
ERROR: graph name must not be NULL
-- age_graph_exists()
CREATE FUNCTION raise_notice(graph_name TEXT)
RETURNS void AS $$
DECLARE
res BOOLEAN;
BEGIN
-- this tests whether graph_exists works with IF-ELSE.
SELECT graph_exists('graph1') INTO res;
IF res THEN
RAISE NOTICE 'graph exists';
ELSE
RAISE NOTICE 'graph does not exist';
END IF;
END $$ LANGUAGE plpgsql;
SELECT graph_exists('graph1');
graph_exists
--------------
false
(1 row)

SELECT create_graph('graph1');
NOTICE: graph "graph1" has been created
create_graph
--------------

(1 row)

SELECT graph_exists('graph1');
graph_exists
--------------
true
(1 row)

SELECT raise_notice('graph1');
NOTICE: graph exists
raise_notice
--------------

(1 row)

SELECT drop_graph('graph1', true);
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to table graph1._ag_label_vertex
drop cascades to table graph1._ag_label_edge
NOTICE: graph "graph1" has been dropped
drop_graph
------------

(1 row)

SELECT graph_exists('graph1');
graph_exists
--------------
false
(1 row)

SELECT raise_notice('graph1');
NOTICE: graph does not exist
raise_notice
--------------

(1 row)

DROP FUNCTION raise_notice(TEXT);
-- dropping the graph
SELECT drop_graph('graph', true);
NOTICE: drop cascades to 2 other objects
Expand Down
29 changes: 27 additions & 2 deletions regress/sql/catalog.sql
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,32 @@ SELECT create_elabel(NULL, 'r');
SELECT create_vlabel(NULL, NULL);
SELECT create_elabel(NULL, NULL);

-- age_graph_exists()
CREATE FUNCTION raise_notice(graph_name TEXT)
RETURNS void AS $$
DECLARE
res BOOLEAN;
BEGIN
-- this tests whether graph_exists works with IF-ELSE.
SELECT graph_exists('graph1') INTO res;
IF res THEN
RAISE NOTICE 'graph exists';
ELSE
RAISE NOTICE 'graph does not exist';
END IF;
END $$ LANGUAGE plpgsql;

SELECT graph_exists('graph1');

SELECT create_graph('graph1');
SELECT graph_exists('graph1');
SELECT raise_notice('graph1');

SELECT drop_graph('graph1', true);
SELECT graph_exists('graph1');
SELECT raise_notice('graph1');

DROP FUNCTION raise_notice(TEXT);

-- dropping the graph
SELECT drop_graph('graph', true);


4 changes: 4 additions & 0 deletions sql/age_agtype.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1060,3 +1060,7 @@ CREATE OPERATOR CLASS graphid_ops_hash
OPERATOR 1 =,
FUNCTION 1 ag_catalog.graphid_hash_cmp(graphid);

CREATE FUNCTION ag_catalog.graph_exists(graph_name name)
RETURNS agtype
LANGUAGE c
AS 'MODULE_PATHNAME', 'age_graph_exists';
29 changes: 28 additions & 1 deletion src/backend/commands/graph_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,34 @@ Datum create_graph(PG_FUNCTION_ARGS)
(errmsg("graph \"%s\" has been created", NameStr(*graph_name))));

/* according to postgres specification of c-language functions if function returns void this is the syntax */
PG_RETURN_VOID();
PG_RETURN_VOID();
}

PG_FUNCTION_INFO_V1(age_graph_exists);

Datum age_graph_exists(PG_FUNCTION_ARGS)
{
Name graph_name;
char *graph_name_str;

/* if no argument is passed with the function, graph name cannot be null */
if (PG_ARGISNULL(0))
{
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("graph name can not be NULL")));
}

graph_name = PG_GETARG_NAME(0);
graph_name_str = NameStr(*graph_name);

if (graph_exists(graph_name_str))
{
return boolean_to_agtype(true);
}
else
{
return boolean_to_agtype(false);
}
}

static Oid create_schema_for_graph(const Name graph_name)
Expand Down
Loading