1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\DataDb\Helper; |
4
|
|
|
|
5
|
|
|
use Graze\DataDb\Dialect\DialectInterface; |
6
|
|
|
use Graze\DataDb\Dialect\RedshiftDialect; |
7
|
|
|
use Graze\DataDb\TableNodeInterface; |
8
|
|
|
use Graze\DataFile\Format\CsvFormat; |
9
|
|
|
use Graze\DataFile\Format\CsvFormatInterface; |
10
|
|
|
use Graze\DataFile\Format\FormatInterface; |
11
|
|
|
use Graze\DataFile\Format\JsonFormat; |
12
|
|
|
use Graze\DataFile\Format\JsonFormatInterface; |
13
|
|
|
use Psr\Log\LogLevel; |
14
|
|
|
|
15
|
|
|
class RedshiftHelper extends AbstractHelper |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* RedshiftDialect constructor. |
19
|
|
|
* |
20
|
|
|
* @param DialectInterface|null $dialect |
21
|
|
|
* @param string|null $timezone |
22
|
|
|
*/ |
23
|
|
|
public function __construct(DialectInterface $dialect = null, $timezone = null) |
24
|
|
|
{ |
25
|
|
|
$this->dialect = $dialect ?: new RedshiftDialect(null, $timezone); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param TableNodeInterface $table |
30
|
|
|
* @param array $columns [[:column, :type, :nullable, ::primary, :index]] |
31
|
|
|
* |
32
|
|
|
* @return bool |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function createTable(TableNodeInterface $table, array $columns) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$this->log(LogLevel::INFO, "Creating Table {table} with columns: {columns}", [ |
37
|
|
|
'table' => $table->getFullName(), |
38
|
|
|
'columns' => implode(',', array_keys($columns)), |
39
|
|
|
]); |
40
|
|
|
|
41
|
|
|
$dist = []; |
42
|
|
|
$sorted = []; |
43
|
|
|
$columnStrings = []; |
44
|
|
|
|
45
|
|
|
foreach ($columns as $column) { |
46
|
|
|
if ($column['primary'] && !$dist) { |
|
|
|
|
47
|
|
|
$dist[] = $this->dialect->getPrimaryKeyDefinition($column); |
48
|
|
|
} elseif ($column['primary'] || $column['index']) { |
49
|
|
|
$sorted[] = $this->dialect->getIndexDefinition($column); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$columnStrings[] = $this->dialect->getColumnDefinition($column); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
list ($sql, $params) = $this->dialect->getCreateTable($table, $columnStrings, $dist, $sorted); |
56
|
|
|
|
57
|
|
|
$db = $table->getAdapter(); |
58
|
|
|
$db->query(trim($sql), $params); |
59
|
|
|
|
60
|
|
|
return true; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param TableNodeInterface $table |
65
|
|
|
* |
66
|
|
|
* @return array [:column => [:schema, :table, :column, :type, :nullable, :primary, :index]] |
67
|
|
|
*/ |
68
|
|
|
public function describeTable(TableNodeInterface $table) |
69
|
|
|
{ |
70
|
|
|
list ($sql, $params) = $this->dialect->getDescribeTable($table); |
71
|
|
|
|
72
|
|
|
$db = $table->getAdapter(); |
73
|
|
|
$description = $db->fetchAll(trim($sql), $params); |
74
|
|
|
|
75
|
|
|
$output = []; |
76
|
|
|
|
77
|
|
|
foreach ($description as $row) { |
78
|
|
|
$output[$row['column']] = [ |
79
|
|
|
'schema' => $table->getSchema(), |
80
|
|
|
'table' => $table->getTable(), |
81
|
|
|
'column' => $row['column'], |
82
|
|
|
'type' => $row['type'], |
83
|
|
|
'nullable' => (bool) $row['notnull'], |
84
|
|
|
'primary' => (bool) $row['distkey'], |
85
|
|
|
'index' => (bool) ($row['sortkey'] != 0), |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $output; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Produce the create syntax for a table |
94
|
|
|
* |
95
|
|
|
* @param TableNodeInterface $table |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getCreateSyntax(TableNodeInterface $table) |
100
|
|
|
{ |
101
|
|
|
list ($sql, $params) = $this->dialect->getCreateSyntax($table); |
102
|
|
|
|
103
|
|
|
$db = $table->getAdapter(); |
104
|
|
|
$result = $db->fetchAll(trim($sql), $params); |
105
|
|
|
|
106
|
|
|
if ($result) { |
|
|
|
|
107
|
|
|
$col = array_map(function ($row) { |
108
|
|
|
return $row['ddl']; |
109
|
|
|
}, $result); |
110
|
|
|
|
111
|
|
|
return implode("\n", $col); |
112
|
|
|
} else { |
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return FormatInterface |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
public function getDefaultExportFormat() |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
return new CsvFormat([ |
123
|
|
|
CsvFormat::OPTION_DELIMITER => ',', |
124
|
|
|
CsvFormat::OPTION_NEW_LINE => "\n", |
125
|
|
|
CsvFormat::OPTION_QUOTE => '"', |
126
|
|
|
CsvFormat::OPTION_NULL => '\\N', |
127
|
|
|
CsvFormat::OPTION_HEADER_ROW => -1, |
128
|
|
|
CsvFormat::OPTION_ESCAPE => '\\', |
129
|
|
|
CsvFormat::OPTION_ENCODING => 'UTF-8', |
130
|
|
|
CsvFormat::OPTION_DOUBLE_QUOTE => false, |
131
|
|
|
CsvFormat::OPTION_BOM => null, |
132
|
|
|
]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return FormatInterface |
137
|
|
|
*/ |
138
|
|
|
public function getDefaultImportFormat() |
139
|
|
|
{ |
140
|
|
|
return new JsonFormat([ |
141
|
|
|
JsonFormat::OPTION_FILE_TYPE => JsonFormat::JSON_FILE_TYPE_EACH_LINE, |
142
|
|
|
]); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param FormatInterface $format |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function isValidExportFormat(FormatInterface $format) |
151
|
|
|
{ |
152
|
|
|
return ($format->getType() == 'csv' |
153
|
|
|
&& $format instanceof CsvFormatInterface |
154
|
|
|
&& $format->getNewLine() == "\n" |
155
|
|
|
&& $format->getQuote() == '"' |
156
|
|
|
&& $format->getEscape() == '\\' |
157
|
|
|
&& $format->getEncoding() == 'UTF-8' |
158
|
|
|
&& !$format->useDoubleQuotes() |
159
|
|
|
&& is_null($format->getBom())); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param FormatInterface $format |
164
|
|
|
* |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
public function isValidImportFormat(FormatInterface $format) |
168
|
|
|
{ |
169
|
|
|
return ( |
170
|
|
|
($format->getType() == 'csv' |
171
|
|
|
&& $format instanceof CsvFormatInterface |
172
|
|
|
&& $format->getNewLine() == "\n" |
173
|
|
|
&& $format->getQuote() == '"' |
174
|
|
|
&& ( |
175
|
|
|
($format->getEscape() == '\\' && !$format->useDoubleQuotes()) || |
176
|
|
|
(!$format->hasEscape() && $format->useDoubleQuotes()) |
177
|
|
|
) |
178
|
|
|
&& $format->getEncoding() == 'UTF-8' |
179
|
|
|
&& is_null($format->getBom())) |
180
|
|
|
|| ($format->getType() == 'json' |
181
|
|
|
&& $format instanceof JsonFormatInterface |
182
|
|
|
&& $format->getJsonFileType() == JsonFormat::JSON_FILE_TYPE_EACH_LINE |
183
|
|
|
) |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
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.