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

« back to all changes in this revision

Viewing changes to src/main/java/cz/startnet/utils/pgdiff/schema/PgSchema.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: PgSchema.java 83 2007-09-09 16:17:03Z 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 schema information.
 
14
 *
 
15
 * @author fordfrog
 
16
 * @version $Id: PgSchema.java 83 2007-09-09 16:17:03Z fordfrog $
 
17
 */
 
18
public class PgSchema {
 
19
    /**
 
20
     * List of functions defined in the schema.
 
21
     */
 
22
    private final List<PgFunction> functions = new ArrayList<PgFunction>();
 
23
 
 
24
    /**
 
25
     * List of sequences defined in the schema.
 
26
     */
 
27
    private final List<PgSequence> sequences = new ArrayList<PgSequence>();
 
28
 
 
29
    /**
 
30
     * List of tables defined in the schema.
 
31
     */
 
32
    private final List<PgTable> tables = new ArrayList<PgTable>();
 
33
 
 
34
    /**
 
35
     * List of views defined in the schema.
 
36
     */
 
37
    private final List<PgView> views = new ArrayList<PgView>();
 
38
 
 
39
    /**
 
40
     * Name of the schema.
 
41
     */
 
42
    private final String name;
 
43
 
 
44
    /**
 
45
     * Schema authorization.
 
46
     */
 
47
    private String authorization;
 
48
 
 
49
    /**
 
50
     * Creates a new PgSchema object.
 
51
     *
 
52
     * @param name {@link #name}
 
53
     */
 
54
    public PgSchema(final String name) {
 
55
        super();
 
56
        this.name = name;
 
57
    }
 
58
 
 
59
    /**
 
60
     * Setter for {@link #authorization}.
 
61
     *
 
62
     * @param authorization {@link #authorization}
 
63
     */
 
64
    public void setAuthorization(final String authorization) {
 
65
        this.authorization = authorization;
 
66
    }
 
67
 
 
68
    /**
 
69
     * Getter for {@link #authorization}.
 
70
     *
 
71
     * @return {@link #authorization}
 
72
     */
 
73
    public String getAuthorization() {
 
74
        return authorization;
 
75
    }
 
76
 
 
77
    /**
 
78
     * Creates and returns SQL for creation of the schema.
 
79
     *
 
80
     * @param quoteNames whether names should be quoted
 
81
     *
 
82
     * @return created SQL
 
83
     */
 
84
    public String getCreationSQL(final boolean quoteNames) {
 
85
        final StringBuilder sbSQL = new StringBuilder();
 
86
        sbSQL.append("CREATE SCHEMA ");
 
87
        sbSQL.append(PgDiffUtils.getQuotedName(getName(), quoteNames));
 
88
 
 
89
        if (getAuthorization() != null) {
 
90
            sbSQL.append(" AUTHORIOZATION ");
 
91
            sbSQL.append(
 
92
                    PgDiffUtils.getQuotedName(getAuthorization(), quoteNames));
 
93
        }
 
94
 
 
95
        sbSQL.append(';');
 
96
 
 
97
        return sbSQL.toString();
 
98
    }
 
99
 
 
100
    /**
 
101
     * Finds function according to specified function
 
102
     * <code>declaration</code>.
 
103
     *
 
104
     * @param declaration declaration of the function to be searched
 
105
     *
 
106
     * @return found function or null if no such function has been found
 
107
     */
 
108
    public PgFunction getFunction(final String declaration) {
 
109
        PgFunction function = null;
 
110
 
 
111
        for (PgFunction curFunction : functions) {
 
112
            if (curFunction.getDeclaration().equals(declaration)) {
 
113
                function = curFunction;
 
114
 
 
115
                break;
 
116
            }
 
117
        }
 
118
 
 
119
        return function;
 
120
    }
 
121
 
 
122
    /**
 
123
     * Getter for {@link #functions}.
 
124
     *
 
125
     * @return {@link #functions}
 
126
     */
 
127
    public List<PgFunction> getFunctions() {
 
128
        return functions;
 
129
    }
 
130
 
 
131
    /**
 
132
     * Getter for {@link #name}.
 
133
     *
 
134
     * @return {@link #name}
 
135
     */
 
136
    public String getName() {
 
137
        return name;
 
138
    }
 
139
 
 
140
    /**
 
141
     * Finds sequence according to specified sequence
 
142
     * <code>name</code>.
 
143
     *
 
144
     * @param name name of the sequence to be searched
 
145
     *
 
146
     * @return found sequence or null if no such sequence has been found
 
147
     */
 
148
    public PgSequence getSequence(final String name) {
 
149
        PgSequence sequence = null;
 
150
 
 
151
        for (PgSequence curSequence : sequences) {
 
152
            if (curSequence.getName().equals(name)) {
 
153
                sequence = curSequence;
 
154
 
 
155
                break;
 
156
            }
 
157
        }
 
158
 
 
159
        return sequence;
 
160
    }
 
161
 
 
162
    /**
 
163
     * Getter for {@link #sequences}.
 
164
     *
 
165
     * @return {@link #sequences}
 
166
     */
 
167
    public List<PgSequence> getSequences() {
 
168
        return sequences;
 
169
    }
 
170
 
 
171
    /**
 
172
     * Finds table according to specified table <code>name</code>.
 
173
     *
 
174
     * @param name name of the table to be searched
 
175
     *
 
176
     * @return found table or null if no such table has been found
 
177
     */
 
178
    public PgTable getTable(final String name) {
 
179
        PgTable table = null;
 
180
 
 
181
        for (PgTable curTable : tables) {
 
182
            if (curTable.getName().equals(name)) {
 
183
                table = curTable;
 
184
 
 
185
                break;
 
186
            }
 
187
        }
 
188
 
 
189
        return table;
 
190
    }
 
191
 
 
192
    /**
 
193
     * Getter for {@link #tables}.
 
194
     *
 
195
     * @return {@link #tables}
 
196
     */
 
197
    public List<PgTable> getTables() {
 
198
        return tables;
 
199
    }
 
200
 
 
201
    /**
 
202
     * Finds view according to specified view <code>name</code>.
 
203
     *
 
204
     * @param name name of the view to be searched
 
205
     *
 
206
     * @return found view or null if no such view has been found
 
207
     */
 
208
    public PgView getView(final String name) {
 
209
        PgView view = null;
 
210
 
 
211
        for (PgView curView : views) {
 
212
            if (curView.getName().equals(name)) {
 
213
                view = curView;
 
214
 
 
215
                break;
 
216
            }
 
217
        }
 
218
 
 
219
        return view;
 
220
    }
 
221
 
 
222
    /**
 
223
     * Getter for {@link #views}.
 
224
     *
 
225
     * @return {@link #views}
 
226
     */
 
227
    public List<PgView> getViews() {
 
228
        return views;
 
229
    }
 
230
 
 
231
    /**
 
232
     * Adds <code>function</code> to the list of functions.
 
233
     *
 
234
     * @param function function
 
235
     */
 
236
    public void addFunction(final PgFunction function) {
 
237
        functions.add(function);
 
238
    }
 
239
 
 
240
    /**
 
241
     * Adds <code>sequence</code> to the list of sequences.
 
242
     *
 
243
     * @param sequence sequence
 
244
     */
 
245
    public void addSequence(final PgSequence sequence) {
 
246
        sequences.add(sequence);
 
247
    }
 
248
 
 
249
    /**
 
250
     * Adds <code>table</code> to the list of tables.
 
251
     *
 
252
     * @param table table
 
253
     */
 
254
    public void addTable(final PgTable table) {
 
255
        tables.add(table);
 
256
    }
 
257
 
 
258
    /**
 
259
     * Adds <code>view</code> to the list of views.
 
260
     *
 
261
     * @param view view
 
262
     */
 
263
    public void addView(final PgView view) {
 
264
        views.add(view);
 
265
    }
 
266
 
 
267
    /**
 
268
     * Returns true if schema contains function with given
 
269
     * <code>declaration</code>, otherwise false.
 
270
     *
 
271
     * @param declaration declaration of the function
 
272
     *
 
273
     * @return true if schema contains function with given
 
274
     *         <code>declaration</code>, otherwise false
 
275
     */
 
276
    public boolean containsFunction(final String declaration) {
 
277
        boolean found = false;
 
278
 
 
279
        for (PgFunction function : functions) {
 
280
            if (function.getDeclaration().equals(declaration)) {
 
281
                found = true;
 
282
 
 
283
                break;
 
284
            }
 
285
        }
 
286
 
 
287
        return found;
 
288
    }
 
289
 
 
290
    /**
 
291
     * Returns true if schema contains sequence with given
 
292
     * <code>name</code>, otherwise false.
 
293
     *
 
294
     * @param name name of the sequence
 
295
     *
 
296
     * @return true if schema contains sequence with given <code>name</code>,
 
297
     *         otherwise false
 
298
     */
 
299
    public boolean containsSequence(final String name) {
 
300
        boolean found = false;
 
301
 
 
302
        for (PgSequence sequence : sequences) {
 
303
            if (sequence.getName().equals(name)) {
 
304
                found = true;
 
305
 
 
306
                break;
 
307
            }
 
308
        }
 
309
 
 
310
        return found;
 
311
    }
 
312
 
 
313
    /**
 
314
     * Returns true if schema contains table with given
 
315
     * <code>name</code>, otherwise false.
 
316
     *
 
317
     * @param name name of the table
 
318
     *
 
319
     * @return true if schema contains table with given <code>name</code>,
 
320
     *         otherwise false.
 
321
     */
 
322
    public boolean containsTable(final String name) {
 
323
        boolean found = false;
 
324
 
 
325
        for (PgTable table : tables) {
 
326
            if (table.getName().equals(name)) {
 
327
                found = true;
 
328
 
 
329
                break;
 
330
            }
 
331
        }
 
332
 
 
333
        return found;
 
334
    }
 
335
 
 
336
    /**
 
337
     * Returns true if schema contains view with given
 
338
     * <code>name</code>, otherwise false.
 
339
     *
 
340
     * @param name name of the view
 
341
     *
 
342
     * @return true if schema contains view with given <code>name</code>,
 
343
     *         otherwise false.
 
344
     */
 
345
    public boolean containsView(final String name) {
 
346
        boolean found = false;
 
347
 
 
348
        for (PgView view : views) {
 
349
            if (view.getName().equals(name)) {
 
350
                found = true;
 
351
 
 
352
                break;
 
353
            }
 
354
        }
 
355
 
 
356
        return found;
 
357
    }
 
358
}