2
* $Id: PgDiffConstraints.java 80 2007-09-01 20:25:45Z fordfrog $
4
package cz.startnet.utils.pgdiff;
6
import cz.startnet.utils.pgdiff.schema.PgConstraint;
7
import cz.startnet.utils.pgdiff.schema.PgSchema;
8
import cz.startnet.utils.pgdiff.schema.PgTable;
10
import java.io.PrintWriter;
12
import java.util.ArrayList;
13
import java.util.List;
20
* @version $Id: PgDiffConstraints.java 80 2007-09-01 20:25:45Z fordfrog $
22
public class PgDiffConstraints {
24
* Creates a new instance of PgDiffConstraints.
26
private PgDiffConstraints() {
31
* Outputs commands for differences in constraints.
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
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;
49
if (oldSchema == null) {
52
oldTable = oldSchema.getTable(newTable.getName());
55
// Drop constraints that no more exist or are modified
56
for (PgConstraint constraint : getDropConstraints(
61
writer.println(constraint.getDropSQL(arguments.isQuoteNames()));
64
// Add new constraints
65
for (PgConstraint constraint : getNewConstraints(
71
constraint.getCreationSQL(arguments.isQuoteNames()));
77
* Returns list of constraints that should be dropped.
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
84
* @return list of constraints that should be dropped
86
* @todo Constraints that are depending on a removed field should not be
87
* added to drop because they are already removed.
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>();
95
if ((newTable != null) && (oldTable != null)) {
96
for (final PgConstraint constraint : oldTable.getConstraints()) {
98
(constraint.isPrimaryKeyConstraint() == primaryKey)
99
&& (!newTable.containsConstraint(constraint.getName())
100
|| !newTable.getConstraint(constraint.getName()).equals(
102
list.add(constraint);
111
* Returns list of constraints that should be added.
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
118
* @return list of constraints that should be added
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>();
126
if (newTable != null) {
127
if (oldTable == null) {
128
for (final PgConstraint constraint : newTable.getConstraints()) {
129
if (constraint.isPrimaryKeyConstraint() == primaryKey) {
130
list.add(constraint);
134
for (final PgConstraint constraint : newTable.getConstraints()) {
136
(constraint.isPrimaryKeyConstraint() == primaryKey)
137
&& (!oldTable.containsConstraint(
138
constraint.getName())
139
|| !oldTable.getConstraint(constraint.getName()).equals(
141
list.add(constraint);