Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Per-field quoting #39

Closed
metametadata opened this issue Sep 27, 2019 · 7 comments
Closed

Per-field quoting #39

metametadata opened this issue Sep 27, 2019 · 7 comments
Milestone

Comments

@metametadata
Copy link

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:

  • add an additional flag to appendField
  • add a new method appendDelimitedField
  • make alwaysDelimitText field mutable so that the consumer can turn it off/on before appending the specific field
@osiegmar
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

Ah, yes, you're right.

@osiegmar
Copy link
Owner

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

@metametadata
Copy link
Author

Yes, looks like it will do the trick.

@osiegmar osiegmar added this to the 2.0 milestone Nov 4, 2020
@osiegmar
Copy link
Owner

osiegmar commented Jan 1, 2021

Implemented in version 2.

@osiegmar osiegmar closed this as completed Jan 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants