Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Commit

Permalink
Fixed issue with custom limit being removed from query
Browse files Browse the repository at this point in the history
Fixed issue with the LIMIt in an SQL query being removed after processing query
  • Loading branch information
boardy committed Nov 1, 2017
1 parent b4c408f commit a9620d9
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion QueryExecManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,18 @@ function executeQuery($postArray)
}
$this->logger->writeToLog("$totalRows row(s) has so far been returned");
$queryLimitedTo = null;

//Remove any line breaks from the query, and replace with a space
$postArray["query"] = str_replace("\n", " ", $postArray["query"]);


if (stripos($postArray["query"], "SELECT") !== false)
{
//This fixes issues where WHERE parameters add slashes around the surrounding speech marks
//quotes causing an issue with the query
$this->logger->writeToLog("Before adding slashes query: " . $postArray["query"]);
$postArray["query"] = $this->addSlahesToWhereParameters($postArray["query"], $connManager->conn);
$this->logger->writeToLog("After adding slashes query: " . $postArray["query"]);
if ($postArray["defaultLimit"] < API_RESULTSET_LIMIT)
{
$queryLimitedTo = $postArray["defaultLimit"];
Expand Down Expand Up @@ -343,6 +350,7 @@ private function convertLimit($query, &$startIndex, $totalRowsCurrentlyDownloade
*/
private function getCountOfLimitsInQuery($query)
{
$this->logger->writeToLog("Checking limit count query: $query");
return substr_count(strtoupper($query), "LIMIT");
}

Expand All @@ -367,6 +375,9 @@ private function addSlahesToWhereParameters($query, $conn)
//First check if the query has already been escaped, it should have done as that's how queries work
//however, remove them, get the WHERE clause and get PHP to escape the parameters values

//Get the LIMIT string
$limitString = substr($query, stripos($query, "LIMIT"));

$query = str_replace("\\'", "'", $query);
$query = str_replace('\\"', '"', $query);

Expand Down Expand Up @@ -463,7 +474,7 @@ private function addSlahesToWhereParameters($query, $conn)
$processedWhere .= " $key LIKE '$value'";
}
}
$newQuery .= " $processedWhere";
$newQuery .= " $processedWhere $limitString";
return $newQuery;
}
else
Expand Down

0 comments on commit a9620d9

Please sign in to comment.