osiegmar / FastCSV

CSV library for Java that is fast, RFC-compliant and dependency-free.
https://fastcsv.org/
MIT License
542 stars 93 forks source link

Per-field quoting #39

Closed metametadata closed 3 years ago

metametadata commented 4 years ago

I need a way to enforce per-field quoting in order to generate CSV for PostgreSQL COPY statement because quoted empty string is treated as NULL, while totally empty field is treated as an empty string:

1,,3 ---> ""
1,"",3 ---> NULL

The current CsvAppender API doesn't support such behavior. Possible solutions:

osiegmar commented 4 years ago

According to https://www.postgresql.org/docs/current/sql-copy.html you can use \N (or other value with the NULL AS clause) to differentiate between empty and null fields.

metametadata commented 4 years ago

Yes, I'm using "NULL AS" as a workaround (\N is for text format, not CSV). The drawback of this workaround is that the sentinel null string cannot be used in the CSV as a real value anymore.

osiegmar commented 4 years ago

OK, understood. But your example is mixed up, I think. According to the doc, an unquoted empty string is handled as NULL, not a quoted one. So, what you would need is:

1,,3 ---> NULL
1,"",3 ---> ""

Is that correct?

metametadata commented 4 years ago

Ah, yes, you're right.

osiegmar commented 4 years ago

I consider this for the 2.0 release. Could you please check the commit faac27111e5acd183b0c09471b24ea8b50f35f23 to see if it covers your requirements. I don't think that a per field setting is required, though.

metametadata commented 4 years ago

Yes, looks like it will do the trick.

osiegmar commented 3 years ago

Implemented in version 2.