From 4a7d46dd1fb3ee11949b99c8569ddc85a734571a Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Thu, 16 Nov 2023 22:30:35 -0800 Subject: [PATCH] partial #514 solution improve default quoting strategy (all numeric with _ is OK, leading alpha with optional alphanumeric/_ is OK, entity with leading digit then alphanumeric needs quoting). plus *always-quote* dynamic var that can be bound to regex. --- src/honey/sql.cljc | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 187bef2..f86d74b 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -138,6 +138,9 @@ ;; in entities; if someone complains about this check, an option ;; can be added to format to turn this on: (def ^:private ^:dynamic *allow-suspicious-entities* false) +;; adding this as an experimental approach for #514 -- will become private +;; and an option added if this works out: +(def ^:dynamic *always-quote* nil) ;; "linting" mode (:none, :basic, :strict): (def ^:private ^:dynamic *checking* @default-checking) ;; the current DSL hash map being formatted (for clause-body / contains-clause?): @@ -243,6 +246,14 @@ ;; WHERE `mulog/data.account` = 'foo-account-id' LIMIT 2000 SINCE 2 DAYS AGO"] ) +(def ^:private alphanumeric + "Basic regex for entities that do not need quoting. + Either: + * all numeric + * leading alpha followed by optional alphanumerics + (with _ allowed in either)" + #"^([0-9_]+|[A-Za-z_][A-Za-z0-9_]*)$") + (defn format-entity "Given a simple SQL entity (a keyword or symbol -- or string), return the equivalent SQL fragment (as a string -- no parameters). @@ -265,9 +276,13 @@ ;; characters in entity, then quote it: (nil? *quoted*) (fn opt-quote [part] - (if (re-find #"^[A-Za-z0-9_]+$" part) - part - (dialect-q part))) + (cond (re-find alphanumeric part) + part + (and *always-quote* + (re-find *always-quote* part)) + (dialect-q part) + :else + (dialect-q part))) :else identity) parts-fn (or (:parts-fn *dialect*)