You are here

public function FrxPDO::__construct in Forena Reports 7.4

Same name and namespace in other branches
  1. 6.2 plugins/FrxPDO.inc \FrxPDO::__construct()
  2. 6 plugins/FrxPDO.inc \FrxPDO::__construct()
  3. 7 plugins/FrxPDO.inc \FrxPDO::__construct()
  4. 7.2 plugins/FrxPDO.inc \FrxPDO::__construct()
  5. 7.3 plugins/FrxPDO.inc \FrxPDO::__construct()

Object constructor

Parameters

unknown_type $uri Database connection string.:

string $repos_path Path to location of data block definitions:

Overrides FrxDataSource::__construct

File

plugins/FrxPDO.inc, line 21
General database engine used to do sql queries.

Class

FrxPDO
@file General database engine used to do sql queries.

Code

public function __construct($conf, $repos_path, $name) {
  parent::__construct($conf, $repos_path, $name);
  $uri = $conf['uri'];
  $this->debug = @$conf['debug'];
  if ($uri) {

    // Test for PDO suport
    if (!class_exists('PDO')) {
      $this
        ->error('PDO support not installed.', 'PDO support not installed.');
      return;
    }
    $options = array();
    if (@$conf['mysql_charset']) {
      $options = array(
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $conf['mysql_charset'],
      );
    }

    // Test for driver support
    @(list($prot, $c) = explode(':', $uri, 2));
    $drivers = PDO::getAvailableDrivers();
    $this->db_type = $prot;
    if ($drivers && array_search($prot, $drivers) === FALSE) {
      $msg = 'PDO driver support for ' . $prot . ' not installed';
      $this
        ->error($msg, $msg);
      return;
    }
    try {
      if (isset($conf['user'])) {
        $db = new PDO($uri, $conf['user'], @$conf['password'], $options);
      }
      else {
        $db = new PDO($uri, NULL, NULL, $options);
      }
      $this->db = $db;
      if (!is_object($db)) {
        $this
          ->error('Unknown error connecting to database ' . $uri);
      }
    } catch (PDOException $e) {
      $this
        ->error('Unable to connect to database', $e
        ->getMessage());
    }
    if ($this->db) {
      $this->db
        ->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
    }
  }
  else {
    $this
      ->error('No database connection string specified');
  }

  // Set up the stuff required to translate.
  $this->te = new FrxSyntaxEngine(FRX_SQL_TOKEN, ':', $this);
}