Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 45b5c744d01eb8ca0a302041b598c7af174e2882 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Copyright (c) 2013 Eike Stepper (Loehne, 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;

import org.eclipse.net4j.db.DBException;
import org.eclipse.net4j.db.IDBAdapter;
import org.eclipse.net4j.db.IDBConnectionProvider;
import org.eclipse.net4j.db.ddl.IDBField;
import org.eclipse.net4j.db.ddl.IDBIndex.Type;
import org.eclipse.net4j.db.ddl.IDBSchema;
import org.eclipse.net4j.db.ddl.IDBSchemaElement;
import org.eclipse.net4j.db.ddl.IDBTable;
import org.eclipse.net4j.db.ddl.SchemaElementNotFoundException;
import org.eclipse.net4j.db.ddl.delta.IDBSchemaDelta;
import org.eclipse.net4j.spi.db.ddl.InternalDBSchema;

import javax.sql.DataSource;

import java.io.PrintStream;
import java.sql.Connection;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Eike Stepper
 */
public final class DelegatingDBSchema extends DelegatingDBSchemaElement implements InternalDBSchema
{
  DelegatingDBSchema(InternalDBSchema delegate)
  {
    super(delegate);
  }

  @Override
  public InternalDBSchema getDelegate()
  {
    return (InternalDBSchema)super.getDelegate();
  }

  @Override
  public void setDelegate(IDBSchemaElement delegate)
  {
    IDBTable[] wrapperTables = getTables();

    IDBSchema delegateSchema = (IDBSchema)delegate;
    super.setDelegate(delegateSchema);

    for (IDBTable wrapperTable : wrapperTables)
    {
      ((DelegatingDBSchemaElement)wrapperTable).setDelegate(delegateSchema.getTable(wrapperTable.getName()));
    }
  }

  public IDBSchema getWrapper()
  {
    return this;
  }

  @Override
  public IDBSchemaElement getParent()
  {
    return wrap(getDelegate().getParent());
  }

  public IDBTable addTable(String name)
  {
    return wrap(getDelegate().addTable(name));
  }

  public IDBTable removeTable(String name)
  {
    return wrap(getDelegate().removeTable(name));
  }

  public String createIndexName(IDBTable table, Type type, IDBField[] fields, int position)
  {
    return getDelegate().createIndexName(unwrap(table), type, fields, position);
  }

  public boolean isLocked()
  {
    return getDelegate().isLocked();
  }

  public boolean lock()
  {
    return getDelegate().lock();
  }

  @SuppressWarnings("unchecked")
  public <T extends IDBSchemaElement> T findElement(IDBSchemaElement prototype)
  {
    T unwrapped = (T)unwrap(prototype);
    return (T)wrap(getDelegate().findElement(unwrapped));
  }

  public boolean unlock()
  {
    return getDelegate().unlock();
  }

  public void assertUnlocked() throws DBException
  {
    getDelegate().assertUnlocked();
  }

  public IDBTable getTableSafe(String name) throws SchemaElementNotFoundException
  {
    return wrap(getDelegate().getTableSafe(name));
  }

  public IDBTable getTable(String name)
  {
    return wrap(getDelegate().getTable(name));
  }

  public IDBTable[] getTables()
  {
    IDBTable[] tables = getDelegate().getTables();
    IDBTable[] wrappers = new IDBTable[tables.length];
    for (int i = 0; i < tables.length; i++)
    {
      wrappers[i] = wrap(tables[i]);
    }

    return wrappers;
  }

  public Set<IDBTable> create(IDBAdapter dbAdapter, Connection connection) throws DBException
  {
    return wrap(getDelegate().create(dbAdapter, connection));
  }

  public Set<IDBTable> create(IDBAdapter dbAdapter, DataSource dataSource) throws DBException
  {
    return wrap(getDelegate().create(dbAdapter, dataSource));
  }

  public Set<IDBTable> create(IDBAdapter dbAdapter, IDBConnectionProvider connectionProvider) throws DBException
  {
    return wrap(getDelegate().create(dbAdapter, connectionProvider));
  }

  public void drop(IDBAdapter dbAdapter, Connection connection) throws DBException
  {
    getDelegate().drop(dbAdapter, connection);
  }

  public void drop(IDBAdapter dbAdapter, DataSource dataSource) throws DBException
  {
    getDelegate().drop(dbAdapter, dataSource);
  }

  public void drop(IDBAdapter dbAdapter, IDBConnectionProvider connectionProvider) throws DBException
  {
    getDelegate().drop(dbAdapter, connectionProvider);
  }

  public void export(Connection connection, PrintStream out) throws DBException
  {
    getDelegate().export(connection, out);
  }

  public void export(DataSource dataSource, PrintStream out) throws DBException
  {
    getDelegate().export(dataSource, out);
  }

  public void export(IDBConnectionProvider connectionProvider, PrintStream out) throws DBException
  {
    getDelegate().export(connectionProvider, out);
  }

  public IDBSchemaDelta compare(IDBSchema oldSchema)
  {
    return getDelegate().compare(unwrap(oldSchema));
  }

  private static Set<IDBTable> wrap(Set<IDBTable> tables)
  {
    return wrap(tables, new HashSet<IDBTable>());
  }
}

Back to the top