Thanks to Qmynd, no need for the IS NULL query dance anymore.

This commit is contained in:
Dimitri Fontaine 2013-12-09 11:22:52 +01:00
parent 47d8e0af1c
commit 477a52a6d3
2 changed files with 21 additions and 24 deletions

View File

@ -310,8 +310,10 @@ GROUP BY table_name, index_name;" dbname))
(:point (format nil "astext(`~a`) as `~a`" name name))
(t (format nil "`~a`" name))))
(defun get-column-list-with-is-nulls (dbname table-name)
"We can't just SELECT *, we need to cater for NULLs in text columns ourselves.
(defun get-column-list (dbname table-name)
"Some MySQL datatypes have a meaningless default output representation, we
need to process them on the SQL side (geometric data types).
This function assumes a valid connection to the MySQL server has been
established already."
(loop
@ -321,14 +323,7 @@ GROUP BY table_name, index_name;" dbname))
where table_schema = '~a' and table_name = '~a'
order by ordinal_position" dbname table-name)
:result-type 'list)
for is-null = (member type '("char" "varchar" "text"
"tinytext" "mediumtext" "longtext")
:test #'string-equal)
collect nil into nulls
collect (get-column-sql-expression name type) into cols
when is-null
collect (format nil "`~a` is null" name) into cols and collect t into nulls
finally (return (values cols nulls))))
collect (get-column-sql-expression name type)))
(declaim (inline fix-nulls))

View File

@ -54,19 +54,18 @@
(mysql-query "SET NAMES 'utf8'")
(mysql-query "SET character_set_results = utf8;")
(multiple-value-bind (cols nulls)
(get-column-list-with-is-nulls dbname table-name)
(let* ((sql (format nil "SELECT ~{~a~^, ~} FROM ~a;" cols table-name))
(row-fn
(lambda (row)
(pgstate-incf *state* (target mysql) :read 1)
(funcall process-row-fn (fix-nulls row nulls)))))
(handler-bind
((babel-encodings:character-decoding-error
#'(lambda (e)
(pgstate-incf *state* (target mysql) :errs 1)
(log-message :error "~a" e))))
(mysql-query sql :row-fn row-fn)))))))
(let* ((cols (get-column-list dbname table-name))
(sql (format nil "SELECT ~{~a~^, ~} FROM ~a;" cols table-name))
(row-fn
(lambda (row)
(pgstate-incf *state* (target mysql) :read 1)
(funcall process-row-fn row))))
(handler-bind
((babel-encodings:character-decoding-error
#'(lambda (e)
(pgstate-incf *state* (target mysql) :errs 1)
(log-message :error "~a" e))))
(mysql-query sql :row-fn row-fn))))))
;;;
;;; Use map-rows and pgsql-text-copy-format to fill in a CSV file on disk
@ -125,7 +124,10 @@
(loop for tasks below 2 do (lp:receive-result channel)
finally
(log-message :info "COPY ~a.~a done." dbname table-name)
(unless k-s-p (lp:end-kernel))))))
(unless k-s-p (lp:end-kernel))))
;; return the copy-mysql object we just did the COPY for
mysql))
;;;