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

« back to all changes in this revision

Viewing changes to src/main/java/cz/startnet/utils/pgdiff/schema/PgConstraint.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: PgConstraint.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.regex.Pattern;
 
9
 
 
10
 
 
11
/**
 
12
 * Stores table constraint information.
 
13
 *
 
14
 * @author fordfrog
 
15
 * @version $Id: PgConstraint.java 80 2007-09-01 20:25:45Z fordfrog $
 
16
 */
 
17
public class PgConstraint {
 
18
    /**
 
19
     * Pattern for checking whether the constraint is PRIMARY KEY
 
20
     * constraint.
 
21
     */
 
22
    private static final Pattern PATTERN_PRIMARY_KEY =
 
23
        Pattern.compile(".*PRIMARY[\\s]+KEY.*", Pattern.CASE_INSENSITIVE);
 
24
 
 
25
    /**
 
26
     * Definition of the constraint.
 
27
     */
 
28
    private String definition;
 
29
 
 
30
    /**
 
31
     * Name of the constraint.
 
32
     */
 
33
    private String name;
 
34
 
 
35
    /**
 
36
     * Name of the table the constraint is defined on.
 
37
     */
 
38
    private String tableName;
 
39
 
 
40
    /**
 
41
     * Creates a new PgConstraint object.
 
42
     *
 
43
     * @param name {@link #name}
 
44
     */
 
45
    public PgConstraint(String name) {
 
46
        this.name = name;
 
47
    }
 
48
 
 
49
    /**
 
50
     * Creates and returns SQL for creation of the constraint.
 
51
     *
 
52
     * @param quoteNames whether names should be quoted
 
53
     *
 
54
     * @return created SQL
 
55
     */
 
56
    public String getCreationSQL(final boolean quoteNames) {
 
57
        final StringBuilder sbSQL = new StringBuilder();
 
58
        sbSQL.append("ALTER TABLE ");
 
59
        sbSQL.append(PgDiffUtils.getQuotedName(getTableName(), quoteNames));
 
60
        sbSQL.append("\n\tADD CONSTRAINT ");
 
61
        sbSQL.append(PgDiffUtils.getQuotedName(getName(), quoteNames));
 
62
        sbSQL.append(' ');
 
63
        sbSQL.append(getDefinition());
 
64
        sbSQL.append(';');
 
65
 
 
66
        return sbSQL.toString();
 
67
    }
 
68
 
 
69
    /**
 
70
     * Setter for {@link #definition}.
 
71
     *
 
72
     * @param definition {@link #definition}
 
73
     */
 
74
    public void setDefinition(final String definition) {
 
75
        this.definition = definition;
 
76
    }
 
77
 
 
78
    /**
 
79
     * Getter for {@link #definition}.
 
80
     *
 
81
     * @return {@link #definition}
 
82
     */
 
83
    public String getDefinition() {
 
84
        return definition;
 
85
    }
 
86
 
 
87
    /**
 
88
     * Creates and returns SQL for dropping the constraint.
 
89
     *
 
90
     * @param quoteNames whether names should be quoted
 
91
     *
 
92
     * @return created SQL
 
93
     */
 
94
    public String getDropSQL(final boolean quoteNames) {
 
95
        final StringBuilder sbSQL = new StringBuilder();
 
96
        sbSQL.append("ALTER TABLE ");
 
97
        sbSQL.append(PgDiffUtils.getQuotedName(getTableName(), quoteNames));
 
98
        sbSQL.append("\n\tDROP CONSTRAINT ");
 
99
        sbSQL.append(PgDiffUtils.getQuotedName(getName(), quoteNames));
 
100
        sbSQL.append(';');
 
101
 
 
102
        return sbSQL.toString();
 
103
    }
 
104
 
 
105
    /**
 
106
     * Setter for {@link #name}.
 
107
     *
 
108
     * @param name {@link #name}
 
109
     */
 
110
    public void setName(final String name) {
 
111
        this.name = name;
 
112
    }
 
113
 
 
114
    /**
 
115
     * Getter for {@link #name}.
 
116
     *
 
117
     * @return {@link #name}
 
118
     */
 
119
    public String getName() {
 
120
        return name;
 
121
    }
 
122
 
 
123
    /**
 
124
     * Returns true if this is a PRIMARY KEY constraint, otherwise
 
125
     * false.
 
126
     *
 
127
     * @return true if this is a PRIMARY KEY constraint, otherwise false
 
128
     */
 
129
    public boolean isPrimaryKeyConstraint() {
 
130
        return PATTERN_PRIMARY_KEY.matcher(definition).matches();
 
131
    }
 
132
 
 
133
    /**
 
134
     * Setter for {@link #tableName}.
 
135
     *
 
136
     * @param tableName {@link #tableName}
 
137
     */
 
138
    public void setTableName(final String tableName) {
 
139
        this.tableName = tableName;
 
140
    }
 
141
 
 
142
    /**
 
143
     * Getter for {@link #tableName}.
 
144
     *
 
145
     * @return {@link #tableName}
 
146
     */
 
147
    public String getTableName() {
 
148
        return tableName;
 
149
    }
 
150
 
 
151
    /**
 
152
     * {@inheritDoc}
 
153
     *
 
154
     * @param object {@inheritDoc}
 
155
     *
 
156
     * @return {@inheritDoc}
 
157
     */
 
158
    @Override
 
159
    public boolean equals(final Object object) {
 
160
        boolean equals = false;
 
161
 
 
162
        if (this == object) {
 
163
            equals = true;
 
164
        } else if (object instanceof PgConstraint) {
 
165
            final PgConstraint constraint = (PgConstraint) object;
 
166
            equals =
 
167
                definition.equals(constraint.definition)
 
168
                && name.equals(constraint.name)
 
169
                && tableName.equals(constraint.tableName);
 
170
        }
 
171
 
 
172
        return equals;
 
173
    }
 
174
 
 
175
    /**
 
176
     * {@inheritDoc}
 
177
     *
 
178
     * @return {@inheritDoc}
 
179
     */
 
180
    @Override
 
181
    public int hashCode() {
 
182
        return (getClass().getName() + "|" + definition + "|" + name + "|"
 
183
        + tableName).hashCode();
 
184
    }
 
185
}