From 5ce92492ce9a2f102b756a39486f58558218f4ca Mon Sep 17 00:00:00 2001 From: Dimitri Fontaine Date: Sat, 22 Nov 2014 00:04:34 +0100 Subject: [PATCH] Cast MySQL default values in some cases, fixing #124. When querying the default values of MySQL tables against MySQL catalogs, the default value is always returned as a string. When the column having the default actually is a "binary" column, we want the transformation functions to receive a proper vector of bytes. This patch adds some hard-coded rules to be smarted about the situation for columns of type "binary". --- src/sources.lisp | 4 ++-- src/sources/mysql/mysql-schema.lisp | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/sources.lisp b/src/sources.lisp index d6b19e7..04e23c4 100644 --- a/src/sources.lisp +++ b/src/sources.lisp @@ -431,8 +431,8 @@ "Returns suitably quoted default value for CREATE TABLE command." (cond ((null default) "NULL") - ((string= "NULL" default) default) - ((string= "CURRENT_TIMESTAMP" default) default) + ((and (stringp default) (string= "NULL" default)) default) + ((and (stringp default) (string= "CURRENT_TIMESTAMP" default)) default) (t ;; apply the transformation function to the default value (if using-cast-fn (format-pgsql-default-value diff --git a/src/sources/mysql/mysql-schema.lisp b/src/sources/mysql/mysql-schema.lisp index 05a556b..2ad1779 100644 --- a/src/sources/mysql/mysql-schema.lisp +++ b/src/sources/mysql/mysql-schema.lisp @@ -159,6 +159,15 @@ order by table_name" dbname only-tables)))) (cons (format nil "~:[~;NOT ~]REGEXP '~a'" not (cadr filter))))) filter-list)) +(defun cleanup-default-value (dtype default) + "MySQL catalog query always returns the default value as a string, but in + the case of a binary data type we actually want a byte vector." + (cond ((string= "binary" dtype) + (when default + (babel:string-to-octets default))) + + (t default))) + (defun list-all-columns (&key (dbname *my-dbname*) (table-type :table) @@ -192,10 +201,10 @@ order by table_name, ordinal_position" excluding ; do we print the clause? (filter-list-to-where-clause excluding t))) do - (let ((entry (assoc table-name schema :test 'equal)) - (column - (make-mysql-column - table-name name dtype ctype default nullable extra))) + (let* ((entry (assoc table-name schema :test 'equal)) + (def-val (cleanup-default-value dtype default)) + (column (make-mysql-column + table-name name dtype ctype def-val nullable extra))) (if entry (push column (cdr entry)) (push (cons table-name (list column)) schema)))