From 3e8b7df0d34aef7d95dbc63e52dc33b573963f2c Mon Sep 17 00:00:00 2001 From: Dimitri Fontaine Date: Wed, 16 Mar 2016 21:46:41 +0100 Subject: [PATCH] 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... --- src/pgsql/schema.lisp | 13 +++++++++---- src/utils/pg-format-column.lisp | 11 +++++++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/pgsql/schema.lisp b/src/pgsql/schema.lisp index 5140333..b95748e 100644 --- a/src/pgsql/schema.lisp +++ b/src/pgsql/schema.lisp @@ -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) diff --git a/src/utils/pg-format-column.lisp b/src/utils/pg-format-column.lisp index f215d75..9ec100b 100644 --- a/src/utils/pg-format-column.lisp +++ b/src/utils/pg-format-column.lisp @@ -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)