You are here

public function FrxPDO::__construct in Forena Reports 8

Object constructor

Parameters

string $name: PDO data connection name

array $conf: Array containing configuration data.

Overrides DriverBase::__construct

File

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

Class

FrxPDO
Class FrxPDO

Namespace

Drupal\forena\FrxPlugin\Driver

Code

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

    // Test for PDO suport
    if (!class_exists('PDO')) {
      $this
        ->app()
        ->error('PDO support not installed.', 'PDO support not installed.');
      return NULL;
    }
    $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
        ->app()
        ->error($msg, $msg);
      return NULL;
    }
    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
          ->app()
          ->error('Unknown error connecting to database ' . $uri);
      }
    } catch (\PDOException $e) {
      $this
        ->app()
        ->error('Unable to connect to database', $e
        ->getMessage());
    }
    if ($this->db) {
      $this->db
        ->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_OBJ);
    }
  }
  else {
    $this
      ->app()
      ->error('No database connection string specified');
  }

  // Set up the stuff required to translate.
  $this->te = new SQLReplacer($this);
}