[GRASS-git] [OSGeo/grass] dfc9b0: v.db.select: Handle all formats in option, add CSV...

Vaclav Petras noreply at github.com
Sat Jun 19 18:16:01 PDT 2021

  Branch: refs/heads/master
  Home:   https://github.com/OSGeo/grass
  Commit: dfc9b05cbfe5bb7cfa58fcbf328b06202ff64304
  Author: Vaclav Petras <wenzeslaus at gmail.com>
  Date:   2021-06-19 (Sat, 19 Jun 2021)

  Changed paths:
    M vector/v.db.select/main.c
    M vector/v.db.select/testsuite/test_v_db_select.py
    A vector/v.db.select/testsuite/test_v_db_select_json_csv.py
    M vector/v.db.select/v.db.select.html

  Log Message:
  v.db.select: Handle all formats in option, add CSV, fix JSON (#1121)

Instead of separate flags for JSON and vertical output format, use format option,
so -j is replaced by format=json, etc. The formats are no longer spread among
the other flags and a written command is more readble. It is also more explicit
since the default format is now represented in the interface and vertical output
clearly stands out as yet another alternative.

The default format is named plain, alternative names considered and rejected were text, ascii and default.
It is meant to be read by humans, but now it can be also newly safely parsed when set as TSV,
however, this feature is questionable and the idea is that plain is not used for parsing.

The use of an option does not change anything major in the code in terms of output
except for use of an enum instead of directly accessing the flags' answers.
The parameter handling changed significantly. The standard exclude function
cannot be used and custom checks and messages are needed to disallow combinations
such as JSON with custom null value.

The significant clean-up related changes to existing formats are:

* The JSON output of NULL values is fixed, writing JSON literal null.
* JSON outputs are now always Object (dictionary) so theoretically they can be combined or enhanced in the future. 
* Different default separator for different formats and outputs (pipe for plain, comma for CSV).
* Separator for -e can now be customized.
* All required JSON escapes are escaped.
* Tab is escaped when escaping is enabled (so you could theoretically write TSV with escapes using plain).
* For all formats, JSON-style escapes except for double quotes are applied when escaping is enabled.
* Vertical output does not add a newline when newline is vertical separator (no more two newlines).

This adds another format, CSV, as format=csv, i.e., without need to add another
flag (e.g., -c and -v were already taken). Most of the code was already there
as basic structure was the original plain default output and quoting was already
done for the JSON output. The original format was not a proper CSV because it
lacked handling quoting of fields which for common CSV parsers takes care of new line
characters and separators in values/cells. It was not completely safe even with escaping
because it did does not escape to the separator (to be delimiter separated values format
with escapes). Delimiter for CSV can be customized, but the quoting cannot.

Now in CSV, all non-numeric columns are quoted (with two quotes representing a quote in the text).
The quotes used are double quotes, the separator is derived by the horizontal/field
separator option. By default is is a comma (a different separator is set for each format by default).
CSV output is available even for the -r flag (minimal region/bbox).

More information about the grass-commit mailing list