~ubuntu-branches/ubuntu/raring/apgdiff/raring

« back to all changes in this revision

Viewing changes to src/main/java/cz/startnet/utils/pgdiff/schema/PgTable.java

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Berg
  • Date: 2010-10-11 09:08:18 UTC
  • mfrom: (2.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20101011090818-sdw8yfemrnxo328k
Tags: 2.2.2-1
* New upstream version.
* Using changelog included in zipfile, thanks Miroslav for providing this.
* Update manpage.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2006 StartNet s.r.o.
 
3
 *
 
4
 * Distributed under MIT license
 
5
 */
1
6
package cz.startnet.utils.pgdiff.schema;
2
7
 
3
8
import cz.startnet.utils.pgdiff.PgDiffUtils;
4
9
 
5
10
import java.util.ArrayList;
 
11
import java.util.Collections;
6
12
import java.util.List;
7
13
 
8
14
/**
15
21
    /**
16
22
     * List of columns defined on the table.
17
23
     */
 
24
    @SuppressWarnings("CollectionWithoutInitialCapacity")
18
25
    private final List<PgColumn> columns = new ArrayList<PgColumn>();
19
26
    /**
20
27
     * List of constraints defined on the table.
21
28
     */
 
29
    @SuppressWarnings("CollectionWithoutInitialCapacity")
22
30
    private final List<PgConstraint> constraints =
23
31
            new ArrayList<PgConstraint>();
24
32
    /**
25
33
     * List of indexes defined on the table.
26
34
     */
 
35
    @SuppressWarnings("CollectionWithoutInitialCapacity")
27
36
    private final List<PgIndex> indexes = new ArrayList<PgIndex>();
28
37
    /**
29
38
     * List of triggers defined on the table.
30
39
     */
 
40
    @SuppressWarnings("CollectionWithoutInitialCapacity")
31
41
    private final List<PgTrigger> triggers = new ArrayList<PgTrigger>();
32
42
    /**
33
43
     * Name of the index on which the table is clustered
34
44
     */
35
45
    private String clusterIndexName;
36
46
    /**
37
 
     * Definition of names of inherited tables.
 
47
     * List of names of inherited tables.
38
48
     */
39
 
    private String inherits;
 
49
    @SuppressWarnings("CollectionWithoutInitialCapacity")
 
50
    private final List<String> inherits = new ArrayList<String>();
40
51
    /**
41
52
     * Name of the table.
42
53
     */
43
54
    private String name;
44
55
    /**
45
 
     * Whether WITH OIDS is used.
46
 
     */
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.
 
58
     */
 
59
    private String with;
 
60
    /**
 
61
     * Tablespace value.
 
62
     */
 
63
    private String tablespace;
48
64
 
49
65
    /**
50
66
     * Creates a new PgTable object.
95
111
    }
96
112
 
97
113
    /**
98
 
     * Getter for {@link #columns}.
 
114
     * Getter for {@link #columns}. The list cannot be modified.
99
115
     *
100
116
     * @return {@link #columns}
101
117
     */
102
118
    public List<PgColumn> getColumns() {
103
 
        return columns;
 
119
        return Collections.unmodifiableList(columns);
104
120
    }
105
121
 
106
122
    /**
126
142
    }
127
143
 
128
144
    /**
129
 
     * Getter for {@link #constraints}.
 
145
     * Getter for {@link #constraints}. The list cannot be modified.
130
146
     *
131
147
     * @return {@link #constraints}
132
148
     */
133
149
    public List<PgConstraint> getConstraints() {
134
 
        return constraints;
 
150
        return Collections.unmodifiableList(constraints);
135
151
    }
136
152
 
137
153
    /**
138
154
     * Creates and returns SQL for creation of the table.
139
155
     *
140
 
     * @param quoteNames whether names should be quoted
141
 
     *
142
 
     * @return created SQL command
 
156
     * @return created SQL statement
143
157
     */
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");
149
163
 
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");
154
168
        }
155
169
 
156
170
        sbSQL.setLength(sbSQL.length() - 2);
157
171
        sbSQL.append("\n)");
158
172
 
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 (");
 
175
 
 
176
            boolean first = true;
 
177
 
 
178
            for (final String tableName : inherits) {
 
179
                if (first) {
 
180
                    first = false;
 
181
                } else {
 
182
                    sbSQL.append(", ");
 
183
                }
 
184
 
 
185
                sbSQL.append(tableName);
 
186
            }
 
187
 
 
188
            sbSQL.append(")");
 
189
        }
 
190
 
 
191
        if (with != null && !with.isEmpty()) {
 
192
            sbSQL.append("\n");
 
193
 
 
194
            if ("OIDS=false".equalsIgnoreCase(with)) {
 
195
                sbSQL.append("WITHOUT OIDS");
 
196
            } else {
 
197
                sbSQL.append("WITH ");
 
198
 
 
199
                if ("OIDS".equalsIgnoreCase(with)
 
200
                        || "OIDS=true".equalsIgnoreCase(with)) {
 
201
                    sbSQL.append("OIDS");
 
202
                } else {
 
203
                    sbSQL.append(with);
 
204
                }
 
205
            }
 
206
        }
 
