You are here

private function FrxPostgres::postgres_xml in Forena Reports 8

Generate xml from sql using the provided f_forena

Parameters

string $sql:

Return value

SimpleXMLElement XML Representation of query results.

1 call to FrxPostgres::postgres_xml()
FrxPostgres::sqlData in src/FrxPlugin/Driver/FrxPostgres.php
Get data based on file data block in the repository.

File

src/FrxPlugin/Driver/FrxPostgres.php, line 113
Postgres specific driver that takes advantage of native XML support

Class

FrxPostgres
Class FrxPostgres

Namespace

Drupal\forena\FrxPlugin\Driver

Code

private function postgres_xml($sql, $block) {
  $db = $this->db;

  //$rs->debugDumpParams();
  $fsql = 'select query_to_xml($1,true,false,$2);';
  $rs = @pg_query_params($db, $fsql, array(
    $sql,
    '',
  ));
  $e = pg_last_error();
  if ($e) {
    $text = $e;
    if (!$this->block_name) {
      $short = t('%e', array(
        '%e' => $text,
      ));
    }
    else {
      $short = t('SQL Error in %b.sql', array(
        '%b' => $this->block_name,
      ));
    }
    $this
      ->app()
      ->error($short, $text);
    return NULL;
  }
  $xml_text = '';
  if ($rs) {
    $row = pg_fetch_row($rs);
    $xml_text = $row[0];
  }
  $xml = NULL;
  if ($xml_text) {
    $xml = new \SimpleXMLElement($xml_text);
    if ($xml
      ->getName() == 'error') {
      $msg = (string) $xml . ' in ' . $block . '.sql. ';
      $this
        ->app()
        ->error($msg . 'See logs for more info', $msg . ' in <pre> ' . $sql . '</pre>');
    }
    if (!$xml
      ->children()) {
      $xml = '';
    }
  }
  if ($rs) {
    pg_free_result($rs);
  }
  return $xml;
}