Allow for more options in the CSV escape character, fix #38.

To allow for importing JSON one-liners as-is in the database it can be
interesting to leverage the CSV parser in a compatible setup. That setup
requires being able to use any separator character as the escape
character.
This commit is contained in:
Dimitri Fontaine 2015-05-22 12:31:01 +02:00
parent 62931e0312
commit bffec4cc63
5 changed files with 54 additions and 5 deletions

View File

@ -878,10 +878,11 @@ The `csv` format command accepts the following clauses and options:
- *fields escaped by*
Takes either the special value *backslash-quote* or *double-quote*.
This value is used to recognize escaped field separators when they
are to be found within the data fields themselves. Defaults to
*double-quote*.
Takes either the special value *backslash-quote* or *double-quote*,
or any value supported by the *fields terminated by* option (see
below). This value is used to recognize escaped field separators
when they are to be found within the data fields themselves.
Defaults to *double-quote*.
- *fields terminated by*

View File

@ -65,7 +65,9 @@
(defrule backslash-quote "backslash-quote" (:constant "\\\""))
(defrule escaped-quote-name (or quote-quote backslash-quote))
(defrule escaped-quote-literal (or (and #\" #\") (and #\\ #\")) (:text t))
(defrule escaped-quote (or escaped-quote-literal escaped-quote-name))
(defrule escaped-quote (or escaped-quote-literal
escaped-quote-name
separator))
(defrule option-fields-escaped-by (and kw-fields kw-escaped kw-by escaped-quote)
(:lambda (esc)

View File

@ -11,6 +11,7 @@ REGRESS= allcols.load \
csv-error.load \
csv-filename-pattern.load \
csv-header.load \
csv-json.load \
csv-keep-extra-blanks.load \
csv-nulls.load \
csv-trim-extra-blanks.load \

30
test/csv-json.load Normal file
View File

@ -0,0 +1,30 @@
LOAD CSV
FROM INLINE
INTO postgresql:///pgloader?json
WITH truncate,
fields not enclosed,
fields terminated by '0x02',
fields escaped by '0x02'
BEFORE LOAD DO
$$ drop table if exists json; $$,
$$ CREATE TABLE json (json text); $$;
{"table-name": "fetch","read":0,"imported":0,"errors":0,"time":"0.000s"}
{"table-name": "fetch meta data","read":8,"imported":8,"errors":0,"time":"0.026s"}
{"table-name": "create, truncate","read":0,"imported":0,"errors":0,"time":"0.046s"}
{"table-name": "long","read":0,"imported":2,"errors":0,"time":"0.069s"}
{"table-name": "blobs","read":0,"imported":1,"errors":0,"time":"0.021s"}
{"table-name": "unsigned","read":0,"imported":2,"errors":0,"time":"0.007s"}
{"table-name": "reals","read":0,"imported":3,"errors":0,"time":"0.007s"}
{"table-name": "ints","read":0,"imported":3,"errors":0,"time":"0.006s"}
{"table-name": "def","read":0,"imported":2,"errors":0,"time":"0.007s"}
{"table-name": "stamps","read":0,"imported":2,"errors":0,"time":"0.007s"}
{"table-name": "character","read":0,"imported":4,"errors":0,"time":"0.005s"}
{"table-name": "index build completion","read":0,"imported":0,"errors":0,"time":"0.000s"}
{"table-name": "Create Indexes","read":0,"imported":0,"errors":0,"time":"0.000s"}
{"table-name": "Reset Sequences","read":0,"imported":0,"errors":0,"time":"0.015s"}
{"table-name": "Total streaming time","read":0,"imported":19,"errors":0,"time":"0.216s"}

View File

@ -0,0 +1,15 @@
{"table-name": "fetch","read":0,"imported":0,"errors":0,"time":"0.000s"},
{"table-name": "fetch meta data","read":8,"imported":8,"errors":0,"time":"0.026s"},
{"table-name": "create, truncate","read":0,"imported":0,"errors":0,"time":"0.046s"},
{"table-name": "long","read":0,"imported":2,"errors":0,"time":"0.069s"},
{"table-name": "blobs","read":0,"imported":1,"errors":0,"time":"0.021s"},
{"table-name": "unsigned","read":0,"imported":2,"errors":0,"time":"0.007s"},
{"table-name": "reals","read":0,"imported":3,"errors":0,"time":"0.007s"},
{"table-name": "ints","read":0,"imported":3,"errors":0,"time":"0.006s"},
{"table-name": "def","read":0,"imported":2,"errors":0,"time":"0.007s"},
{"table-name": "stamps","read":0,"imported":2,"errors":0,"time":"0.007s"},
{"table-name": "character","read":0,"imported":4,"errors":0,"time":"0.005s"},
{"table-name": "index build completion","read":0,"imported":0,"errors":0,"time":"0.000s"},
{"table-name": "Create Indexes","read":0,"imported":0,"errors":0,"time":"0.000s"},
{"table-name": "Reset Sequences","read":0,"imported":0,"errors":0,"time":"0.015s"},
{"table-name": "Total streaming time","read":0,"imported":19,"errors":0,"time":"0.216s"}