Move DB3 transformations function in pglaoder.transforms package.

That makes them available for user defined cast rules.

Fix #929.
This commit is contained in:
Dimitri Fontaine 2019-04-29 16:52:24 +02:00
parent febb2c11be
commit d4369cc605
2 changed files with 40 additions and 38 deletions

View File

@ -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)))))

View File

@ -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)))))