You are here

public function FrxPDO::__construct in Forena Reports 6.2

Same name and namespace in other branches
  1. 6 plugins/FrxPDO.inc \FrxPDO::__construct()
  2. 7 plugins/FrxPDO.inc \FrxPDO::__construct()
  3. 7.2 plugins/FrxPDO.inc \FrxPDO::__construct()
  4. 7.3 plugins/FrxPDO.inc \FrxPDO::__construct()
  5. 7.4 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 FrxDataProvider::__construct

File

plugins/FrxPDO.inc, line 22
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) {
  parent::__construct($conf, $repos_path);
  $uri = $conf['uri'];
  $this->debug = $conf['debug'];
  if ($uri) {

    // Test for PDO suport
    if (!class_exists('PDO')) {
      FrxReportGenerator::instance()
        ->error('PDO support not installed.', 'PDO support not installed.');
      return;
    }

    // Fix for php 5.2 utf-8 support
    $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();
    if ($drivers && array_search($prot, $drivers) === FALSE) {
      $msg = 'PDO driver support for ' . $prot . ' not installed';
      FrxReportGenerator::instance()
        ->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)) {
        FrxReportGenerator::instance()
          ->error('Unknown error connecting to database ' . $uri);
      }
    } catch (PDOException $e) {
      FrxReportGenerator::instance()
        ->error('Unable to connect to database', $e
        ->getMessage());
    }
  }
  else {
    FrxReportGenerator::instance()
      ->error('No database connection string specified');
  }

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