Changeset: 92053d1bffe0 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=92053d1bffe0
Modified Files:
        clients/mapiclient/dump.c
        clients/mapiclient/dumpcomment.c
        clients/mapiclient/dumpcomment.h
        clients/mapiclient/mclient.c
        clients/mapiclient/msqldump.c
        clients/mapiclient/msqldump.h
        sql/test/Tests/comment-on.stable.out
Branch: comment-on
Log Message:

Simplify code

Always create comments->buf instead of only on demand.
Eliminate sname parameter of dump_functions because it isn't used.


diffs (229 lines):

diff --git a/clients/mapiclient/dump.c b/clients/mapiclient/dump.c
--- a/clients/mapiclient/dump.c
+++ b/clients/mapiclient/dump.c
@@ -1246,7 +1246,7 @@ dump_table(Mapi mid, char *schema, char 
 }
 
 int
-dump_functions(Mapi mid, stream *toConsole, const char *sname, const char 
*fname)
+dump_functions(Mapi mid, stream *toConsole, const char *fname)
 {
        const char functions[] =
                "SELECT f.func "
@@ -1261,22 +1261,20 @@ dump_functions(Mapi mid, stream *toConso
        char *q;
        size_t l;
        char dumpSystem;
-       char *schema = NULL;
-
-       if (sname == NULL) {
-               if (fname == NULL) {
-                       schema = NULL;
-               } else if ((schema = strchr(fname, '.')) != NULL) {
-                       size_t len = schema - fname;
+       char *sname = NULL;
+       char *dot = NULL; /* location of dot in fname */
 
-                       schema = malloc(len + 1);
-                       strncpy(schema, fname, len);
-                       schema[len] = 0;
-                       fname += len + 1;
-               } else if ((schema = get_schema(mid)) == NULL) {
-                       return 1;
-               }
-               sname = schema;
+       if (fname == NULL) {
+               sname = NULL;
+       } else if ((dot = strchr(fname, '.')) != NULL) {
+               size_t len = dot - fname;
+
+               sname = malloc(len + 1);
+               strncpy(sname, fname, len);
+               sname[len] = 0;
+               fname += len + 1;
+       } else if ((sname = get_schema(mid)) == NULL) {
+               return 1;
        }
 
        dumpSystem = sname && fname;
@@ -1303,14 +1301,14 @@ dump_functions(Mapi mid, stream *toConso
        }
        if (mapi_error(mid))
                goto bailout;
-       if (schema)
-               free(schema);
+       if (sname)
+               free(sname);
        mapi_close_handle(hdl);
        return mnstr_errnr(toConsole) != 0;
 
   bailout:
-       if (schema)
-               free(schema);
+       if (sname)
+               free(sname);
        if (hdl) {
                if (mapi_result_error(hdl))
                        mapi_explain_result(hdl, stderr);
diff --git a/clients/mapiclient/dumpcomment.c b/clients/mapiclient/dumpcomment.c
--- a/clients/mapiclient/dumpcomment.c
+++ b/clients/mapiclient/dumpcomment.c
@@ -54,18 +54,39 @@ struct comment_buffer {
 comment_buffer*
 comment_buffer_create(void)
 {
+        buffer *buf;
+        stream *s;
         comment_buffer *comments;
 
+        buf = buffer_create(4000);
+        if (!buf)
+        return NULL;
+
+        s = buffer_wastream(buf, "comments_buffer");
+        if (s == NULL) {
+                buffer_destroy(buf);
+                return NULL;
+        }
+
         comments = malloc(sizeof(*comments));
-        if (comments == NULL)
+        if (comments == NULL) {
+                mnstr_destroy(s);
+                buffer_destroy(buf);
                 return NULL;
-        
-        comments->buf = NULL;
-        comments->append = NULL;
+        }
+
+        comments->buf = buf;
+        comments->append = s;
 
         return comments;
 }
 
+stream *
+comment_appender(comment_buffer *comments)
+{
+        return comments->append;
+}
+
 int
 append_comment(
         comment_buffer *comments,
@@ -81,23 +102,6 @@ append_comment(
         if (!remark)
                 return 0;
 
-        assert((comments->buf == NULL) == (comments->append == NULL));
-        if (comments->buf == NULL) {
-                buffer *buf;
-                stream *s;
-
-                buf  = buffer_create(4000);
-                if (buf == NULL)
-                        return 1;
-                s = buffer_wastream(buf, "comments_buffer");
-                if (s == NULL) {
-                        buffer_destroy(buf);
-                        return 1;
-                }
-                comments->buf = buf;
-                comments->append = s;
-        }
-
         mnstr_printf(comments->append, "COMMENT ON %s ", obj_type);
         if (schema_name) {
                 mnstr_printf(comments->append, "%s", sep);
@@ -122,7 +126,7 @@ append_comment(
 
         return 0;
 }
-  
+
 int
 write_comment_buffer(stream *out, comment_buffer *comments)
 {
@@ -144,11 +148,7 @@ write_comment_buffer(stream *out, commen
 void
 comment_buffer_destroy(comment_buffer *comments)
 {
-        if (comments == NULL) 
-                return;
-        if (comments->append)
-                mnstr_destroy(comments->append);
-        if (comments->buf)
-                buffer_destroy(comments->buf);
+        mnstr_destroy(comments->append);
+        buffer_destroy(comments->buf);
         free(comments);
 }
diff --git a/clients/mapiclient/dumpcomment.h b/clients/mapiclient/dumpcomment.h
--- a/clients/mapiclient/dumpcomment.h
+++ b/clients/mapiclient/dumpcomment.h
@@ -12,6 +12,8 @@ typedef struct comment_buffer comment_bu
 
 comment_buffer *comment_buffer_create(void);
 
+stream *comment_appender(comment_buffer *comments);
+
 int append_comment(
         comment_buffer *comments,
         const char *obj_type,
diff --git a/clients/mapiclient/mclient.c b/clients/mapiclient/mclient.c
--- a/clients/mapiclient/mclient.c
+++ b/clients/mapiclient/mclient.c
@@ -2523,7 +2523,7 @@ doFile(Mapi mid, stream *fp, int useinse
                                                if (x & MD_SEQ)
                                                        describe_sequence(mid, 
NULL, line, toConsole);
                                                if (x & MD_FUNC)
-                                                       dump_functions(mid, 
toConsole, NULL, line);
+                                                       dump_functions(mid, 
toConsole, line);
                                                if (x & MD_SCHEMA)
                                                        describe_schema(mid, 
line, toConsole);
 #ifdef HAVE_POPEN
diff --git a/clients/mapiclient/msqldump.c b/clients/mapiclient/msqldump.c
--- a/clients/mapiclient/msqldump.c
+++ b/clients/mapiclient/msqldump.c
@@ -219,7 +219,7 @@ main(int argc, char **argv)
                dump_version(mid, out, "--");
        }
        if (functions)
-               c = dump_functions(mid, out, NULL, NULL);
+               c = dump_functions(mid, out, NULL);
        else if (table)
                c = dump_table(mid, NULL, table, out, describe, 1, useinserts);
        else
diff --git a/clients/mapiclient/msqldump.h b/clients/mapiclient/msqldump.h
--- a/clients/mapiclient/msqldump.h
+++ b/clients/mapiclient/msqldump.h
@@ -10,6 +10,6 @@ extern int describe_table(Mapi mid, char
 extern int describe_sequence(Mapi mid, char *schema, char *sname, stream 
*toConsole);
 extern int describe_schema(Mapi mid, char *sname, stream *toConsole);
 extern int dump_table(Mapi mid, char *schema, char *tname, stream *toConsole, 
int describe, int foreign, const char useInserts);
-extern int dump_functions(Mapi mid, stream *toConsole, const char *sname, 
const char *fname);
+extern int dump_functions(Mapi mid, stream *toConsole, const char *fname);
 extern int dump_database(Mapi mid, stream *toConsole, int describe, const char 
useInserts);
 extern void dump_version(Mapi mid, stream *toConsole, const char *prefix);
diff --git a/sql/test/Tests/comment-on.stable.out 
b/sql/test/Tests/comment-on.stable.out
--- a/sql/test/Tests/comment-on.stable.out
+++ b/sql/test/Tests/comment-on.stable.out
@@ -81,7 +81,7 @@ COMMENT ON SCHEMA "foo" IS 'foo bar';
 SCHEMA  foo
 CREATE SCHEMA "foo" AUTHORIZATION "monetdb";
 #CREATE SCHEMA "space separated";
-SCHEMA  foo
+SCHEMA  foo              'one final comment'
 SCHEMA  space separated  'space separated'
 CREATE SCHEMA "space separated" AUTHORIZATION "monetdb";
 COMMENT ON SCHEMA "space separated" IS 'space separated';
@@ -139,7 +139,7 @@ CREATE SEQUENCE "foo"."counter" START WI
 COMMENT ON SEQUENCE "foo"."counter" IS 'counting';
 #SET SCHEMA sys;
 #SET SCHEMA foo;
-SEQUENCE  foo.counter
+SEQUENCE  foo.counter  'still counting'
 
 # 15:45:23 >  
 # 15:45:23 >  "Done."
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to