You are here

public function FrxPDO::sqlData in Forena Reports 8

Get data based on file data block in the repository.

Parameters

string $sql : Query to execute

array $options: Array of parameter types for the query.

Return value

SimpleXMLElement | array Data from executed SQL query.

File

src/FrxPlugin/Driver/FrxPDO.php, line 111
General database engine used to do sql queries.

Class

FrxPDO
Class FrxPDO

Namespace

Drupal\forena\FrxPlugin\Driver

Code

public function sqlData($sql, $options = array()) {

  // Load the block from the file
  $db = $this->db;
  $xml = '';

  // Load the types array based on data
  $this->types = isset($options['type']) ? $options['type'] : array();
  if ($sql && $db) {
    $sql = $this->te
      ->replace($sql);
    try {
      $rs = $db
        ->query($sql);
    } catch (\PDOException $e) {
      $line = $e
        ->getLine();
      $text = $e
        ->getMessage();
      $this
        ->app()
        ->error('PDO_error: $line', $text);
      return NULL;
    }
    if (@$options['return_type'] == 'raw') {
      return $rs;
    }
    $xml = new \SimpleXMLElement('<table/>');
    $e = $db
      ->errorCode();
    if ($e != '00000') {
      $i = $db
        ->errorInfo();
      $text = $i[0] . ':' . $i[2];

      //if (forena_user_access_check('build forena sql blocks')) {
      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);
    }
    else {
      if ($rs && $rs
        ->columnCount()) {
        if (@$options['return_type'] == 'raw') {
          return $rs;
        }
        $rownum = 0;
        foreach ($rs as $data) {
          $rownum++;
          $row_node = $xml
            ->addChild('row');
          $row_node['num'] = $rownum;
          foreach ($data as $key => $value) {
            $row_node
              ->addChild($key, htmlspecialchars($value));
          }
        }
      }
    }
    if ($this->debug) {
      $d = '';
      if ($xml) {
        $d = htmlspecialchars($xml
          ->asXML());
      }
      $this
        ->app()
        ->debug('SQL: ' . $sql, '<pre> SQL:' . $sql . "\n XML: " . $d . "/n</pre>");
    }
    return $xml;
  }
  else {
    return NULL;
  }
}