Skip to main content
summaryrefslogtreecommitdiffstats
blob: b47cdcd73dbbfcb3f06a6ae599a79996a0d7ef2d (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
/*
 * 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.IDBSchema;
import org.eclipse.net4j.db.ddl.IDBTable;
import org.eclipse.net4j.db.ddl.delta.IDBDeltaVisitor;
import org.eclipse.net4j.db.ddl.delta.IDBSchemaDelta;
import org.eclipse.net4j.db.ddl.delta.IDBTableDelta;
import org.eclipse.net4j.spi.db.DBSchema;

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

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

  private Map<String, IDBTableDelta> tableDeltas = new HashMap<String, IDBTableDelta>();

  public DBSchemaDelta(String name, ChangeKind changeKind)
  {
    super(null, name, changeKind);
  }

  public DBSchemaDelta(DBSchema schema, IDBSchema oldSchema)
  {
    this(schema.getName(), oldSchema == null ? ChangeKind.ADDED : ChangeKind.CHANGED);

    IDBTable[] tables = schema.getTables();
    IDBTable[] oldTables = oldSchema == null ? DBSchema.NO_TABLES : oldSchema.getTables();
    compare(tables, oldTables, new SchemaElementComparator<IDBTable>()
    {
      public void compare(IDBTable table, IDBTable oldTable)
      {
        DBTableDelta tableDelta = new DBTableDelta(DBSchemaDelta.this, table, oldTable);
        addTableDelta(tableDelta);
      }
    });
  }

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

  public Map<String, IDBTableDelta> getTableDeltas()
  {
    return Collections.unmodifiableMap(tableDeltas);
  }

  public boolean isEmpty()
  {
    return tableDeltas.isEmpty();
  }

  public IDBTableDelta[] getElements()
  {
    IDBTableDelta[] elements = tableDeltas.values().toArray(new IDBTableDelta[tableDeltas.size()]);
    Arrays.sort(elements);
    return elements;
  }

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

  public void addTableDelta(IDBTableDelta tableDelta)
  {
    tableDeltas.put(tableDelta.getName(), tableDelta);
  }

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

  public IDBSchema getElement(IDBSchema schema)
  {
    return schema;
  }

  public void applyTo(IDBSchema schema)
  {
    IDBDeltaVisitor visitor = new IDBDeltaVisitor.Applier(schema);
    accept(visitor);
  }
}

Back to the top