~ubuntu-branches/ubuntu/vivid/apgdiff/vivid

« back to all changes in this revision

Viewing changes to src/main/java/cz/startnet/utils/pgdiff/PgDiffSequences.java

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Berg
  • Date: 2010-10-02 19:35:17 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20101002193517-9wesve8sksxcktkc
Tags: 2.2-1
* New upstream version.
* Update homepage location.
* Finally enable test suite, yay!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2006 StartNet s.r.o.
 
3
 *
 
4
 * Distributed under MIT license
 
5
 */
1
6
package cz.startnet.utils.pgdiff;
2
7
 
3
8
import cz.startnet.utils.pgdiff.schema.PgSchema;
16
21
     * Creates a new instance of PgDiffSequences.
17
22
     */
18
23
    private PgDiffSequences() {
19
 
        super();
20
24
    }
21
25
 
22
26
    /**
23
 
     * Outputs commands for creation of new sequences.
 
27
     * Outputs statements for creation of new sequences.
24
28
     *
25
29
     * @param writer writer the output should be written to
26
 
     * @param arguments object containing arguments settings
27
30
     * @param oldSchema original schema
28
31
     * @param newSchema new schema
29
32
     */
30
33
    public static void createSequences(final PrintWriter writer,
31
 
            final PgDiffArguments arguments, final PgSchema oldSchema,
32
 
            final PgSchema newSchema) {
 
34
            final PgSchema oldSchema, final PgSchema newSchema) {
33
35
        // Add new sequences
34
36
        for (final PgSequence sequence : newSchema.getSequences()) {
35
37
            if (oldSchema == null
36
38
                    || !oldSchema.containsSequence(sequence.getName())) {
37
39
                writer.println();
38
 
                writer.println(
39
 
                        sequence.getCreationSQL(arguments.isQuoteNames()));
 
40
                writer.println(sequence.getCreationSQL());
40
41
            }
41
42
        }
42
43
    }
43
44
 
44
45
    /**
45
 
     * Outputs commands for dropping of sequences that do not exist anymore.
 
46
     * Outputs statements for dropping of sequences that do not exist anymore.
46
47
     *
47
48
     * @param writer writer the output should be written to
48
 
     * @param arguments object containing arguments settings
49
49
     * @param oldSchema original schema
50
50
     * @param newSchema new schema
51
51
     */
52
52
    public static void dropSequences(final PrintWriter writer,
53
 
            final PgDiffArguments arguments, final PgSchema oldSchema,
54
 
            final PgSchema newSchema) {
 
53
            final PgSchema oldSchema, final PgSchema newSchema) {
55
54
        // Drop sequences that do not exist in new schema
56
55
        if (oldSchema != null) {
57
56
            for (final PgSequence sequence : oldSchema.getSequences()) {
58
57
                if (!newSchema.containsSequence(sequence.getName())) {
59
58
                    writer.println();
60
 
                    writer.println(
61
 
                            sequence.getDropSQL(arguments.isQuoteNames()));
 
59
                    writer.println(sequence.getDropSQL());
62
60
                }
63
61
            }
64
62
        }
65
63
    }
66
64
 
67
65
    /**
68
 
     * Outputs command for modified sequences.
 
66
     * Outputs statement for modified sequences.
69
67
     *
70
68
     * @param writer writer the output should be written to
71
69
     * @param arguments object containing arguments settings
75
73
    public static void alterSequences(final PrintWriter writer,
76
74
            final PgDiffArguments arguments, final PgSchema oldSchema,
77
75
            final PgSchema newSchema) {
78
 
        final StringBuilder sbSQL = new StringBuilder();
 
76
        final StringBuilder sbSQL = new StringBuilder(100);
79
77
 
80
78
        for (final PgSequence newSequence : newSchema.getSequences()) {
81
79
            if (oldSchema != null
96
94
                final String oldMinValue = oldSequence.getMinValue();
97
95
                final String newMinValue = newSequence.getMinValue();
98
96
 
99
 
                if ((newMinValue == null) && (oldMinValue != null)) {
 
97
                if (newMinValue == null && oldMinValue != null) {
100
98
                    sbSQL.append("\n\tNO MINVALUE");
101
99
                } else if (newMinValue != null
102
100
                        && !newMinValue.equals(oldMinValue)) {
107
105
                final String oldMaxValue = oldSequence.getMaxValue();
108
106
                final String newMaxValue = newSequence.getMaxValue();
109
107
 
110
 
                if ((newMaxValue == null) && (oldMaxValue != null)) {
 
108
                if (newMaxValue == null && oldMaxValue != null) {
111
109
                    sbSQL.append("\n\tNO MAXVALUE");
112
110
                } else if (newMaxValue != null
113
111
                        && !newMaxValue.equals(oldMaxValue)) {
119
117
                    final String oldStart = oldSequence.getStartWith();
120
118
                    final String newStart = newSequence.getStartWith();
121
119
 
122
 
                    if ((newStart != null) && !newStart.equals(oldStart)) {
 
120
                    if (newStart != null && !newStart.equals(oldStart)) {
123
121
                        sbSQL.append("\n\tRESTART WITH ");
124
122
                        sbSQL.append(newStart);
125
123
                    }
128
126
                final String oldCache = oldSequence.getCache();
129
127
                final String newCache = newSequence.getCache();
130
128
 
131
 
                if ((newCache != null) && !newCache.equals(oldCache)) {
 
129
                if (newCache != null && !newCache.equals(oldCache)) {
132
130
                    sbSQL.append("\n\tCACHE ");
133
131
                    sbSQL.append(newCache);
134
132
                }
145
143
                if (sbSQL.length() > 0) {
146
144
                    writer.println();
147
145
                    writer.print("ALTER SEQUENCE "
148
 
                            + PgDiffUtils.getQuotedName(newSequence.getName(),
149
 
                            arguments.isQuoteNames()));
 
146
                            + PgDiffUtils.getQuotedName(newSequence.getName()));
150
147
                    writer.print(sbSQL.toString());
151
148
                    writer.println(';');
152
149
                }