1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Phinx\Db\Adapter; |
4
|
|
|
|
5
|
|
|
use Phinx\Db\Table\Table; |
6
|
|
|
use Phinx\Db\Table\Column; |
7
|
|
|
|
8
|
|
|
class RedshiftAdapter extends PostgresAdapter |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
public function createTable(Table $table, array $columns = [], array $indexes = []) |
12
|
|
|
{ |
13
|
|
|
$options = $table->getOptions(); |
14
|
|
|
$parts = $this->getSchemaName($table->getName()); |
15
|
|
|
|
16
|
|
|
// Add the default primary key |
17
|
|
|
if (!isset($options['id']) || ( isset($options['id']) && $options['id'] === true )) { |
18
|
|
|
$column = new Column(); |
19
|
|
|
$column->setName('id') |
20
|
|
|
->setType('integer') |
21
|
|
|
->setIdentity(true); |
22
|
|
|
|
23
|
|
|
array_unshift($columns, $column); |
24
|
|
|
$options['primary_key'] = 'id'; |
25
|
|
|
} elseif (isset($options['id']) && |
26
|
|
|
is_string($options['id']) ) {// Handle id => "field_name" to support AUTO_INCREMENT |
27
|
|
|
$column = new Column(); |
28
|
|
|
$column->setName($options['id']) |
29
|
|
|
->setType('integer') |
30
|
|
|
->setIdentity(true); |
31
|
|
|
|
32
|
|
|
array_unshift($columns, $column); |
33
|
|
|
$options['primary_key'] = $options['id']; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// TODO - process table options like collation etc |
37
|
|
|
$sql = 'CREATE TABLE '; |
38
|
|
|
$sql .= $this->quoteTableName($table->getName()) . ' ('; |
39
|
|
|
|
40
|
|
|
$this->columnsWithComments = []; |
41
|
|
View Code Duplication |
foreach ($columns as $column) { |
|
|
|
|
42
|
|
|
$sql .= $this->quoteColumnName($column->getName()) . ' ' . $this->getColumnSqlDefinition($column) . |
43
|
|
|
', '; |
44
|
|
|
|
45
|
|
|
// set column comments, if needed |
46
|
|
|
if ($column->getComment()) { |
47
|
|
|
$this->columnsWithComments[] = $column; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// set the primary key(s) |
52
|
|
View Code Duplication |
if (isset($options['primary_key'])) { |
|
|
|
|
53
|
|
|
$sql = rtrim($sql); |
54
|
|
|
$sql .= sprintf(' CONSTRAINT %s PRIMARY KEY (', $this->quoteColumnName($parts['table'] . '_pkey')); |
55
|
|
|
if (is_string($options['primary_key'])) { // handle primary_key => 'id' |
56
|
|
|
$sql .= $this->quoteColumnName($options['primary_key']); |
57
|
|
|
} elseif (is_array($options['primary_key'])) { // handle primary_key => array('tag_id', 'resource_id') |
|
|
|
|
58
|
|
|
$sql .= implode(',', array_map([ $this, 'quoteColumnName' ], $options['primary_key'])); |
59
|
|
|
} |
60
|
|
|
$sql .= ')'; |
61
|
|
|
} else { |
62
|
|
|
$sql = rtrim($sql, ', '); // no primary keys |
63
|
|
|
} |
64
|
|
|
$sql .= ') '; |
65
|
|
|
|
66
|
|
|
// process redshift sortkeys & distkey |
67
|
|
|
$sortKeys = isset($options['sortkeys']) ? $options['sortkeys'] : null; |
68
|
|
|
|
69
|
|
|
$distKey = isset($options['distkey']) ? $options['distkey'] : null; |
70
|
|
|
|
71
|
|
|
$distStyle = isset($options['diststyle']) ? $options['diststyle'] : null; |
72
|
|
|
|
73
|
|
|
$interleavedSortKey = isset($options['interleaved']) ? (bool)$options['interleaved'] : false; |
74
|
|
|
|
75
|
|
|
if (!empty($distKey)) { |
76
|
|
|
$sql .= ' distkey(' . addslashes($distKey) . ')'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (!empty($sortKeys)) { |
80
|
|
|
$sortKeyStr = is_array($sortKeys) ? addslashes(implode(',', $sortKeys)) : addslashes($sortKeys); |
81
|
|
|
|
82
|
|
|
if (!$interleavedSortKey) { |
83
|
|
|
$sql .= sprintf(' compound sortkey (%s) ', $sortKeyStr); |
84
|
|
|
} else { |
85
|
|
|
$sql .= sprintf(' interleaved sortkey (%s) ', $sortKeyStr); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (!empty($distStyle)) { |
90
|
|
|
$sql .= sprintf(' diststyle %s', $distStyle); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$sql .= ';'; |
94
|
|
|
|
95
|
|
|
// process column comments |
96
|
|
View Code Duplication |
if (!empty($this->columnsWithComments)) { |
|
|
|
|
97
|
|
|
foreach ($this->columnsWithComments as $column) { |
98
|
|
|
$sql .= $this->getColumnCommentSqlDefinition($column, $table->getName()); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// set the indexes |
103
|
|
|
if (!empty($indexes)) { |
104
|
|
|
foreach ($indexes as $index) { |
105
|
|
|
$sql .= $this->getIndexSqlDefinition($index, $table->getName()); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// execute the sql |
110
|
|
|
$this->execute($sql); |
111
|
|
|
|
112
|
|
|
// process table comments |
113
|
|
View Code Duplication |
if (isset($options['comment'])) { |
|
|
|
|
114
|
|
|
$sql = sprintf( |
115
|
|
|
'COMMENT ON TABLE %s IS %s', |
116
|
|
|
$this->quoteTableName($table->getName()), |
117
|
|
|
$this->getConnection() |
118
|
|
|
->quote($options['comment']) |
119
|
|
|
); |
120
|
|
|
$this->execute($sql); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $tableName Table name |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
View Code Duplication |
private function getSchemaName($tableName) |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$schema = $this->getGlobalSchemaName(); |
132
|
|
|
$table = $tableName; |
133
|
|
|
if (false !== strpos($tableName, '.')) { |
134
|
|
|
list( $schema, $table ) = explode('.', $tableName); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return [ |
138
|
|
|
'schema' => $schema, |
139
|
|
|
'table' => $table, |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Gets the schema name. |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
private function getGlobalSchemaName() |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
$options = $this->getOptions(); |
151
|
|
|
|
152
|
|
|
return empty($options['schema']) ? 'public' : $options['schema']; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
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.