hiddentao / squel

:office: SQL query string builder for Javascript
https://hiddentao.github.io/squel
MIT License
1.56k stars 231 forks source link

Multiple dots in alias causes invalid quotation of field aliases #89

Open alekbarszczewski opened 10 years ago

alekbarszczewski commented 10 years ago

sequel = require 'squel'

squelOptions =
  autoQuoteTableNames: true
  autoQuoteFieldNames: true
  nameQuoteCharacter: '"'
  tableAliasQuoteCharacter: '"'

q = sequel.select(squelOptions)
    .from('comment','User.Comment')
    .field('User.Comment.content','User.Comment.content')

console.log q.toString()

Code above produces:

SELECT "User"."Comment"."content" AS "User.Comment.content" FROM "comment" "User.Comment"

What I want is to get: ("User.Comment"."content" AS... instead of "User"."Comment"."content")

SELECT "User.Comment"."content" AS "User.Comment.content" FROM "comment" "User.Comment"

I tried this:

.field('User.Comment.content','"User.Comment".content')

But it just generates ""User"."Comment""."content" AS...

Maybe Squel should for example check if there already exist any quotation character (" in this example) in field name string and if it does then don't auto quote?

Maybe there is other option to make it work as I want?

hiddentao commented 10 years ago

I think you want the ignorePeriodsForFieldNameQuotes option, see http://hiddentao.github.io/squel/#autoquotes

alert(
  squel.select({ autoQuoteTableNames: true, autoQuoteFieldNames: true })
    .from("students", "s")
    .field("s.name", "Student name", { ignorePeriodsForFieldNameQuotes: true })
);

/*   SELECT `s.name` AS "Student name" FROM `students` `s`  */
alekbarszczewski commented 10 years ago

Not exactly. When using ignorePeriodsForFieldNameQuotes whole field name is quoted regardless of periods presence. What I need is "User.Comment"."content". Anyway I just turned off autoQuoteFieldNames and I am quoting field names manually.

hiddentao commented 10 years ago

Ah I see. Yeah, that's not supported out of the box as squel doesn't inherently understand the field name structure.

hiddentao commented 9 years ago

This could perhaps be fixed by having Squel assume that the last period (.) separates the field name from the db-table specifier. Right now it just tokenizes around every period (.) and adds quotes. What do people think?

nlang commented 8 years ago

I think there has to be a more flexible solution. I am working on a flavour/port for SAP HANA Database, and I'm struggling with a similar problem regarding table aliases in front of field names where I would need something like "t0"."type.field".

hiddentao commented 8 years ago

Sounds like the user needs to be able to specify a custom function for handling the quotation of field names.

hiddentao commented 8 years ago

Sounds like the user needs to be able to specify a custom function for handling the quotation of field names.