From f6cb428c6dae8ee663868623c102eb14c4f0a31d Mon Sep 17 00:00:00 2001 From: Dimitri Fontaine Date: Tue, 4 Jul 2017 23:15:47 +0200 Subject: [PATCH] Check empty strings in DB3 numeric fields. Another blind attempt at fixing pgloader from a bug report on gitter, see --- src/sources/db3/db3-schema.lisp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/sources/db3/db3-schema.lisp b/src/sources/db3/db3-schema.lisp index 1e99ee8..53e5b41 100644 --- a/src/sources/db3/db3-schema.lisp +++ b/src/sources/db3/db3-schema.lisp @@ -54,15 +54,17 @@ (defmethod cast ((field db3-field)) "Return the PostgreSQL type definition given the DB3 one." - (let ((type (db3-field-type field))) + (let* ((type (db3-field-type field)) + (transform + (cond ((string= type "C") #'db3-trim-string) + ((string= type "N") #'db3-numeric-to-pgsql-numeric) + ((string= type "L") #'logical-to-boolean) + ((string= type "D") #'db3-date-to-pgsql-date) + (t nil)))) (make-column :name (apply-identifier-case (db3-field-name field)) - :type-name (cdr (assoc type - *db3-pgsql-type-mapping* + :type-name (cdr (assoc type *db3-pgsql-type-mapping* :test #'string=)) - :transform (cond ((string= type "L") #'logical-to-boolean) - ((string= type "C") #'db3-trim-string) - ((string= type "D") #'db3-date-to-pgsql-date) - (t nil))))) + :transform transform))) (declaim (inline logical-to-boolean db3-trim-string @@ -76,6 +78,12 @@ "DB3 Strings a right padded with spaces, fix that." (string-right-trim '(#\Space) value)) +(defun db3-numeric-to-pgsql-numeric (value) + "DB3 numerics should be good to go, but might contain spaces." + (let ((trimmed-string (string-right-trim '(#\Space) value))) + (unless (string= "" trimmed-string) + trimmed-string))) + (defun db3-date-to-pgsql-date (value) "Convert a DB3 date to a PostgreSQL date." (when (and value (string/= "" value) (= 8 (length value)))