Skip to content

Commit

Permalink
Merge pull request #10 from Multisistemas/dev
Browse files Browse the repository at this point in the history
Add mssql database connector.
  • Loading branch information
leomarquine committed Jun 1, 2018
2 parents bfe14a5 + ccbe631 commit 6c49756
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Database/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ protected function getConnector($driver)
return new Connectors\MySqlConnector;
case 'pgsql':
return new Connectors\PostgresConnector;
case 'mssql':
return new Connectors\MsSqlConnector;
}

throw new InvalidArgumentException("Unsupported driver: $driver");
Expand Down
67 changes: 67 additions & 0 deletions src/Database/Connectors/MsSqlConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Marquine\Etl\Database\Connectors;

class MsSqlConnector 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) && ! isset($unix_socket)) {
$dsn['host'] = $host;
}

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

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

return 'dblib:' . 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($database)) {
$connection->exec("USE $database");
}

}
}

0 comments on commit 6c49756

Please sign in to comment.