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

« back to all changes in this revision

Viewing changes to src/main/java/cz/startnet/utils/pgdiff/PgDiffViews.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;
4
9
import cz.startnet.utils.pgdiff.schema.PgView;
5
10
 
6
11
import java.io.PrintWriter;
 
12
import java.util.Arrays;
 
13
import java.util.List;
7
14
 
8
15
/**
9
16
 * Diffs views.
16
23
     * Creates a new instance of PgDiffViews.
17
24
     */
18
25
    private PgDiffViews() {
19
 
        super();
20
26
    }
21
27
 
22
28
    /**
23
 
     * Outputs commands for creation of views.
 
29
     * Outputs statements for creation of views.
24
30
     *
25
31
     * @param writer writer the output should be written to
26
 
     * @param arguments object containing arguments settings
27
32
     * @param oldSchema original schema
28
33
     * @param newSchema new schema
29
34
     */
30
35
    public static void createViews(final PrintWriter writer,
31
 
            final PgDiffArguments arguments, final PgSchema oldSchema,
32
 
            final PgSchema newSchema) {
 
36
            final PgSchema oldSchema, final PgSchema newSchema) {
33
37
        for (final PgView newView : newSchema.getViews()) {
34
38
            if (oldSchema == null
35
39
                    || !oldSchema.containsView(newView.getName())
36
40
                    || isViewModified(
37
41
                    oldSchema.getView(newView.getName()), newView)) {
38
42
                writer.println();
39
 
                writer.println(
40
 
                        newView.getCreationSQL(arguments.isQuoteNames()));
 
43
                writer.println(newView.getCreationSQL());
41
44
            }
42
45
        }
43
46
    }
44
47
 
45
48
    /**
46
 
     * Outputs commands for dropping views.
 
49
     * Outputs statements for dropping views.
47
50
     *
48
51
     * @param writer writer the output should be written to
49
 
     * @param arguments object containing arguments settings
50
52
     * @param oldSchema original schema
51
53
     * @param newSchema new schema
52
54
     */
