protected function showFormatted($query, $format, $columns) { $query = $query->getQuery(); switch ($format) { case 'json': echo json_encode($query->fetchAll()); break; case 'csv': Csv::fromQuery($query)->dump(); break; default: preg_match_all('~\\$([a-z0-9_-]+)\\$~', $format, $m); $words = array(); foreach ($columns as $key => $col) { if (is_numeric($key)) { if (in_array($col, $m[1])) { $words[] = $col; } } else { if (in_array($key, $m[1])) { $words[] = $key; } } } foreach ($query->fetchAll() as $row) { $output = $format; foreach ($words as $word) { $output = preg_replace('~\\$' . $word . '\\$~', $row->{$word}, $output); } echo $output . "\n"; } } exit; }
protected function handleFormatRequest($query) { if ($this->_getParam('format') === 'sql') { echo '<pre>' . htmlspecialchars(wordwrap($query->dump())) . '</pre>'; exit; } if ($this->_getParam('format') === 'json' || $this->_request->getHeader('Accept') === 'application/json') { header('Content-type: application/json'); echo json_encode($query->getQuery()->fetchAll()); exit; } if ($this->_getParam('format') === 'csv' || $this->_request->getHeader('Accept') === 'text/csv') { Csv::fromQuery($query)->dump(); exit; } }
public function testWhetherValidCsvIsRendered() { $queryMock = Mockery::mock('Icinga\\Data\\SimpleQuery', array('fetchAll' => array(array('col1' => 'val1', 'col2' => 'val2', 'col3' => 'val3', 'col4' => 'val4'), array('col1' => 'val5', 'col2' => 'val6', 'col3' => 'val7', 'col4' => 'val8')))); $csv = Csv::fromQuery($queryMock); $this->assertEquals(join("\r\n", array('col1,col2,col3,col4', '"val1","val2","val3","val4"', '"val5","val6","val7","val8"')) . "\r\n", (string) $csv, 'Csv does not render valid/correct csv structured data'); }
public function testWhetherValidCsvIsRendered() { $data = new ArrayDatasource(array(array('col1' => 'val1', 'col2' => 'val2', 'col3' => 'val3', 'col4' => 'val4'), array('col1' => 'val5', 'col2' => 'val6', 'col3' => 'val7', 'col4' => 'val8'))); $csv = Csv::fromQuery($data->select()); $this->assertEquals(implode("\r\n", array('col1,col2,col3,col4', '"val1","val2","val3","val4"', '"val5","val6","val7","val8"')) . "\r\n", (string) $csv, 'Csv does not render valid/correct csv structured data'); }