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

« back to all changes in this revision

Viewing changes to src/main/java/cz/startnet/utils/pgdiff/PgDiffConstraints.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: PgDiffConstraints.java 80 2007-09-01 20:25:45Z fordfrog $
 
3
 */
 
4
package cz.startnet.utils.pgdiff;
 
5
 
 
6
import cz.startnet.utils.pgdiff.schema.PgConstraint;
 
7
import cz.startnet.utils.pgdiff.schema.PgSchema;
 
8
import cz.startnet.utils.pgdiff.schema.PgTable;
 
9
 
 
10
import java.io.PrintWriter;
 
11
 
 
12
import java.util.ArrayList;
 
13
import java.util.List;
 
14
 
 
15
 
 
16
/**
 
17
 * Diffs constraints.
 
18
 *
 
19
 * @author fordfrog
 
20
 * @version $Id: PgDiffConstraints.java 80 2007-09-01 20:25:45Z fordfrog $
 
21
 */
 
22
public class PgDiffConstraints {
 
23
    /**
 
24
     * Creates a new instance of PgDiffConstraints.
 
25
     */
 
26
    private PgDiffConstraints() {
 
27
        super();
 
28
    }
 
29
 
 
30
    /**
 
31
     * Outputs commands for differences in constraints.
 
32
     *
 
33
     * @param writer writer the output should be written to
 
34
     * @param arguments object containing arguments settings
 
35
     * @param oldSchema original schema
 
36
     * @param newSchema new schema
 
37
     * @param primaryKey determines whether primery keys should be processed or
 
38
     *        any other constraints should be processed
 
39
     */
 
40
    public static void diffConstraints(
 
41
        final PrintWriter writer,
 
42
        final PgDiffArguments arguments,
 
43
        final PgSchema oldSchema,
 
44
        final PgSchema newSchema,
 
45
        final boolean primaryKey) {
 
46
        for (PgTable newTable : newSchema.getTables()) {
 
47
            final PgTable oldTable;
 
48
 
 
49
            if (oldSchema == null) {
 
50
                oldTable = null;
 
51
            } else {
 
52
                oldTable = oldSchema.getTable(newTable.getName());
 
53
            }
 
54
 
 
55
            // Drop constraints that no more exist or are modified
 
56
            for (PgConstraint constraint : getDropConstraints(
 
57
                        oldTable,
 
58
                        newTable,
 
59
                        primaryKey)) {
 
60
                writer.println();
 
61
                writer.println(constraint.getDropSQL(arguments.isQuoteNames()));
 
62
            }
 
63
 
 
64
            // Add new constraints
 
65
            for (PgConstraint constraint : getNewConstraints(
 
66
                        oldTable,
 
67
                        newTable,
 
68
                        primaryKey)) {
 
69
                writer.println();
 
70
                writer.println(
 
71
                        constraint.getCreationSQL(arguments.isQuoteNames()));
 
72
            }
 
73
        }
 
74
    }
 
75
 
 
76
    /**
 
77
     * Returns list of constraints that should be dropped.
 
78
     *
 
79
     * @param oldTable original table or null
 
80
     * @param newTable new table or null
 
81
     * @param primaryKey determines whether primery keys should be processed or
 
82
     *        any other constraints should be processed
 
83
     *
 
84
     * @return list of constraints that should be dropped
 
85
     *
 
86
     * @todo Constraints that are depending on a removed field should not be
 
87
     *       added to drop because they are already removed.
 
88
     */
 
89
    private static List<PgConstraint> getDropConstraints(
 
90
        final PgTable oldTable,
 
91
        final PgTable newTable,
 
92
        final boolean primaryKey) {
 
93
        final List<PgConstraint> list = new ArrayList<PgConstraint>();
 
94
 
 
95
        if ((newTable != null) && (oldTable != null)) {
 
96
            for (final PgConstraint constraint : oldTable.getConstraints()) {
 
97
                if (
 
98
                    (constraint.isPrimaryKeyConstraint() == primaryKey)
 
99
                        && (!newTable.containsConstraint(constraint.getName())
 
100
                        || !newTable.getConstraint(constraint.getName()).equals(
 
101
                                constraint))) {
 
102
                    list.add(constraint);
 
103
                }
 
104
            }
 
105
        }
 
106
 
 
107
        return list;
 
108
    }
 
109
 
 
110
    /**
 
111
     * Returns list of constraints that should be added.
 
112
     *
 
113
     * @param oldTable original table
 
114
     * @param newTable new table
 
115
     * @param primaryKey determines whether primery keys should be processed or
 
116
     *        any other constraints should be processed
 
117
     *
 
118
     * @return list of constraints that should be added
 
119
     */
 
120
    private static List<PgConstraint> getNewConstraints(
 
121
        final PgTable oldTable,
 
122
        final PgTable newTable,
 
123
        final boolean primaryKey) {
 
124
        final List<PgConstraint> list = new ArrayList<PgConstraint>();
 
125
 
 
126
        if (newTable != null) {
 
127
            if (oldTable == null) {
 
128
                for (final PgConstraint constraint : newTable.getConstraints()) {
 
129
                    if (constraint.isPrimaryKeyConstraint() == primaryKey) {
 
130
                        list.add(constraint);
 
131
                    }
 
132
                }
 
133
            } else {
 
134
                for (final PgConstraint constraint : newTable.getConstraints()) {
 
135
                    if (
 
136
                        (constraint.isPrimaryKeyConstraint() == primaryKey)
 
137
                            && (!oldTable.containsConstraint(
 
138
                                    constraint.getName())
 
139
                            || !oldTable.getConstraint(constraint.getName()).equals(
 
140
                                    constraint))) {
 
141
                        list.add(constraint);
 
142
                    }
 
143
                }
 
144
            }
 
145
        }
 
146
 
 
147
        return list;
 
148
    }
 
149
}