This is an automated email from the ASF dual-hosted git repository.

jgemignani pushed a commit to branch PG14
in repository https://gitbox.apache.org/repos/asf/age.git


The following commit(s) were added to refs/heads/PG14 by this push:
     new 7fbee760 Add graph_exists function (#1958) (#1967)
7fbee760 is described below

commit 7fbee76085c1585507327854b40725aad4df66e3
Author: Rafsun Masud <[email protected]>
AuthorDate: Tue Jul 9 11:22:39 2024 -0700

    Add graph_exists function (#1958) (#1967)
    
    This function returns if a graph exists.
---
 age--1.5.0--y.y.y.sql                 |  4 +++
 regress/expected/catalog.out          | 64 +++++++++++++++++++++++++++++++++++
 regress/sql/catalog.sql               | 29 ++++++++++++++--
 sql/age_agtype.sql                    |  4 +++
 src/backend/commands/graph_commands.c | 29 +++++++++++++++-
 5 files changed, 127 insertions(+), 3 deletions(-)

diff --git a/age--1.5.0--y.y.y.sql b/age--1.5.0--y.y.y.sql
index ef5bf0a6..1620dfb4 100644
--- a/age--1.5.0--y.y.y.sql
+++ b/age--1.5.0--y.y.y.sql
@@ -125,3 +125,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';
diff --git a/regress/expected/catalog.out b/regress/expected/catalog.out
index bc7b9434..d06a0ce6 100644
--- a/regress/expected/catalog.out
+++ b/regress/expected/catalog.out
@@ -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
diff --git a/regress/sql/catalog.sql b/regress/sql/catalog.sql
index 96c63e47..85fc4e8a 100644
--- a/regress/sql/catalog.sql
+++ b/regress/sql/catalog.sql
@@ -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);
-
-
diff --git a/sql/age_agtype.sql b/sql/age_agtype.sql
index 944508dd..a6932cb1 100644
--- a/sql/age_agtype.sql
+++ b/sql/age_agtype.sql
@@ -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';
diff --git a/src/backend/commands/graph_commands.c 
b/src/backend/commands/graph_commands.c
index a558d298..3d51e67a 100644
--- a/src/backend/commands/graph_commands.c
+++ b/src/backend/commands/graph_commands.c
@@ -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)

Reply via email to