diff options
| author | Damien Regad <dregad@mantisbt.org> | 2022-09-08 19:09:53 +0200 |
|---|---|---|
| committer | Damien Regad <dregad@mantisbt.org> | 2022-09-09 09:24:31 +0200 |
| commit | 10111e2e16a9a2b51fb0a5a695e7c76aeb212a92 (patch) | |
| tree | 6372e160f849f7c74e87515d81f9a0028c6fc528 /adodb-lib.inc.php | |
| parent | 0a910450861a4b5a710360d99d4e9c1a511f1dac (diff) | |
| download | adodb-10111e2e16a9a2b51fb0a5a695e7c76aeb212a92.tar.gz adodb-10111e2e16a9a2b51fb0a5a695e7c76aeb212a92.tar.bz2 adodb-10111e2e16a9a2b51fb0a5a695e7c76aeb212a92.zip | |
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
Diffstat (limited to 'adodb-lib.inc.php')
| -rw-r--r-- | adodb-lib.inc.php | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/adodb-lib.inc.php b/adodb-lib.inc.php index de6c8217..4c2668b2 100644 --- a/adodb-lib.inc.php +++ b/adodb-lib.inc.php @@ -36,12 +36,16 @@ $ADODB_INCLUDED_LIB = 1; */ 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; } |
