Skip to content

Commit bedc939

Browse files
committed
added server connector
1 parent 3599e2d commit bedc939

31 files changed

Lines changed: 417 additions & 93 deletions

app/Application.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ public function processDisk($path)
2525
/**
2626
* Initializes the extractor using the input MySQL credentials and runs the scan.
2727
*
28-
* @param $mysqlCredentials object {host,port,dbuser,dbpass,dbname}
28+
* @param $mysqlCredentials object {host,dbuser,dbpass,dbname}
2929
* @throws InvalidPathException
3030
*/
31-
// public function processServer($mysqlCredentials)
32-
// {
33-
// $this->extractor = new Driver\Server($mysqlCredentials);
34-
// $this->extractor->execute();
35-
// }
31+
public function processServer($mysqlCredentials)
32+
{
33+
$this->extractor = new Driver\Server($mysqlCredentials);
34+
$this->extractor->execute();
35+
}
3636

3737
/**
3838
* For each extracted database, dump a JSON file with the detected structure.

app/Common/System.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,16 @@ public static function getDirectoryIterator($source)
3131
}
3232
return new \DirectoryIterator($source);
3333
}
34+
35+
public static function getPDO($credentials)
36+
{
37+
if (!is_null(self::$mock)) {
38+
return call_user_func_array(array(self::$mock, 'getPDO'), array($credentials));
39+
}
40+
return new \PDO(
41+
'mysql:host=' . $credentials->host . ';dbname=' . $credentials->dbname,
42+
$credentials->dbuser,
43+
$credentials->dbpass
44+
);
45+
}
3446
}

app/Driver/Disk.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
class Disk extends Driver
1010
{
1111
protected $source;
12-
protected $entries;
13-
protected $databases = array();
1412

1513
public function __construct($path)
1614
{
@@ -51,11 +49,6 @@ public function execute()
5149
}
5250
}
5351

54-
public function databases()
55-
{
56-
return $this->databases;
57-
}
58-
5952
/**
6053
* @return array
6154
*/

app/Driver/Driver.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,13 @@
22
namespace MySQLExtractor\Driver;
33

44
abstract class Driver {
5+
protected $entries;
6+
protected $databaseExtractor;
7+
protected $databases = array();
58
abstract public function execute();
9+
10+
public function databases()
11+
{
12+
return $this->databases;
13+
}
614
}

app/Driver/Server.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
namespace MySQLExtractor\Driver;
3+
use MySQLExtractor\Common\Collection;
4+
use MySQLExtractor\Common\System;
5+
use MySQLExtractor\Extractor\Databases;
6+
use MySQLExtractor\Exceptions\InvalidSourceException;
7+
8+
class Server extends Driver
9+
{
10+
protected $PDO;
11+
12+
public function __construct($mysqlCredentials)
13+
{
14+
$this->entries = new Collection();
15+
$this->databaseExtractor = new Databases();
16+
$this->PDO = System::getPDO($mysqlCredentials);
17+
}
18+
19+
public function execute()
20+
{
21+
$this->extractRemoteDatabases();
22+
23+
if ($this->entries->count() > 0) {
24+
$this->databases = $this->databaseExtractor->from($this->entries)->get();
25+
} else {
26+
throw new InvalidSourceException("There were no entries found.", 1);
27+
}
28+
}
29+
30+
protected function extractRemoteDatabases()
31+
{
32+
$databases = $this->PDO->query('SHOW DATABASES');
33+
foreach ($databases as $database) {
34+
System::flush();
35+
$this->extractDatabaseDefinition($database[0]);
36+
}
37+
}
38+
39+
protected function extractDatabaseDefinition($databaseName)
40+
{
41+
$this->PDO->query("use " . $databaseName);
42+
$tables = $this->PDO->query('SHOW TABLES');
43+
44+
foreach ($tables as $table) {
45+
System::flush();
46+
$this->extractTableDefinition($table[0], $databaseName);
47+
}
48+
}
49+
50+
protected function extractTableDefinition($tableName, $databaseName)
51+
{
52+
$tableContents = $this->PDO->query("SHOW CREATE TABLE `" . $tableName . "`")->fetch();
53+
54+
if (is_array($tableContents) && isset($tableContents['Create Table'])) {
55+
$filename = $databaseName . '.' . $tableName . '.sql';
56+
$this->entries->set($filename, $tableContents['Create Table']);
57+
}
58+
}
59+
}

