-
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.
Merge pull request #10 from Multisistemas/dev
Add mssql database connector.
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 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
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"); | ||
} | ||
|
||
} | ||
} |