Ff00ff/mammoth

Bug: alias "name" needs to have wrapped quotes

Closed this issue · 3 comments

A query that tries to use 'name' as an alias will fail, like below:

await db
  .select(
    db.users.fullName.as('name')
  )
  .from(db.users)
ERROR:  syntax error at or near "name"

It looks like 'name' is a reserved keyword in this particular context.

I took a look at

export const wrapQuotes = (string: string) => {
const isCamelCase = string.match(/[A-Z]/);
const isReserved = reservedKeywords.has(string);
const shouldWrap = isReserved || isCamelCase;
return shouldWrap ? `"${string}"` : string;
};

and I'm thinking a solution could be to always wrap with double quotes. No harm in always wrapping, right?

Yes, a solution could be to just always wrap. There is no harm, but if that's the bar to decide whether to do something or not I think we're setting the bar very low. The idea is for the SQL output of mammoth to be decent, nice and maybe comparable to how you would write it yourself.

Having said that, the solution I think is to include the name keyword in the reservedKeywords list. The list is taken from https://www.postgresql.org/docs/12/sql-keywords-appendix.html but I'm assuming I excluded all the non-reserved keywords even though that should be included.

Is this something you could pick up in a pull request?

Similar issue applies for the word group when calling await db.insertInto(db.table).values({id, value, lastUpdate: new Date(), group}) I get error: syntax error at or near "group". Seems a separate issue, addressed it in PR #273

This is fixed. There is now an extended reserved keywords list which the .as() / alias uses so name gets wrapped in quotes.

The fix is in master and will be available in the next release in a few days.