I’ve put the log in debug mode, and when i just press Accounts, it shows me the list of my accounts(the first 20. In the suitecrm.log appears that query. I’ve looked for this type of query and i fount that in:
include/database/MssqlManager.php
on line 432 in the public function limitQuery:
public function limitQuery($sql, $start, $count, $dieOnError = false, $msg = '', $execute = true)
{
$start = (int)$start;
$count = (int)$count;
$countVar = '(@topCount)';
$newSQL = $sql;
$distinctSQLARRAY = array();
if (strpos($sql, 'UNION') && !preg_match("/(')(UNION).?(')/i", $sql)) {
$newSQL = $this->handleUnionLimitQuery($sql, $start, $count);
} else {
if ($start < 0) {
$start = 0;
}
$GLOBALS['log']->debug(print_r(func_get_args(), true));
$this->lastsql = $sql;
$matches = array();
preg_match('/^(.*SELECT\b)(.*?\bFROM\b.*\bWHERE\b)(.*)$/isU', $sql, $matches);
if (!empty($matches[3])) {
if ($start == 0) {
$match_two = strtolower($matches[2]);
if (!strpos($match_two, 'distinct') > 0 && strpos($match_two, 'distinct') !== 0) {
$orderByMatch = array();
preg_match('/^(.*)(\bORDER BY\b)(.*)$/is', $matches[3], $orderByMatch);
if (!empty($orderByMatch[3])) {
$selectPart = array();
preg_match('/^(.*)(\bFROM\b.*)$/isU', $matches[2], $selectPart);
$newSQL = "SELECT TOP $countVar * FROM
(
" . $matches[1] . $selectPart[1] . ', ROW_NUMBER()
OVER (ORDER BY ' . $this->returnOrderBy($sql, $orderByMatch[3]) . ') AS row_number
' . $selectPart[2] . $orderByMatch[1] . "
) AS a
WHERE row_number > $start";
} else {
$newSQL = $matches[1] . " TOP $countVar " . $matches[2] . $matches[3];
}
} else {
$distinct_o = strpos($match_two, 'distinct');
$up_to_distinct_str = substr($match_two, 0, $distinct_o);
//check to see if the distinct is within a function, if so, then proceed as normal
if (strpos($up_to_distinct_str, '(')) {
//proceed as normal
$newSQL = $matches[1] . " TOP $countVar " . $matches[2] . $matches[3];
} else {
//if distinct is not within a function, then parse
//string contains distinct clause, "TOP needs to come after Distinct"
//get position of distinct
$match_zero = strtolower($matches[0]);
$distinct_pos = strpos($match_zero, 'distinct');
//get position of where
$where_pos = strpos($match_zero, 'where');
//parse through string
$beg = substr($matches[0], 0, $distinct_pos + 9);
$mid = substr($matches[0], strlen($beg), ($where_pos + 5) - strlen($beg));
$end = substr($matches[0], strlen($beg) + strlen($mid));
//repopulate matches array
$matches[1] = $beg;
$matches[2] = $mid;
$matches[3] = $end;
$newSQL = $matches[1] . " TOP $countVar " . $matches[2] . $matches[3];
}
}
} else {
$orderByMatch = array();
preg_match('/^(.*)(\bORDER BY\b)(.*)$/is', $matches[3], $orderByMatch);
//if there is a distinct clause, parse sql string as we will have to insert the rownumber
//for paging, AFTER the distinct clause
$grpByStr = '';
$hasDistinct = strpos(strtolower($matches[0]), 'distinct');
$hasGroupBy = strpos(strtolower($matches[0]), 'group by');
require_once 'include/php-sql-parser.php';
$parser = new PHPSQLParser();
$sqlArray = $parser->parse($sql);
if ($hasDistinct) {
$matches_sql = strtolower($matches[0]);
//remove reference to distinct and select keywords, as we will use a group by instead
//we need to use group by because we are introducing
// rownumber column which would make every row unique
//take out the select and distinct from string so we can reuse in group by
$dist_str = 'distinct';
preg_match('/\b' . $dist_str . '\b/simU', $matches_sql, $matchesPartSQL, PREG_OFFSET_CAPTURE);
$matches_sql = trim(substr($matches_sql, $matchesPartSQL[0][1] + strlen($dist_str)));
//get the position of where and from for further processing
preg_match('/\bfrom\b/simU', $matches_sql, $matchesPartSQL, PREG_OFFSET_CAPTURE);
$from_pos = $matchesPartSQL[0][1];
preg_match('/\where\b/simU', $matches_sql, $matchesPartSQL, PREG_OFFSET_CAPTURE);
$where_pos = $matchesPartSQL[0][1];
//split the sql into a string before and after the from clause
//we will use the columns being selected to construct the group by clause
if ($from_pos > 0) {
$distinctSQLARRAY[0] = substr($matches_sql, 0, $from_pos);
$distinctSQLARRAY[1] = substr($matches_sql, $from_pos);
//get position of order by (if it exists) so we can strip it from the string
$ob_pos = strpos($distinctSQLARRAY[1], 'order by');
if ($ob_pos) {
$distinctSQLARRAY[1] = substr($distinctSQLARRAY[1], 0, $ob_pos);
}
// strip off last closing parentheses from the where clause
$distinctSQLARRAY[1] = preg_replace('/\)\s$/', ' ', $distinctSQLARRAY[1]);
}
$grpByStr = array();
foreach ($sqlArray['SELECT'] as $record) {
if ($record['expr_type'] === 'const') {
continue;
}
$grpByStr[] = trim($record['base_expr']);
}
$grpByStr = implode(', ', $grpByStr);
} elseif ($hasGroupBy) {
$groupBy = explode('group by', strtolower($matches[0]));
$groupByVars = explode(',', $groupBy[1]);
$grpByStr = $groupByVars[0];
}
if (!empty($orderByMatch[3])) {
//if there is a distinct clause, form query with rownumber after distinct
if ($hasDistinct) {
$newSQL = "SELECT TOP $countVar * FROM
(
SELECT ROW_NUMBER()
OVER (ORDER BY " . preg_replace('/^' . $dist_str . '\s+/', '',
$this->returnOrderBy($sql, $orderByMatch[3])) . ') AS row_number,
count(*) counter, ' . $distinctSQLARRAY[0] . '
' . $distinctSQLARRAY[1] . '
group by ' . $grpByStr . "
) AS a
WHERE row_number > $start";
} else {
$newSQL = "SELECT TOP $countVar * FROM
(
" . $matches[1] . ' ROW_NUMBER()
OVER (ORDER BY ' . $this->returnOrderBy($sql, $orderByMatch[3]) . ') AS row_number,
' . $matches[2] . $orderByMatch[1] . "
) AS a
WHERE row_number > $start";
}
} else {
//if there is a distinct clause, form query with rownumber after distinct
if ($hasDistinct) {
$newSQL = "SELECT TOP $countVar * FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY " . $grpByStr . ') AS row_number, count(*) counter, ' . $distinctSQLARRAY[0] . '
' . $distinctSQLARRAY[1] . '
group by ' . $grpByStr . "
)
AS a
WHERE row_number > $start";
} elseif ($hasGroupBy) {
$newSQL = "SELECT TOP $countVar * FROM
(
" . $matches[1] . ' ROW_NUMBER() OVER (ORDER BY ' . $grpByStr . ') AS row_number, ' . $matches[2] . $matches[3] . "
)
AS a
WHERE row_number > $start";
} else {
$newSQL = "SELECT TOP $countVar * FROM
(
" . $matches[1] . ' ROW_NUMBER() OVER (ORDER BY ' . $sqlArray['FROM'][0]['alias'] . '.id) AS row_number, ' . $matches[2] . $matches[3] . "
)
AS a
WHERE row_number > $start";
}
}
}
}
}
$newSQL = "DECLARE @topCount INT SET @topCount = $count " . $newSQL;
$GLOBALS['log']->debug('Limit Query: ' . $newSQL);
if ($execute) {
$result = $this->query($newSQL, $dieOnError, $msg);
$this->dump_slow_queries($newSQL);
return $result;
}
return $newSQL;
}
But atually i don’t know whith if create that specific query, some of them are similar