2
* $Id: PgTable.java 80 2007-09-01 20:25:45Z fordfrog $
4
package cz.startnet.utils.pgdiff.schema;
6
import cz.startnet.utils.pgdiff.PgDiffUtils;
8
import java.util.ArrayList;
13
* Stores table information.
16
* @version $Id: PgTable.java 80 2007-09-01 20:25:45Z fordfrog $
18
public class PgTable {
20
* List of columns defined on the table.
22
private final List<PgColumn> columns = new ArrayList<PgColumn>();
25
* List of constraints defined on the table.
27
private final List<PgConstraint> constraints =
28
new ArrayList<PgConstraint>();
31
* List of indexes defined on the table.
33
private final List<PgIndex> indexes = new ArrayList<PgIndex>();
36
* List of triggers defined on the table.
38
private final List<PgTrigger> triggers = new ArrayList<PgTrigger>();
41
* Name of the index on which the table is clustered
43
private String clusterIndexName;
46
* Definition of names of inherited tables.
48
private String inherits;
56
* Whether WITH OIDS is used.
58
private boolean withOIDS;
61
* Creates a new PgTable object.
63
* @param name {@link #name}
65
public PgTable(final String name) {
70
* Setter for {@link #clusterIndexName}.
72
* @param name {@link #clusterIndexName}
74
public void setClusterIndexName(final String name) {
75
clusterIndexName = name;
79
* Getter for {@link #clusterIndexName}.
81
* @return {@link #clusterIndexName}
83
public String getClusterIndexName() {
84
return clusterIndexName;
88
* Finds column according to specified column <code>name</code>.
90
* @param name name of the column to be searched
92
* @return found column or null if no such column has been found
94
public PgColumn getColumn(final String name) {
95
PgColumn column = null;
97
for (PgColumn curColumn : columns) {
98
if (curColumn.getName().equals(name)) {
109
* Getter for {@link #columns}.
111
* @return {@link #columns}
113
public List<PgColumn> getColumns() {
118
* Finds constraint according to specified constraint
121
* @param name name of the constraint to be searched
123
* @return found constraint or null if no such constraint has been found
125
public PgConstraint getConstraint(final String name) {
126
PgConstraint constraint = null;
128
for (PgConstraint curConstraint : constraints) {
129
if (curConstraint.getName().equals(name)) {
130
constraint = curConstraint;
140
* Getter for {@link #constraints}.
142
* @return {@link #constraints}
144
public List<PgConstraint> getConstraints() {
149
* Creates and returns SQL for creation of the table.
151
* @param quoteNames whether names should be quoted
153
* @return created SQL command
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");
161
for (PgColumn column : columns) {
163
sbSQL.append(column.getFullDefinition(quoteNames, false));
167
sbSQL.setLength(sbSQL.length() - 2);
170
if ((inherits != null) && (inherits.length() > 0)) {
171
sbSQL.append("\nINHERITS ");
172
sbSQL.append(inherits);
177
for (PgColumn column : getColumnsWithStatistics()) {
178
sbSQL.append("\nALTER TABLE ONLY ");
179
sbSQL.append(PgDiffUtils.getQuotedName(name, quoteNames));
180
sbSQL.append(" ALTER COLUMN ");
182
PgDiffUtils.getQuotedName(column.getName(), quoteNames));
183
sbSQL.append(" SET STATISTICS ");
184
sbSQL.append(column.getStatistics());
188
return sbSQL.toString();
192
* Creates and returns SQL command for dropping the table.
194
* @param quoteNames whether names should be quoted
196
* @return created SQL command
198
public String getDropSQL(final boolean quoteNames) {
199
return "DROP TABLE " + PgDiffUtils.getQuotedName(getName(), quoteNames)
204
* Finds index according to specified index <code>name</code>.
206
* @param name name of the index to be searched
208
* @return found index or null if no such index has been found
210
public PgIndex getIndex(final String name) {
211
PgIndex index = null;
213
for (PgIndex curIndex : indexes) {
214
if (curIndex.getName().equals(name)) {
225
* Getter for {@link #indexes}.
227
* @return {@link #indexes}
229
public List<PgIndex> getIndexes() {
234
* Setter for {@link #inherits}.
236
* @param inherits {@link #inherits}
238
public void setInherits(final String inherits) {
239
this.inherits = inherits;
243
* Getter for {@link #inherits}.
245
* @return {@link #inherits}
247
public String getInherits() {
252
* Setter for {@link #name}.
254
* @param name {@link #name}
256
public void setName(final String name) {
261
* Getter for {@link #name}.
263
* @return {@link #name}
265
public String getName() {
270
* Getter for {@link #triggers}.
272
* @return {@link #triggers}
274
public List<PgTrigger> getTriggers() {
279
* Setter for {@link #withOIDS}.
281
* @param withOIDS {@link #withOIDS}
283
public void setWithOIDS(final boolean withOIDS) {
284
this.withOIDS = withOIDS;
288
* Getter for {@link #withOIDS}
290
* @return {@link #withOIDS}
292
public boolean isWithOIDS() {
297
* Adds <code>column</code> to the list of columns.
299
* @param column column
301
public void addColumn(final PgColumn column) {
306
* Adds <code>constraint</code> to the list of constraints.
308
* @param constraint constraint
310
public void addConstraint(final PgConstraint constraint) {
311
constraints.add(constraint);
315
* Adds <code>index</code> to the list of indexes.
319
public void addIndex(final PgIndex index) {
324
* Adds <code>trigger</code> to the list of triggers.
326
* @param trigger trigger
328
public void addTrigger(final PgTrigger trigger) {
329
triggers.add(trigger);
333
* Returns true if table contains given column <code>name</code>,
336
* @param name name of the column
338
* @return true if table contains given column <code>name</code>, otherwise
341
public boolean containsColumn(final String name) {
342
boolean found = false;
344
for (PgColumn column : columns) {
345
if (column.getName().equals(name)) {
356
* Returns true if table contains given constraint
357
* <code>name</code>, otherwise false.
359
* @param name name of the constraint
361
* @return true if table contains given constraint <code>name</code>,
364
public boolean containsConstraint(final String name) {
365
boolean found = false;
367
for (PgConstraint constraint : constraints) {
368
if (constraint.getName().equals(name)) {
379
* Returns true if table contains given index <code>name</code>,
382
* @param name name of the index
384
* @return true if table contains given index <code>name</code>, otherwise
387
public boolean containsIndex(final String name) {
388
boolean found = false;
390
for (PgIndex index : indexes) {
391
if (index.getName().equals(name)) {
402
* Returns list of columns that have statistics defined.
404
* @return list of columns that have statistics defined
406
private List<PgColumn> getColumnsWithStatistics() {
407
final List<PgColumn> list = new ArrayList<PgColumn>();
409
for (PgColumn column : columns) {
410
if (column.getStatistics() != null) {