2
* $Id: PgSchema.java 83 2007-09-09 16:17:03Z fordfrog $
4
package cz.startnet.utils.pgdiff.schema;
6
import cz.startnet.utils.pgdiff.PgDiffUtils;
8
import java.util.ArrayList;
13
* Stores schema information.
16
* @version $Id: PgSchema.java 83 2007-09-09 16:17:03Z fordfrog $
18
public class PgSchema {
20
* List of functions defined in the schema.
22
private final List<PgFunction> functions = new ArrayList<PgFunction>();
25
* List of sequences defined in the schema.
27
private final List<PgSequence> sequences = new ArrayList<PgSequence>();
30
* List of tables defined in the schema.
32
private final List<PgTable> tables = new ArrayList<PgTable>();
35
* List of views defined in the schema.
37
private final List<PgView> views = new ArrayList<PgView>();
42
private final String name;
45
* Schema authorization.
47
private String authorization;
50
* Creates a new PgSchema object.
52
* @param name {@link #name}
54
public PgSchema(final String name) {
60
* Setter for {@link #authorization}.
62
* @param authorization {@link #authorization}
64
public void setAuthorization(final String authorization) {
65
this.authorization = authorization;
69
* Getter for {@link #authorization}.
71
* @return {@link #authorization}
73
public String getAuthorization() {
78
* Creates and returns SQL for creation of the schema.
80
* @param quoteNames whether names should be quoted
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));
89
if (getAuthorization() != null) {
90
sbSQL.append(" AUTHORIOZATION ");
92
PgDiffUtils.getQuotedName(getAuthorization(), quoteNames));
97
return sbSQL.toString();
101
* Finds function according to specified function
102
* <code>declaration</code>.
104
* @param declaration declaration of the function to be searched
106
* @return found function or null if no such function has been found
108
public PgFunction getFunction(final String declaration) {
109
PgFunction function = null;
111
for (PgFunction curFunction : functions) {
112
if (curFunction.getDeclaration().equals(declaration)) {
113
function = curFunction;
123
* Getter for {@link #functions}.
125
* @return {@link #functions}
127
public List<PgFunction> getFunctions() {
132
* Getter for {@link #name}.
134
* @return {@link #name}
136
public String getName() {
141
* Finds sequence according to specified sequence
144
* @param name name of the sequence to be searched
146
* @return found sequence or null if no such sequence has been found
148
public PgSequence getSequence(final String name) {
149
PgSequence sequence = null;
151
for (PgSequence curSequence : sequences) {
152
if (curSequence.getName().equals(name)) {
153
sequence = curSequence;
163
* Getter for {@link #sequences}.
165
* @return {@link #sequences}
167
public List<PgSequence> getSequences() {
172
* Finds table according to specified table <code>name</code>.
174
* @param name name of the table to be searched
176
* @return found table or null if no such table has been found
178
public PgTable getTable(final String name) {
179
PgTable table = null;
181
for (PgTable curTable : tables) {
182
if (curTable.getName().equals(name)) {
193
* Getter for {@link #tables}.
195
* @return {@link #tables}
197
public List<PgTable> getTables() {
202
* Finds view according to specified view <code>name</code>.
204
* @param name name of the view to be searched
206
* @return found view or null if no such view has been found
208
public PgView getView(final String name) {
211
for (PgView curView : views) {
212
if (curView.getName().equals(name)) {
223
* Getter for {@link #views}.
225
* @return {@link #views}
227
public List<PgView> getViews() {
232
* Adds <code>function</code> to the list of functions.
234
* @param function function
236
public void addFunction(final PgFunction function) {
237
functions.add(function);
241
* Adds <code>sequence</code> to the list of sequences.
243
* @param sequence sequence
245
public void addSequence(final PgSequence sequence) {
246
sequences.add(sequence);
250
* Adds <code>table</code> to the list of tables.
254
public void addTable(final PgTable table) {
259
* Adds <code>view</code> to the list of views.
263
public void addView(final PgView view) {
268
* Returns true if schema contains function with given
269
* <code>declaration</code>, otherwise false.
271
* @param declaration declaration of the function
273
* @return true if schema contains function with given
274
* <code>declaration</code>, otherwise false
276
public boolean containsFunction(final String declaration) {
277
boolean found = false;
279
for (PgFunction function : functions) {
280
if (function.getDeclaration().equals(declaration)) {
291
* Returns true if schema contains sequence with given
292
* <code>name</code>, otherwise false.
294
* @param name name of the sequence
296
* @return true if schema contains sequence with given <code>name</code>,
299
public boolean containsSequence(final String name) {
300
boolean found = false;
302
for (PgSequence sequence : sequences) {
303
if (sequence.getName().equals(name)) {
314
* Returns true if schema contains table with given
315
* <code>name</code>, otherwise false.
317
* @param name name of the table
319
* @return true if schema contains table with given <code>name</code>,
322
public boolean containsTable(final String name) {
323
boolean found = false;
325
for (PgTable table : tables) {
326
if (table.getName().equals(name)) {
337
* Returns true if schema contains view with given
338
* <code>name</code>, otherwise false.
340
* @param name name of the view
342
* @return true if schema contains view with given <code>name</code>,
345
public boolean containsView(final String name) {
346
boolean found = false;
348
for (PgView view : views) {
349
if (view.getName().equals(name)) {