Skip to main content
summaryrefslogtreecommitdiffstats
blob: 555bcd97bb702789ed991f7475e69db56e62ce8b (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * 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;

import org.eclipse.net4j.db.DBException;
import org.eclipse.net4j.db.DBType;
import org.eclipse.net4j.db.ddl.IDBField;
import org.eclipse.net4j.db.ddl.IDBIndex;
import org.eclipse.net4j.db.ddl.IDBSchemaElement;
import org.eclipse.net4j.db.ddl.IDBSchemaVisitor;
import org.eclipse.net4j.db.ddl.IDBTable;
import org.eclipse.net4j.db.ddl.SchemaElementNotFoundException;
import org.eclipse.net4j.spi.db.DBSchema;
import org.eclipse.net4j.spi.db.DBSchemaElement;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Eike Stepper
 */
public class DBTable extends DBSchemaElement implements IDBTable
{
  public static final IDBField[] NO_FIELDS = {};

  public static final IDBIndex[] NO_INDICES = {};

  private static final long serialVersionUID = 1L;

  private DBSchema schema;

  private List<DBField> fields = new ArrayList<DBField>();

  private List<DBIndex> indices = new ArrayList<DBIndex>();

  public DBTable(DBSchema schema, String name)
  {
    super(name);
    this.schema = schema;
  }

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

  public SchemaElementType getSchemaElementType()
  {
    return SchemaElementType.TABLE;
  }

  public DBSchema getSchema()
  {
    return schema;
  }

  public DBSchema getParent()
  {
    return getSchema();
  }

  public DBField addField(String name, DBType type)
  {
    return addField(name, type, IDBField.DEFAULT, IDBField.DEFAULT, false);
  }

  public DBField addField(String name, DBType type, boolean notNull)
  {
    return addField(name, type, IDBField.DEFAULT, IDBField.DEFAULT, notNull);
  }

  public DBField addField(String name, DBType type, int precision)
  {
    return addField(name, type, precision, IDBField.DEFAULT, false);
  }

  public DBField addField(String name, DBType type, int precision, boolean notNull)
  {
    return addField(name, type, precision, IDBField.DEFAULT, notNull);
  }

  public DBField addField(String name, DBType type, int precision, int scale)
  {
    return addField(name, type, precision, scale, false);
  }

  public DBField addField(String name, DBType type, int precision, int scale, boolean notNull)
  {
    assertUnlocked();

    if (getField(name) != null)
    {
      throw new DBException("Field exists: " + name); //$NON-NLS-1$
    }

    int position = fields.size();
    DBField field = new DBField(this, name, type, precision, scale, notNull, position);
    fields.add(field);
    resetElements();
    return field;
  }

  public void removeField(IDBField fieldToRemove)
  {
    assertUnlocked();

    boolean found = false;
    for (Iterator<DBField> it = fields.iterator(); it.hasNext();)
    {
      DBField field = it.next();
      if (found)
      {
        field.setPosition(field.getPosition() - 1);
      }
      else if (field == fieldToRemove)
      {
        it.remove();
        found = true;
      }
    }

    resetElements();
  }

  public DBField getFieldSafe(String name) throws SchemaElementNotFoundException
  {
    DBField field = getField(name);
    if (field == null)
    {
      throw new SchemaElementNotFoundException(this, SchemaElementType.FIELD, name);
    }

    return field;
  }

  public DBField getField(String name)
  {
    return findElement(getFields(), name);
  }

  public DBField getField(int position)
  {
    return fields.get(position);
  }

  public int getFieldCount()
  {
    return fields.size();
  }

  public DBField[] getFields()
  {
    return fields.toArray(new DBField[fields.size()]);
  }

  public DBField[] getFields(String... fieldNames) throws SchemaElementNotFoundException
  {
    List<DBField> result = new ArrayList<DBField>();
    for (String fieldName : fieldNames)
    {
      DBField field = getFieldSafe(fieldName);
      result.add(field);
    }

    return result.toArray(new DBField[result.size()]);
  }

  public DBIndex addIndex(String name, IDBIndex.Type type, IDBField... fields)
  {
    assertUnlocked();

    int position = indices.size();
    if (name == null)
    {
      name = schema.createIndexName(this, type, fields, position);
    }

    if (getIndex(name) != null)
    {
      throw new DBException("Index exists: " + name); //$NON-NLS-1$
    }

    if (type == IDBIndex.Type.PRIMARY_KEY)
    {
      for (DBIndex index : getIndices())
      {
        if (index.getType() == IDBIndex.Type.PRIMARY_KEY)
        {
          throw new DBException("Primary key exists: " + index); //$NON-NLS-1$
        }
      }
    }

    DBIndex index = new DBIndex(this, name, type, fields, position);
    indices.add(index);
    resetElements();
    return index;
  }

  public DBIndex addIndex(String name, IDBIndex.Type type, String... fieldNames)
  {
    return addIndex(name, type, getFields(fieldNames));
  }

  public DBIndex addIndexEmpty(String name, IDBIndex.Type type)
  {
    return addIndex(name, type, NO_FIELDS);
  }

  public DBIndex addIndex(IDBIndex.Type type, IDBField... fields)
  {
    return addIndex(null, type, fields);
  }

  public DBIndex addIndex(IDBIndex.Type type, String... fieldNames)
  {
    DBField[] fields = getFields(fieldNames);
    return addIndex(type, fields);
  }

  public DBIndex addIndexEmpty(IDBIndex.Type type)
  {
    return addIndex(type, NO_FIELDS);
  }

  @SuppressWarnings("deprecation")
  public void removeIndex(IDBIndex indexToRemove)
  {
    assertUnlocked();

    boolean found = false;
    for (Iterator<DBIndex> it = indices.iterator(); it.hasNext();)
    {
      DBIndex index = it.next();
      if (found)
      {
        index.setPosition(index.getPosition() - 1);
      }
      else if (index == indexToRemove)
      {
        it.remove();
        found = true;
      }
    }

    resetElements();
  }

  public DBIndex getIndexSafe(String name) throws SchemaElementNotFoundException
  {
    DBIndex index = getIndex(name);
    if (index == null)
    {
      throw new SchemaElementNotFoundException(this, SchemaElementType.INDEX, name);
    }

    return index;
  }

  public DBIndex getIndex(String name)
  {
    return findElement(getIndices(), name);
  }

  public DBIndex getIndex(int position)
  {
    return indices.get(position);
  }

  public int getIndexCount()
  {
    return indices.size();
  }

  public DBIndex[] getIndices()
  {
    return indices.toArray(new DBIndex[indices.size()]);
  }

  public DBIndex getPrimaryKeyIndex()
  {
    for (DBIndex index : indices)
    {
      if (index.getType() == IDBIndex.Type.PRIMARY_KEY)
      {
        return index;
      }
    }

    return null;
  }

  public String getFullName()
  {
    return getName();
  }

  public void remove()
  {
    schema.removeTable(getName());
  }

  public String sqlInsert()
  {
    StringBuilder builder = new StringBuilder();
    builder.append("INSERT INTO "); //$NON-NLS-1$
    builder.append(getName());
    builder.append(" VALUES ("); //$NON-NLS-1$

    for (int i = 0; i < fields.size(); i++)
    {
      if (i > 0)
      {
        builder.append(", "); //$NON-NLS-1$
      }

      builder.append("?"); //$NON-NLS-1$
    }

    builder.append(")"); //$NON-NLS-1$
    return builder.toString();
  }

  @Override
  protected void collectElements(List<IDBSchemaElement> elements)
  {
    elements.addAll(fields);
    elements.addAll(indices);
  }

  @Override
  protected void doAccept(IDBSchemaVisitor visitor)
  {
    visitor.visit(this);
  }

  private void assertUnlocked()
  {
    schema.assertUnlocked();
  }
}

Back to the top