Skip to content

Commit

Permalink
Add a parameter to get response string, instead of printing it.
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebin committed Jul 5, 2019
1 parent ef33c25 commit aa6eec7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
37 changes: 26 additions & 11 deletions lib/MC/Google/Visualization.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,13 @@ public function setDefaultFormat($type, $format)
/**
* Handle the entire request, pulling the query from the $_GET variables, and printing the results directly.
*
* @param bool $echo print response and set header
*
* @throws Visualization_Error
*
* @return string the javascript response
*/
public function handleRequest()
public function handleRequest($echo = true)
{
$query = $_GET['tq'];
$params = ['version' => $this->version, 'responseHandler' => 'google.visualization.Query.setResponse'];
Expand All @@ -166,19 +170,28 @@ public function handleRequest()
$params['responseHandler'] = $_GET['responseHandler'];
}

@header('Content-Type: text/javascript; charset=utf-8');
$this->handleQuery($query, $params);
$response = $this->handleQuery($query, $params);

if ($echo) {
header('Content-Type: text/javascript; charset=utf-8');
echo $response;
}

return $response;
}

/**
* Handle a specific query. Use this if you want to gather the query parameters yourself instead of using handleRequest().
*
* @param string $query the visualization query to parse and execute
* @param array $params all extra params sent along with the query - must include at least "reqId" key
*
* @return string the javascript response
*/
public function handleQuery($query, $params)
{
$reqid = null;
$response = '';

try {
if (!($this->db instanceof PDO)) {
Expand All @@ -194,26 +207,28 @@ public function handleQuery($query, $params)

$stmt = $this->db->query($sql);
//If we got here, there's no errors
echo $this->getSuccessInit($meta);
$response .= $this->getSuccessInit($meta);
$first = true;
foreach ($stmt as $row) {
if (!$first) {
echo ',';
$response .= ',';
}
echo $this->getRowValues($row, $meta);
$response .= $this->getRowValues($row, $meta);
$first = false;
}

echo $this->getSuccessClose();
$response .= $this->getSuccessClose();
} catch (Visualization_Error $e) {
echo $this->handleError($reqid, $e->getMessage(), $params['responseHandler'], $e->type, $e->summary);
$response .= $this->handleError($reqid, $e->getMessage(), $params['responseHandler'], $e->type, $e->summary);
} catch (PDOException $e) {
echo $this->handleError($reqid, $e->getMessage(), $params['responseHandler'], 'invalid_query', 'Invalid Query - PDO exception');
$response .= $this->handleError($reqid, $e->getMessage(), $params['responseHandler'], 'invalid_query', 'Invalid Query - PDO exception');
} catch (ParseError $e) {
echo $this->handleError($reqid, $e->getMessage(), $params['responseHandler'], 'invalid_query', 'Invalid Query - Parse Error');
$response .= $this->handleError($reqid, $e->getMessage(), $params['responseHandler'], 'invalid_query', 'Invalid Query - Parse Error');
} catch (Exception $e) {
echo $this->handleError($reqid, $e->getMessage(), $params['responseHandler']);
$response .= $this->handleError($reqid, $e->getMessage(), $params['responseHandler']);
}

return $response;
}

/**
Expand Down
12 changes: 3 additions & 9 deletions tests/ExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ public function testQueryComplete()
$_GET['tq'] = 'select country, year, birth_control, infant_mort where birth_control!=0 AND infant_mort!=0 group by country, year label country "Country", year "Year", birth_control "Birth Control Penetration", gdp_us "Per-capita GDP (US Dollars)", savings_rate "Savings Rate", investment_rate "Investment Rate", infant_mort "Infant Mortality", life_expect "Life Expectancy" format year "%d"';
$_GET['tqx'] = 'reqId:1';

ob_start();
$vis->handleRequest();
$output = ob_get_clean();
$output = $vis->handleRequest(false);

//file_put_contents(__DIR__.'/result1.js', $output);
static::assertStringEqualsFile(__DIR__.'/result1.js', $output);
Expand All @@ -75,9 +73,7 @@ public function testQuerySimple()
$_GET['tq'] = 'select id, name from countries order by name label id "ID", name "Name"';
$_GET['tqx'] = 'reqId:2';

ob_start();
$vis->handleRequest();
$output = ob_get_clean();
$output = $vis->handleRequest(false);

//file_put_contents(__DIR__.'/result2.js', $output);
static::assertStringEqualsFile(__DIR__.'/result2.js', $output);
Expand Down Expand Up @@ -110,9 +106,7 @@ public function testQueryJoins()
$_GET['tq'] = 'select avg(life_male), avg(life_female), avg(life_both) from countries label life_male "Life Expectancy (Male)", life_female "Life Expectancy (Female)", life_both "Life Expectancy (Combined)" format life_male "%.2f years", life_female "%.2f years", life_both "%.2f years"';
$_GET['tqx'] = 'reqId:3';

ob_start();
$vis->handleRequest();
$output = ob_get_clean();
$output = $vis->handleRequest(false);

//file_put_contents(__DIR__.'/result3.js', $output);
static::assertStringEqualsFile(__DIR__.'/result3.js', $output);
Expand Down

0 comments on commit aa6eec7

Please sign in to comment.