53
55
    public static void dropViews(final PrintWriter writer,
54
 
            final PgDiffArguments arguments, final PgSchema oldSchema,
55
 
            final PgSchema newSchema) {
 
56
            final PgSchema oldSchema, final PgSchema newSchema) {
56
57
        if (oldSchema != null) {
57
58
            for (final PgView oldView : oldSchema.getViews()) {
58
59
                final PgView newView = newSchema.getView(oldView.getName());
59
60
 
60
 
                if ((newView == null) || isViewModified(oldView, newView)) {
 
61
                if (newView == null || isViewModified(oldView, newView)) {
61
62
                    writer.println();
62
 
                    writer.println(
63
 
                            oldView.getDropSQL(arguments.isQuoteNames()));
 
63
                    writer.println(oldView.getDropSQL());
64
64
                }
65
65
            }
66
66
        }
77
77
     */
78
78
    private static boolean isViewModified(final PgView oldView,
79
79
            final PgView newView) {
80
 
        final String oldViewColumnNames;
81
 
 
82
 
        if (oldView.getColumnNames() == null) {
83
 
            oldViewColumnNames = "";
84
 
        } else {
85
 
            oldViewColumnNames = oldView.getColumnNames();
86
 
        }
87
 
 
88
 
        final String newViewColumnNames;
89
 
 
90
 
        if (newView.getColumnNames() == null) {
91
 
            newViewColumnNames = "";
92
 
        } else {
93
 
            newViewColumnNames = newView.getColumnNames();
94
 
        }
95
 
 
96
 
        return (!oldViewColumnNames.equals(newViewColumnNames)
97
 
                || !oldView.getQuery().equals(newView.getQuery()));
 
80
        final String[] oldViewColumnNames;
 
81
 
 
82
        if (oldView.getColumnNames() == null
 
83
                || oldView.getColumnNames().isEmpty()) {
 
84
            oldViewColumnNames = null;
 
85
        } else {
 
86
            oldViewColumnNames = oldView.getColumnNames().toArray(
 
87
                    new String[oldView.getColumnNames().size()]);
 
88
        }
 
89
 
 
90
        final String[] newViewColumnNames;
 
91
 
 
92
        if (newView.getColumnNames() == null
 
93
                || newView.getColumnNames().isEmpty()) {
 
94
            newViewColumnNames = null;
 
95
        } else {
 
96
            newViewColumnNames = newView.getColumnNames().toArray(
 
97
                    new String[newView.getColumnNames().size()]);
 
98
        }
 
99
 
 
100
        if (oldViewColumnNames == null && newViewColumnNames == null) {
 
101
            return !oldView.getQuery().trim().equals(newView.getQuery().trim());
 
102
        } else {
 
103
            return !Arrays.equals(oldViewColumnNames, newViewColumnNames);
 
104
        }
 
105
    }
 
106
 
 
107
    /**
 
108
     * Outputs statements for altering view default values.
 
109
     *
 
110
     * @param writer writer
 
111
     * @param oldSchema old schema
 
112
     * @param newSchema new schema
 
113
     */
 
114
    public static void alterViews(final PrintWriter writer,
 
115
            final PgSchema oldSchema, final PgSchema newSchema) {
 
116
        if (oldSchema != null) {
 
117
            for (final PgView oldView : oldSchema.getViews()) {
 
118
                final PgView newView = newSchema.getView(oldView.getName());
 
119
 
 
120
                if (oldView != null && newView != null) {
 
121
                    diffDefaultValues(writer, oldView, newView);
 
122
                }
 
123
            }
 
124
        }
 
125
    }
 
126
 
 
127
    /**
 
128
     * Diffs default values in views.
 
129
     *
 
130
     * @param writer writer
 
131
     * @param oldView old view
 
132
     * @param newView new view
 
133
     */
 
134
    private static void diffDefaultValues(final PrintWriter writer,
 
135
            final PgView oldView, final PgView newView) {
 
136
        final List<PgView.DefaultValue> oldValues =
 
137
                oldView.getDefaultValues();
 
138
        final List<PgView.DefaultValue> newValues =
 
139
                newView.getDefaultValues();
 
140
 
 
141
        // modify defaults that are in old view
 
142
        for (final PgView.DefaultValue oldValue : oldValues) {
 
143
            boolean found = false;
 
144
 
 
145
            for (final PgView.DefaultValue newValue : newValues) {
 
146
                if (oldValue.getColumnName().equals(newValue.getColumnName())
 
147
                        && !oldValue.getDefaultValue().equals(
 
148
                        newValue.getDefaultValue())) {
 
149
                    found = true;
 
150
 
 
151
                    writer.println();
 
152
                    writer.print("ALTER VIEW ");
 
153
                    writer.print(PgDiffUtils.getQuotedName(newView.getName()));
 
154
                    writer.print(" ALTER COLUMN ");
 
155
                    writer.print(PgDiffUtils.getQuotedName(
 
156
                            newValue.getColumnName()));
 
157
                    writer.print(" SET DEFAULT ");
 
158
                    writer.print(newValue.getDefaultValue());
 
159
                    writer.println(';');
 
160
 
 
161
                    break;
 
162
                }
 
163
            }
 
164
 
 
165
            if (!found) {
 
166
                writer.println();
 
167
                writer.print("ALTER VIEW ");
 
168
                writer.print(PgDiffUtils.getQuotedName(newView.getName()));
 
169
                writer.print(" ALTER COLUMN ");
 
170
                writer.print(PgDiffUtils.getQuotedName(
 
171
                        oldValue.getColumnName()));
 
172
                writer.print(" DROP DEFAULT;");
 
173
            }
 
174
        }
 
175
 
 
176
        // add new defaults
 
177
        for (final PgView.DefaultValue newValue : newValues) {
 
178
            boolean found = false;
 
179
 
 
180
            for (final PgView.DefaultValue oldValue : oldValues) {
 
181
                if (newValue.getColumnName().equals(oldValue.getColumnName())) {
 
182
                    found = true;
 
183
                    break;
 
184
                }
 
185
            }
 
186
 
 
187
            if (found) {
 
188
                continue;
 
189
            }
 
190
 
 
191
            writer.println();
 
192
            writer.print("ALTER VIEW ");
 
193
            writer.print(PgDiffUtils.getQuotedName(newView.getName()));
 
194
            writer.print(" ALTER COLUMN ");
 
195
            writer.print(PgDiffUtils.getQuotedName(newValue.getColumnName()));
 
196
            writer.print(" SET DEFAULT ");
 
197
            writer.print(newValue.getDefaultValue());
 
198
            writer.print(';');
 
199
        }
98
200
    }
99
201
}