Skip to content

Commit

Permalink
Refactor database configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
leomarquine committed Aug 17, 2016
1 parent 2c96730 commit e49197d
Show file tree
Hide file tree
Showing 16 changed files with 214 additions and 172 deletions.
90 changes: 43 additions & 47 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,63 +10,59 @@ composer require marquine\php-etl


## Setup
### Global configuration
Global configuration can be set using the `config` method. You can skip this configuration and use the full path when working with files.
Global configuration can be set using the `config` method.
```php
use Marquine\Etl\Etl;

$config = [
'default_path' => '/path/to/etl/files',
];

Etl::config($config);
```
// If not provided, you can use the full path when working with files.
'path' => '/path/to/etl/files',

### Database
SQLite connection:
```php
$connection = [
'driver' => 'sqlite',
'database' => '/path/to/database.sqlite'
];
```
// Currently supported databases: SQLite, MySQL, PostgreSQL
'database' => [

MySQL connection
```php
$connection = [
'host' => localhost,
'port' => '3306',
'database' => dbname,
'username' => user,
'password' => pass,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci'
];
```
'default' => 'sqlite',

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

Adding connections:
```php
use Marquine\Etl\Job;
'sqlite' => [
'driver' => 'sqlite',
'database' => '/path/to/database.sqlite',
],

'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => '3306',
'database' => 'dbname',
'username' => 'user',
'password' => 'pass',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
],

'pgsql' => [
'driver' => 'pgsql',
'host' => 'localhost',
'port' => '5432',
'database' => 'dbname',
'username' => 'user',
'password' => 'pass',
'charset' => 'utf8',
'schema' => 'public',
],

// default connection
Etl::addConnection($connection);
],

// named connection
Etl::addConnection($connection, 'connection_name');
],

];

Etl::config($config);
```


## Laravel Setup
If you are using Laravel, PHP ETL provides a default configuration file and will register all supported connections of your application.

Expand All @@ -77,7 +73,7 @@ Marquine\Etl\Providers\Laravel\EtlServiceProvider::class,

Publish the configuration file (`config/etl.php`) using the artisan command:
```
php artisan vendor:publish --provider="Marquine\Etl\Providers\Laravel\EtlServiceProvider"
php artisan vendor:publish
```

## Example
Expand Down
33 changes: 3 additions & 30 deletions src/Etl.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,42 +30,15 @@ class Etl
public static function config($config, $default = null)
{
if (is_string($config)) {
$value = static::$config;

foreach (explode('.', $config) as $segment) {
$value = static::$config[$segment];
$value = isset($value[$segment]) ? $value[$segment] : null;
}

return $value ?: $default;
}

static::$config = $config;

if (isset($config['connections'])) {
foreach ($config['connections'] as $name => $connection) {
static::addConnection($connection, $name);
}
}
}

/**
* Add a database connection.
*
* @param array $connection
* @param string $name
* @return void
*/
public static function addConnection($connection, $name = 'default')
{
static::$connections[$name] = ConnectionFactory::make($connection);
}

/**
* Get a database connection.
*
* @param string $name
* @return \Marquine\Etl\Database\Connection
*/
public static function connection($name = 'default')
{
return static::$connections[$name];
}
}
6 changes: 5 additions & 1 deletion src/Extractors/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Marquine\Etl\Extractors;

use Marquine\Etl\Etl;
use Marquine\Etl\Traits\Database;

