You are here

public function FrxPDO::sqlData in Forena Reports 7.5

Get data based on file data block in the repository.

Parameters

String $block_name:

Array $parm_data:

Query $subQuery:

Overrides FrxDataSource::sqlData

File

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

Class

FrxPDO

Namespace

Drupal\forena\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) {
      watchdog_exception('error', $e);
      $line = $e
        ->getLine();
      $text = $e
        ->getMessage();
      drupal_set_message($short, 'error', FALSE);
      return;
    }
    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 (user_access('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
        ->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
        ->debug('SQL: ' . $sql, '<pre> SQL:' . $sql . "\n XML: " . $d . "/n</pre>");
    }
    return $xml;
  }
}