Changeset: 34d585f98f7b for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=34d585f98f7b
Modified Files:
clients/mapiclient/dump.c
sql/test/Tests/comment-dump.stable.out
sql/test/Tests/comment-on.stable.out
Branch: default
Log Message:
Restructured comment dumping.
No need to save comments for later: we have a database to save stuff.
Also fixed a bug dumping comments on views.
diffs (truncated from 595 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
@@ -48,129 +48,23 @@ quoted_print(stream *f, const char *s, b
mnstr_write(f, singleq ? "'" : "\"", 1, 1);
}
-/*Used to buffer comment values while we're in the process of
- * dumping something else. For example, while we're dumping the
- * columns of a table, we buffer any column comments we find so we
- * can emit them after the closing ')' of the CREATE TABLE statement.
- */
-typedef struct comment_buffer {
- buffer *buf;
- stream *append;
-} comment_buffer;
-static comment_buffer *comment_buffer_create(void);
-stream *comment_appender(comment_buffer *comments);
-static int append_comment(
- comment_buffer *comments,
- const char *obj_type,
- const char *schema_name,
- const char *outer_name,
- const char *inner_name,
- void *parameter_types,
- const char *remark
-);
-static int write_comment_buffer(stream *out, comment_buffer *comments);
-static void comment_buffer_destroy(comment_buffer *comments);
-
-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) {
- mnstr_destroy(s);
- buffer_destroy(buf);
- return NULL;
- }
-
- comments->buf = buf;
- comments->append = s;
-
- return comments;
-}
-
-stream *
-comment_appender(comment_buffer *comments)
+static void
+comment_on(stream *toConsole, const char *object,
+ const char *ident1, const char *ident2, const char *ident3,
+ const char *remark)
{
- return comments->append;
-}
-
-int
-append_comment(
- comment_buffer *comments,
- const char *obj_type,
- const char *schema_name,
- const char *outer_name,
- const char *inner_name,
- void *parameter_types,
- const char *remark
-) {
- char *sep = "";
-
- if (!remark)
- return 0;
-
- mnstr_printf(comments->append, "COMMENT ON %s ", obj_type);
- if (schema_name) {
- mnstr_printf(comments->append, "%s", sep);
- quoted_print(comments->append, schema_name, false);
- sep = ".";
- }
- if (outer_name) {
- mnstr_printf(comments->append, "%s", sep);
- quoted_print(comments->append, outer_name, false);
- sep = ".";
- }
- if (inner_name) {
- mnstr_printf(comments->append, "%s", sep);
- quoted_print(comments->append, inner_name, false);
- sep = ".";
- }
- (void) parameter_types;
-
- mnstr_printf(comments->append, " IS ");
- quoted_print(comments->append, remark, true);
- mnstr_printf(comments->append, ";\n");
-
- return 0;
-}
-
-int
-write_comment_buffer(stream *out, comment_buffer *comments)
-{
- assert((comments->buf == NULL) == (comments->append == NULL));
- if (comments->buf == NULL)
- return 0;
-
- if (out) {
- char *text = buffer_get_buf(comments->buf);
- if (text) {
- mnstr_printf(out, "%s", text);
- free(text);
- }
- }
-
- return 0;
-}
-
-void
-comment_buffer_destroy(comment_buffer *comments)
-{
- mnstr_destroy(comments->append);
- buffer_destroy(comments->buf);
- free(comments);
+ if (remark) {
+ mnstr_printf(toConsole, "COMMENT ON %s \"%s\"", object, ident1);
+ if (ident2) {
+ mnstr_printf(toConsole, ".\"%s\"", ident2);
+ if (ident3) {
+ mnstr_printf(toConsole, ".\"%s\"", ident3);
+ }
+ }
+ mnstr_write(toConsole, " IS ", 1, 4);
+ quoted_print(toConsole, remark, true);
+ mnstr_write(toConsole, ";\n", 1, 2);
+ }
}
static char *actions[] = {
@@ -222,7 +116,10 @@ has_hugeint(Mapi mid)
{
MapiHdl hdl;
int ret;
+ static int answer = -1;
+ if (answer >= 0)
+ return answer;
if ((hdl = mapi_query(mid,
"SELECT id "
"FROM sys.types "
@@ -237,6 +134,7 @@ has_hugeint(Mapi mid)
if (mapi_error(mid))
goto bailout;
mapi_close_handle(hdl);
+ answer = ret;
return ret;
bailout:
@@ -471,7 +369,6 @@ toUpper(const char *s)
static int dump_column_definition(
Mapi mid,
stream *toConsole,
- comment_buffer *comments,
const char *schema,
const char *tname,
const char *tid,
@@ -589,7 +486,7 @@ dump_type(Mapi mid, stream *toConsole, c
space = mnstr_printf(toConsole, "DECIMAL");
} else if (strcmp(c_type, "table") == 0) {
mnstr_printf(toConsole, "TABLE ");
- dump_column_definition(mid, toConsole, NULL, NULL, NULL,
c_type_digits, 1, hashge);
+ dump_column_definition(mid, toConsole, NULL, NULL,
c_type_digits, 1, hashge);
} else if (strcmp(c_type, "geometry") == 0 &&
strcmp(c_type_digits, "0") != 0) {
const char *geom = NULL;
@@ -625,7 +522,7 @@ dump_type(Mapi mid, stream *toConsole, c
}
static int
-dump_column_definition(Mapi mid, stream *toConsole, comment_buffer *comments,
const char *schema, const char *tname, const char *tid, int foreign, int hashge)
+dump_column_definition(Mapi mid, stream *toConsole, const char *schema, const
char *tname, const char *tid, int foreign, int hashge)
{
MapiHdl hdl = NULL;
char *query;
@@ -653,9 +550,8 @@ dump_column_definition(Mapi mid, stream
"c.type_scale, " /* 3 */
"c.\"null\", " /* 4 */
"c.\"default\", " /* 5 */
- "c.number, " /* 6 */
- "rem.remark " /* 7 */
- "FROM sys._columns c LEFT OUTER JOIN sys.comments rem
ON c.id = rem.id "
+ "c.number " /* 6 */
+ "FROM sys._columns c "
"WHERE c.table_id = %s "
"ORDER BY number", tid);
else
@@ -666,9 +562,8 @@ dump_column_definition(Mapi mid, stream
"c.type_scale, " /* 3 */
"c.\"null\", " /* 4 */
"c.\"default\", " /* 5 */
- "c.number, " /* 6 */
- "rem.remark " /* 7 */
- "FROM sys._columns c LEFT OUTER JOIN sys.comments rem
ON c.id = rem.id, "
+ "c.number " /* 6 */
+ "FROM sys._columns c, "
"sys._tables t, "
"sys.schemas s "
"WHERE c.table_id = t.id AND "
@@ -688,7 +583,6 @@ dump_column_definition(Mapi mid, stream
const char *c_type_scale = mapi_fetch_field(hdl, 3);
const char *c_null = mapi_fetch_field(hdl, 4);
const char *c_default = mapi_fetch_field(hdl, 5);
- const char *c_remark = mapi_fetch_field(hdl, 7);
int space;
if (mapi_error(mid))
@@ -708,8 +602,6 @@ dump_column_definition(Mapi mid, stream
mnstr_printf(toConsole, "%*s DEFAULT %s",
CAP(13 - space), "", c_default);
- if (schema && tname)
- append_comment(comments, "COLUMN", schema, tname,
c_name, NULL, c_remark);
cnt++;
if (mnstr_errnr(toConsole))
goto bailout;
@@ -880,7 +772,6 @@ describe_table(Mapi mid, const char *sch
size_t maxquerylen;
char *sname = NULL;
int hashge;
- comment_buffer *comments = comment_buffer_create();
if (schema == NULL) {
if ((sname = strchr(tname, '.')) != NULL) {
@@ -922,11 +813,13 @@ describe_table(Mapi mid, const char *sch
}
if (mapi_error(mid)) {
view = NULL;
+ remark = NULL;
goto bailout;
}
- append_comment(comments, type != 1 ? "TABLE" : "VIEW", sname, tname,
NULL, NULL, remark);
if (view)
view = strdup(view);
+ if (remark)
+ remark = strdup(remark);
mapi_close_handle(hdl);
hdl = NULL;
@@ -942,6 +835,7 @@ describe_table(Mapi mid, const char *sch
if (type == 1) {
/* the table is actually a view */
mnstr_printf(toConsole, "%s\n", view);
+ comment_on(toConsole, "VIEW", schema, tname, NULL, remark);
} else {
/* the table is a real table */
mnstr_printf(toConsole, "CREATE %sTABLE \"%s\".\"%s\" ",
@@ -952,22 +846,21 @@ describe_table(Mapi mid, const char *sch
"",
schema, tname);
- if (dump_column_definition(mid, toConsole, comments, schema,
tname, NULL, foreign, hashge))
+ if (dump_column_definition(mid, toConsole, schema, tname, NULL,
foreign, hashge))
goto bailout;
if (type == 5)
mnstr_printf(toConsole, " ON '%s'", view);
mnstr_printf(toConsole, ";\n");
+ comment_on(toConsole, "TABLE", schema, tname, NULL, remark);
snprintf(query, maxquerylen,
"SELECT i.name, " /* 0 */
"k.name, " /* 1 */
"kc.nr, " /* 2 */
"c.name, " /* 3 */
- "i.type, " /* 4 */
- "rem.remark " /* 5 */
+ "i.type " /* 4 */
"FROM sys.idxs AS i "
- "LEFT JOIN sys.keys AS k ON i.name = k.name "
- "LEFT OUTER JOIN sys.comments rem ON i.id =
rem.id, "
+ "LEFT JOIN sys.keys AS k ON i.name = k.name, "
"sys.objects AS kc, "
"sys._columns AS c, "
"sys.schemas s, "
@@ -991,7 +884,6 @@ describe_table(Mapi mid, const char *sch
const char *kc_nr = mapi_fetch_field(hdl, 2);
const char *c_name = mapi_fetch_field(hdl, 3);
const char *i_type = mapi_fetch_field(hdl, 4);
- const char *remark = mapi_fetch_field(hdl, 5);
if (mapi_error(mid))
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list