From 7de62306256d738736d46a683c6dfb4f4b01b69f Mon Sep 17 00:00:00 2001 From: Damien Regad Date: Wed, 7 Sep 2022 17:32:35 +0200 Subject: [PATCH 1/8] Update Changelog https://github.com/ADOdb/ADOdb/issues/124#issuecomment-1238798400 --- docs/changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changelog.md b/docs/changelog.md index cdc4af8fe..c9ff709a7 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -19,6 +19,7 @@ Older changelogs: ### Fixed - alterColumnSql() and changeTableSQL() produce different SQL + [#124](https://github.com/ADOdb/ADOdb/issues/124) [#383](https://github.com/ADOdb/ADOdb/issues/383) [#865](https://github.com/ADOdb/ADOdb/issues/865) - Fix PHP 8.1 deprecated warning in GetUpdateSQL() From b5469219512d271836cec86a2b68e0caa37ce895 Mon Sep 17 00:00:00 2001 From: Damien Regad Date: Thu, 8 Sep 2022 19:12:04 +0200 Subject: [PATCH 2/8] Bump version to 5.22.4-dev --- adodb.inc.php | 2 +- docs/changelog.md | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/adodb.inc.php b/adodb.inc.php index 7e7815b65..f8dce5781 100644 --- a/adodb.inc.php +++ b/adodb.inc.php @@ -198,7 +198,7 @@ function ADODB_Setup() { /** * ADODB version as a string. */ - $ADODB_vers = 'v5.22.3 2022-09-06'; + $ADODB_vers = 'v5.22.4-dev Unreleased'; /** * Determines whether recordset->RecordCount() is used. diff --git a/docs/changelog.md b/docs/changelog.md index c9ff709a7..d8e0dedfb 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -14,6 +14,8 @@ Older changelogs: -------------------------------------------------------------------------------- +## [5.22.4] - Unreleased + ## [5.22.3] - 2022-09-06 ### Fixed @@ -1313,6 +1315,7 @@ Released together with [v4.95](changelog_v4.x.md#495---17-may-2007) - Adodb5 version,more error checking code now will use exceptions if available. +[5.22.4]: https://github.com/adodb/adodb/compare/v5.22.3...v5.22.4 [5.22.3]: https://github.com/adodb/adodb/compare/v5.22.2...v5.22.3 [5.22.2]: https://github.com/adodb/adodb/compare/v5.22.1...v5.22.2 [5.22.1]: https://github.com/adodb/adodb/compare/v5.22.0...v5.22.1 From 19ad1135642f44d57b8d01914e4c47c9cc16b008 Mon Sep 17 00:00:00 2001 From: Damien Regad Date: Thu, 8 Sep 2022 19:15:59 +0200 Subject: [PATCH 3/8] Fix mysqli_result could not be converted to int Partial revert of 721c31492ac77aa1bd9bdd01193cd6071087b49f. This was an early attempt to fix #848; the actual fix was implemented in the PostgreSQL driver, but the change to ADORecordSet::__destruct() was not reverted. Fixes #867 --- adodb.inc.php | 4 +--- docs/changelog.md | 6 ++++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/adodb.inc.php b/adodb.inc.php index f8dce5781..c96c9fab7 100644 --- a/adodb.inc.php +++ b/adodb.inc.php @@ -3943,9 +3943,7 @@ function __construct($queryID,$mode=false) { } function __destruct() { - if($this->_queryID != -1) { - $this->Close(); - } + $this->Close(); } #[\ReturnTypeWillChange] diff --git a/docs/changelog.md b/docs/changelog.md index d8e0dedfb..ee11fa0c1 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -16,6 +16,12 @@ Older changelogs: ## [5.22.4] - Unreleased +### Fixed + +- mysqli: Fix mysqli_result could not be converted to int + [#867](https://github.com/ADOdb/ADOdb/issues/867) + + ## [5.22.3] - 2022-09-06 ### Fixed From 85c27f0ce1303f6aad1800beb22b06320debe40d Mon Sep 17 00:00:00 2001 From: Damien Regad Date: Thu, 8 Sep 2022 18:50:34 +0200 Subject: [PATCH 4/8] Refactor adodb_strip_order_by() The replacement of preg_match() by preg_match_all() in commit 8eaf842d19e4206e1c44e0eda44688ebfa1728ed basically made most of the code in the function useless (the block parsing the SQL statement for paretheses was never called). Also, the logic to retrieve the last ORDER BY clause was a bit contrived. Fixes #869 --- adodb-lib.inc.php | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/adodb-lib.inc.php b/adodb-lib.inc.php index 92e02df56..de6c8217b 100644 --- a/adodb-lib.inc.php +++ b/adodb-lib.inc.php @@ -27,34 +27,22 @@ global $ADODB_INCLUDED_LIB; $ADODB_INCLUDED_LIB = 1; +/** + * Strip the ORDER BY clause from the outer SELECT. + * + * @param string $sql + * + * @return string + */ function adodb_strip_order_by($sql) { - preg_match_all('/(\sORDER\s+BY\s(?:[^)](?!LIMIT))*)/is', $sql, $arr); - if ($arr) - { - $tmp = array_pop($arr); - $arr = [1=>array_pop($tmp)]; - } - if ($arr) - if (strpos($arr[1], '(') !== false) { - $at = strpos($sql, $arr[1]); - $cntin = 0; - for ($i=$at, $max=strlen($sql); $i < $max; $i++) { - $ch = $sql[$i]; - if ($ch == '(') { - $cntin += 1; - } elseif($ch == ')') { - $cntin -= 1; - if ($cntin < 0) { - break; - } - } - } - $sql = substr($sql,0,$at).substr($sql,$i); - } else { - $sql = str_replace($arr[1], '', $sql); - } + $num = preg_match_all('/(\sORDER\s+BY\s(?:[^)](?!LIMIT))*)/is', $sql, $matches); + if ($num) { + // Get the last match + $last_order_by = array_pop($matches[1]); + $sql = str_replace($last_order_by, '', $sql); + } return $sql; } From 1494108c04ea072c93bb751e4a5af0721fbcac5f Mon Sep 17 00:00:00 2001 From: Damien Regad Date: Thu, 8 Sep 2022 18:54:55 +0200 Subject: [PATCH 5/8] Test case for adodb_strip_order_by() --- tests/LibTest.php | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/LibTest.php b/tests/LibTest.php index 2f02e00e0..971dc6e62 100644 --- a/tests/LibTest.php +++ b/tests/LibTest.php @@ -39,6 +39,51 @@ public function setUp(): void $this->db = ADONewConnection('mysqli'); } + /** + * Test for {@see adodb_strip_order_by()} + * + * @dataProvider providerStripOrderBy + */ + public function testStripOrderBy($sql, $stripped): void + { + $this->assertSame($stripped, adodb_strip_order_by($sql)); + } + + /** + * Data provider for {@see testStripOrderBy()} + * + * @return array [SQL statement, SQL with ORDER BY clause stripped] + */ + public function providerStripOrderBy(): array + { + return [ + 'No order by clause' => [ + "SELECT name FROM table", + "SELECT name FROM table" + ], + 'Simple order by clause' => [ + "SELECT name FROM table ORDER BY name", + "SELECT name FROM table" + ], + 'Order by clause descending' => [ + "SELECT name FROM table ORDER BY name DESC", + "SELECT name FROM table" + ], + 'Order by clause with limit' => [ + "SELECT name FROM table ORDER BY name LIMIT 5", + "SELECT name FROM table LIMIT 5" + ], + 'Ordered Subquery with outer order by' => [ + "SELECT * FROM table WHERE name IN (SELECT TOP 5 name FROM table_b ORDER by name) ORDER BY name DESC", + "SELECT * FROM table WHERE name IN (SELECT TOP 5 name FROM table_b ORDER by name)" + ], + 'Ordered Subquery without outer order by' => [ + "SELECT * FROM table WHERE name IN (SELECT TOP 5 name FROM table_b ORDER by name)", + "SELECT * FROM table WHERE name IN (SELECT TOP 5 name FROM table_b ORDER by name)" + ], + ]; + } + /** * Test for {@see _adodb_quote_fieldname()} * From 0a910450861a4b5a710360d99d4e9c1a511f1dac Mon Sep 17 00:00:00 2001 From: Damien Regad Date: Thu, 8 Sep 2022 18:55:10 +0200 Subject: [PATCH 6/8] Add PHPUnit cache files to .gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 043eaf271..d1cf53e71 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,7 @@ phpdoc.xml docs/api docs/cache + +# PHPUnit +/.phpunit.cache +.phpunit.result.cache From 10111e2e16a9a2b51fb0a5a695e7c76aeb212a92 Mon Sep 17 00:00:00 2001 From: Damien Regad Date: Thu, 8 Sep 2022 19:09:53 +0200 Subject: [PATCH 7/8] Don't strip ORDER BY clause from subqueries adodb_strip_order_by() should only remove the ORDER BY clause from the outer SELECT statement, not from subqueries. Fixes #870 --- adodb-lib.inc.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/adodb-lib.inc.php b/adodb-lib.inc.php index de6c8217b..4c2668b26 100644 --- a/adodb-lib.inc.php +++ b/adodb-lib.inc.php @@ -36,12 +36,16 @@ */ function adodb_strip_order_by($sql) { - $num = preg_match_all('/(\sORDER\s+BY\s(?:[^)](?!LIMIT))*)/is', $sql, $matches); + $num = preg_match_all('/(\sORDER\s+BY\s(?:[^)](?!LIMIT))*)/is', $sql, $matches, PREG_OFFSET_CAPTURE); if ($num) { // Get the last match - $last_order_by = array_pop($matches[1]); + list($last_order_by, $offset) = array_pop($matches[1]); - $sql = str_replace($last_order_by, '', $sql); + // If we find a ')' after the last order by, then it belongs to a + // sub-query, not the outer SQL statement and should not be stripped + if (strpos($sql, ')', $offset) === false) { + $sql = str_replace($last_order_by, '', $sql); + } } return $sql; } From e2610ad2aae96ddd0dd439df2ba50126e753395c Mon Sep 17 00:00:00 2001 From: Damien Regad Date: Thu, 8 Sep 2022 19:12:38 +0200 Subject: [PATCH 8/8] Update Changelog --- docs/changelog.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/changelog.md b/docs/changelog.md index ee11fa0c1..575632970 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -18,6 +18,10 @@ Older changelogs: ### Fixed +- adodb_strip_order_by() throws deprecated warnings on PHP 8.1 + [#869](https://github.com/ADOdb/ADOdb/issues/869) +- adodb_strip_order_by() shouldn't strip clause from subqueries + [#870](https://github.com/ADOdb/ADOdb/issues/870) - mysqli: Fix mysqli_result could not be converted to int [#867](https://github.com/ADOdb/ADOdb/issues/867)