Changeset: afd404ded48f for MonetDB URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=afd404ded48f Modified Files: clients/ChangeLog clients/src/mapiclient/dump.c clients/src/mapiclient/mclient.c clients/src/mapiclient/msqldump.h Branch: default Log Message:
mclient: allow to list and describe sequences added a function to create a sequence creation statement that matches the current state of the sequence added code to deal with \ds to activate sequence listing We differ from psql in that \d for us means \dtv, and not \dtvs diffs (truncated from 325 to 300 lines): diff -r e34a5ab6cb5e -r afd404ded48f clients/ChangeLog --- a/clients/ChangeLog Wed Nov 24 21:02:48 2010 +0100 +++ b/clients/ChangeLog Thu Nov 25 11:19:28 2010 +0100 @@ -1,6 +1,9 @@ # ChangeLog file for clients # This file is updated with Maddlog +* Thu Nov 25 2010 Fabian Groffen <fab...@cwi.nl> +- Added support for \ds to list sequences or describe a specific one + * Mon Nov 22 2010 Fabian Groffen <fab...@cwi.nl> - Added support for wildcards * and ? in object names given to \d commands, such that pattern matching is possible, e.g. \d my* diff -r e34a5ab6cb5e -r afd404ded48f clients/src/mapiclient/dump.c --- a/clients/src/mapiclient/dump.c Wed Nov 24 21:02:48 2010 +0100 +++ b/clients/src/mapiclient/dump.c Thu Nov 25 11:19:28 2010 +0100 @@ -870,6 +870,97 @@ } int +describe_sequence(Mapi mid, char *schema, char *tname, stream *toConsole) +{ + MapiHdl hdl = NULL; + char *query; + size_t maxquerylen; + char *sname = NULL; + + if (schema == NULL) { + if ((sname = strchr(tname, '.')) != NULL) { + size_t len = sname - tname; + + sname = malloc(len + 1); + strncpy(sname, tname, len); + sname[len] = 0; + tname += len + 1; + } else if ((sname = get_schema(mid)) == NULL) { + return 1; + } + schema = sname; + } + + maxquerylen = 512 + strlen(tname) + strlen(schema); + + query = malloc(maxquerylen); + snprintf(query, maxquerylen, + "SELECT \"s\".\"name\"," + "\"seq\".\"name\"," + "get_value_for(\"s\".\"name\",\"seq\".\"name\")," + "\"seq\".\"minvalue\"," + "\"seq\".\"maxvalue\"," + "\"seq\".\"increment\"," + "\"seq\".\"cycle\" " + "FROM \"sys\".\"sequences\" \"seq\", " + "\"sys\".\"schemas\" \"s\" " + "WHERE \"s\".\"id\" = \"seq\".\"schema_id\" " + "AND \"s\".\"name\" = '%s' " + "AND \"seq\".\"name\" = '%s' " + "ORDER BY \"s\".\"name\",\"seq\".\"name\"", + schema, tname); + + if ((hdl = mapi_query(mid, query)) == NULL || mapi_error(mid)) + goto bailout; + + while (mapi_fetch_row(hdl) != 0) { + char *schema = mapi_fetch_field(hdl, 0); + char *name = mapi_fetch_field(hdl, 1); + char *start = mapi_fetch_field(hdl, 2); + char *minvalue = mapi_fetch_field(hdl, 3); + char *maxvalue = mapi_fetch_field(hdl, 4); + char *increment = mapi_fetch_field(hdl, 5); + char *cycle = mapi_fetch_field(hdl, 6); + + mnstr_printf(toConsole, + "CREATE SEQUENCE \"%s\".\"%s\" START WITH %s", + schema, name, start); + if (strcmp(increment, "1") != 0) + mnstr_printf(toConsole, " INCREMENT BY %s", increment); + if (strcmp(minvalue, "0") != 0) + mnstr_printf(toConsole, " MINVALUE %s", minvalue); + if (strcmp(maxvalue, "0") != 0) + mnstr_printf(toConsole, " MAXVALUE %s", maxvalue); + mnstr_printf(toConsole, " %sCYCLE;\n", strcmp(cycle, "true") == 0 ? "" : "NO "); + if (mnstr_errnr(toConsole)) { + mapi_close_handle(hdl); + hdl = NULL; + goto bailout; + } + } + if (mapi_error(mid)) + goto bailout; + mapi_close_handle(hdl); + hdl = NULL; + return 0; + +bailout: + if (hdl) { + if (mapi_result_error(hdl)) + mapi_explain_result(hdl, stderr); + else if (mapi_error(mid)) + mapi_explain_query(hdl, stderr); + mapi_close_handle(hdl); + } else if (mapi_error(mid)) + mapi_explain(mid, stderr); + if (sname != NULL) + free(sname); + if (query != NULL) + free(query); + return 1; +} + +int dump_table_data(Mapi mid, char *schema, char *tname, stream *toConsole) { int cnt, i; diff -r e34a5ab6cb5e -r afd404ded48f clients/src/mapiclient/mclient.c --- a/clients/src/mapiclient/mclient.c Wed Nov 24 21:02:48 2010 +0100 +++ b/clients/src/mapiclient/mclient.c Thu Nov 25 11:19:28 2010 +0100 @@ -1762,19 +1762,22 @@ mnstr_printf(toConsole, "\\q - terminate session\n"); } +#define MD_TABLE 1 +#define MD_VIEW 2 +#define MD_SEQ 4 +#define MD_FUNC 8 +#define MD_SCHEMA 16 + static int doFileByLines(Mapi mid, FILE *fp, const char *prompt) { - char *line = NULL, *p = NULL; + char *line = NULL; char *oldbuf = NULL, *buf = NULL; size_t length; MapiHdl hdl = mapi_get_active(mid); MapiMsg rc = MOK; int sent = 0; /* whether we sent any data to the server */ int lineno = 1; - char hasWildcard = 0; - char hasSchema = 0; - char wantsSystem = 0; #ifdef HAVE_LIBREADLINE if (prompt == NULL) @@ -1957,18 +1960,35 @@ case 'r': rowsperpage = atoi(line + 2); continue; - case 'd': + case 'd': { + char hasWildcard = 0; + char hasSchema = 0; + char wantsSystem = 0; + unsigned int x = 0; + char *p; if (mode != SQL) break; while (isascii((int) line[length - 1]) && isspace((int) line[length - 1])) line[--length] = 0; - hasSchema = 0; - hasWildcard = 0; - wantsSystem = 0; for (line += 2; *line && !(isascii((int) *line) && isspace((int) *line)); line++) { switch (*line) { + case 't': + x |= MD_TABLE; + break; + case 'v': + x |= MD_VIEW; + break; + case 's': + x |= MD_SEQ; + break; + case 'f': + x |= MD_FUNC; + break; + case 'n': + x |= MD_SCHEMA; + break; case 'S': wantsSystem = 1; break; @@ -1981,6 +2001,8 @@ } if (length == 0) continue; + if (x == 0) /* default to tables and views */ + x = MD_TABLE | MD_VIEW; for ( ; *line && isascii((int) *line) && isspace((int) *line); line++) ; @@ -2027,13 +2049,16 @@ start_pager(&saveFD, &saveFD_raw); #endif - describe_table(mid, NULL, line, toConsole, 1); + if (x & MD_TABLE || x & MD_VIEW) + describe_table(mid, NULL, line, toConsole, 1); + if (x & MD_SEQ) + describe_sequence(mid, NULL, line, toConsole); #ifdef HAVE_POPEN end_pager(saveFD, saveFD_raw); #endif } else { - /* get all table names in current schema */ - char *type, *name, *system, *schema; + /* get all object names in current schema */ + char *type, *name, *schema; char q[1024]; char nameq[256]; if (!*line) { @@ -2042,50 +2067,79 @@ } if (hasSchema) { snprintf(nameq, 256, - "AND \"s\".\"name\" || '.' || \"t\".\"name\" LIKE '%s'", + "AND \"s\".\"name\" || '.' || \"o\".\"name\" LIKE '%s'", line); } else { snprintf(nameq, 256, "AND \"s\".\"name\" = \"current_schema\" " - "AND \"t\".\"name\" LIKE '%s'", + "AND \"o\".\"name\" LIKE '%s'", line); } snprintf(q, 1024, - "SELECT \"t\".\"name\", \"t\".\"type\", " - "\"t\".\"system\", \"s\".\"name\" " - "FROM \"sys\".\"_tables\" \"t\", " + "SELECT \"name\", " + "CAST(\"type\" AS VARCHAR(30)) AS \"type\", " + "\"system\", \"sname\", " + "\"ntype\" " + "FROM (" + "SELECT \"o\".\"name\", " + "(CASE \"o\".\"system\" " + "WHEN true THEN 'SYSTEM ' " + "ELSE '' " + "END || " + "CASE \"o\".\"type\" " + "WHEN 0 THEN 'TABLE' " + "WHEN 1 THEN 'VIEW' " + "ELSE '' " + "END) AS \"type\", " + "\"o\".\"system\", " + "\"s\".\"name\" AS \"sname\", " + "CASE \"o\".\"type\" " + "WHEN 0 THEN %d " + "WHEN 1 THEN %d " + "ELSE 0 " + "END AS \"ntype\" " + "FROM \"sys\".\"_tables\" \"o\", " "\"sys\".\"schemas\" \"s\" " - "WHERE \"t\".\"schema_id\" = \"s\".\"id\" " + "WHERE \"o\".\"schema_id\" = \"s\".\"id\" " "%s %s " - "AND \"t\".\"type\" IN (0, 1) " - "ORDER BY \"t\".\"name\"", + "AND \"o\".\"type\" IN (0, 1) " + "UNION " + "SELECT \"o\".\"name\", " + "'SEQUENCE' AS \"type\", " + "false AS \"system\", " + "\"s\".\"name\" AS \"sname\", " + "%d AS \"ntype\" " + "FROM \"sys\".\"sequences\" \"o\", " + "\"sys\".\"schemas\" \"s\" " + "WHERE \"o\".\"schema_id\" = \"s\".\"id\" " + "%s " + ") AS \"all\" " + "WHERE \"ntype\" & %d > 0 " + "ORDER BY \"system\", \"name\"", + MD_TABLE, MD_VIEW, nameq, (wantsSystem ? "" : - "AND \"t\".\"system\" = false")); + "AND \"o\".\"system\" = false"), + MD_SEQ, + nameq, + x); hdl = mapi_query(mid, q); CHECK_RESULT(mid, hdl, buf, continue); - while (fetch_row(hdl) == 4) { + while (fetch_row(hdl) == 5) { name = mapi_fetch_field(hdl, 0); type = mapi_fetch_field(hdl, 1); - system = mapi_fetch_field(hdl, 2); schema = mapi_fetch_field(hdl, 3); - if (strcmp(system, "true") == 0) { - system = "SYSTEM "; - } else { - system = ""; - } mnstr_printf(toConsole, - "%s%-6s %s.%s\n", - system, - *type == '1' ? "VIEW" : "TABLE", _______________________________________________ Checkin-list mailing list Checkin-list@monetdb.org http://mail.monetdb.org/mailman/listinfo/checkin-list