summaryrefslogtreecommitdiff
path: root/adodb-lib.inc.php
diff options
context:
space:
mode:
authorDamien Regad <dregad@mantisbt.org>2022-09-08 18:50:34 +0200
committerDamien Regad <dregad@mantisbt.org>2022-09-09 09:24:31 +0200
commit85c27f0ce1303f6aad1800beb22b06320debe40d (patch)
tree26e8facdb25e611329f0dc7fd9879073b7b87cfb /adodb-lib.inc.php
parent19ad1135642f44d57b8d01914e4c47c9cc16b008 (diff)
downloadadodb-85c27f0ce1303f6aad1800beb22b06320debe40d.tar.gz
adodb-85c27f0ce1303f6aad1800beb22b06320debe40d.tar.bz2
adodb-85c27f0ce1303f6aad1800beb22b06320debe40d.zip
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
Diffstat (limited to 'adodb-lib.inc.php')
-rw-r--r--adodb-lib.inc.php38
1 files changed, 13 insertions, 25 deletions
diff --git a/adodb-lib.inc.php b/adodb-lib.inc.php
index 92e02df5..de6c8217 100644
--- a/adodb-lib.inc.php
+++ b/adodb-lib.inc.php
@@ -27,34 +27,22 @@ if (!defined('ADODB_DIR')) die();
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;
}