Skip to content

Commit 83fc5f9

Browse files
committed
refactor: match csv_options field names
1 parent c9e2959 commit 83fc5f9

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

src/aggs.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,32 @@ static inline bool needs_quote(const char *s, size_t n, char delim) {
2424

2525
void parse_csv_options(HeapTupleHeader opts_hdr, CsvOptions *csv_opts) {
2626
// defaults
27-
csv_opts->delim = ',';
28-
csv_opts->with_bom = false;
29-
csv_opts->header = true;
27+
csv_opts->delimiter = ',';
28+
csv_opts->bom = false;
29+
csv_opts->header = true;
3030

3131
if (opts_hdr == NULL) return;
3232

3333
TupleDesc desc = lookup_rowtype_tupdesc(HeapTupleHeaderGetTypeId(opts_hdr),
3434
HeapTupleHeaderGetTypMod(opts_hdr));
3535

36-
Datum values[3];
37-
bool nulls[3];
36+
Datum values[csv_options_count];
37+
bool nulls[csv_options_count];
3838

3939
heap_deform_tuple(
4040
&(HeapTupleData){.t_len = HeapTupleHeaderGetDatumLength(opts_hdr), .t_data = opts_hdr}, desc,
4141
values, nulls);
4242

4343
if (!nulls[0]) {
44-
csv_opts->delim = DatumGetChar(values[0]);
45-
if (is_reserved(csv_opts->delim))
44+
csv_opts->delimiter = DatumGetChar(values[0]);
45+
if (is_reserved(csv_opts->delimiter))
4646
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4747
errmsg("delimiter cannot be newline, carriage return or "
4848
"double quote")));
4949
}
5050

5151
if (!nulls[1]) {
52-
csv_opts->with_bom = DatumGetBool(values[1]);
52+
csv_opts->bom = DatumGetBool(values[1]);
5353
}
5454

5555
if (!nulls[2]) {

src/aggs.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#ifndef AGGS_H
22
#define AGGS_H
33

4+
// mirrors the SQL csv_options type
45
typedef struct {
5-
char delim;
6-
bool with_bom;
6+
char delimiter;
7+
bool bom;
78
bool header;
89
} CsvOptions;
10+
#define csv_options_count 3
911

1012
typedef struct {
1113
StringInfoData accum_buf;

src/pg_csv.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Datum csv_agg_transfn(PG_FUNCTION_ARGS) {
5959
TupleDesc tdesc =
6060
lookup_rowtype_tupdesc(HeapTupleHeaderGetTypeId(next), HeapTupleHeaderGetTypMod(next));
6161

62-
if (state->options->with_bom) appendBinaryStringInfo(&state->accum_buf, BOM, sizeof(BOM));
62+
if (state->options->bom) appendBinaryStringInfo(&state->accum_buf, BOM, sizeof(BOM));
6363

6464
// build header row
6565
if (state->options->header) {
@@ -69,10 +69,10 @@ Datum csv_agg_transfn(PG_FUNCTION_ARGS) {
6969
continue;
7070

7171
if (i > 0) // only append delimiter after the first value
72-
appendStringInfoChar(&state->accum_buf, state->options->delim);
72+
appendStringInfoChar(&state->accum_buf, state->options->delimiter);
7373

7474
char *cstr = NameStr(att->attname);
75-
csv_append_field(&state->accum_buf, cstr, strlen(cstr), state->options->delim);
75+
csv_append_field(&state->accum_buf, cstr, strlen(cstr), state->options->delimiter);
7676
}
7777

7878
appendStringInfoChar(&state->accum_buf, NEWLINE);
@@ -107,12 +107,12 @@ Datum csv_agg_transfn(PG_FUNCTION_ARGS) {
107107
if (att->attisdropped) // pg always keeps dropped columns, guard against this
108108
continue;
109109

110-
if (i > 0) appendStringInfoChar(&state->accum_buf, state->options->delim);
110+
if (i > 0) appendStringInfoChar(&state->accum_buf, state->options->delimiter);
111111

112112
if (nulls[i]) continue; // empty field for NULL
113113

114114
char *cstr = datum_to_cstring(datums[i], att->atttypid);
115-
csv_append_field(&state->accum_buf, cstr, strlen(cstr), state->options->delim);
115+
csv_append_field(&state->accum_buf, cstr, strlen(cstr), state->options->delimiter);
116116
}
117117

118118
PG_RETURN_POINTER(state);

0 commit comments

Comments
 (0)