2
* $Id: PgDiffSequences.java 80 2007-09-01 20:25:45Z fordfrog $
4
package cz.startnet.utils.pgdiff;
6
import cz.startnet.utils.pgdiff.schema.PgSchema;
7
import cz.startnet.utils.pgdiff.schema.PgSequence;
9
import java.io.PrintWriter;
16
* @version $Id: PgDiffSequences.java 80 2007-09-01 20:25:45Z fordfrog $
18
public class PgDiffSequences {
20
* Creates a new instance of PgDiffSequences.
22
private PgDiffSequences() {
27
* Outputs commands for differences in sequences.
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
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())) {
45
sequence.getDropSQL(arguments.isQuoteNames()));
51
for (PgSequence sequence : newSchema.getSequences()) {
54
|| !oldSchema.containsSequence(sequence.getName())) {
57
sequence.getCreationSQL(arguments.isQuoteNames()));
61
// Alter modified sequences
62
addModifiedSequences(writer, arguments, oldSchema, newSchema);
66
* Returns list of modified sequences.
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
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();
80
for (final PgSequence newSequence : newSchema.getSequences()) {
83
&& oldSchema.containsSequence(newSequence.getName())) {
84
final PgSequence oldSequence =
85
oldSchema.getSequence(newSequence.getName());
88
final String oldIncrement = oldSequence.getIncrement();
89
final String newIncrement = newSequence.getIncrement();
92
(newIncrement != null)
93
&& !newIncrement.equals(oldIncrement)) {
94
sbSQL.append("\n\tINCREMENT BY ");
95
sbSQL.append(newIncrement);
98
final String oldMinValue = oldSequence.getMinValue();
99
final String newMinValue = newSequence.getMinValue();
101
if ((newMinValue == null) && (oldMinValue != null)) {
102
sbSQL.append("\n\tNO MINVALUE");
104
(newMinValue != null)
105
&& !newMinValue.equals(oldMinValue)) {
106
sbSQL.append("\n\tMINVALUE ");
107
sbSQL.append(newMinValue);
110
final String oldMaxValue = oldSequence.getMaxValue();
111
final String newMaxValue = newSequence.getMaxValue();
113
if ((newMaxValue == null) && (oldMaxValue != null)) {
114
sbSQL.append("\n\tNO MAXVALUE");
116
(newMaxValue != null)
117
&& !newMaxValue.equals(oldMaxValue)) {
118
sbSQL.append("\n\tMAXVALUE ");
119
sbSQL.append(newMaxValue);
122
if (!arguments.isIgnoreStartWith()) {
123
final String oldStart = oldSequence.getStartWith();
124
final String newStart = newSequence.getStartWith();
126
if ((newStart != null) && !newStart.equals(oldStart)) {
127
sbSQL.append("\n\tRESTART WITH ");
128
sbSQL.append(newStart);
132
final String oldCache = oldSequence.getCache();
133
final String newCache = newSequence.getCache();
135
if ((newCache != null) && !newCache.equals(oldCache)) {
136
sbSQL.append("\n\tCACHE ");
137
sbSQL.append(newCache);
140
final boolean oldCycle = oldSequence.isCycle();
141
final boolean newCycle = newSequence.isCycle();
143
if (oldCycle && !newCycle) {
144
sbSQL.append("\n\tNO CYCLE");
145
} else if (!oldCycle && newCycle) {
146
sbSQL.append("\n\tCYCLE");
149
if (sbSQL.length() > 0) {
153
+ PgDiffUtils.getQuotedName(
154
newSequence.getName(),
155
arguments.isQuoteNames()));
156
writer.print(sbSQL.toString());