From c38ef4c2356cb6493b97fb2bb51982bbd8bdd3c3 Mon Sep 17 00:00:00 2001 From: Victor Kryukov Date: Wed, 18 Feb 2015 17:09:38 -0800 Subject: [PATCH] Make quoting identifiers more robust: do not quote already quoted string, and double quotes when quoting. Fix #180. --- src/pgsql/schema.lisp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/pgsql/schema.lisp b/src/pgsql/schema.lisp index dba801e..ddd3259 100644 --- a/src/pgsql/schema.lisp +++ b/src/pgsql/schema.lisp @@ -6,6 +6,12 @@ (defvar *pgsql-reserved-keywords* nil "We need to always quote PostgreSQL reserved keywords") +(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 @@ -18,7 +24,11 @@ ;; SQL identifiers and key words must begin with a letter (a-z, but ;; also letters with diacritical marks and non-Latin letters) or an ;; underscore (_). - (cond ((cl-ppcre:scan "^[^A-Za-z_]" identifier) + (cond + ((quoted-p identifier) + :none) + + ((cl-ppcre:scan "^[^A-Za-z_]" identifier) :quote) ((member lowercase-identifier *pgsql-reserved-keywords* @@ -34,7 +44,8 @@ (ecase *identifier-case* (:downcase lowercase-identifier) - (:quote (format nil "\"~a\"" identifier)) + (:quote (format nil "\"~a\"" + (cl-ppcre:regex-replace-all "\"" identifier "\"\""))) (:none identifier)))) ;;;