Move all typemod functions at the same place.

Having the parse-column-typemod function in the pgloader.transforms package
makes it available from everywhere in the pgloader code base.
This commit is contained in:
Dimitri Fontaine 2018-05-19 19:13:50 +02:00
parent 1f354131d0
commit 8fce6c84fc
3 changed files with 27 additions and 23 deletions

View File

@ -14,6 +14,8 @@
(:export #:precision
#:scale
#:intern-symbol
#:parse-column-typemod
#:typemod-expr-matches-p
#:typemod-expr-to-function))
(defpackage #:pgloader.logs
@ -439,6 +441,8 @@
#:precision
#:scale
#:intern-symbol
#:parse-column-typemod
#:typemod-expr-matches-p
#:typemod-expr-to-function)
(:import-from #:pgloader.parse-date
#:parse-date-string

View File

@ -10,29 +10,6 @@
(defvar *default-cast-rules* nil "Default casting rules.")
(defvar *cast-rules* nil "Specific casting rules added in the command.")
;;;
;;; Handling typmod in the general case, don't apply to ENUM types
;;;
(defun parse-column-typemod (data-type column-type)
"Given int(7), returns the number 7.
Beware that some data-type are using a typmod looking definition for
things that are not typmods at all: enum."
(unless (or (string= "enum" data-type)
(string= "set" data-type))
(let ((start-1 (position #\( column-type)) ; just before start position
(end (position #\) column-type))) ; just before end position
(when start-1
(destructuring-bind (a &optional b)
(mapcar #'parse-integer
(sq:split-sequence #\, column-type
:start (+ 1 start-1) :end end))
(list a b))))))
(defun typemod-expr-matches-p (rule-typemod-expr typemod)
"Check if an expression such as (< 10) matches given typemod."
(funcall (compile nil (typemod-expr-to-function rule-typemod-expr)) typemod))
(defun parse-column-unsigned (data-type column-type)
"See if we find the term unsigned in the column-type."
(declare (ignore data-type))

View File

@ -41,6 +41,29 @@
(intern (string-upcase symbol-name)
(find-package "PGLOADER.USER-SYMBOLS"))))))))
;;;
;;; Handling typmod in the general case, don't apply to ENUM types
;;;
(defun parse-column-typemod (data-type column-type)
"Given int(7), returns the number 7.
Beware that some data-type are using a typmod looking definition for
things that are not typmods at all: enum."
(unless (or (string= "enum" data-type)
(string= "set" data-type))
(let ((start-1 (position #\( column-type)) ; just before start position
(end (position #\) column-type))) ; just before end position
(when start-1
(destructuring-bind (a &optional b)
(mapcar #'parse-integer
(sq:split-sequence #\, column-type
:start (+ 1 start-1) :end end))
(list a b))))))
(defun typemod-expr-matches-p (rule-typemod-expr typemod)
"Check if an expression such as (< 10) matches given typemod."
(funcall (compile nil (typemod-expr-to-function rule-typemod-expr)) typemod))
(defun typemod-expr-to-function (expr)
"Transform given EXPR into a callable function object."
`(lambda (typemod)