-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f39e6de
commit 2c96730
Showing
3 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |