Skip to content

Commit

Permalink
Merge pull request #5 from tristanhall/features/20171009-pdo-custom-o…
Browse files Browse the repository at this point in the history
…ptions

Add array merge of "pdo_options" key from the connection config to allow
  • Loading branch information
leomarquine committed Oct 9, 2017
2 parents 0896b06 + 5688758 commit 6406e2f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
53 changes: 28 additions & 25 deletions src/Database/Connectors/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,48 @@

abstract class Connector
{

/**
* The default PDO connection options.
*
* @var array
*/
protected $options = [
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::ATTR_EMULATE_PREPARES => true,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];

/**
* Connect to a database.
*
* @param array $config
* @return \PDO
*/
abstract public function connect($config);

/**
* The default PDO connection options.
*
* @var array
*/
protected $options = [
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::ATTR_EMULATE_PREPARES => true,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];

/**
* Connect to a database.
*
* @param array $config
* @return \PDO
*/
abstract public function connect($config);

/**
* Create a new PDO connection.
*
* @param string $dsn
* @param array $config
* @param array $options
* @return \PDO
*/
public function createConnection($dsn, array $config)
{
// Merge custom PDO options from the connection config if available
if (array_key_exists('pdo_options', $config) && is_array($config['pdo_options'])) {
$this->options = array_merge($this->options, $config['pdo_options']);
}

$username = isset($config['username']) ? $config['username'] : null;

$password = isset($config['password']) ? $config['password'] : null;

return new PDO($dsn, $username, $password, $this->options);
}


// TODO: method to merge custom options
}
2 changes: 0 additions & 2 deletions src/Database/Connectors/MySqlConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Marquine\Etl\Database\Connectors;

use PDO;

class MySqlConnector extends Connector
{
/**
Expand Down

0 comments on commit 6406e2f

Please sign in to comment.