Skip to content

Commit

Permalink
Add MySQL connection
Browse files Browse the repository at this point in the history
  • Loading branch information
leomarquine committed Aug 16, 2016
1 parent 7036d85 commit d0b5ec1
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Database/Connectors/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public static function selectConnector($driver)
switch ($driver) {
case 'sqlite':
return new SqliteConnector;
case 'mysql':
return new MySqlConnector;
}

throw new InvalidArgumentException('The specified driver is not valid.');
Expand Down
18 changes: 18 additions & 0 deletions src/Database/Connectors/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,23 @@ abstract class Connector
*/
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)
{
$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
}
87 changes: 87 additions & 0 deletions src/Database/Connectors/MySqlConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Marquine\Etl\Database\Connectors;

use PDO;

class MySqlConnector 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->postConnection($connection, $config);

return $connection;
}

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

$dsn = [];

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

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

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

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

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

/**
* Handle post connection setup.
*
* @param \PDO $connection
* @param array $config
* @return void
*/
public function postConnection($connection, $config)
{
extract($config, EXTR_SKIP);

if (isset($database)) {
$connection->exec("use `$database`");
}

if (isset($charset)) {
$statement = "set names '$charset'";

if (isset($collation)) {
$statement .= " collate '$collation'";
}

$connection->prepare($statement)->execute();
}

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

}
}
2 changes: 1 addition & 1 deletion src/Loaders/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ protected function insert($items)
$item['updated_at'] = $this->time;
}

$statement->execute(array_values($item));
$statement->execute($item);
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/Providers/Laravel/EtlServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function boot()
]);

foreach (config('database.connections') as $name => $connection) {
if ($connection['driver'] == 'sqlite') {
if (in_array($connection['driver'], ['sqlite', 'mysql'])) {
if ($name == config('database.default')) {
Etl::addConnection($connection, 'default');
}
Expand Down

0 comments on commit d0b5ec1

Please sign in to comment.