Skip to content

Commit

Permalink
fix csv escaping, new export formats - sql values and pipe separated
Browse files Browse the repository at this point in the history
  • Loading branch information
okbob committed Jun 4, 2021
1 parent c725955 commit 85e6767
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ see http://okbob.blogspot.cz/2017/07/i-hope-so-every-who-uses-psql-uses-less.htm
| `\N+` | go to N lines forward |
| `\N-` | go to N lines backward |
| `\theme N` | set theme number |
| `\copy [all\|selected] [nullstr "str"] [csv\|tsv\|insert\|text]` | copy data to clipboard |
| `\save [all\|selected] [nullstr "str"] [csv\|tsv\|insert\|text]` | copy data to clipboard |
| `\copy [all\|selected] [nullstr "str"] [csv\|tsv\|insert\|text\|pipesep\|sqlvalues]` | copy data to clipboard |
| `\save [all\|selected] [nullstr "str"] [csv\|tsv\|insert\|text\|pipesep\|sqlvalues]` | copy data to clipboard |
| `\order [N\|colum name]` | sort by colum |
| `\orderd [N\|colum name]` | desc sort by column |
| `\search [back] [selected] [colum name] [string\|"string"]` | search string in data |
Expand Down
4 changes: 4 additions & 0 deletions src/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ cmd_string(int cmd)
return "UseClipboardFormatTSVC";
case cmd_UseClipboard_text:
return "UseClipboardFormattext";
case cmd_UseClipboard_SQL_values:
return "UseClipboardFormatSQLValues";
case cmd_UseClipboard_pipe_separated:
return "UseClipboardFormatPipeSeparated";
case cmd_UseClipboard_INSERT:
return "UseClipboardFormatINSERT";
case cmd_UseClipboard_INSERT_with_comments:
Expand Down
2 changes: 2 additions & 0 deletions src/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ typedef enum PspgCommand
cmd_SetCopyClipboard,
cmd_UseClipboard_CSV,
cmd_UseClipboard_TSVC,
cmd_UseClipboard_SQL_values,
cmd_UseClipboard_text,
cmd_UseClipboard_pipe_separated,
cmd_UseClipboard_INSERT,
cmd_UseClipboard_INSERT_with_comments,
cmd_TogleEmptyStringIsNULL,
Expand Down
4 changes: 3 additions & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ typedef enum
CLIPBOARD_FORMAT_CSV,
CLIPBOARD_FORMAT_TSVC,
CLIPBOARD_FORMAT_TEXT,
CLIPBOARD_FORMAT_PIPE_SEPARATED,
CLIPBOARD_FORMAT_SQL_VALUES,
CLIPBOARD_FORMAT_INSERT,
CLIPBOARD_FORMAT_INSERT_WITH_COMMENTS
} ClipboardFormat;

#define DSV_FORMAT_TYPE(f) (f == CLIPBOARD_FORMAT_CSV || f == CLIPBOARD_FORMAT_TSVC)
#define DSV_FORMAT_TYPE(f) (f == CLIPBOARD_FORMAT_CSV || f == CLIPBOARD_FORMAT_TSVC || f == CLIPBOARD_FORMAT_SQL_VALUES)
#define INSERT_FORMAT_TYPE(f) (f == CLIPBOARD_FORMAT_INSERT || f == CLIPBOARD_FORMAT_INSERT_WITH_COMMENTS)

