~ubuntu-branches/ubuntu/wily/apgdiff/wily

« 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: 2009-12-02 11:11:33 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20091202111133-k5wrh3zb2pzmbv8c
Tags: 1.4-1
* New upstream version. (Without changelog, I am afraid.)
* Test suite still not enabled as it needs network access. (The junit
  version in Debian needs upgrading.)
* Optimize rules files a bit so debhelper doesn't clean twice, and quilt's
  patch target doesn't prevent build-stamp from working.

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
1
package cz.startnet.utils.pgdiff;
5
2
 
6
3
import cz.startnet.utils.pgdiff.schema.PgSchema;
8
5
 
9
6
import java.io.PrintWriter;
10
7
 
11
 
 
12
8
/**
13
9
 * Diffs sequences.
14
10
 *
15
11
 * @author fordfrog
16
 
 * @version $Id: PgDiffSequences.java 80 2007-09-01 20:25:45Z fordfrog $
17
12
 */
18
13
public class PgDiffSequences {
 
14
 
19
15
    /**
20
16
     * Creates a new instance of PgDiffSequences.
21
17
     */
24
20
    }
25
21
 
26
22
    /**
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) {
 
23
     * Outputs commands for creation of new sequences.
 
24
     *
 
25
     * @param writer writer the output should be written to
 
26
     * @param arguments object containing arguments settings
 
27
     * @param oldSchema original schema
 
28
     * @param newSchema new schema
 
29
     */
 
30
    public static void createSequences(final PrintWriter writer,
 
31
            final PgDiffArguments arguments, final PgSchema oldSchema,
 
32
            final PgSchema newSchema) {
 
33
        // Add new sequences
 
34
        for (final PgSequence sequence : newSchema.getSequences()) {
 
35
            if (oldSchema == null
 
36
                    || !oldSchema.containsSequence(sequence.getName())) {
 
37
                writer.println();
 
38
                writer.println(
 
39
                        sequence.getCreationSQL(arguments.isQuoteNames()));
 
40
            }
 
41
        }
 
42
    }
 
43
 
 
44
    /**
 
45
     * Outputs commands for dropping of sequences that do not exist anymore.
 
46
     *
 
47
     * @param writer writer the output should be written to
 
48
     * @param arguments object containing arguments settings
 
49
     * @param oldSchema original schema
 
50
     * @param newSchema new schema
 
51
     */
 
52
    public static void dropSequences(final PrintWriter writer,
 
53
            final PgDiffArguments arguments, final PgSchema oldSchema,
 
54
            final PgSchema newSchema) {
39
55
        // Drop sequences that do not exist in new schema
40
56
        if (oldSchema != null) {
41
 
            for (PgSequence sequence : oldSchema.getSequences()) {
 
57
            for (final PgSequence sequence : oldSchema.getSequences()) {
42
58
                if (!newSchema.containsSequence(sequence.getName())) {
43
59
                    writer.println();
44
60
                    writer.println(
46
62
                }
47
63
            }
48
64
        }
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
65
    }
64
66
 
65
67
    /**
66
 
     * Returns list of modified sequences.
 
68
     * Outputs command for modified sequences.
67
69
     *
68
70
     * @param writer writer the output should be written to
69
71
     * @param arguments object containing arguments settings
70
72
     * @param oldSchema original schema
71
73
     * @param newSchema new schema
72
74
     */
73
 
    private static void addModifiedSequences(
74
 
        final PrintWriter writer,
75
 
        final PgDiffArguments arguments,
76
 
        final PgSchema oldSchema,
77
 
        final PgSchema newSchema) {
 
75
    public static void alterSequences(final PrintWriter writer,
 
76
            final PgDiffArguments arguments, final PgSchema oldSchema,
 
77
            final PgSchema newSchema) {
78
78
        final StringBuilder sbSQL = new StringBuilder();
79
79
 
80
80
        for (final PgSequence newSequence : newSchema.getSequences()) {
81
 
            if (
82
 
                (oldSchema != null)
 
81
            if (oldSchema != null
83
82
                    && oldSchema.containsSequence(newSequence.getName())) {
84
83
                final PgSequence oldSequence =
85
 
                    oldSchema.getSequence(newSequence.getName());
 
84
                        oldSchema.getSequence(newSequence.getName());
86
85
                sbSQL.setLength(0);
87
86
 
88
87
                final String oldIncrement = oldSequence.getIncrement();
89
88
                final String newIncrement = newSequence.getIncrement();
90
89
 
91
 
                if (
92
 
                    (newIncrement != null)
 
90
                if (newIncrement != null
93
91
                        && !newIncrement.equals(oldIncrement)) {
94
92
                    sbSQL.append("\n\tINCREMENT BY ");
95
93
                    sbSQL.append(newIncrement);
100
98
 
101
99
                if ((newMinValue == null) && (oldMinValue != null)) {
102
100
                    sbSQL.append("\n\tNO MINVALUE");
103
 
                } else if (
104
 
                    (newMinValue != null)
 
101
                } else if (newMinValue != null
105
102
                        && !newMinValue.equals(oldMinValue)) {
106
103
                    sbSQL.append("\n\tMINVALUE ");
107
104
                    sbSQL.append(newMinValue);
112
109
 
113
110
                if ((newMaxValue == null) && (oldMaxValue != null)) {
114
111
                    sbSQL.append("\n\tNO MAXVALUE");
115
 
                } else if (
116
 
                    (newMaxValue != null)
 
112
                } else if (newMaxValue != null
117
113
                        && !newMaxValue.equals(oldMaxValue)) {
118
114
                    sbSQL.append("\n\tMAXVALUE ");
119
115
                    sbSQL.append(newMaxValue);
148
144
 
149
145
                if (sbSQL.length() > 0) {
150
146
                    writer.println();
151
 
                    writer.print(
152
 
                            "ALTER SEQUENCE "
153
 
                            + PgDiffUtils.getQuotedName(
154
 
                                    newSequence.getName(),
155
 
                                    arguments.isQuoteNames()));
 
147
                    writer.print("ALTER SEQUENCE "
 
148
                            + PgDiffUtils.getQuotedName(newSequence.getName(),
 
149
                            arguments.isQuoteNames()));
156
150
                    writer.print(sbSQL.toString());
157
151
                    writer.println(';');
158
152
                }