Skip to content

Formatting issue after LIKE Predicate Escape Character '\' #31

Open

Description

@Darphbobo1573

This only seems to happen when using '' as the escape character. I've tired all the supported languages using the of(...) with the same results. I've provide a simple sample code and console output.

String a = "SELECT * FROM psprcsrqst WHERE prcsname LIKE 'A\_%' ESCAPE '\' AND prcstype = :1";
System.out.println(SqlFormatter.format(a));

Output:
SELECT
*
FROM
psprcsrqst
WHERE
prcsname LIKE 'A_%' ESCAPE '' AND prcstype = :1

Using some other character, in this case '!'
String a = "SELECT * FROM psprcsrqst WHERE prcsname LIKE 'A!_%' ESCAPE '!' AND prcstype = :1";
System.out.println(SqlFormatter.format(a));

Output:
SELECT
*
FROM
psprcsrqst
WHERE
prcsname LIKE 'A!_%' ESCAPE '!'
AND prcstype = :1

Activity

  1. vertical-blank commented on Jan 21, 2021

    @vertical-blank
    Owner

    It seems that your sinppet contains compilation error about escape sequences.

    error: illegal escape character
        String a = "SELECT * FROM psprcsrqst WHERE prcsname LIKE 'A\_%' ESCAPE '\' AND prcstype = :1";
                                                                    ^
    1 error
    

    When I correct it as:

        String a = "SELECT * FROM psprcsrqst WHERE prcsname LIKE 'A\\_%' ESCAPE '\\' AND prcstype = :1";
    

    Then output is:

    SELECT
      *
    FROM
      psprcsrqst
    WHERE
      prcsname LIKE 'A\_%' ESCAPE '\' AND prcstype = :1
    
  2. Darphbobo1573 commented on Jan 21, 2021

    @Darphbobo1573
    Author

    Yes I see the typo I had for which I'm sorry; However, the output you got after correcting the typo, when using the backslash as the escape character, is still not formatted properly as the AND prcstype = :1 didn't wrap to the next line as one would expect when formatting. This behavior is what I was trying to convey as the issue, albeit I slight issue.

  3. manticore-projects commented on May 29, 2023

    @manticore-projects

    Greetings.

    With JSQLFormatter, those queries will be formatted well.
    Although it took me quite some effort to get it right: The problem is the greediness of the Regular Expressions, since \' would be interpreted as escaped ' already. You will need to mangle the matches as shown for QUOTED_IDENTIFIER at https://github.com/manticore-projects/JSqlParser/blob/5e7732c870a66afff5b39214b2cfbb0409e12585/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt#L562

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions