Skip to content

Commit

Permalink
Merge branch 'hotfix/5.22'
Browse files Browse the repository at this point in the history
# Conflicts:
#	adodb.inc.php
#	docs/changelog.md
  • Loading branch information
dregad committed Sep 9, 2022
2 parents 078d6cd + e2610ad commit e3e3d14
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 28 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -16,3 +16,7 @@
phpdoc.xml
docs/api
docs/cache

# PHPUnit
/.phpunit.cache
.phpunit.result.cache
42 changes: 17 additions & 25 deletions adodb-lib.inc.php
Expand Up @@ -27,34 +27,26 @@
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, PREG_OFFSET_CAPTURE);
if ($num) {
// Get the last match
list($last_order_by, $offset) = array_pop($matches[1]);

// 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;
}

Expand Down
4 changes: 1 addition & 3 deletions adodb.inc.php
Expand Up @@ -3980,9 +3980,7 @@ function __construct($queryID,$mode=false) {
}

function __destruct() {
if($this->_queryID != -1) {
$this->Close();
}
$this->Close();
}

#[\ReturnTypeWillChange]
Expand Down
14 changes: 14 additions & 0 deletions docs/changelog.md
Expand Up @@ -28,11 +28,24 @@ Older changelogs:
[#814](https://github.com/ADOdb/ADOdb/issues/814)


## [5.22.4] - Unreleased

### 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)


## [5.22.3] - 2022-09-06

### 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()
Expand Down Expand Up @@ -1328,6 +1341,7 @@ Released together with [v4.95](changelog_v4.x.md#495---17-may-2007)

[5.23.0]: https://github.com/adodb/adodb/compare/v5.22.3...master

[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
Expand Down
45 changes: 45 additions & 0 deletions tests/LibTest.php
Expand Up @@ -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()}
*
Expand Down

0 comments on commit e3e3d14

Please sign in to comment.