Exemple #1
0
 /**
  * Constructs a new database object based on the given params
  *
  * @param   array  $options  The database connection params
  * @return  void
  * @since   2.0.0
  */
 public function __construct($options)
 {
     // Make sure the pdo extension for PHP is installed and enabled
     if (!class_exists('PDO')) {
         throw new ConnectionFailedException('PDO does not appear to be installed or enabled.', 500);
     }
     // Try to connect
     try {
         // Add "extra" options as needed
         $extras = [];
         // Check if we're trying to make an SSL connection
         if (isset($options['ssl_ca']) && $options['ssl_ca'] && $options['host'] != 'localhost') {
             $extras[PDO::MYSQL_ATTR_SSL_CA] = $options['ssl_ca'];
         }
         // Establish connection string
         $parameters = "mysql:host={$options['host']};charset=utf8";
         $parameters .= $options['select'] ? ";dbname={$options['database']}" : '';
         $this->setConnection(new \PDO($parameters, $options['user'], $options['password'], $extras));
     } catch (\PDOException $e) {
         throw new ConnectionFailedException($e->getMessage(), 500);
     }
     // Set error reporting to throw exceptions
     $this->throwExceptions();
     // Call parent construct
     parent::__construct($options);
     // @FIXME: Set sql_mode to non_strict mode?
 }