Fixes related to constructing and handling queries
ClosedPublic

Authored by staniek on Dec 19 2017, 1:31 PM.

Details

Summary
  1. SQL parser test: also test second call to parser->query(), it should return nullptr

    GIT_SILENT use scoped pointer for owned query
  1. SQL parser test: use the cached query, parser->query() is nullptr by then
  1. Setup query in connection: parsing failure can delete the query object so do not try to access it

    Query schema: fix possible crashes - explicitly/properly remove query from connection's cache when needed
  1. SQL parser: add Q_REQUIRED_RESULT flags when table or query objects are returned with ownership

    +remove overload that makes no sense (because object with ownership passed should not be const)
  1. Naming in KDbQuerySchemaFieldsExpanded: ownedVisibleColumns -> ownedVisibleFields
  1. Query schema: fix memory leaks - remove internal expressions
  1. Correct result set retrieved from the backend when internal JOINs are in use, also fix possible crash

Example user's query (e.g. for the Simple_Database.kexi): "SELECT *, ownership.owner FROM ownership"
Assumption: the "ownership" table is related to persons table via ownership.owner=persons.id
Assumption: this relationship is used to obtain person's surname for display

Before the fix generated query was like this:
SELECT *, ownership.owner, persons.surname, ownership.OID
FROM ownership LEFT OUTER JOIN persons
ON ownership.owner=persons.id

This means the result set also included persons.* what is not what we expect:

  • for performance reasons
  • we have not allocated space for it --> crash

The fix is based on enumerating all explicitly used tables
(T1.*, T2.*, ... Tn.*) in the SELECT in place of the "*".

After the fix generated query is like this:
SELECT ownership.*, ownership.owner, persons.surname, ownership.OID
FROM ownership LEFT OUTER JOIN persons ON ownership.owner=persons.id

This means the result set includes columns that we need.

(redundancy in columns would be fixed elsewhere)

Affected drivers: any, crashes were confirmed in SQLite.

For other example see the Northwind tables mentioned in the patch.

FIXED-IN:3.1.0

Test Plan

Please also apply D9409 to KDb and test with it.

Play with queries in KEXI e.g. as mentioned in D9409. For the item number 7 see its description above.

Diff Detail

Repository
R15 KDb
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.
staniek created this revision.Dec 19 2017, 1:31 PM
Restricted Application added a project: KDb. · View Herald TranscriptDec 19 2017, 1:31 PM
staniek requested review of this revision.Dec 19 2017, 1:31 PM
staniek edited the summary of this revision. (Show Details)Dec 19 2017, 2:50 PM
piggz added a comment.Jan 3 2018, 10:10 PM

Is it possible to add some tests for the query strings that would be impacted by this?

staniek added a comment.EditedJan 3 2018, 11:05 PM
In D9410#185773, @piggz wrote:

Is it possible to add some tests for the query strings that would be impacted by this?

Yes, this is always best way to avoid regressions. For now autotests only run for SQLite but we want to extend to 2 other drivers.

Task created: T7710

piggz accepted this revision.Jan 11 2018, 7:50 AM
This revision is now accepted and ready to land.Jan 11 2018, 7:50 AM
staniek edited the summary of this revision. (Show Details)Jan 11 2018, 11:44 PM
This revision was automatically updated to reflect the committed changes.