diff --git a/src/package.lisp b/src/package.lisp index 57befea..1aa7217 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -35,6 +35,7 @@ (:export #:apply-identifier-case #:build-identifier #:quoted-p + #:ensure-quoted #:ensure-unquoted #:camelCase-to-colname)) diff --git a/src/pgsql/pgsql-ddl.lisp b/src/pgsql/pgsql-ddl.lisp index 4fe2316..a6f1b2a 100644 --- a/src/pgsql/pgsql-ddl.lisp +++ b/src/pgsql/pgsql-ddl.lisp @@ -144,7 +144,7 @@ (make-column :default transformed-default))) (format-default-value transformed-column)) (if default - (format stream "'~a'" default) + (ensure-quoted default #\') (format stream "NULL")))))) diff --git a/src/utils/quoting.lisp b/src/utils/quoting.lisp index f6cc05d..d52702d 100644 --- a/src/utils/quoting.lisp +++ b/src/utils/quoting.lisp @@ -7,11 +7,6 @@ (in-package :pgloader.quoting) -(defun quoted-p (s) - "Return true if s is a double-quoted string" - (and (eq (char s 0) #\") - (eq (char s (- (length s) 1)) #\"))) - (defun apply-identifier-case (identifier) "Return given IDENTIFIER with CASE handled to be PostgreSQL compatible." (let* ((lowercase-identifier (cl-ppcre:regex-replace-all @@ -47,6 +42,11 @@ (cl-ppcre:regex-replace-all "\"" identifier "\"\""))) (:none identifier)))) +(defun quoted-p (s &optional (quote-char #\")) + "Return true if s is a double-quoted string" + (and (eq (char s 0) quote-char) + (eq (char s (- (length s) 1)) quote-char))) + (defun ensure-unquoted (identifier) (cond ((quoted-p identifier) ;; when the table name comes from the user (e.g. in the @@ -57,6 +57,11 @@ (t identifier))) +(defun ensure-quoted (value &optional (quote-char #\")) + (if (quoted-p value quote-char) + value + (format nil "~c~a~c" quote-char value quote-char))) + (defun build-identifier (sep &rest parts) "Concatenante PARTS into a PostgreSQL identifier, with SEP in between parts. That's useful for creating an index name from a table's oid and name."