~ubuntu-branches/ubuntu/raring/apgdiff/raring

« 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: 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: PgDiffSequences.java 80 2007-09-01 20:25:45Z fordfrog $
 
3
 */
 
4
package cz.startnet.utils.pgdiff;
 
5
 
 
6
import cz.startnet.utils.pgdiff.schema.PgSchema;
 
7
import cz.startnet.utils.pgdiff.schema.PgSequence;
 
8
 
 
9
import java.io.PrintWriter;
 
10
 
 
11
 
 
12
/**
 
13
 * Diffs sequences.
 
14
 *
 
15
 * @author fordfrog
 
16
 * @version $Id: PgDiffSequences.java 80 2007-09-01 20:25:45Z fordfrog $
 
17
 */
 
18
public class PgDiffSequences {
 
19
    /**
 
20
     * Creates a new instance of PgDiffSequences.
 
21
     */
 
22
    private PgDiffSequences() {
 
23
        super();
 
24
    }
 
25
 
 
26
    /**
 
27
     * Outputs commands for differences in sequences.
 
28
     *
 
29
     * @param writer writer the output should be written to
 
30
     * @param arguments object containing arguments settings
 
31
     * @param oldSchema original schema
 
32
     * @param newSchema new schema
 
33
     */
 
34
    public static void diffSequences(
 
35
        final PrintWriter writer,
 
36
        final PgDiffArguments arguments,
 
37
        final PgSchema oldSchema,
 
38
        final PgSchema newSchema) {
 
39
        // Drop sequences that do not exist in new schema
 
40
        if (oldSchema != null) {
 
41
            for (PgSequence sequence : oldSchema.getSequences()) {
 
42
                if (!newSchema.containsSequence(sequence.getName())) {
 
43
                    writer.println();
 
44
                    writer.println(
 
45
                            sequence.getDropSQL(arguments.isQuoteNames()));
 
46
                }
 
47
            }
 
48
        }
 
49
 
 
50
        // Add new sequences
 
51
        for (PgSequence sequence : newSchema.getSequences()) {
 
52
            if (
 
53
                (oldSchema == null)
 
54
                    || !oldSchema.containsSequence(sequence.getName())) {
 
55
                writer.println();
 
56
                writer.println(
 
57
                        sequence.getCreationSQL(arguments.isQuoteNames()));
 
58
            }
 
59
        }
 
60
 
 
61
        // Alter modified sequences
 
62
        addModifiedSequences(writer, arguments, oldSchema, newSchema);
 
63
    }
 
64
 
 
65
    /**
 
66
     * Returns list of modified sequences.
 
67
     *
 
68
     * @param writer writer the output should be written to
 
69
     * @param arguments object containing arguments settings
 
70
     * @param oldSchema original schema
 
71
     * @param newSchema new schema
 
72
     */
 
73
    private static void addModifiedSequences(
 
74
        final PrintWriter writer,
 
75
        final PgDiffArguments arguments,
 
76
        final PgSchema oldSchema,
 
77
        final PgSchema newSchema) {
 
78
        final StringBuilder sbSQL = new StringBuilder();
 
79
 
 
80
        for (final PgSequence newSequence : newSchema.getSequences()) {
 
81
            if (
 
82
                (oldSchema != null)
 
83
                    && oldSchema.containsSequence(newSequence.getName())) {
 
84
                final PgSequence oldSequence =
 
85
                    oldSchema.getSequence(newSequence.getName());
 
86
                sbSQL.setLength(0);
 
87
 
 
88
                final String oldIncrement = oldSequence.getIncrement();
 
89
                final String newIncrement = newSequence.getIncrement();
 
90
 
 
91
                if (
 
92
                    (newIncrement != null)
 
93
                        && !newIncrement.equals(oldIncrement)) {
 
94
                    sbSQL.append("\n\tINCREMENT BY ");
 
95
                    sbSQL.append(newIncrement);
 
96
                }
 
97
 
 
98
                final String oldMinValue = oldSequence.getMinValue();
 
99
                final String newMinValue = newSequence.getMinValue();
 
100
 
 
101
                if ((newMinValue == null) && (oldMinValue != null)) {
 
102
                    sbSQL.append("\n\tNO MINVALUE");
 
103
                } else if (
 
104
                    (newMinValue != null)
 
105
                        && !newMinValue.equals(oldMinValue)) {
 
106
                    sbSQL.append("\n\tMINVALUE ");
 
107
                    sbSQL.append(newMinValue);
 
108
                }
 
109
 
 
110
                final String oldMaxValue = oldSequence.getMaxValue();
 
111
                final String newMaxValue = newSequence.getMaxValue();
 
112
 
 
113
                if ((newMaxValue == null) && (oldMaxValue != null)) {
 
114
                    sbSQL.append("\n\tNO MAXVALUE");
 
115
                } else if (
 
116
                    (newMaxValue != null)
 
117
                        && !newMaxValue.equals(oldMaxValue)) {
 
118
                    sbSQL.append("\n\tMAXVALUE ");
 
119
                    sbSQL.append(newMaxValue);
 
120
                }
 
121
 
 
122
                if (!arguments.isIgnoreStartWith()) {
 
123
                    final String oldStart = oldSequence.getStartWith();
 
124
                    final String newStart = newSequence.getStartWith();
 
125
 
 
126
                    if ((newStart != null) && !newStart.equals(oldStart)) {
 
127
                        sbSQL.append("\n\tRESTART WITH ");
 
128
                        sbSQL.append(newStart);
 
129
                    }
 
130
                }
 
131
 
 
132
                final String oldCache = oldSequence.getCache();
 
133
                final String newCache = newSequence.getCache();
 
134
 
 
135
                if ((newCache != null) && !newCache.equals(oldCache)) {
 
136
                    sbSQL.append("\n\tCACHE ");
 
137
                    sbSQL.append(newCache);
 
138
                }
 
139
 
 
140
                final boolean oldCycle = oldSequence.isCycle();
 
141
                final boolean newCycle = newSequence.isCycle();
 
142
 
 
143
                if (oldCycle && !newCycle) {
 
144
                    sbSQL.append("\n\tNO CYCLE");
 
145
                } else if (!oldCycle && newCycle) {
 
146
                    sbSQL.append("\n\tCYCLE");
 
147
                }
 
148
 
 
149
                if (sbSQL.length() > 0) {
 
150
                    writer.println();
 
151
                    writer.print(
 
152
                            "ALTER SEQUENCE "
 
153
                            + PgDiffUtils.getQuotedName(
 
154
                                    newSequence.getName(),
 
155
                                    arguments.isQuoteNames()));
 
156
                    writer.print(sbSQL.toString());
 
157
                    writer.println(';');
 
158
                }
 
159
            }
 
160
        }
 
161
    }
 
162
}