2
* $Id: PgFunction.java 90 2008-08-01 17:06:19Z fordfrog $
4
package cz.startnet.utils.pgdiff.schema;
6
import java.util.regex.Matcher;
7
import java.util.regex.Pattern;
10
* Stores function information.
13
* @version $Id: PgFunction.java 90 2008-08-01 17:06:19Z fordfrog $
15
public class PgFunction {
18
* Pattern for checking whether function definition contains CREATE
19
* OR REPLACE FUNCTION string.
21
private static final Pattern PATTERN_CREATE_FUNCTION =
23
"(?:CREATE[\\s]+FUNCTION)([\\s]+.*)",
24
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
26
* Declaration of the function. Contains function name and
29
private String declaration;
31
* Whole definition of the function.
33
private String definition;
35
* Name of the function including argument types.
40
* Returns creation SQL of the function.
42
* @return creation SQL
44
public String getCreationSQL() {
46
final Matcher matcher = PATTERN_CREATE_FUNCTION.matcher(definition);
48
if (matcher.matches()) {
49
result = "CREATE OR REPLACE FUNCTION" + matcher.group(1);
51
result = getDefinition();
58
* Setter for {@link #declaration}.
60
* @param declaration {@link #declaration}
62
public void setDeclaration(final String declaration) {
63
this.declaration = declaration;
67
* Getter for {@link #declaration}.
69
* @return {@link #declaration}
71
public String getDeclaration() {
76
* Setter for {@link #definition}.
78
* @param definition {@link #definition}
80
public void setDefinition(final String definition) {
81
this.definition = definition;
85
* Getter for {@link #definition}.
87
* @return {@link #definition}
89
public String getDefinition() {
94
* Creates and returns SQL for dropping the function.
98
public String getDropSQL() {
99
return "DROP FUNCTION " + getDeclaration() + ";";
103
* Setter for {@link #name}.
105
* @param name {@link #name}
107
public void setName(final String name) {
112
* Getter for {@link #name}.
114
* @return {@link #name}
116
public String getName() {
123
* @param object {@inheritDoc}
125
* @return {@inheritDoc}
128
public boolean equals(final Object object) {
129
return equals(object, false);
133
* Compares two objects whether they are equal. If both objects are of the
134
* same class but they equal just in whitespace in {@link #definition},
135
* they are considered being equal.
137
* @param object object to be compared
138
* @param ignoreFunctionWhitespace whether multiple whitespaces in function
139
* {@link #definition} should be ignored
141
* @return {@inheritDoc}
143
public boolean equals(final Object object,
144
final boolean ignoreFunctionWhitespace) {
145
boolean equals = false;
147
if (this == object) {
149
} else if (object instanceof PgFunction) {
150
final PgFunction function = (PgFunction) object;
151
final String thisDefinition;
152
final String thatDefinition;
154
if (ignoreFunctionWhitespace) {
155
thisDefinition = getDefinition().replaceAll("\\s+", " ");
157
function.getDefinition().replaceAll("\\s+", " ");
159
thisDefinition = getDefinition();
160
thatDefinition = function.getDefinition();
163
declaration.equals(function.declaration) && thisDefinition.
164
equals(thatDefinition) && name.equals(function.name);
173
* @return {@inheritDoc}
176
public int hashCode() {
177
return (getClass().getName() + "|" + declaration + "|" + getDefinition() +
178
"|" + name).hashCode();