app/Driver/Server.php.draft

Lines changed: 0 additions & 36 deletions
This file was deleted.

example/index.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@
44

55
$worker = new \MySQLExtractor\Application;
66

7+
// $mysqlCredentials = new stdClass();
8+
// $mysqlCredentials->host = 'localhost:3306';
9+
// $mysqlCredentials->dbuser = 'user';
10+
// $mysqlCredentials->dbpass = 'pass';
11+
// $mysqlCredentials->dbname = 'database';
12+
//
13+
// $worker->processServer($mysqlCredentials);
14+
//
15+
// or:
716
$worker->processDisk($path . '/test_sql/'); // folder containing .sql files (format: dbname.sql)
17+
818
$worker->output($path . '/output/'); // will output .json files for each database
919

1020
echo "Finished. Check the output folder.\n\n" . implode("\n", $worker->statistics()) . "\n\n";

tests/Application/statisticsTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
class statisticsTest extends \PHPUnit_Framework_TestCase
99
{
10-
/**
11-
* when entries were found then return array of messages
12-
*/
13-
public function testWhenEntriesWereFoundThenReturnArrayOfMessages()
14-
{
10+
/**
11+
* when entries were found then return array of messages
12+
*/
13+
public function testWhenEntriesWereFoundThenReturnArrayOfMessages()
14+
{
1515
$expected = array(
1616
'- database [db1] with [3] tables.',
1717
'- database [db2] with [2] tables.'
@@ -49,5 +49,5 @@ public function testWhenEntriesWereFoundThenReturnArrayOfMessages()
4949
$statistics = $worker->statistics();
5050

5151
$this->assertEquals($expected, $statistics);
52-
}
52+
}
5353
}

tests/Common/Collection/collectionTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33

44
class CollectionTest extends \PHPUnit_Framework_TestCase
55
{
6-
/**
7-
* when no input is sent to the constructor then initialize using an empty array
8-
*/
9-
public function testWhenNoInputIsSentToTheConstructorThenInitializeUsingAnEmptyArray()
10-
{
6+
/**
7+
* when no input is sent to the constructor then initialize using an empty array
8+
*/
9+
public function testWhenNoInputIsSentToTheConstructorThenInitializeUsingAnEmptyArray()
10+
{
1111
$Collection = new \MySQLExtractor\Common\Collection();
1212
$elements = \PHPUnit_Framework_Assert::readAttribute($Collection, 'elements');
1313
$this->assertEquals(array(), $elements);
14-
}
14+
}
1515

1616
/**
1717
* when input is sent to the constructor then initialize using the input
1818
*/
19-
public function testWhenInputIsSentToTheConstructorThenInitializeUsingTheInput()
19+
public function testWhenInputIsSentToTheConstructorThenInitializeUsingTheInput()
2020
{
2121
$input = array(
2222
'a' => 'b',

tests/Common/Collection/containsKeyTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class containsKeyTest extends \PHPUnit_Framework_TestCase
66
/**
77
* containsKey will return true if an element is set for the input key
88
*/
9-
public function testContainsKeyWillReturnTrueIfAnElementIsSetForTheInputKey()
9+
public function testContainsKeyWillReturnTrueIfAnElementIsSetForTheInputKey()
1010
{
1111
$element1 = new \stdClass();
1212
$element1->Name = 'EL 1';
@@ -25,7 +25,7 @@ public function testContainsKeyWillReturnTrueIfAnElementIsSetForTheInputKey()
2525
/**
2626
* containsKey will return false if an element is not set for the input key
2727
*/
28-
public function testContainsKeyWillReturnFalseIfAnElementIsNotSetForTheInputKey()
28+
public function testContainsKeyWillReturnFalseIfAnElementIsNotSetForTheInputKey()
2929
{
3030
$element1 = new \stdClass();
3131
$element1->Name = 'EL 1';

0 commit comments

Comments
 (0)