layerware / hugsql

A Clojure library for embracing SQL
https://hugsql.org
Apache License 2.0
690 stars 54 forks source link

Make % an allowed keyword character #136

Closed Rovanion closed 2 years ago

Rovanion commented 2 years ago

I encountered a database column with a percentage sign in its name. When trying to list it using a generic sql function that inserts a row with column names based on the keys given in a map, I found that HugSQL would lex :portion_% into :portion_ and then error out telling the user:

Parameter Mismatch: :portion_ parameter data not found.

This commit expands the characters allowed in keywords to include the percentage sign %.

Rovanion commented 2 years ago
;; This is an interesting expression. Below we modify the internals of hugsql.parser so that %\
 is allowed as a keyword character.                                                            
(intern 'hugsql.parser 'symbol-char?
        (fn [c]
          (boolean (re-matches #"[\pL\pM\pS\d\_\-\.\+\*\?\:\/%]" (str c)))))

Until a patched version is released I'll leave here the above jank monkeypatch.

csummers commented 2 years ago

This is probably OK to allow. The actual implementation of the Clojure reader accepts % in a keyword/symbol, but the reader docs don't specifically allow it (https://clojure.org/reference/reader#_symbols):

Symbols begin with a non-numeric character and can contain alphanumeric characters and *, +, !, -, _, ', ?, <, > and = (other characters may be allowed eventually).

On the other hand, the clojure.edn docs do specifically allow % (https://github.com/edn-format/edn#symbols):

Symbols begin with a non-numeric character and can contain alphanumeric characters and . * + ! - _ ? $ % & = < >.

So, let's include it! Thanks for the PR!

Rovanion commented 2 years ago

Cheerio!

csummers commented 2 years ago

Released in 0.5.3. Thanks again!