typedef enum
Expand Down
57 changes: 51 additions & 6 deletions src/export.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ csv_format(char *str, int *slen,
int size = force8bit ? 1 : utf8charlen(*ptr);

if (*str == '"')
{
*ptr++ = '"';
*slen += 1;
}

_slen -= size;
*slen += size;
Expand Down Expand Up @@ -589,11 +592,44 @@ process_item(ExportState *expstate,
fputc('\n', expstate->fp);
}

else if (expstate->format == CLIPBOARD_FORMAT_PIPE_SEPARATED)
{
errno = 0;

if (!is_colname)
{
if (typ != 'N')
{
if (typ == 'I' || typ == 'd')
{
/* Ignore items outer to selected range */
if (expstate->xmin != -1 &&
(xpos <= expstate->xmin ||
expstate->xmax <= xpos))
return true;

if (typ == 'd')
{
field = trim_str(field, &size, expstate->force8bit);
fwrite(field, size, 1, expstate->fp);
}
else
fputs(" | ", expstate->fp);
}
}
else
fputc('\n', expstate->fp);
}
}

/*
* Export in CSV or TSV format
*/
else if (DSV_FORMAT_TYPE(expstate->format))
{
if (expstate->format == CLIPBOARD_FORMAT_SQL_VALUES && is_colname)
return true;

if (typ == 'N' &&
!expstate->copy_line_extended &&
!has_continue_mark)
Expand All @@ -610,12 +646,20 @@ process_item(ExportState *expstate,

field = trim_str(field, &size, expstate->force8bit);

_field = csv_format(field, &size,
expstate->force8bit,
expstate->empty_string_is_null,
expstate->nullstr,
expstate->nullstrlen);
if (expstate->format == CLIPBOARD_FORMAT_SQL_VALUES)
_field = quote_sql_literal(field,
&size,
expstate->force8bit,
expstate->empty_string_is_null,
expstate->nullstr,
expstate->nullstrlen);
else

_field = csv_format(field, &size,
expstate->force8bit,
expstate->empty_string_is_null,
expstate->nullstr,
expstate->nullstrlen);

if (expstate->copy_line_extended && is_colname)
{
Expand Down Expand Up @@ -645,7 +689,8 @@ process_item(ExportState *expstate,
if (expstate->colno > 0)
{

if (expstate->format == CLIPBOARD_FORMAT_CSV)
if (expstate->format == CLIPBOARD_FORMAT_CSV ||
expstate->format == CLIPBOARD_FORMAT_SQL_VALUES)
fputc(',', expstate->fp);
else if (expstate->format == CLIPBOARD_FORMAT_TSVC)
fputc('\t', expstate->fp);
Expand Down
5 changes: 5 additions & 0 deletions src/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ ST_MENU_ITEM _copy[] = {
{"_2_Use formatted text", cmd_UseClipboard_text, NULL, 0, 0, 0, NULL},
{"_3_Use INSERT format", cmd_UseClipboard_INSERT, NULL, 0, 0, 0, NULL},
{"_4_Use commented INSERT format", cmd_UseClipboard_INSERT_with_comments, NULL, 0, 0, 0, NULL},
{"_5_Use SQL Values format", cmd_UseClipboard_SQL_values, NULL, 0, 0, 0, NULL},
{"_6_Use pipe separated text", cmd_UseClipboard_pipe_separated, NULL, 0, 0, 0, NULL},
{NULL, 0, NULL, 0, 0, 0, NULL}
};

Expand Down Expand Up @@ -415,6 +417,9 @@ refresh_clipboard_options(Options *opts, struct ST_MENU *menu)
st_menu_set_option(menu, cmd_UseClipboard_INSERT, ST_MENU_OPTION_MARKED, opts->clipboard_format == CLIPBOARD_FORMAT_INSERT);
st_menu_set_option(menu, cmd_UseClipboard_INSERT_with_comments, ST_MENU_OPTION_MARKED,
opts->clipboard_format == CLIPBOARD_FORMAT_INSERT_WITH_COMMENTS);

st_menu_set_option(menu, cmd_UseClipboard_SQL_values, ST_MENU_OPTION_MARKED, opts->clipboard_format == CLIPBOARD_FORMAT_SQL_VALUES);
st_menu_set_option(menu, cmd_UseClipboard_pipe_separated, ST_MENU_OPTION_MARKED, opts->clipboard_format == CLIPBOARD_FORMAT_PIPE_SEPARATED);
}

void
Expand Down
41 changes: 40 additions & 1 deletion src/pspg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,7 @@ get_string(Options *opts,
}
else
{
rl_completer_word_break_characters = NULL;
rl_completer_word_break_characters = (char *) rl_basic_word_break_characters;
rl_completer_quote_characters = NULL;
}

Expand Down Expand Up @@ -1328,6 +1328,7 @@ get_string(Options *opts,
prev_c = c;

forward_to_readline(c);

wrefresh(bottom_bar);

if (!input_is_valid)
Expand Down Expand Up @@ -2766,9 +2767,11 @@ const char *export_opts[] = {
"csv",
"tsvc",
"text",
"pipesep",
"insert",
"cinsert",
"nullstr",
"sqlvalues",
NULL
};

Expand Down Expand Up @@ -3241,11 +3244,23 @@ parse_exported_spec(Options *opts,
spec->format = CLIPBOARD_FORMAT_TSVC;
format_specified = true;
}
else if (IS_TOKEN(token, n, "sqlval") ||
IS_TOKEN(token, n, "sqlvalues"))
{
spec->format = CLIPBOARD_FORMAT_SQL_VALUES;
format_specified = true;
}
else if (IS_TOKEN(token, n, "text"))
{
spec->format = CLIPBOARD_FORMAT_TEXT;
format_specified = true;
}
else if (IS_TOKEN(token, n, "pipesep") ||
IS_TOKEN(token, n, "ps"))
{
spec->format = CLIPBOARD_FORMAT_PIPE_SEPARATED;
format_specified = true;
}
else if (IS_TOKEN(token, n, "insert"))
{
spec->format = CLIPBOARD_FORMAT_INSERT;
Expand Down Expand Up @@ -5207,6 +5222,8 @@ main(int argc, char *argv[])
next_command != cmd_UseClipboard_text &&
next_command != cmd_UseClipboard_INSERT &&
next_command != cmd_UseClipboard_INSERT_with_comments &&
next_command != cmd_UseClipboard_SQL_values &&
next_command != cmd_UseClipboard_pipe_separated &&
next_command != cmd_SetCopyFile &&
next_command != cmd_SetCopyClipboard &&
next_command != cmd_TogleEmptyStringIsNULL &&
Expand Down Expand Up @@ -5457,6 +5474,28 @@ main(int argc, char *argv[])

refresh_clipboard_options(&opts, menu);

#endif

break;

case cmd_UseClipboard_SQL_values:
opts.clipboard_format = CLIPBOARD_FORMAT_SQL_VALUES;

#ifdef COMPILE_MENU

refresh_clipboard_options(&opts, menu);

#endif

break;

case cmd_UseClipboard_pipe_separated:
opts.clipboard_format = CLIPBOARD_FORMAT_PIPE_SEPARATED;

#ifdef COMPILE_MENU

refresh_clipboard_options(&opts, menu);

#endif

break;
Expand Down

0 comments on commit 85e6767

Please sign in to comment.