Changeset: 58db8a2e4921 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=58db8a2e4921
Removed Files:
        sql/test/Tests/comment-on-routine.sql
        sql/test/Tests/comment-on-routine.stable.err
        sql/test/Tests/comment-on-routine.stable.out
Modified Files:
        clients/mapiclient/dump.c
        sql/scripts/97_comments.sql
        sql/test/Tests/comment-on.sql
        sql/test/Tests/comment-on.stable.err
        sql/test/Tests/comment-on.stable.out
Branch: comment-on
Log Message:

Describe and dump COMMENT ON FUNCTION


diffs (truncated from 541 to 300 lines):

diff --git a/clients/mapiclient/dump.c b/clients/mapiclient/dump.c
--- a/clients/mapiclient/dump.c
+++ b/clients/mapiclient/dump.c
@@ -1245,6 +1245,94 @@ dump_table(Mapi mid, char *schema, char 
        return rc;
 }
 
+static int
+dump_function_comments(Mapi mid, stream *toConsole, const char *schema, const 
char *fname, char wantSystem)
+{
+       size_t len = 400 + (schema ? strlen(schema) : 0) + (fname ? 
strlen(fname) : 0);
+       char *query = malloc(len);
+       char *q = query;
+       char *end_q = q + len;
+       char *where = "WHERE";
+       MapiHdl hdl = NULL;
+       int hashge;
+       
+       if (!query)
+               return 1;
+
+       q += snprintf(q, end_q - q, "SELECT category, schema, name, type, 
type_digits, type_scale, remark ");
+       q += snprintf(q, end_q - q, "FROM sys.commented_function_signatures");
+       if (!wantSystem) {
+               q += snprintf(q, end_q - q, " %s system = FALSE", where);
+               where = "AND";
+       }
+       if (schema) {
+               q += snprintf(q, end_q - q, " %s schema = '%s'", where, schema);
+               where = "AND";
+       }
+       if (fname) {
+               q += snprintf(q, end_q - q, " %s fname = '%s'", where, fname);
+               where = "AND";
+       }
+       q += snprintf(q, end_q - q, " ORDER BY line;");
+       if (q >= end_q - 1)
+               goto bailout;
+
+       hashge = has_hugeint(mid);
+       hdl = mapi_query(mid, query);
+       if (hdl == NULL || mapi_error(mid))
+               goto bailout;
+       while (!mnstr_errnr(toConsole) && mapi_fetch_row(hdl) != 0) {
+               int i = 0;
+               char *category = mapi_fetch_field(hdl, i++);
+               char *sname = mapi_fetch_field(hdl, i++);
+               char *name = mapi_fetch_field(hdl, i++);
+               char *type = mapi_fetch_field(hdl, i++);
+               char *type_digits = mapi_fetch_field(hdl, i++);
+               char *type_scale = mapi_fetch_field(hdl, i++);
+               char *remark = mapi_fetch_field(hdl, i++);
+
+               if (name) {
+                       mnstr_printf(toConsole, "COMMENT ON %s ", category);
+                       quoted_print(toConsole, sname, 0);
+                       mnstr_printf(toConsole, ".");
+                       quoted_print(toConsole, name, 0);
+                       mnstr_printf(toConsole, "(");
+               } else {
+                       mnstr_printf(toConsole, ", ");
+               }
+
+               if (type) {
+                       dump_type(NULL, toConsole, type, type_digits, 
type_scale, hashge);
+               }
+
+               if (remark) {
+                       mnstr_printf(toConsole, ") IS ");
+                       quoted_print(toConsole, remark, 1);
+                       mnstr_printf(toConsole, ";\n");
+               }
+       }
+       if (mapi_error(mid))
+               goto bailout;
+
+       free(query);
+       mapi_close_handle(hdl);
+       return 0;
+
+       bailout:
+       if (query)
+               free(query);
+       if (hdl) {
+               if (mapi_result_error(hdl))
+                       mapi_explain_result(hdl, stderr);
+               else
+                       mapi_explain_query(hdl, stderr);
+               mapi_close_handle(hdl);
+       } else
+               mapi_explain(mid, stderr);
+
+       return 1;
+}
+
 int
 dump_functions(Mapi mid, stream *toConsole, const char *fname)
 {
@@ -1277,7 +1365,7 @@ dump_functions(Mapi mid, stream *toConso
                return 1;
        }
 
-       dumpSystem = sname && fname;
+       dumpSystem = !!fname;
 
        l = sizeof(functions) + (sname ? strlen(sname) : 0) + 100;
        q = malloc(l);
@@ -1301,6 +1389,12 @@ dump_functions(Mapi mid, stream *toConso
        }
        if (mapi_error(mid))
                goto bailout;
+
+       if (dump_function_comments(mid, toConsole, sname, fname, fname != NULL) 
!= 0)
+               goto bailout;
+
+       if (mapi_error(mid))
+               goto bailout;
        if (sname)
                free(sname);
        mapi_close_handle(hdl);
@@ -1796,6 +1890,9 @@ dump_database(Mapi mid, stream *toConsol
        mapi_close_handle(hdl);
        hdl = NULL;
 
+       if (dump_function_comments(mid, toConsole, NULL, NULL, 0) != 0)
+               goto bailout;
+
        if (curschema) {
                if (strcmp(sname ? sname : "sys", curschema) != 0) {
                        mnstr_printf(toConsole, "SET SCHEMA \"%s\";\n",
diff --git a/sql/scripts/97_comments.sql b/sql/scripts/97_comments.sql
--- a/sql/scripts/97_comments.sql
+++ b/sql/scripts/97_comments.sql
@@ -22,7 +22,6 @@ END;
 
 
 
--- Temporary home for this function.
 -- We have to create table systemfunctions first, because describe_all_objects 
uses it
 -- to recognize system functions.  For some reason, the functions table does 
not have a
 -- 'system' column.
@@ -68,7 +67,7 @@ BEGIN
                    SELECT  schema_id AS sid,
                            id,
                            name,
-                           (id IN (SELECT function_id FROM 
sys.systemfunctions)) AS system,
+                           EXISTS (SELECT function_id FROM sys.systemfunctions 
WHERE function_id = id) AS system,
                            8 AS ntype,
                            'FUNCTION' AS type
                    FROM sys.functions
@@ -106,3 +105,45 @@ BEGIN
            ORDER BY system, name, fullname, ntype
        );
 END;
+
+CREATE VIEW commented_function_signatures AS
+WITH
+params AS (
+        SELECT * FROM sys.args WHERE inout = 1
+),
+commented_function_params AS (
+        SELECT  f.id AS fid,
+                f.name AS fname,
+                s.name AS schema,
+                f.type AS ftype,
+                c.remark AS remark,
+                p.number AS n,
+                p.name AS aname,
+                p.type AS type,
+                p.type_digits AS type_digits,
+                p.type_scale AS type_scale,
+                RANK() OVER (PARTITION BY f.id ORDER BY number ASC) AS 
asc_rank,
+                RANK() OVER (PARTITION BY f.id ORDER BY number DESC) AS 
desc_rank
+        FROM    sys.functions f
+                JOIN sys.schemas s ON f.schema_id = s.id
+                JOIN sys.comments c ON f.id = c.id
+                LEFT OUTER JOIN params p ON f.id = p.func_id
+)
+SELECT  
+        schema,
+        fname,
+        CASE ftype
+                WHEN 1 THEN 'FUNCTION'
+                WHEN 2 THEN 'PROCEDURE'
+                WHEN 3 THEN 'AGGREGATE'
+                WHEN 4 THEN 'FILTER FUNCTION'
+                WHEN 7 THEN 'LOADER'
+                ELSE 'ROUTINE'
+        END AS category,
+        EXISTS (SELECT function_id FROM sys.systemfunctions WHERE fid = 
function_id) AS system,
+        CASE WHEN asc_rank = 1 THEN fname ELSE NULL END AS name,
+        CASE WHEN desc_rank = 1 THEN remark ELSE NULL END AS remark,
+        type, type_digits, type_scale,
+        ROW_NUMBER() OVER (ORDER BY fid, n) AS line
+FROM commented_function_params
+ORDER BY line;
diff --git a/sql/test/Tests/comment-on-routine.sql 
b/sql/test/Tests/comment-on-routine.sql
deleted file mode 100644
--- a/sql/test/Tests/comment-on-routine.sql
+++ /dev/null
@@ -1,69 +0,0 @@
-DROP SCHEMA IF EXISTS sch;
-CREATE SCHEMA sch;
-SET SCHEMA sch;
-
-CREATE TABLE origs AS SELECT id FROM sys.comments;
-
-CREATE FUNCTION all_kinds_of_object()
-RETURNS TABLE (id INTEGER, name VARCHAR(80), type VARCHAR(30))
-RETURN TABLE (
-        SELECT id, name, table_type_name
-        FROM sys.tables, sys.table_types
-        WHERE type = table_type_id
-        UNION ALL
-        SELECT id, name, 'SCHEMA'
-        FROM sys.schemas
-        UNION ALL
-        SELECT c.id, t.name || '.' || c.name, 'COLUMN'
-        FROM sys.columns AS c, sys.tables AS t
-        WHERE c.table_id = t.id
-        UNION ALL
-        SELECT id, name, 'INDEX'
-        FROM sys.idxs
-        UNION ALL
-        SELECT id, name, 'SEQUENCE'
-        FROM sys.sequences
-        UNION ALL
-        SELECT id, name, function_type_name
-        FROM sys.functions, sys.function_types
-        WHERE type = function_type_id
-);
-
-CREATE FUNCTION new_comments()
-RETURNS TABLE (name VARCHAR(80), source VARCHAR(30), remark CLOB)
-RETURN TABLE (
-        SELECT o.name, o.type, c.remark
-        FROM sys.comments AS c LEFT JOIN all_kinds_of_object() AS o ON c.id = 
o.id
-        WHERE c.id NOT IN (SELECT id FROM origs)
-);
-
-------------------------------------------------------------------------
-
-CREATE FUNCTION f() RETURNS INT RETURN 42;
-CREATE FUNCTION f(i INT) RETURNS INT RETURN 2 * i;
-CREATE FUNCTION f(i INT, j INT) RETURNS INT RETURN i + j;
-CREATE FUNCTION g(i INT) RETURNS TABLE (j INT) RETURN SELECT i;
-
-COMMENT ON FUNCTION f() IS 'function with no parameters';
-COMMENT ON FUNCTION f(INT) IS 'function with one parameter';
-COMMENT ON FUNCTION f(INT, INT) IS 'function with two parameters';
-COMMENT ON FUNCTION g IS 'table returning function';
-COMMENT ON PROCEDURE sys.comment_on(int, varchar(65000)) IS 'sys comment_on';
-SELECT * FROM new_comments();
-
--- drop one, check if the right one disappears
-COMMENT ON FUNCTION f(INT) is NULL;
-SELECT * FROM new_comments();
-
--- if there is no ambiguity we can leave out the parameter list
-COMMENT ON PROCEDURE sys.comment_on IS NULL;
-SELECT * FROM new_comments();
-
--- if there is ambiguity we can't
-COMMENT ON FUNCTION f IS 'ambiguous';
-
--- dropping the functions cascades to the comments
-DROP FUNCTION f(INT);
-DROP FUNCTION f(INT, INT);
-DROP FUNCTION f();
-SELECT * FROM new_comments();
diff --git a/sql/test/Tests/comment-on-routine.stable.err 
b/sql/test/Tests/comment-on-routine.stable.err
deleted file mode 100644
--- a/sql/test/Tests/comment-on-routine.stable.err
+++ /dev/null
@@ -1,39 +0,0 @@
-stderr of test 'comment-on-routine` in directory 'sql/test` itself:
-
-
-# 14:09:09 >  
-# 14:09:09 >  "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set" 
"mapi_open=true" "--set" "mapi_port=31366" "--set" 
"mapi_usock=/var/tmp/mtest-70902/.s.monetdb.31366" "--set" "monet_prompt=" 
"--forcemito" 
"--dbpath=/Users/joeri/monets/default/var/MonetDB/mTests_sql_test" "--set" 
"embedded_r=yes"
-# 14:09:09 >  
-
-# builtin opt  gdk_dbpath = 
/Users/joeri/monets/default/var/monetdb5/dbfarm/demo
-# builtin opt  gdk_debug = 0
-# builtin opt  gdk_vmtrim = no
-# builtin opt  monet_prompt = >
-# builtin opt  monet_daemon = no
-# builtin opt  mapi_port = 50000
-# builtin opt  mapi_open = false
-# builtin opt  mapi_autosense = false
-# builtin opt  sql_optimizer = default_pipe
-# builtin opt  sql_debug = 0
-# cmdline opt  gdk_nr_threads = 0
-# cmdline opt  mapi_open = true
-# cmdline opt  mapi_port = 31366
-# cmdline opt  mapi_usock = /var/tmp/mtest-70902/.s.monetdb.31366
-# cmdline opt  monet_prompt = 
-# cmdline opt  gdk_dbpath = 
/Users/joeri/monets/default/var/MonetDB/mTests_sql_test
-# cmdline opt  embedded_r = yes
-# cmdline opt  gdk_debug = 536870922
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to