doug-martin / goqu

SQL builder and query library for golang
http://doug-martin.github.io/goqu/
MIT License
2.37k stars 207 forks source link

Add support for escaping double quotes in Postgres Identifiers #428

Open nickzelei opened 1 month ago

nickzelei commented 1 month ago

Is your feature request related to a problem? Please describe. Hello 👋

So we use Goqu all throughout Neosync and I've just run into a little issue with special characters in schemas and tables (I guess columns as well). Goqu handles special characters pretty well by simply escaping the schema/table in double quotes. However, where this gets weird is if the schema/table itself contains a double quote.

Something like: user"s_data must be escaped like this: "users""s_data".

CREATE TABLE "user""s_data" (id TEXT NOT NULL PRIMARY KEY);

SELECT * from "users""s_data";

You can also do wild things like "myescaped_table", which to be valid SQL, becomes this grotesque statement:

SELECT * FROM """myescapedschema"""."""myescapedtable""";

Describe the solution you'd like I would love if Goqu could handle this for me when using goqu.T. I'm sure there are a lot of edge cases here, and possibly other characters? But double quotes is the only one I've been actively testing with that breaks when passing it into Goqu. Also unclear if it should even be in Goqu's wheelhouse to do something like this.

This is pretty advanced, but a part of what Neosync is doing is retrieving schema information from the pg_catalog and then using that to do other queries. So in most cases, it comes out of the database like users"s_data, because, well, that is what it is actually named! But it makes for a real bear when trying to actually query the thing.

Describe alternatives you've considered The main alternative I've considered is imply writing a function that does this for me before passing it into goqu.T

Dialect

I imagine this is an issue with any of the supported dialects that have special characters that allow double quotes.

Additional context Love this library Doug. Has made handling SQL in Neosync much, much smoother that it would have been otherwise.