From f5c703c206c782c183fc25371c4feb33296d64b8 Mon Sep 17 00:00:00 2001 From: Dimitri Fontaine Date: Mon, 16 Jun 2014 17:33:14 +0200 Subject: [PATCH] Handle camelCase column names for CSV, fix #79 again. The previous patch didn't take into account the need to retain the case of the PostgreSQL column names when using double-quotes in the load command, which is now properly forwarded down in the COPY command. --- src/parser.lisp | 8 ++++++-- src/sources/csv.lisp | 5 ++++- test/csv.load | 6 +++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/parser.lisp b/src/parser.lisp index 8c9e457..c79df24 100644 --- a/src/parser.lisp +++ b/src/parser.lisp @@ -1558,13 +1558,17 @@ load database (:lambda (options) (alexandria:alist-plist options))) -(defrule csv-bare-field-name (and (or #\_ (alpha-char-p character)) +(defrule csv-raw-field-name (and (or #\_ (alpha-char-p character)) (* (or (alpha-char-p character) (digit-char-p character) #\_))) (:text t)) -(defrule csv-quoted-field-name (and #\" csv-bare-field-name #\") +(defrule csv-bare-field-name csv-raw-field-name + (:lambda (name) + (string-downcase name))) + +(defrule csv-quoted-field-name (and #\" csv-raw-field-name #\") (:lambda (csv-field-name) (destructuring-bind (open name close) csv-field-name (declare (ignore open close)) diff --git a/src/sources/csv.lisp b/src/sources/csv.lisp index a54dc2a..834b844 100644 --- a/src/sources/csv.lisp +++ b/src/sources/csv.lisp @@ -134,7 +134,10 @@ ;; this function update :rows stats #'pgloader.pgsql:copy-from-queue dbname table-name queue ;; we only are interested into the column names here - :columns (mapcar #'car (columns csv)) + :columns (mapcar (lambda (col) + ;; always double quote column names + (format nil "~s" (car col))) + (columns csv)) :truncate truncate) ;; now wait until both the tasks are over diff --git a/test/csv.load b/test/csv.load index 9e1ab9d..8e6d21c 100644 --- a/test/csv.load +++ b/test/csv.load @@ -16,8 +16,8 @@ */ LOAD CSV - FROM inline (x, y, a, b, c, d) - INTO postgresql:///pgloader?csv (a, b, d, c) + FROM inline (x, y, a, b, c, "camelCase") + INTO postgresql:///pgloader?csv (a, b, "camelCase", c) WITH truncate, skip header = 1, @@ -35,7 +35,7 @@ LOAD CSV a bigint, b bigint, c char(2), - d text + "camelCase" text ); $$;