Skip to main content
summaryrefslogtreecommitdiffstats
blob: 73edbf30d98368b0a4b7063f99dd1e6bc512ee58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.net4j.internal.db.ddl.delta;

import org.eclipse.net4j.db.ddl.IDBField;
import org.eclipse.net4j.db.ddl.IDBIndex;
import org.eclipse.net4j.db.ddl.IDBSchema;
import org.eclipse.net4j.db.ddl.IDBTable;
import org.eclipse.net4j.db.ddl.delta.IDBDelta;
import org.eclipse.net4j.db.ddl.delta.IDBDeltaVisitor;
import org.eclipse.net4j.db.ddl.delta.IDBFieldDelta;
import org.eclipse.net4j.db.ddl.delta.IDBIndexDelta;
import org.eclipse.net4j.db.ddl.delta.IDBSchemaDelta;
import org.eclipse.net4j.db.ddl.delta.IDBTableDelta;
import org.eclipse.net4j.db.ddl.delta.IDBTableElementDelta;
import org.eclipse.net4j.internal.db.ddl.DBTable;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Eike Stepper
 */
public final class DBTableDelta extends DBDelta implements IDBTableDelta
{
  private static final long serialVersionUID = 1L;

  private Map<String, IDBFieldDelta> fieldDeltas = new HashMap<String, IDBFieldDelta>();

  private Map<String, IDBIndexDelta> indexDeltas = new HashMap<String, IDBIndexDelta>();

  public DBTableDelta(IDBDelta parent, String name, ChangeKind changeKind)
  {
    super(parent, name, changeKind);
  }

  public DBTableDelta(DBSchemaDelta parent, IDBTable table, IDBTable oldTable)
  {
    this(parent, table.getName(), oldTable == null ? ChangeKind.ADDED : ChangeKind.CHANGED);

    IDBField[] fields = table.getFields();
    IDBField[] oldFields = oldTable == null ? DBTable.NO_FIELDS : oldTable.getFields();
    compare(fields, oldFields, new SchemaElementComparator<IDBField>()
    {
      public void compare(IDBField field, IDBField oldField)
      {
        DBFieldDelta fieldDelta = new DBFieldDelta(DBTableDelta.this, field, oldField);
        addFieldDelta(fieldDelta);
      }
    });

    IDBIndex[] indices = table.getIndices();
    IDBIndex[] oldIndices = oldTable == null ? DBTable.NO_INDICES : oldTable.getIndices();
    compare(indices, oldIndices, new SchemaElementComparator<IDBIndex>()
    {
      public void compare(IDBIndex index, IDBIndex oldIndex)
      {
        DBIndexDelta indexDelta = new DBIndexDelta(DBTableDelta.this, index, oldIndex);
        addIndexDelta(indexDelta);
      }
    });
  }

  /**
   * Constructor for deserialization.
   */
  protected DBTableDelta()
  {
  }

  @Override
  public IDBSchemaDelta getParent()
  {
    return (IDBSchemaDelta)super.getParent();
  }

  public IDBFieldDelta[] getFieldDeltasSortedByPosition()
  {
    IDBFieldDelta[] result = fieldDeltas.values().toArray(new IDBFieldDelta[fieldDeltas.size()]);
    Arrays.sort(result, new Comparator<IDBFieldDelta>()
    {
      public int compare(IDBFieldDelta o1, IDBFieldDelta o2)
      {
        int v1 = getValue(o1);
        int v2 = getValue(o2);
        return v2 - v1;
      }

      private Integer getValue(IDBFieldDelta fieldDelta)
      {
        Integer value = fieldDelta.getPropertyValue(IDBFieldDelta.POSITION_PROPERTY);
        if (value == null)
        {
          return 0;
        }

        return value;
      }
    });

    return result;
  }

  public Map<String, IDBFieldDelta> getFieldDeltas()
  {
    return Collections.unmodifiableMap(fieldDeltas);
  }

  public Map<String, IDBIndexDelta> getIndexDeltas()
  {
    return Collections.unmodifiableMap(indexDeltas);
  }

  public boolean isEmpty()
  {
    return fieldDeltas.isEmpty() && indexDeltas.isEmpty();
  }

  public IDBTableElementDelta[] getElements()
  {
    IDBTableElementDelta[] elements = new IDBTableElementDelta[fieldDeltas.size() + indexDeltas.size()];
    int i = 0;

    for (IDBFieldDelta fieldDelta : fieldDeltas.values())
    {
      elements[i++] = fieldDelta;
    }

    for (IDBIndexDelta indexDelta : indexDeltas.values())
    {
      elements[i++] = indexDelta;
    }

    Arrays.sort(elements);
    return elements;
  }

  public int compareTo(IDBTableDelta o)
  {
    return getName().compareTo(o.getName());
  }

  public void addFieldDelta(IDBFieldDelta fieldDelta)
  {
    fieldDeltas.put(fieldDelta.getName(), fieldDelta);
  }

  public void addIndexDelta(IDBIndexDelta indexDelta)
  {
    indexDeltas.put(indexDelta.getName(), indexDelta);
  }

  public void accept(IDBDeltaVisitor visitor)
  {
    visitor.visit(this);
    for (IDBTableElementDelta tableElementDelta : getElements())
    {
      tableElementDelta.accept(visitor);
    }
  }

  public IDBTable getElement(IDBSchema schema)
  {
    return schema.getTable(getName());
  }
}

Back to the top