207
 
 
208
        if (tablespace != null && !tablespace.isEmpty()) {
 
209
            sbSQL.append("\nTABLESPACE ");
 
210
            sbSQL.append(tablespace);
162
211
        }
163
212
 
164
213
        sbSQL.append(';');
165
214
 
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 ");
170
219
            sbSQL.append(
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(';');
178
227
    }
179
228
 
180
229
    /**
181
 
     * Creates and returns SQL command for dropping the table.
182
 
     *
183
 
     * @param quoteNames whether names should be quoted
184
 
     *
185
 
     * @return created SQL command
 
230
     * Creates and returns SQL statement for dropping the table.
 
231
     *
 
232
     * @return created SQL statement
186
233
     */
187
 
    public String getDropSQL(final boolean quoteNames) {
188
 
        return "DROP TABLE " + PgDiffUtils.getQuotedName(getName(), quoteNames)
189
 
                + ";";
 
234
    public String getDropSQL() {
 
235
        return "DROP TABLE " + PgDiffUtils.getQuotedName(getName()) + ";";
190
236
    }
191
237
 
192
238
    /**
211
257
    }
212
258
 
213
259
    /**
214
 
     * Getter for {@link #indexes}.
 
260
     * Getter for {@link #indexes}. The list cannot be modified.
215
261
     *
216
262
     * @return {@link #indexes}
217
263
     */
218
264
    public List<PgIndex> getIndexes() {
219
 
        return indexes;
 
265
        return Collections.unmodifiableList(indexes);
220
266
    }
221
267
 
222
268
    /**
223
269
     * Setter for {@link #inherits}.
224
270
     *
225
 
     * @param inherits {@link #inherits}
 
271
     * @param tableName name of inherited table
226
272
     */
227
 
    public void setInherits(final String inherits) {
228
 
        this.inherits = inherits;
 
273
    public void addInherits(final String tableName) {
 
274
        inherits.add(tableName);
229
275
    }
230
276
 
231
277
    /**
233
279
     *
234
280
     * @return {@link #inherits}
235
281
     */
236
 
    public String getInherits() {
237
 
        return inherits;
 
282
    public List<String> getInherits() {
 
283
        return Collections.unmodifiableList(inherits);
238
284
    }
239
285
 
240
286
    /**
256
302
    }
257
303
 
258
304
    /**
259
 
     * Getter for {@link #triggers}.
 
305
     * Getter for {@link #triggers}. The list cannot be modified.
260
306
     *
261
307
     * @return {@link #triggers}
262
308
     */
263
309
    public List<PgTrigger> getTriggers() {
264
 
        return triggers;
265
 
    }
266
 
 
267
 
    /**
268
 
     * Setter for {@link #withOIDS}.
269
 
     *
270
 
     * @param withOIDS {@link #withOIDS}
271
 
     */
272
 
    public void setWithOIDS(final boolean withOIDS) {
273
 
        this.withOIDS = withOIDS;
274
 
    }
275
 
 
276
 
    /**
277
 
     * Getter for {@link #withOIDS}
278
 
     *
279
 
     * @return {@link #withOIDS}
280
 
     */
281
 
    public boolean isWithOIDS() {
282
 
        return withOIDS;
 
310
        return Collections.unmodifiableList(triggers);
 
311
    }
 
312
 
 
313
    /**
 
314
     * Setter for {@link #with}.
 
315
     *
 
316
     * @param with {@link #with}
 
317
     */
 
318
    public void setWith(final String with) {
 
319
        this.with = with;
 
320
    }
 
321
 
 
322
    /**
 
323
     * Getter for {@link #with}
 
324
     *
 
325
     * @return {@link #with}
 
326
     */
 
327
    public String getWith() {
 
328
        return with;
 
329
    }
 
330
 
 
331
    /**
 
332
     * Getter for {@link #tablespace}.
 
333
     *
 
334
     * @return {@link #tablespace}
 
335
     */
 
336
    public String getTablespace() {
 
337
        return tablespace;
 
338
    }
 
339
 
 
340
    /**
 
341
     * Setter for {@link #tablespace}.
 
342
     *
 
343
     * @param tablespace {@link #tablespace}
 
344
     */
 
345
    public void setTablespace(final String tablespace) {
 
346
        this.tablespace = tablespace;
283
347
    }
284
348
 
285
349
    /**
393
457
     * @return list of columns that have statistics defined
394
458
     */
395
459
    private List<PgColumn> getColumnsWithStatistics() {
 
460
        @SuppressWarnings("CollectionWithoutInitialCapacity")
396
461
        final List<PgColumn> list = new ArrayList<PgColumn>();
397
462
 
398
463
        for (PgColumn column : columns) {