class Query implements ExtractorInterface
{
use Database;
/**
* The connection name.
*
Expand All @@ -28,7 +30,9 @@ class Query implements ExtractorInterface
*/
public function extract($query)
{
$query = Etl::connection($this->connection)->prepare($query);
$this->connect($this->connection);

$query = $this->db->prepare($query);

$query->execute($this->bindings);

Expand Down
7 changes: 6 additions & 1 deletion src/Extractors/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace Marquine\Etl\Extractors;

use Marquine\Etl\Etl;
use Marquine\Etl\Traits\Database;

class Table implements ExtractorInterface
{
use Database;

/**
* The connection name.
*
Expand Down Expand Up @@ -35,10 +38,12 @@ class Table implements ExtractorInterface
*/
public function extract($table)
{
$this->connect($this->connection);

if (is_string($this->columns)) {
$this->columns = [$this->columns];
}

return Etl::connection($this->connection)->select($table, $this->columns, $this->where);
return $this->db->select($table, $this->columns, $this->where);
}
}
23 changes: 13 additions & 10 deletions src/Loaders/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace Marquine\Etl\Loaders;

use Marquine\Etl\Etl;
use Marquine\Etl\Traits\Database;
use Marquine\Etl\Traits\Indexable;

class Table implements LoaderInterface
{
use Indexable;
use Indexable, Database;

/**
* The connection name.
Expand Down Expand Up @@ -102,6 +103,8 @@ class Table implements LoaderInterface
*/
public function load($table, $items)
{
$this->connect($this->connection);

$this->table = $table;

$this->time = date('Y-m-d G:i:s');
Expand All @@ -113,7 +116,7 @@ public function load($table, $items)
$old = [];

if (! $this->skipDataCheck) {
$select = Etl::connection($this->connection)->select($this->table);
$select = $this->db->select($this->table);

$old = $this->index($select, $this->keys);

Expand Down Expand Up @@ -149,7 +152,7 @@ protected function insert($items)
return;
}

$statement = Etl::connection($this->connection)->prepareInsert($this->table, $this->columns);
$statement = $this->db->prepareInsert($this->table, $this->columns);

$callback = function ($items) use ($statement) {
foreach ($items as $item) {
Expand All @@ -162,7 +165,7 @@ protected function insert($items)
}
};

Etl::connection($this->connection)->transaction($items, $callback, $this->transaction);
$this->db->transaction($items, $callback, $this->transaction);
}

/**
Expand All @@ -178,7 +181,7 @@ protected function update($items, $old)
return;
}

$statement = Etl::connection($this->connection)->prepareUpdate($this->table, $this->columns, $this->keys);
$statement = $this->db->prepareUpdate($this->table, $this->columns, $this->keys);

$callback = function($items) use ($statement, $old) {
foreach ($items as $key => $item) {
Expand All @@ -197,7 +200,7 @@ protected function update($items, $old)
}
};

Etl::connection($this->connection)->transaction($items, $callback, $this->transaction);
$this->db->transaction($items, $callback, $this->transaction);
}

/**
Expand All @@ -212,15 +215,15 @@ protected function delete($items)
return;
}

$statement = Etl::connection($this->connection)->prepareDelete($this->table, $this->keys);
$statement = $this->db->prepareDelete($this->table, $this->keys);

$callback = function ($items) use ($statement) {
foreach ($items as $item) {
$statement->execute(array_intersect_key($item, $this->keys));
}
};

Etl::connection($this->connection)->transaction($items, $callback, $this->transaction);
$this->db->transaction($items, $callback, $this->transaction);
}

/**
Expand All @@ -235,7 +238,7 @@ protected function softDelete($items)
return;
}

$statement = Etl::connection($this->connection)->prepareUpdate($this->table, $this->columns, $this->keys);
$statement = $this->db->prepareUpdate($this->table, $this->columns, $this->keys);

$callback = function ($items) use ($statement) {
foreach ($items as $item) {
Expand All @@ -245,7 +248,7 @@ protected function softDelete($items)
}
};

Etl::connection($this->connection)->transaction($items, $callback, $this->transaction);
$this->db->transaction($items, $callback, $this->transaction);
}

/**
Expand Down
16 changes: 2 additions & 14 deletions src/Providers/Laravel/EtlServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,10 @@ class EtlServiceProvider extends ServiceProvider
public function boot()
{
$this->publishes([
__DIR__ . '/config.php' => config_path('etl.php'),
__DIR__ . '/etl.php' => config_path('etl.php'),
]);

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

Etl::addConnection($connection, $name);
}
}

if (config('etl')) {
Etl::config(config('etl'));
}
Etl::config(config('etl'));
}

/**
Expand Down
5 changes: 0 additions & 5 deletions src/Providers/Laravel/config.php

This file was deleted.

16 changes: 16 additions & 0 deletions src/Providers/Laravel/etl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

return [

// If not provided, you can use the full path when working with files.
'path' => env('ETL_PATH', base_path('etl')),

// Currently supported databases: SQLite, MySQL, PostgreSQL
'database' => [

'default' => config('database.default'),

'connections' => config('database.connections'),
],

];
34 changes: 34 additions & 0 deletions src/Traits/Database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Marquine\Etl\Traits;

use Marquine\Etl\Etl;
use Marquine\Etl\Database\Connectors\ConnectionFactory;


trait Database
{
/**
* Database connection.
*
* @var \Marquine\Etl\Database\Connection
*/
protected $db;

/**
* Validate the given source.
*
* @param string $source
* @return string
*/
protected function connect($connection)
{
if ($connection == 'default') {
$connection = Etl::config('database.default');
}

$connection = Etl::config("database.connections.$connection");

$this->db = ConnectionFactory::make($connection);
}
}

0 comments on commit e49197d

Please sign in to comment.