~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: 2010-10-11 09:08:18 UTC
  • mfrom: (2.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20101011090818-sdw8yfemrnxo328k
Tags: 2.2.2-1
* New upstream version.
* Using changelog included in zipfile, thanks Miroslav for providing this.
* Update manpage.

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
 
32
     * @param searchPathHelper search path helper
29
33
     */
30
34
    public static void createSequences(final PrintWriter writer,
31
 
            final PgDiffArguments arguments, final PgSchema oldSchema,
32
 
            final PgSchema newSchema) {
 
35
            final PgSchema oldSchema, final PgSchema newSchema,
 
36
            final SearchPathHelper searchPathHelper) {
33
37
        // Add new sequences
34
38
        for (final PgSequence sequence : newSchema.getSequences()) {
35
39
            if (oldSchema == null
36
40
                    || !oldSchema.containsSequence(sequence.getName())) {
 
41
                searchPathHelper.outputSearchPath(writer);
37
42
                writer.println();
38
 
                writer.println(
39
 
                        sequence.getCreationSQL(arguments.isQuoteNames()));
 
43
                writer.println(sequence.getCreationSQL());
40
44
            }
41
45
        }
42
46
    }
43
47
 
44
48
    /**
45
 
     * Outputs commands for dropping of sequences that do not exist anymore.
 
49
     * Outputs statements for dropping of sequences that do not exist anymore.
46
50
     *
47
51
     * @param writer writer the output should be written to
48
 
     * @param arguments object containing arguments settings
49
52
     * @param oldSchema original schema
50
53
     * @param newSchema new schema
 
54
     * @param searchPathHelper search path helper
51
55
     */
52
56
    public static void dropSequences(final PrintWriter writer,
53
 
            final PgDiffArguments arguments, final PgSchema oldSchema,
54
 
            final PgSchema newSchema) {
 
57
            final PgSchema oldSchema, final PgSchema newSchema,
 
58
            final SearchPathHelper searchPathHelper) {
55
59
        // Drop sequences that do not exist in new schema
56
60
        if (oldSchema != null) {
57
61
            for (final PgSequence sequence : oldSchema.getSequences()) {
58
62
                if (!newSchema.containsSequence(sequence.getName())) {
 
63
                    searchPathHelper.outputSearchPath(writer);
59
64
                    writer.println();
60
 
                    writer.println(
61
 
                            sequence.getDropSQL(arguments.isQuoteNames()));
 
65
                    writer.println(sequence.getDropSQL());
62
66
                }
63
67
            }
64
68
        }
65
69
    }
66
70
 
67
71
    /**
68
 
     * Outputs command for modified sequences.
 
72
     * Outputs statement for modified sequences.
69
73
     *
70
74
     * @param writer writer the output should be written to
71
75
     * @param arguments object containing arguments settings
72
76
     * @param oldSchema original schema
73
77
     * @param newSchema new schema
 
78
     * @param searchPathHelper search path helper
74
79
     */
75
80
    public static void alterSequences(final PrintWriter writer,
76
81
            final PgDiffArguments arguments, final PgSchema oldSchema,
77
 
            final PgSchema newSchema) {
78
 
        final StringBuilder sbSQL = new StringBuilder();
 
82
            final PgSchema newSchema, final SearchPathHelper searchPathHelper) {
 
83
        final StringBuilder sbSQL = new StringBuilder(100);
79
84
 
80
85
        for (final PgSequence newSequence : newSchema.getSequences()) {
81
86
            if (oldSchema != null
96
101
                final String oldMinValue = oldSequence.getMinValue();
97
102
                final String newMinValue = newSequence.getMinValue();
98
103
 
99
 
                if ((newMinValue == null) && (oldMinValue != null)) {
 
104
                if (newMinValue == null && oldMinValue != null) {
100
105
                    sbSQL.append("\n\tNO MINVALUE");
101
106
                } else if (newMinValue != null
102
107
                        && !newMinValue.equals(oldMinValue)) {
107
112
                final String oldMaxValue = oldSequence.getMaxValue();
108
113
                final String newMaxValue = newSequence.getMaxValue();
109
114
 
110
 
                if ((newMaxValue == null) && (oldMaxValue != null)) {
 
115
                if (newMaxValue == null && oldMaxValue != null) {
111
116
                    sbSQL.append("\n\tNO MAXVALUE");
112
117
                } else if (newMaxValue != null
113
118
                        && !newMaxValue.equals(oldMaxValue)) {
119
124
                    final String oldStart = oldSequence.getStartWith();
120
125
                    final String newStart = newSequence.getStartWith();
121
126
 
122
 
                    if ((newStart != null) && !newStart.equals(oldStart)) {
 
127
                    if (newStart != null && !newStart.equals(oldStart)) {
123
128
                        sbSQL.append("\n\tRESTART WITH ");
124
129
                        sbSQL.append(newStart);
125
130
                    }
128
133
                final String oldCache = oldSequence.getCache();
129
134
                final String newCache = newSequence.getCache();
130
135
 
131
 
                if ((newCache != null) && !newCache.equals(oldCache)) {
 
136
                if (newCache != null && !newCache.equals(oldCache)) {
132
137
                    sbSQL.append("\n\tCACHE ");
133
138
                    sbSQL.append(newCache);
134
139
                }
143
148
                }
144
149
 
145
150
                if (sbSQL.length() > 0) {
 
151
                    searchPathHelper.outputSearchPath(writer);
146
152
                    writer.println();
147
153
                    writer.print("ALTER SEQUENCE "
148
 
                            + PgDiffUtils.getQuotedName(newSequence.getName(),
149
 
                            arguments.isQuoteNames()));
 
154
                            + PgDiffUtils.getQuotedName(newSequence.getName()));
150
155
                    writer.print(sbSQL.toString());
151
156
                    writer.println(';');
152
157
                }