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

« back to all changes in this revision

Viewing changes to src/main/java/cz/startnet/utils/pgdiff/schema/PgFunction.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: PgFunction.java 90 2008-08-01 17:06:19Z fordfrog $
 
3
 */
 
4
package cz.startnet.utils.pgdiff.schema;
 
5
 
 
6
import java.util.regex.Matcher;
 
7
import java.util.regex.Pattern;
 
8
 
 
9
/**
 
10
 * Stores function information.
 
11
 *
 
12
 * @author fordfrog
 
13
 * @version $Id: PgFunction.java 90 2008-08-01 17:06:19Z fordfrog $
 
14
 */
 
15
public class PgFunction {
 
16
 
 
17
    /**
 
18
     * Pattern for checking whether function definition contains CREATE
 
19
     * OR REPLACE FUNCTION string.
 
20
     */
 
21
    private static final Pattern PATTERN_CREATE_FUNCTION =
 
22
        Pattern.compile(
 
23
        "(?:CREATE[\\s]+FUNCTION)([\\s]+.*)",
 
24
        Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
 
25
    /**
 
26
     * Declaration of the function. Contains function name and
 
27
     * arguments.
 
28
     */
 
29
    private String declaration;
 
30
    /**
 
31
     * Whole definition of the function.
 
32
     */
 
33
    private String definition;
 
34
    /**
 
35
     * Name of the function including argument types.
 
36
     */
 
37
    private String name;
 
38
 
 
39
    /**
 
40
     * Returns creation SQL of the function.
 
41
     *
 
42
     * @return creation SQL
 
43
     */
 
44
    public String getCreationSQL() {
 
45
        final String result;
 
46
        final Matcher matcher = PATTERN_CREATE_FUNCTION.matcher(definition);
 
47
 
 
48
        if (matcher.matches()) {
 
49
            result = "CREATE OR REPLACE FUNCTION" + matcher.group(1);
 
50
        } else {
 
51
            result = getDefinition();
 
52
        }
 
53
 
 
54
        return result;
 
55
    }
 
56
 
 
57
    /**
 
58
     * Setter for {@link #declaration}.
 
59
     *
 
60
     * @param declaration {@link #declaration}
 
61
     */
 
62
    public void setDeclaration(final String declaration) {
 
63
        this.declaration = declaration;
 
64
    }
 
65
 
 
66
    /**
 
67
     * Getter for {@link #declaration}.
 
68
     *
 
69
     * @return {@link #declaration}
 
70
     */
 
71
    public String getDeclaration() {
 
72
        return declaration;
 
73
    }
 
74
 
 
75
    /**
 
76
     * Setter for {@link #definition}.
 
77
     *
 
78
     * @param definition {@link #definition}
 
79
     */
 
80
    public void setDefinition(final String definition) {
 
81
        this.definition = definition;
 
82
    }
 
83
 
 
84
    /**
 
85
     * Getter for {@link #definition}.
 
86
     *
 
87
     * @return {@link #definition}
 
88
     */
 
89
    public String getDefinition() {
 
90
        return definition;
 
91
    }
 
92
 
 
93
    /**
 
94
     * Creates and returns SQL for dropping the function.
 
95
     *
 
96
     * @return created SQL
 
97
     */
 
98
    public String getDropSQL() {
 
99
        return "DROP FUNCTION " + getDeclaration() + ";";
 
100
    }
 
101
 
 
102
    /**
 
103
     * Setter for {@link #name}.
 
104
     *
 
105
     * @param name {@link #name}
 
106
     */
 
107
    public void setName(final String name) {
 
108
        this.name = name;
 
109
    }
 
110
 
 
111
    /**
 
112
     * Getter for {@link #name}.
 
113
     *
 
114
     * @return {@link #name}
 
115
     */
 
116
    public String getName() {
 
117
        return name;
 
118
    }
 
119
 
 
120
    /**
 
121
     * {@inheritDoc}
 
122
     *
 
123
     * @param object {@inheritDoc}
 
124
     *
 
125
     * @return {@inheritDoc}
 
126
     */
 
127
    @Override
 
128
    public boolean equals(final Object object) {
 
129
        return equals(object, false);
 
130
    }
 
131
 
 
132
    /**
 
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.
 
136
     *
 
137
     * @param object object to be compared
 
138
     * @param ignoreFunctionWhitespace whether multiple whitespaces in function
 
139
     * {@link #definition} should be ignored
 
140
     *
 
141
     * @return {@inheritDoc}
 
142
     */
 
143
    public boolean equals(final Object object,
 
144
        final boolean ignoreFunctionWhitespace) {
 
145
        boolean equals = false;
 
146
 
 
147
        if (this == object) {
 
148
            equals = true;
 
149
        } else if (object instanceof PgFunction) {
 
150
            final PgFunction function = (PgFunction) object;
 
151
            final String thisDefinition;
 
152
            final String thatDefinition;
 
153
 
 
154
            if (ignoreFunctionWhitespace) {
 
155
                thisDefinition = getDefinition().replaceAll("\\s+", " ");
 
156
                thatDefinition =
 
157
                    function.getDefinition().replaceAll("\\s+", " ");
 
158
            } else {
 
159
                thisDefinition = getDefinition();
 
160
                thatDefinition = function.getDefinition();
 
161
            }
 
162
            equals =
 
163
                declaration.equals(function.declaration) && thisDefinition.
 
164
                equals(thatDefinition) && name.equals(function.name);
 
165
        }
 
166
 
 
167
        return equals;
 
168
    }
 
169
 
 
170
    /**
 
171
     * {@inheritDoc}
 
172
     *
 
173
     * @return {@inheritDoc}
 
174
     */
 
175
    @Override
 
176
    public int hashCode() {
 
177
        return (getClass().getName() + "|" + declaration + "|" + getDefinition() +
 
178
            "|" + name).hashCode();
 
179
    }
 
180
}