1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\DataDb\Import; |
4
|
|
|
|
5
|
|
|
use Graze\DataDb\Dialect\RedshiftDialect; |
6
|
|
|
use Graze\DataDb\Helper\RedshiftHelper; |
7
|
|
|
use Graze\DataDb\TableNodeInterface; |
8
|
|
|
use Graze\DataFile\Format\CsvFormatInterface; |
9
|
|
|
use Graze\DataFile\Format\FormatAwareInterface; |
10
|
|
|
use Graze\DataFile\Format\JsonFormatInterface; |
11
|
|
|
use Graze\DataFile\Helper\Builder\BuilderAwareInterface; |
12
|
|
|
use Graze\DataFile\Helper\Builder\BuilderTrait; |
13
|
|
|
use Graze\DataFile\Node\FileNodeInterface; |
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use League\Flysystem\AwsS3v3\AwsS3Adapter; |
16
|
|
|
|
17
|
|
|
class RedshiftFileImporter implements FileImporterInterface, BuilderAwareInterface |
18
|
|
|
{ |
19
|
|
|
use BuilderTrait; |
20
|
|
|
|
21
|
|
|
/** @var TableNodeInterface */ |
22
|
|
|
private $table; |
23
|
|
|
/** @var RedshiftDialect */ |
24
|
|
|
private $dialect; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* RedshiftFileImporter constructor. |
28
|
|
|
* |
29
|
|
|
* @param TableNodeInterface $table |
30
|
|
|
*/ |
31
|
|
|
public function __construct(TableNodeInterface $table) |
32
|
|
|
{ |
33
|
|
|
$this->dialect = $table->getAdapter()->getDialect(); |
34
|
|
|
if (!$this->dialect instanceof RedshiftDialect) { |
35
|
|
|
throw new InvalidArgumentException("The provided table: $table is not a redshift table"); |
36
|
|
|
} |
37
|
|
|
$this->table = $table; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param FileNodeInterface $file |
42
|
|
|
* |
43
|
|
|
* @return TableNodeInterface |
44
|
|
|
*/ |
45
|
|
|
public function import(FileNodeInterface $file) |
46
|
|
|
{ |
47
|
|
|
$helper = $this->getBuilder()->build(RedshiftHelper::class, $this->dialect); |
48
|
|
|
|
49
|
|
|
if (!$file->getFilesystem()->getAdapter() instanceof AwsS3Adapter) { |
50
|
|
|
throw new InvalidArgumentException("The supplied file: $file is required to be in S3 for import into Redshift"); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($file instanceof FormatAwareInterface) { |
54
|
|
|
$format = $file->getFormat(); |
55
|
|
|
if (!$helper->isValidImportFormat($format)) { |
56
|
|
|
throw new InvalidArgumentException("The supplied file: $file does not have a valid format for redshift"); |
57
|
|
|
} |
58
|
|
|
} else { |
59
|
|
|
throw new InvalidArgumentException("No formatting could not be determined from the supplied file: $file"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($format instanceof JsonFormatInterface) { |
63
|
|
|
list($sql, $bind) = $this->dialect->getImportFromJson($this->table, $file, $format); |
|
|
|
|
64
|
|
|
} elseif ($format instanceof CsvFormatInterface) { |
65
|
|
|
list($sql, $bind) = $this->dialect->getImportFromCsv($this->table, $file, $format); |
66
|
|
|
} else { |
67
|
|
|
throw new InvalidArgumentException("The format type: " . get_class($format) . " can not be used to import into redshift"); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->table->getAdapter()->query($sql, $bind); |
71
|
|
|
|
72
|
|
|
return $this->table; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: