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

Fix #715 by removing subqueries without using regex #723

Merged
merged 3 commits into from
Apr 23, 2021

Conversation

dregad
Copy link
Member

@dregad dregad commented Apr 10, 2021

Alternative to #718

@dregad
Copy link
Member Author

dregad commented Apr 22, 2021

@mnewnham as mentioned in #718 (comment), it would be nice if you could take a look at this.

@dregad dregad self-assigned this Apr 22, 2021
Copy link
Contributor

@mnewnham mnewnham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems all good. I was unable to break it with

  • Subselects within the 'select from'
  • Subselects within the 'where'
  • Unmatched parentheses inside strings within the statements

@dregad
Copy link
Member Author

dregad commented Apr 23, 2021

Great, thanks for testing.

mnewnham and others added 3 commits April 23, 2021 08:06
  - Refactor the driver matching code to make it easier to add.
  - Add support for PDO/mysql driver
Following discussion in #718, using regex to identify and remove
sub-queries in _adodb_getcount() is flawed and can't be fixed. This led
to implementing a hack using _ADODB_COUNT "keyword" to (see #88
41839f5).

This replaces the regex parsing by a simple loop on the query string,
removing everything between the SELECT and the FROM clause, ignoring
whatever is between parentheses (i.e. subqueries).

The _ADODB_COUNT logic is therefore obsolete

Fixes #715
@dregad dregad merged commit 98b5579 into hotfix/5.21 Apr 23, 2021
@dregad dregad deleted the i715-pageexecute-no-regex branch April 23, 2021 06:10
@Mike-Benoit
Copy link
Contributor

While I haven't tested this yet, I disagree with removing the _ADODB_COUNT functionally to override the built-in removal of sub-selects. Without a full SQL parser (which will never happen of course), I fail to see how we can handle every possible SQL query that anyone could ever possibly write across all drivers. It seems like pretty cheap insurance to keep around, and even a potential optimization for queries that may be executed often too.

Based on a very quick read of the code, it would appear that the following real-world query (where _ADODB_COUNT is currently used) could possibly fail:

select
_ADODB_COUNT
a.*,
(
CASE WHEN EXISTS
( select 1 from table1 as y where y.accrual_policy_account_id = a.id and y.deleted = 0)
THEN 1
ELSE
CASE WHEN EXISTS
( select 1 from table2 as z where z.accrual_policy_account_id = a.id and z.deleted = 0)
THEN 1
ELSE 0
END
END
) as in_use
_ADODB_COUNT
FROM table3 as a ...;

@dregad
Copy link
Member Author

dregad commented May 21, 2021

@Mike-Benoit given the above SQL statement (with or without the _ADODB_COUNT markers), the new parser code in _adodb_getcount() correctly transforms it to SELECT COUNT(*) FROM table3 as a, which is the expected result.

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.

PDO: PageExecute bug - pdo error: [1248: Every derived table must have its own alias]
3 participants