Skip to content

doubleQuoteIfNecessary doesn't escape internal double quotes, producing malformed SQL identifiers #1697

@haskiindahouse

Description

@haskiindahouse

The helper that quotes non-ASCII identifiers wraps the input in "…" but never escapes embedded " characters, so an identifier like my"col renders as "my"col" — which most SQL parsers will read as identifier my followed by col" and fail.

The function is duplicated in two codegen paths:

private def doubleQuoteIfNecessary(s: String): String =
if identifierPattern.matches(s) then
s
else
s""""${s}""""

private def doubleQuoteIfNecessary(s: String): String =
  if identifierPattern.matches(s) then
    s
  else
    s""""${s}""""

private def doubleQuoteIfNecessary(s: String): String =
if s.matches("^[_a-zA-Z][_a-zA-Z0-9]*$") then
s
else
s""""${s}""""

private def doubleQuoteIfNecessary(s: String): String =
  if s.matches("^[_a-zA-Z][_a-zA-Z0-9]*$") then
    s
  else
    s""""${s}""""

The single-quote case is already handled correctly elsewhere (SqlGenerator.scala line 1710 uses replaceAll("'", "''")); only the double-quote path was missed.

private def doubleQuoteIfNecessary(s: String): String =
  if s.matches("^[_a-zA-Z][_a-zA-Z0-9]*$") then
    s
  else
    s""""${s.replace("\"", "\"\"")}""""

Happy to PR if you'd like — though the duplication between SqlGenerator and GenSQL looks like it could collapse into a shared helper.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions