1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\DataDb\Export\Table; |
4
|
|
|
|
5
|
|
|
use Graze\DataDb\Export\Query\RedshiftQueryExporter; |
6
|
|
|
use Graze\DataDb\QueryNode; |
7
|
|
|
use Graze\DataDb\TableNodeInterface; |
8
|
|
|
use Graze\DataFile\Helper\Builder\BuilderAwareInterface; |
9
|
|
|
use Graze\DataFile\Helper\Builder\BuilderTrait; |
10
|
|
|
use Graze\DataFile\Helper\OptionalLoggerTrait; |
11
|
|
|
use Graze\DataFile\Node\FileNodeInterface; |
12
|
|
|
use InvalidArgumentException; |
13
|
|
|
use League\Flysystem\AwsS3v3\AwsS3Adapter; |
14
|
|
|
use Psr\Log\LoggerAwareInterface; |
15
|
|
|
use Psr\Log\LogLevel; |
16
|
|
|
|
17
|
|
|
class RedshiftTableExporter implements TableExporterInterface, BuilderAwareInterface, LoggerAwareInterface |
18
|
|
|
{ |
19
|
|
|
use OptionalLoggerTrait; |
20
|
|
|
use BuilderTrait; |
21
|
|
|
|
22
|
|
|
/** @var FileNodeInterface */ |
23
|
|
|
private $file; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* MysqlTableExporter constructor. |
27
|
|
|
* |
28
|
|
|
* @param FileNodeInterface $file |
29
|
|
|
*/ |
30
|
|
|
public function __construct(FileNodeInterface $file) |
31
|
|
|
{ |
32
|
|
|
if (!$this->validateFile($file)) { |
33
|
|
|
throw new InvalidArgumentException("The supplied file: $file should be a s3 file"); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->file = $file; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param FileNodeInterface $file |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
|
|
private function validateFile(FileNodeInterface $file) |
45
|
|
|
{ |
46
|
|
|
$adapter = $file->getFilesystem()->getAdapter(); |
47
|
|
|
return ($adapter instanceof AwsS3Adapter); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param TableNodeInterface $table |
52
|
|
|
* |
53
|
|
|
* @return FileNodeInterface |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function export(TableNodeInterface $table) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
list ($sql, $bind) = $table->getAdapter()->getDialect()->getSelectSyntax($table); |
58
|
|
|
|
59
|
|
|
$query = $this->getBuilder()->build(QueryNode::class, $table->getAdapter(), $sql, $bind); |
60
|
|
|
|
61
|
|
|
$this->log(LogLevel::DEBUG, "Exporting redshift table: {table} to file: {file}", [ |
62
|
|
|
'table' => $table, |
63
|
|
|
'file' => $this->file, |
64
|
|
|
]); |
65
|
|
|
|
66
|
|
|
$exporter = $this->getBuilder()->build(RedshiftQueryExporter::class, $this->file); |
67
|
|
|
return $exporter->export($query); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.