Changeset: 669614968315 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=669614968315
Modified Files:
        clients/mapiclient/dump.c
Branch: default
Log Message:

Merge with Mar2018 branch.


diffs (truncated from 465 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
@@ -256,7 +256,7 @@ dump_foreign_keys(Mapi mid, const char *
                              "pkt.schema_id = ps.id AND "
                              "fkt.schema_id = fs.id AND "
                              "fkt.system = FALSE "
-                       "ORDER BY fs.name,fkt.name, "
+                       "ORDER BY fs.name, fkt.name, "
                              "fkk.name, nr";
        }
        hdl = mapi_query(mid, query);
@@ -804,7 +804,7 @@ describe_table(Mapi mid, const char *sch
 
        query = malloc(maxquerylen);
        snprintf(query, maxquerylen,
-                "%s\n"
+                "%s "
                 "SELECT t.name, t.query, t.type, c.remark "
                 "FROM sys.schemas s, sys._tables t LEFT OUTER JOIN comments c 
ON t.id = c.id "
                 "WHERE s.name = '%s' AND "
@@ -949,8 +949,12 @@ describe_table(Mapi mid, const char *sch
                if (cnt)
                        mnstr_printf(toConsole, ");\n");
                snprintf(query, maxquerylen,
-                       "%s\n"
-                        "SELECT i.name, c.remark FROM sys.idxs i, comments c 
WHERE i.id = c.id AND i.table_id = (SELECT id FROM sys._tables WHERE schema_id 
= (select id FROM sys.schemas WHERE name = '%s') AND name = '%s') ORDER BY 
i.name",
+                       "%s "
+                        "SELECT i.name, c.remark "
+                        "FROM sys.idxs i, comments c "
+                        "WHERE i.id = c.id "
+                          "AND i.table_id = (SELECT id FROM sys._tables WHERE 
schema_id = (select id FROM sys.schemas WHERE name = '%s') AND name = '%s') "
+                        "ORDER BY i.name",
                         get_compat_clause(mid),
                         schema, tname);
                if ((hdl = mapi_query(mid, query)) == NULL || mapi_error(mid))
@@ -965,10 +969,14 @@ describe_table(Mapi mid, const char *sch
        }
 
        snprintf(query, maxquerylen,
-               "%s\n"
-                       "SELECT col.name, com.remark FROM sys._columns col, 
comments com WHERE col.id = com.id AND col.table_id = (SELECT id FROM 
sys._tables WHERE schema_id = (SELECT id FROM sys.schemas WHERE name = '%s') 
AND name = '%s') ORDER BY number",
-                       get_compat_clause(mid),
-                       schema, tname);
+               "%s "
+                "SELECT col.name, com.remark "
+                "FROM sys._columns col, comments com "
+                "WHERE col.id = com.id "
+                  "AND col.table_id = (SELECT id FROM sys._tables WHERE 
schema_id = (SELECT id FROM sys.schemas WHERE name = '%s') AND name = '%s') "
+                "ORDER BY number",
+                get_compat_clause(mid),
+                schema, tname);
        if ((hdl = mapi_query(mid, query)) == NULL || mapi_error(mid))
                goto bailout;
        while (mapi_fetch_row(hdl) != 0) {
@@ -1037,21 +1045,21 @@ describe_sequence(Mapi mid, const char *
 
        query = malloc(maxquerylen);
        snprintf(query, maxquerylen,
-               "%s\n"
+               "%s "
                "SELECT s.name, "
-                    "seq.name, "
-                    "get_value_for(s.name,seq.name), "
-                    "seq.\"minvalue\", "
-                    "seq.\"maxvalue\", "
-                    "seq.\"increment\", "
-                    "seq.\"cycle\", "
-                    "rem.\"remark\" "
+                      "seq.name, "
+                      "get_value_for(s.name, seq.name), "
+                      "seq.\"minvalue\", "
+                      "seq.\"maxvalue\", "
+                      "seq.\"increment\", "
+                      "seq.\"cycle\", "
+                      "rem.\"remark\" "
                "FROM sys.sequences seq LEFT OUTER JOIN comments rem ON seq.id 
= rem.id, "
                     "sys.schemas s "
                "WHERE s.id = seq.schema_id AND "
                      "s.name = '%s' AND "
                      "seq.name = '%s' "
-               "ORDER BY s.name,seq.name",
+               "ORDER BY s.name, seq.name",
                get_compat_clause(mid),
                schema, tname);
 
@@ -1118,7 +1126,7 @@ describe_schema(Mapi mid, const char *sn
        char schemas[5120];
 
        snprintf(schemas, sizeof(schemas),
-               "%s\n"
+               "%s "
                "SELECT s.name, a.name, c.remark "
                "FROM sys.auths a, "
                     "sys.schemas s LEFT OUTER JOIN comments c ON s.id = c.id "
@@ -1163,8 +1171,9 @@ dump_table_data(Mapi mid, const char *sc
                bool useInserts)
 {
        int cnt, i;
+       int64_t rows;
        MapiHdl hdl = NULL;
-       char *query;
+       char *query = NULL;
        size_t maxquerylen;
        unsigned char *string = NULL;
        char *sname = NULL;
@@ -1174,17 +1183,21 @@ dump_table_data(Mapi mid, const char *sc
                        size_t len = sname - tname;
 
                        sname = malloc(len + 1);
+                       if (sname == NULL)
+                               goto bailout;
                        strncpy(sname, tname, len);
                        sname[len] = 0;
                        tname += len + 1;
                } else if ((sname = get_schema(mid)) == NULL) {
-                       return 1;
+                       goto bailout;
                }
                schema = sname;
        }
 
        maxquerylen = 5120 + strlen(tname) + strlen(schema);
        query = malloc(maxquerylen);
+       if (query == NULL)
+               goto bailout;
 
        snprintf(query, maxquerylen,
                 "SELECT t.name, t.query, t.type "
@@ -1214,37 +1227,29 @@ dump_table_data(Mapi mid, const char *sc
        mapi_close_handle(hdl);
        hdl = NULL;
 
-       if (!useInserts) {
-               snprintf(query, maxquerylen, "SELECT count(*) FROM 
\"%s\".\"%s\"",
-                        schema, tname);
-               if ((hdl = mapi_query(mid, query)) == NULL || mapi_error(mid))
-                       goto bailout;
-               if (mapi_fetch_row(hdl)) {
-                       const char *cntfld = mapi_fetch_field(hdl, 0);
-
-                       if (strcmp(cntfld, "0") == 0) {
-                               /* no records to dump, so return early */
-                               goto doreturn;
-                       }
-
-                       mnstr_printf(toConsole,
-                                    "COPY %s RECORDS INTO \"%s\".\"%s\" "
-                                    "FROM stdin USING DELIMITERS 
'\\t','\\n','\"';\n",
-                                    cntfld, schema, tname);
-               }
-               mapi_close_handle(hdl);
-               hdl = NULL;
-       }
-
        snprintf(query, maxquerylen, "SELECT * FROM \"%s\".\"%s\"",
                 schema, tname);
        if ((hdl = mapi_query(mid, query)) == NULL || mapi_error(mid))
                goto bailout;
 
+       rows = mapi_get_row_count(hdl);
+       if (rows == 0) {
+               /* nothing more to do */
+               goto doreturn;
+       }
+
        cnt = mapi_get_field_count(hdl);
        if (cnt < 1 || cnt >= 1 << 29)
                goto bailout;   /* ridiculous number of columns */
+       if (!useInserts) {
+               mnstr_printf(toConsole,
+                            "COPY %" PRId64 " RECORDS INTO \"%s\".\"%s\" "
+                            "FROM stdin USING DELIMITERS '\\t','\\n','\"';\n",
+                            rows, schema, tname);
+       }
        string = malloc(sizeof(unsigned char) * cnt);
+       if (string == NULL)
+               goto bailout;
        for (i = 0; i < cnt; i++) {
                string[i] = (strcmp(mapi_get_type(hdl, i), "char") == 0 ||
                             strcmp(mapi_get_type(hdl, i), "varchar") == 0 ||
@@ -1264,8 +1269,9 @@ dump_table_data(Mapi mid, const char *sc
                        if (s == NULL)
                                mnstr_printf(toConsole, "NULL");
                        else if (string[i]) {
-                               /* write double or single-quoted string with
-                                  certain characters escaped */
+                               /* write double or single-quoted
+                                  string with certain characters
+                                  escaped */
                                quoted_print(toConsole, s, useInserts);
                        } else
                                mnstr_printf(toConsole, "%s", s);
@@ -1304,9 +1310,13 @@ dump_table_data(Mapi mid, const char *sc
                        mapi_explain_result(hdl, stderr);
                else if (mapi_error(mid))
                        mapi_explain_query(hdl, stderr);
+               else
+                       fprintf(stderr, "malloc failure\n");
                mapi_close_handle(hdl);
        } else if (mapi_error(mid))
                mapi_explain(mid, stderr);
+       else
+               fprintf(stderr, "malloc failure\n");
        if (sname != NULL)
                free(sname);
        if (query != NULL)
@@ -1339,23 +1349,22 @@ dump_function_comment(Mapi mid, stream *
                return 1;
 
        snprintf(query, len,
-               "%s\n"
-               "SELECT \n"
-               "        coalesce(function_type_keyword, '') AS category,\n"
-               "        s.name AS schema,\n"
-               "        CASE RANK() OVER (PARTITION BY f.id ORDER BY p.number 
ASC) WHEN 1 THEN f.name ELSE NULL END AS name,\n"
-               "        p.type, \n"
-               "        p.type_digits, \n"
-               "        p.type_scale,\n"
-               "        CASE RANK() OVER (PARTITION BY f.id ORDER BY p.number 
DESC) WHEN 1 THEN c.remark ELSE NULL END AS remark,\n"
-               "        ROW_NUMBER() OVER (ORDER BY f.id, p.number) AS line\n"
-               "FROM sys.functions f\n"
-               "JOIN comments c ON f.id = c.id\n"
-               "JOIN sys.schemas s ON f.schema_id = s.id\n"
-               "LEFT OUTER JOIN sys.function_types ft ON f.type = 
ft.function_type_id\n"
-               "LEFT OUTER JOIN sys.args p ON f.id = p.func_id AND p.inout = 
1\n"
-               "WHERE f.id = %s\n"
-               "ORDER BY line;",
+               "%s "
+               "SELECT coalesce(function_type_keyword, '') AS category, "
+                      "s.name AS schema, "
+                      "CASE RANK() OVER (PARTITION BY f.id ORDER BY p.number 
ASC) WHEN 1 THEN f.name ELSE NULL END AS name, "
+                      "p.type, "
+                      "p.type_digits, "
+                      "p.type_scale, "
+                      "CASE RANK() OVER (PARTITION BY f.id ORDER BY p.number 
DESC) WHEN 1 THEN c.remark ELSE NULL END AS remark, "
+                      "ROW_NUMBER() OVER (ORDER BY f.id, p.number) AS line "
+               "FROM sys.functions f "
+                    "JOIN comments c ON f.id = c.id "
+                    "JOIN sys.schemas s ON f.schema_id = s.id "
+                    "LEFT OUTER JOIN sys.function_types ft ON f.type = 
ft.function_type_id "
+                    "LEFT OUTER JOIN sys.args p ON f.id = p.func_id AND 
p.inout = 1 "
+               "WHERE f.id = %s "
+               "ORDER BY line",
                get_compat_clause(mid),
                id);
 
@@ -1432,8 +1441,22 @@ dump_function(Mapi mid, stream *toConsol
 
        q = query;
        end_q = query + qlen;
-       q += snprintf(q, q - end_q, "%s\n", get_compat_clause(mid));
-       q += snprintf(q, q - end_q, "SELECT f.id, f.func, f.language, f.type, 
s.name, f.name, function_type_keyword, language_keyword FROM sys.functions f 
JOIN sys.schemas s ON f.schema_id = s.id JOIN function_types ft ON f.type = 
ft.function_type_id LEFT OUTER JOIN function_languages fl ON f.language = 
fl.language_id WHERE f.id = %s;", fid);
+       q += snprintf(q, end_q - q,
+                     "%s "
+                     "SELECT f.id, "
+                            "f.func, "
+                            "f.language, "
+                            "f.type, "
+                            "s.name, "
+                            "f.name, "
+                            "function_type_keyword, "
+                            "language_keyword "
+                     "FROM sys.functions f "
+                          "JOIN sys.schemas s ON f.schema_id = s.id "
+                          "JOIN function_types ft ON f.type = 
ft.function_type_id "
+                          "LEFT OUTER JOIN function_languages fl ON f.language 
= fl.language_id "
+                     "WHERE f.id = %s",
+                     get_compat_clause(mid), fid);
        if ((hdl = mapi_query(mid, query)) == NULL || mapi_error(mid)) {
                free(query);
                return 1;
@@ -1567,6 +1590,8 @@ dump_functions(Mapi mid, stream *toConso
                wantSystem = 0;
        }
 
+       hashge = has_hugeint(mid);
+
        len = 5120 + (sname ? strlen(sname) : 0) + (fname ? strlen(fname) : 0);
        query = malloc(len);
        if (query == NULL) {
@@ -1577,29 +1602,31 @@ dump_functions(Mapi mid, stream *toConso
        q = query;
        end_q = query + len;
 
-       q += snprintf(q, end_q - q, "%s\n", get_compat_clause(mid));
        q += snprintf(q, end_q - q,
-               "SELECT s.id, s.name, f.id, LENGTH(rem.remark) AS remark_len "
-               "FROM sys.schemas s "
-               "JOIN sys.functions f ON s.id = f.schema_id "
-               "LEFT OUTER JOIN comments rem ON f.id = rem.id "
-               "WHERE f.language > 0 ");
-       if (sname)
-               q += snprintf(q, end_q - q, "AND s.name = '%s' ", sname);
-       if (fname)
-               q += snprintf(q, end_q - q, "AND f.name = '%s' ", fname);
-       if (id)
+                     "%s "
+                     "SELECT s.id, s.name, f.id, LENGTH(rem.remark) AS 
remark_len "
+                     "FROM sys.schemas s "
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to