Improve column formatting.

Have a pretty-print option where we try to be nice for the reader, and
don't use it in the CAST debug messages. Also allow working with the
real maximum length of column names rather than hardcoding 22 cols...
This commit is contained in:
Dimitri Fontaine 2016-03-16 21:46:41 +01:00
parent f1fe9ab702
commit 3e8b7df0d3
2 changed files with 18 additions and 6 deletions

View File

@ -116,10 +116,15 @@
(format s "CREATE TABLE~:[~; IF NOT EXISTS~] ~a ~%(~%"
if-not-exists
(format-table-name table))
(loop
:for (col . last?) :on (table-column-list table)
:for pg-coldef := (format-column col)
:do (format s " ~a~:[~;,~]~%" pg-coldef last?))
(let ((max (reduce #'max
(mapcar #'length
(mapcar #'column-name (table-column-list table))))))
(loop
:for (col . last?) :on (table-column-list table)
:for pg-coldef := (format-column col
:pretty-print t
:max-column-name-length max )
:do (format s " ~a~:[~;,~]~%" pg-coldef last?)))
(format s ");~%")))
(defun drop-table-if-exists-sql (table)

View File

@ -29,11 +29,18 @@
nil)))
(format nil "'~a'" default)))))
(defmethod format-column ((column column))
(defgeneric format-column (column &key pretty-print)
(:documentation "Format COLUMN definition for CREATE TABLE purpose."))
(defmethod format-column ((column column)
&key
pretty-print
((:max-column-name-length max)))
"Format the PostgreSQL data type."
(format nil
"~a ~22t ~a~:[~*~;~a~]~:[ not null~;~]~:[~; default ~a~]"
"~a~vt~a~:[~*~;~a~]~:[ not null~;~]~:[~; default ~a~]"
(column-name column)
(if pretty-print (or (+ 2 max) 22) 1)
(column-type-name column)
(column-type-mod column)
(column-type-mod column)