2
* Copyright 2006 StartNet s.r.o.
4
* Distributed under MIT license
1
6
package cz.startnet.utils.pgdiff.schema;
3
8
import cz.startnet.utils.pgdiff.PgDiffUtils;
5
10
import java.util.ArrayList;
11
import java.util.Collections;
6
12
import java.util.List;
16
22
* List of columns defined on the table.
24
@SuppressWarnings("CollectionWithoutInitialCapacity")
18
25
private final List<PgColumn> columns = new ArrayList<PgColumn>();
20
27
* List of constraints defined on the table.
29
@SuppressWarnings("CollectionWithoutInitialCapacity")
22
30
private final List<PgConstraint> constraints =
23
31
new ArrayList<PgConstraint>();
25
33
* List of indexes defined on the table.
35
@SuppressWarnings("CollectionWithoutInitialCapacity")
27
36
private final List<PgIndex> indexes = new ArrayList<PgIndex>();
29
38
* List of triggers defined on the table.
40
@SuppressWarnings("CollectionWithoutInitialCapacity")
31
41
private final List<PgTrigger> triggers = new ArrayList<PgTrigger>();
33
43
* Name of the index on which the table is clustered
35
45
private String clusterIndexName;
37
* Definition of names of inherited tables.
47
* List of names of inherited tables.
39
private String inherits;
49
@SuppressWarnings("CollectionWithoutInitialCapacity")
50
private final List<String> inherits = new ArrayList<String>();
41
52
* Name of the table.
43
54
private String name;
45
* Whether WITH OIDS is used.
47
private boolean withOIDS;
56
* WITH clause. If value is null then it is not set, otherwise can be set to
57
* OIDS=true, OIDS=false, or storage parameters can be set.
63
private String tablespace;
50
66
* Creates a new PgTable object.
129
* Getter for {@link #constraints}.
145
* Getter for {@link #constraints}. The list cannot be modified.
131
147
* @return {@link #constraints}
133
149
public List<PgConstraint> getConstraints() {
150
return Collections.unmodifiableList(constraints);
138
154
* Creates and returns SQL for creation of the table.
140
* @param quoteNames whether names should be quoted
142
* @return created SQL command
156
* @return created SQL statement
144
public String getCreationSQL(final boolean quoteNames) {
145
final StringBuilder sbSQL = new StringBuilder();
158
public String getCreationSQL() {
159
final StringBuilder sbSQL = new StringBuilder(1000);
146
160
sbSQL.append("CREATE TABLE ");
147
sbSQL.append(PgDiffUtils.getQuotedName(name, quoteNames));
161
sbSQL.append(PgDiffUtils.getQuotedName(name));
148
162
sbSQL.append(" (\n");
150
164
for (PgColumn column : columns) {
151
165
sbSQL.append("\t");
152
sbSQL.append(column.getFullDefinition(quoteNames, false));
166
sbSQL.append(column.getFullDefinition(false));
153
167
sbSQL.append(",\n");
156
170
sbSQL.setLength(sbSQL.length() - 2);
157
171
sbSQL.append("\n)");
159
if ((inherits != null) && (inherits.length() > 0)) {
160
sbSQL.append("\nINHERITS ");
161
sbSQL.append(inherits);
173
if (inherits != null && !inherits.isEmpty()) {
174
sbSQL.append("\nINHERITS (");
176
boolean first = true;
178
for (final String tableName : inherits) {
185
sbSQL.append(tableName);
191
if (with != null && !with.isEmpty()) {
194
if ("OIDS=false".equalsIgnoreCase(with)) {
195
sbSQL.append("WITHOUT OIDS");
197
sbSQL.append("WITH ");
199
if ("OIDS".equalsIgnoreCase(with)
200
|| "OIDS=true".equalsIgnoreCase(with)) {
201
sbSQL.append("OIDS");
208
if (tablespace != null && !tablespace.isEmpty()) {
209
sbSQL.append("\nTABLESPACE ");
210
sbSQL.append(tablespace);
164
213
sbSQL.append(';');
166
215
for (PgColumn column : getColumnsWithStatistics()) {
167
216
sbSQL.append("\nALTER TABLE ONLY ");
168
sbSQL.append(PgDiffUtils.getQuotedName(name, quoteNames));
217
sbSQL.append(PgDiffUtils.getQuotedName(name));
169
218
sbSQL.append(" ALTER COLUMN ");
171
PgDiffUtils.getQuotedName(column.getName(), quoteNames));
220
PgDiffUtils.getQuotedName(column.getName()));
172
221
sbSQL.append(" SET STATISTICS ");
173
222
sbSQL.append(column.getStatistics());
174
223
sbSQL.append(';');
181
* Creates and returns SQL command for dropping the table.
183
* @param quoteNames whether names should be quoted
185
* @return created SQL command
230
* Creates and returns SQL statement for dropping the table.
232
* @return created SQL statement
187
public String getDropSQL(final boolean quoteNames) {
188
return "DROP TABLE " + PgDiffUtils.getQuotedName(getName(), quoteNames)
234
public String getDropSQL() {
235
return "DROP TABLE " + PgDiffUtils.getQuotedName(getName()) + ";";
214
* Getter for {@link #indexes}.
260
* Getter for {@link #indexes}. The list cannot be modified.
216
262
* @return {@link #indexes}
218
264
public List<PgIndex> getIndexes() {
265
return Collections.unmodifiableList(indexes);
223
269
* Setter for {@link #inherits}.
225
* @param inherits {@link #inherits}
271
* @param tableName name of inherited table
227
public void setInherits(final String inherits) {
228
this.inherits = inherits;
273
public void addInherits(final String tableName) {
274
inherits.add(tableName);
259
* Getter for {@link #triggers}.
305
* Getter for {@link #triggers}. The list cannot be modified.
261
307
* @return {@link #triggers}
263
309
public List<PgTrigger> getTriggers() {
268
* Setter for {@link #withOIDS}.
270
* @param withOIDS {@link #withOIDS}
272
public void setWithOIDS(final boolean withOIDS) {
273
this.withOIDS = withOIDS;
277
* Getter for {@link #withOIDS}
279
* @return {@link #withOIDS}
281
public boolean isWithOIDS() {
310
return Collections.unmodifiableList(triggers);
314
* Setter for {@link #with}.
316
* @param with {@link #with}
318
public void setWith(final String with) {
323
* Getter for {@link #with}
325
* @return {@link #with}
327
public String getWith() {
332
* Getter for {@link #tablespace}.
334
* @return {@link #tablespace}
336
public String getTablespace() {
341
* Setter for {@link #tablespace}.
343
* @param tablespace {@link #tablespace}
345
public void setTablespace(final String tablespace) {
346
this.tablespace = tablespace;