~ubuntu-branches/ubuntu/karmic/apgdiff/karmic

« 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: 2008-09-09 15:42:54 UTC
  • Revision ID: james.westby@ubuntu.com-20080909154254-458sv7ew1rczdal1
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: PgTable.java 80 2007-09-01 20:25:45Z fordfrog $
 
3
 */
 
4
package cz.startnet.utils.pgdiff.schema;
 
5
 
 
6
import cz.startnet.utils.pgdiff.PgDiffUtils;
 
7
 
 
8
import java.util.ArrayList;
 
9
import java.util.List;
 
10
 
 
11
 
 
12
/**
 
13
 * Stores table information.
 
14
 *
 
15
 * @author fordfrog
 
16
 * @version $Id: PgTable.java 80 2007-09-01 20:25:45Z fordfrog $
 
17
 */
 
18
public class PgTable {
 
19
    /**
 
20
     * List of columns defined on the table.
 
21
     */
 
22
    private final List<PgColumn> columns = new ArrayList<PgColumn>();
 
23
 
 
24
    /**
 
25
     * List of constraints defined on the table.
 
26
     */
 
27
    private final List<PgConstraint> constraints =
 
28
        new ArrayList<PgConstraint>();
 
29
 
 
30
    /**
 
31
     * List of indexes defined on the table.
 
32
     */
 
33
    private final List<PgIndex> indexes = new ArrayList<PgIndex>();
 
34
 
 
35
    /**
 
36
     * List of triggers defined on the table.
 
37
     */
 
38
    private final List<PgTrigger> triggers = new ArrayList<PgTrigger>();
 
39
 
 
40
    /**
 
41
     * Name of the index on which the table is clustered
 
42
     */
 
43
    private String clusterIndexName;
 
44
 
 
45
    /**
 
46
     * Definition of names of inherited tables.
 
47
     */
 
48
    private String inherits;
 
49
 
 
50
    /**
 
51
     * Name of the table.
 
52
     */
 
53
    private String name;
 
54
 
 
55
    /**
 
56
     * Whether WITH OIDS is used.
 
57
     */
 
58
    private boolean withOIDS;
 
59
 
 
60
    /**
 
61
     * Creates a new PgTable object.
 
62
     *
 
63
     * @param name {@link #name}
 
64
     */
 
65
    public PgTable(final String name) {
 
66
        this.name = name;
 
67
    }
 
68
 
 
69
    /**
 
70
     * Setter for {@link #clusterIndexName}.
 
71
     *
 
72
     * @param name {@link #clusterIndexName}
 
73
     */
 
74
    public void setClusterIndexName(final String name) {
 
75
        clusterIndexName = name;
 
76
    }
 
77
 
 
78
    /**
 
79
     * Getter for {@link #clusterIndexName}.
 
80
     *
 
81
     * @return {@link #clusterIndexName}
 
82
     */
 
83
    public String getClusterIndexName() {
 
84
        return clusterIndexName;
 
85
    }
 
86
 
 
87
    /**
 
88
     * Finds column according to specified column <code>name</code>.
 
89
     *
 
90
     * @param name name of the column to be searched
 
91
     *
 
92
     * @return found column or null if no such column has been found
 
93
     */
 
94
    public PgColumn getColumn(final String name) {
 
95
        PgColumn column = null;
 
96
 
 
97
        for (PgColumn curColumn : columns) {
 
98
            if (curColumn.getName().equals(name)) {
 
99
                column = curColumn;
 
100
 
 
101
                break;
 
102
            }
 
103
        }
 
104
 
 
105
        return column;
 
106
    }
 
107
 
 
108
    /**
 
109
     * Getter for {@link #columns}.
 
110
     *
 
111
     * @return {@link #columns}
 
112
     */
 
113
    public List<PgColumn> getColumns() {
 
114
        return columns;
 
115
    }
 
116
 
 
117
    /**
 
118
     * Finds constraint according to specified constraint
 
119
     * <code>name</code>.
 
120
     *
 
121
     * @param name name of the constraint to be searched
 
122
     *
 
123
     * @return found constraint or null if no such constraint has been found
 
124
     */
 
125
    public PgConstraint getConstraint(final String name) {
 
126
        PgConstraint constraint = null;
 
127
 
 
128
        for (PgConstraint curConstraint : constraints) {
 
129
            if (curConstraint.getName().equals(name)) {
 
130
                constraint = curConstraint;
 
131
 
 
132
                break;
 
133
            }
 
134
        }
 
135
 
 
136
        return constraint;
 
137
    }
 
138
 
 
139
    /**
 
140
     * Getter for {@link #constraints}.
 
141
     *
 
142
     * @return {@link #constraints}
 
143
     */
 
144
    public List<PgConstraint> getConstraints() {
 
145
        return constraints;
 
146
    }
 
147
 
 
148
    /**
 
149
     * Creates and returns SQL for creation of the table.
 
150
     *
 
151
     * @param quoteNames whether names should be quoted
 
152
     *
 
153
     * @return created SQL command
 
154
     */
 
155
    public String getCreationSQL(final boolean quoteNames) {
 
156
        final StringBuilder sbSQL = new StringBuilder();
 
157
        sbSQL.append("CREATE TABLE ");
 
158
        sbSQL.append(PgDiffUtils.getQuotedName(name, quoteNames));
 
159
        sbSQL.append(" (\n");
 
160
 
 
161
        for (PgColumn column : columns) {
 
162
            sbSQL.append("\t");
 
163
            sbSQL.append(column.getFullDefinition(quoteNames, false));
 
164
            sbSQL.append(",\n");
 
165
        }
 
166
 
 
167
        sbSQL.setLength(sbSQL.length() - 2);
 
168
        sbSQL.append("\n)");
 
169
 
 
170
        if ((inherits != null) && (inherits.length() > 0)) {
 
171
            sbSQL.append("\nINHERITS ");
 
172
            sbSQL.append(inherits);
 
173
        }
 
174
 
 
175
        sbSQL.append(';');
 
176
 
 
177
        for (PgColumn column : getColumnsWithStatistics()) {
 
178
            sbSQL.append("\nALTER TABLE ONLY ");
 
179
            sbSQL.append(PgDiffUtils.getQuotedName(name, quoteNames));
 
180
            sbSQL.append(" ALTER COLUMN ");
 
181
            sbSQL.append(
 
182
                    PgDiffUtils.getQuotedName(column.getName(), quoteNames));
 
183
            sbSQL.append(" SET STATISTICS ");
 
184
            sbSQL.append(column.getStatistics());
 
185
            sbSQL.append(';');
 
186
        }
 
187
 
 
188
        return sbSQL.toString();
 
189
    }
 
190
 
 
191
    /**
 
192
     * Creates and returns SQL command for dropping the table.
 
193
     *
 
194
     * @param quoteNames whether names should be quoted
 
195
     *
 
196
     * @return created SQL command
 
197
     */
 
198
    public String getDropSQL(final boolean quoteNames) {
 
199
        return "DROP TABLE " + PgDiffUtils.getQuotedName(getName(), quoteNames)
 
200
        + ";";
 
201
    }
 
202
 
 
203
    /**
 
204
     * Finds index according to specified index <code>name</code>.
 
205
     *
 
206
     * @param name name of the index to be searched
 
207
     *
 
208
     * @return found index or null if no such index has been found
 
209
     */
 
210
    public PgIndex getIndex(final String name) {
 
211
        PgIndex index = null;
 
212
 
 
213
        for (PgIndex curIndex : indexes) {
 
214
            if (curIndex.getName().equals(name)) {
 
215
                index = curIndex;
 
216
 
 
217
                break;
 
218
            }
 
219
        }
 
220
 
 
221
        return index;
 
222
    }
 
223
 
 
224
    /**
 
225
     * Getter for {@link #indexes}.
 
226
     *
 
227
     * @return {@link #indexes}
 
228
     */
 
229
    public List<PgIndex> getIndexes() {
 
230
        return indexes;
 
231
    }
 
232
 
 
233
    /**
 
234
     * Setter for {@link #inherits}.
 
235
     *
 
236
     * @param inherits {@link #inherits}
 
237
     */
 
238
    public void setInherits(final String inherits) {
 
239
        this.inherits = inherits;
 
240
    }
 
241
 
 
242
    /**
 
243
     * Getter for {@link #inherits}.
 
244
     *
 
245
     * @return {@link #inherits}
 
246
     */
 
247
    public String getInherits() {
 
248
        return inherits;
 
249
    }
 
250
 
 
251
    /**
 
252
     * Setter for {@link #name}.
 
253
     *
 
254
     * @param name {@link #name}
 
255
     */
 
256
    public void setName(final String name) {
 
257
        this.name = name;
 
258
    }
 
259
 
 
260
    /**
 
261
     * Getter for {@link #name}.
 
262
     *
 
263
     * @return {@link #name}
 
264
     */
 
265
    public String getName() {
 
266
        return name;
 
267
    }
 
268
 
 
269
    /**
 
270
     * Getter for {@link #triggers}.
 
271
     *
 
272
     * @return {@link #triggers}
 
273
     */
 
274
    public List<PgTrigger> getTriggers() {
 
275
        return triggers;
 
276
    }
 
277
 
 
278
    /**
 
279
     * Setter for {@link #withOIDS}.
 
280
     *
 
281
     * @param withOIDS {@link #withOIDS}
 
282
     */
 
283
    public void setWithOIDS(final boolean withOIDS) {
 
284
        this.withOIDS = withOIDS;
 
285
    }
 
286
 
 
287
    /**
 
288
     * Getter for {@link #withOIDS}
 
289
     *
 
290
     * @return {@link #withOIDS}
 
291
     */
 
292
    public boolean isWithOIDS() {
 
293
        return withOIDS;
 
294
    }
 
295
 
 
296
    /**
 
297
     * Adds <code>column</code> to the list of columns.
 
298
     *
 
299
     * @param column column
 
300
     */
 
301
    public void addColumn(final PgColumn column) {
 
302
        columns.add(column);
 
303
    }
 
304
 
 
305
    /**
 
306
     * Adds <code>constraint</code> to the list of constraints.
 
307
     *
 
308
     * @param constraint constraint
 
309
     */
 
310
    public void addConstraint(final PgConstraint constraint) {
 
311
        constraints.add(constraint);
 
312
    }
 
313
 
 
314
    /**
 
315
     * Adds <code>index</code> to the list of indexes.
 
316
     *
 
317
     * @param index index
 
318
     */
 
319
    public void addIndex(final PgIndex index) {
 
320
        indexes.add(index);
 
321
    }
 
322
 
 
323
    /**
 
324
     * Adds <code>trigger</code> to the list of triggers.
 
325
     *
 
326
     * @param trigger trigger
 
327
     */
 
328
    public void addTrigger(final PgTrigger trigger) {
 
329
        triggers.add(trigger);
 
330
    }
 
331
 
 
332
    /**
 
333
     * Returns true if table contains given column <code>name</code>,
 
334
     * otherwise false.
 
335
     *
 
336
     * @param name name of the column
 
337
     *
 
338
     * @return true if table contains given column <code>name</code>, otherwise
 
339
     *         false
 
340
     */
 
341
    public boolean containsColumn(final String name) {
 
342
        boolean found = false;
 
343
 
 
344
        for (PgColumn column : columns) {
 
345
            if (column.getName().equals(name)) {
 
346
                found = true;
 
347
 
 
348
                break;
 
349
            }
 
350
        }
 
351
 
 
352
        return found;
 
353
    }
 
354
 
 
355
    /**
 
356
     * Returns true if table contains given constraint
 
357
     * <code>name</code>, otherwise false.
 
358
     *
 
359
     * @param name name of the constraint
 
360
     *
 
361
     * @return true if table contains given constraint <code>name</code>,
 
362
     *         otherwise false
 
363
     */
 
364
    public boolean containsConstraint(final String name) {
 
365
        boolean found = false;
 
366
 
 
367
        for (PgConstraint constraint : constraints) {
 
368
            if (constraint.getName().equals(name)) {
 
369
                found = true;
 
370
 
 
371
                break;
 
372
            }
 
373
        }
 
374
 
 
375
        return found;
 
376
    }
 
377
 
 
378
    /**
 
379
     * Returns true if table contains given index <code>name</code>,
 
380
     * otherwise false.
 
381
     *
 
382
     * @param name name of the index
 
383
     *
 
384
     * @return true if table contains given index <code>name</code>, otherwise
 
385
     *         false
 
386
     */
 
387
    public boolean containsIndex(final String name) {
 
388
        boolean found = false;
 
389
 
 
390
        for (PgIndex index : indexes) {
 
391
            if (index.getName().equals(name)) {
 
392
                found = true;
 
393
 
 
394
                break;
 
395
            }
 
396
        }
 
397
 
 
398
        return found;
 
399
    }
 
400
 
 
401
    /**
 
402
     * Returns list of columns that have statistics defined.
 
403
     *
 
404
     * @return list of columns that have statistics defined
 
405
     */
 
406
    private List<PgColumn> getColumnsWithStatistics() {
 
407
        final List<PgColumn> list = new ArrayList<PgColumn>();
 
408
 
 
409
        for (PgColumn column : columns) {
 
410
            if (column.getStatistics() != null) {
 
411
                list.add(column);
 
412
            }
 
413
        }
 
414
 
 
415
        return list;
 
416
    }
 
417
}