diff --git a/src/sources/db3/db3-cast-rules.lisp b/src/sources/db3/db3-cast-rules.lisp index 0ae96cb..908c670 100644 --- a/src/sources/db3/db3-cast-rules.lisp +++ b/src/sources/db3/db3-cast-rules.lisp @@ -10,23 +10,23 @@ (defparameter *db3-default-cast-rules* `((:source (:type "C") :target (:type "text") - :using db3-trim-string) + :using pgloader.transforms::db3-trim-string) (:source (:type "N") :target (:type "numeric") - :using db3-numeric-to-pgsql-numeric) + :using pgloader.transforms::db3-numeric-to-pgsql-numeric) (:source (:type "L") :target (:type "boolean") - :using logical-to-boolean) + :using pgloader.transforms::logical-to-boolean) (:source (:type "D") :target (:type "date") - :using db3-date-to-pgsql-date) + :using pgloader.transforms::db3-date-to-pgsql-date) (:source (:type "M") :target (:type "text") - :using db3-trim-string)) + :using pgloader.transforms::db3-trim-string)) "Data Type Casting rules to migrate from DB3 to PostgreSQL") (defstruct (db3-field @@ -38,35 +38,3 @@ (let ((table-name (table-name table))) (with-slots (name type length default nullable extra) field (apply-casting-rules table-name name type type default nullable extra)))) - -;;; -;;; Transformation functions -;;; -(declaim (inline logical-to-boolean - db3-trim-string - db3-numeric-to-pgsql-numeric - db3-date-to-pgsql-date)) - -(defun logical-to-boolean (value) - "Convert a DB3 logical value to a PostgreSQL boolean." - (if (string= value "?") nil value)) - -(defun db3-trim-string (value) - "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))) - (let ((year (parse-integer (subseq value 0 4) :junk-allowed t)) - (month (parse-integer (subseq value 4 6) :junk-allowed t)) - (day (parse-integer (subseq value 6 8) :junk-allowed t))) - (when (and year month day) - (format nil "~4,'0d-~2,'0d-~2,'0d" year month day))))) - diff --git a/src/utils/transforms.lisp b/src/utils/transforms.lisp index 6405117..a87614f 100644 --- a/src/utils/transforms.lisp +++ b/src/utils/transforms.lisp @@ -98,7 +98,13 @@ varbinary-to-string base64-decode hex-to-dec - byte-vector-to-hexstring)) + byte-vector-to-hexstring + + ;; db3 specifics + logical-to-boolean + db3-trim-string + db3-numeric-to-pgsql-numeric + db3-date-to-pgsql-date)) ;;; @@ -483,3 +489,31 @@ (null nil) (integer hex-string) (string (write-to-string (parse-integer hex-string :radix 16))))) + + +;;; +;;; DBF/DB3 transformation functions +;;; + +(defun logical-to-boolean (value) + "Convert a DB3 logical value to a PostgreSQL boolean." + (if (string= value "?") nil value)) + +(defun db3-trim-string (value) + "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))) + (let ((year (parse-integer (subseq value 0 4) :junk-allowed t)) + (month (parse-integer (subseq value 4 6) :junk-allowed t)) + (day (parse-integer (subseq value 6 8) :junk-allowed t))) + (when (and year month day) + (format nil "~4,'0d-~2,'0d-~2,'0d" year month day)))))