Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: DelayedLoadRelations uses inconsistent quoting for string-literals. #95

Conversation

vnayar
Copy link
Contributor

@vnayar vnayar commented Nov 6, 2024

If using HQL on an Entity that is implicitly joined to other Entities via @OnetoOne, @onetomany, @manytoone, or @manytomany, and an ID value has a single-quote (') in it, the HQL-escaping ('') is not unescaped, and it gets escaped into the dialect, resulting in (\'\'), and a query that has a syntax error.

Example debug statements:

[trace] session.d:1207:delayedLoadRelations unknownKeys = [Bucky O'Hare] [trace] session.d:1208:delayedLoadRelations createCommaSeparatedKeyList(unknownKeys) = 'Bucky O''Hare'
                                                               extra quote is being added here ^
[trace] session.d:1290:listObjects SQL: SELECT _t1.contract_code, ... FROM finance_contract_refs AS _t1 WHERE _t1.contract_code IN ( 'Bucky O\\'\\'Hare') 

The error is as follows when using a PostgreSQL dialect.

Fatal error executing prepared statement SELECT _t1.contract_code, ...  FROM finance_contract_refs AS _t1 WHERE _t1.contract_code IN ( 'Bucky O\\'\\'Hare'): ERROR:  syntax error at or near "\\"\nLINE 1: ...HERE _t1.contract_code IN ( 'Bucky O\\'\\'Hare...\n     

The fix is to modify the HQL Parser to unescape ('') as ('), and then let specific dialects re-escape the ('). PostgreSQL, by default, does not support C-style escape characters unless quoted as E'hello\nthere\n' (note the leading (E)). However, all dialects support the SQL standard ('') syntax.

If using HQL on an Entity that is implicitly joined to other Entities via @OnetoOne, @onetomany, @manytoone, or @manytomany,
and an ID value has a single-quote (') in it, the HQL-escaping ('') is not unescaped, and it gets escaped into the dialect,
resulting in (\\'\\'), and a query that fails to join.

Example debug statements:
===
[trace] session.d:1207:delayedLoadRelations unknownKeys = [Bucky O'Hare]
[trace] session.d:1208:delayedLoadRelations createCommaSeparatedKeyList(unknownKeys) = 'Bucky O''Hare'
                                                               extra quote is being added here ^
[trace] session.d:1290:listObjects SQL: SELECT _t1.contract_code, ... FROM finance_contract_refs AS _t1 WHERE _t1.contract_code IN ( 'Bucky O\\'\\'Hare')
===

The fix is to modify the HQL Parser to unescape ('') as ('), and then let specific dialects re-escape the ('). PostgreSQL, by default,
does not support C-style escape characters unless quoted as E'hello\nthere\n' (note the leading (E)). However, all dialects support
the SQL standard ('') syntax.
@SingingBush
Copy link
Collaborator

this seems fine but perhaps there also needs to be a test covering the escaped ' within HQL string rather than a parameter. eg: sess.createQuery("FROM Asset WHERE name='Bucky O''Hare'").list!Asset;

@vnayar
Copy link
Contributor Author

vnayar commented Nov 7, 2024

this seems fine but perhaps there also needs to be a test covering the escaped ' within HQL string rather than a parameter. eg: sess.createQuery("FROM Asset WHERE name='Bucky O''Hare'").list!Asset;

Added.

@SingingBush SingingBush merged commit 223a71f into buggins:master Nov 7, 2024
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants