Skip to content

Commit

Permalink
Add PostgreSQL connection
Browse files Browse the repository at this point in the history
  • Loading branch information
leomarquine committed Aug 17, 2016
1 parent f39e6de commit 2c96730
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
16 changes: 15 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ MySQL connection
```php
$connection = [
'host' => localhost,
'port' => 3306,
'port' => '3306',
'database' => dbname,
'username' => user,
'password' => pass,
Expand All @@ -42,6 +42,20 @@ $connection = [
];
```

PostgreSQL connection
```php
$connection = [
'driver' => 'pgsql',
'host' => 'localhost',
'port' => '5432',
'database' => 'dbname',
'username' => 'user',
'password' => 'pass',
'charset' => 'utf8',
'schema' => 'public'
];
```

Adding connections:
```php
use Marquine\Etl\Job;
Expand Down
4 changes: 3 additions & 1 deletion src/Database/Connectors/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function make($config)
/**
* Select the database connector.
*
* @param string driver
* @param string $driver
* @return \Marquine\Etl\Database\Connectors\Connector
*/
public static function selectConnector($driver)
Expand All @@ -38,6 +38,8 @@ public static function selectConnector($driver)
return new SqliteConnector;
case 'mysql':
return new MySqlConnector;
case 'pgsql':
return new PostgresConnector;
}

throw new InvalidArgumentException('The specified driver is not valid.');
Expand Down
97 changes: 97 additions & 0 deletions src/Database/Connectors/PostgresConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Marquine\Etl\Database\Connectors;

use PDO;

class PostgresConnector extends Connector
{
/**
* Connect to a database.
*
* @param array $config
* @return \PDO
*/
public function connect($config)
{
$dsn = $this->getDsn($config);

$connection = $this->createConnection($dsn, $config);

$this->afterConnection($connection, $config);

return $connection;
}

/**
* Get the DSN string.
*
* @param array $config
* @return string
*/
public function getDsn($config)
{
extract($config, EXTR_SKIP);

$dsn = [];

if (isset($host)) {
$dsn['host'] = $host;
}

if (isset($port)) {
$dsn['port'] = $port;
}

if (isset($database)) {
$dsn['dbname'] = $database;
}

return 'pgsql:' . http_build_query($dsn, '', ';');
}

/**
* Handle tasks after connection.
*
* @param \PDO $connection
* @param array $config
* @return void
*/
public function afterConnection($connection, $config)
{
extract($config, EXTR_SKIP);

if (isset($charset)) {
$connection->prepare("set names '$charset'")->execute();
}

if (isset($timezone)) {
$connection->prepare("set time zone '$timezone'")->execute();
}

if (isset($schema)) {
$schema = $this->formatSchema($schema);

$connection->prepare("set search_path to $schema")->execute();
}

if (isset($application_name)) {
$connection->prepare("set application_name to '$application_name'")->execute();
}
}

/**
* Format the schema.
*
* @param array|string $schema
* @return string
*/
public function formatSchema($schema)
{
if (is_string($schema)) {
$schema = [$schema];
}

return implode(', ', $schema);
}
}

0 comments on commit 2c96730

Please sign in to comment.