Escape table name in when building a select statement with row id
ClosedPublic

Authored by jfita on Apr 29 2020, 2:21 PM.

Details

Summary

When a KDbQuerySchema has a table name or alias that is a reserved SQL
keyword, and the alsoRetrieveRecordId statement option is set to true,
KDbNativeStatementBuilder constructed an incorrect statement.

For instance, if trying to create a SELECT statement in SQLite of a
table named “table” with a single “id column, the generated query was:

SELECT [id], table.OID FROM [table] ORDER BY [id]

The execution if that query generated an error due to the unescaped
table keyword. With this commit, the generated query is:

SELECT [id], [table].OID FROM [table] ORDER BY [id]

FIXED-IN:3.2.1

Diff Detail

Repository
R15 KDb
Branch
3.2
Lint
No Linters Available
Unit
No Unit Test Coverage
Build Status
Buildable 26084
Build 26102: arc lint + arc unit
jfita created this revision.Apr 29 2020, 2:21 PM
Restricted Application added a project: KDb. · View Herald TranscriptApr 29 2020, 2:21 PM
Restricted Application added a subscriber: Kexi-Devel-list. · View Herald Transcript
jfita requested review of this revision.Apr 29 2020, 2:21 PM
jfita updated this revision to Diff 81518.Apr 29 2020, 2:24 PM

Fix indent

Related fix in KEXI 3.2 would be welcome.

For now when such a "table" table is open in data view, it looks strange:

And the console shows:

Cannot open cursor
--aborting setData().
 CURSOR(KDbQuerySchema:"SELECT [id] FROM [table] ORDER BY [id]"
 NOT_OPENED  NOT_BUFFERED AT=-1)

Instead error should be presented and the tab should not be open.

To be sure, you're constructing a query using C++ classes and not using the parser?
I am asking because to me "select id from [table]" does not work since we do not (yet) support identifier escaping with [].

Another note so we won't forget. Update for autotests (both positive and negative). Here's the branch https://bugs.kde.org/show_bug.cgi?id=332161#c8

jfita added a comment.Apr 29 2020, 3:33 PM

Thanks. Is there any relation to https://bugs.kde.org/show_bug.cgi?id=420599?

Yes, this is related to both https://bugs.kde.org/show_bug.cgi?id=420599 and https://bugs.kde.org/show_bug.cgi?id=332161 .

To be sure, you're constructing a query using C++ classes and not using the parser?
I am asking because to me "select id from [table]" does not work since we do not (yet) support identifier escaping with [].

I modified KDb in my local machine to accept quoted identifiers using back quotes (this is related to the comment i just posted to https://bugs.kde.org/show_bug.cgi?id=332161) and i was trying it out with an unmodified version of Kexi, to see what issues it raises. Thus i saw the problem this commit fixes using the parser through Kexi, but i assumed that it would be the same using C++ to construct the query, that is why i sent the fix. Also, it will be a problem once KDb has quoted identifiers.

I understand this makes it hard for you to test the changes, though.

Entering "select id from [table]" in KEXI SQL editor or passing it to a parser in C++ raises the same parser's error.
In order to reproduce bug you're fixing above one needs to create a queryfully using the C++ API since the parser won't let the to go reserved identifiers through.
(until the #332161 is solved)

As for using back quotes for any testing. This is not going to be in any user/developer -visible SQL. Only [ ] and Wiki Home Page are. The idea for back quotes is the storage format of KEXI queries to be compatible.

jfita added a comment.Apr 29 2020, 6:50 PM

Related fix in KEXI 3.2 would be welcome.

Created potential fix for this in D29286

Entering "select id from [table]" in KEXI SQL editor or passing it to a parser in C++ raises the same parser's error.
In order to reproduce bug you're fixing above one needs to create a queryfully using the C++ API since the parser won't let the to go reserved identifiers through.
(until the #332161 is solved)

At present, until #332161 is implemented, this is the only way of triggering this bug, as far as i know.

As for using back quotes for any testing. This is not going to be in any user/developer -visible SQL. Only [ ] and [[]] are. The idea for back quotes is the storage format of KEXI queries to be compatible.

Sorry, i did my tests before our discussion in #332161 and was trying out my misconceptions of the conclusions there. I reverted all my changes and will try again with [].

staniek accepted this revision.May 3 2020, 8:18 PM
This revision is now accepted and ready to land.May 3 2020, 8:18 PM
staniek closed this revision.May 3 2020, 8:25 PM