Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 922809559f7f289953f48f05e312eef1f5bfcf62 (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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/***************************************************************************
 * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
 * 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.emf.cdo.server.internal.db;

import org.eclipse.emf.cdo.internal.protocol.CDOIDImpl;
import org.eclipse.emf.cdo.protocol.CDOID;
import org.eclipse.emf.cdo.protocol.model.CDOClass;
import org.eclipse.emf.cdo.protocol.model.CDOClassRef;
import org.eclipse.emf.cdo.protocol.model.resource.CDOPathFeature;
import org.eclipse.emf.cdo.protocol.model.resource.CDOResourceClass;
import org.eclipse.emf.cdo.server.IPackageManager;
import org.eclipse.emf.cdo.server.db.IAttributeMapping;
import org.eclipse.emf.cdo.server.db.IClassMapping;
import org.eclipse.emf.cdo.server.db.IDBStore;
import org.eclipse.emf.cdo.server.db.IDBStoreAccessor;
import org.eclipse.emf.cdo.server.db.IMappingStrategy;
import org.eclipse.emf.cdo.server.internal.db.bundle.OM;

import org.eclipse.net4j.db.DBException;
import org.eclipse.net4j.db.DBUtil;
import org.eclipse.net4j.db.IDBField;
import org.eclipse.net4j.db.IDBTable;
import org.eclipse.net4j.internal.util.om.trace.ContextTracer;
import org.eclipse.net4j.util.io.CloseableIterator;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author Eike Stepper
 */
public abstract class MappingStrategy implements IMappingStrategy
{
  private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, MappingStrategy.class);

  public static final String PROP_MAPPING_PRECEDENCE = "mappingPrecedence";

  public static final String PROP_TO_MANY_REFERENCE_MAPPING = "toManyReferenceMapping";

  public static final String PROP_TO_ONE_REFERENCE_MAPPING = "toOneReferenceMapping";

  private IDBStore store;

  private Map<String, String> properties;

  private Precedence precedence;

  private ToMany toMany;

  private ToOne toOne;

  private Map<Object, IDBTable> referenceTables = new HashMap<Object, IDBTable>();

  private Map<Integer, CDOClassRef> classRefs = new HashMap<Integer, CDOClassRef>();

  private IClassMapping resourceClassMapping;

  private IAttributeMapping resourcePathMapping;

  private IDBTable resourceTable;

  private IDBField resourceIDField;

  private IDBField resourcePathField;

  public MappingStrategy()
  {
  }

  public IDBStore getStore()
  {
    return store;
  }

  public void setStore(IDBStore store)
  {
    this.store = store;
  }

  public Map<String, String> getProperties()
  {
    if (properties == null)
    {
      properties = new HashMap<String, String>();
    }

    return properties;
  }

  public void setProperties(Map<String, String> properties)
  {
    this.properties = properties;
  }

  public Precedence getPrecedence()
  {
    if (precedence == null)
    {
      String value = getProperties().get(PROP_MAPPING_PRECEDENCE);
      precedence = value == null ? Precedence.STRATEGY : Precedence.valueOf(value);
    }

    return precedence;
  }

  public ToMany getToMany()
  {
    if (toMany == null)
    {
      String value = getProperties().get(PROP_TO_MANY_REFERENCE_MAPPING);
      toMany = value == null ? ToMany.PER_REFERENCE : ToMany.valueOf(value);
    }

    return toMany;
  }

  public ToOne getToOne()
  {
    if (toOne == null)
    {
      String value = getProperties().get(PROP_TO_ONE_REFERENCE_MAPPING);
      toOne = value == null ? ToOne.LIKE_ATTRIBUTES : ToOne.valueOf(value);
    }

    return toOne;
  }

  public Map<Object, IDBTable> getReferenceTables()
  {
    return referenceTables;
  }

  public IClassMapping getClassMapping(CDOClass cdoClass)
  {
    IClassMapping mapping = ClassServerInfo.getClassMapping(cdoClass);
    if (mapping == NoClassMapping.INSTANCE)
    {
      return null;
    }

    if (mapping == null)
    {
      mapping = createClassMapping(cdoClass);
      ClassServerInfo.setClassMapping(cdoClass, mapping == null ? NoClassMapping.INSTANCE : mapping);
    }

    return mapping;
  }

  public IClassMapping getResourceClassMapping()
  {
    if (resourceClassMapping == null)
    {
      initResourceInfos();
    }

    return resourceClassMapping;
  }

  public IAttributeMapping getResourcePathMapping()
  {
    if (resourcePathMapping == null)
    {
      initResourceInfos();
    }

    return resourcePathMapping;
  }

  public IDBTable getResourceTable()
  {
    if (resourceTable == null)
    {
      initResourceInfos();
    }

    return resourceTable;
  }

  public IDBField getResourceIDField()
  {
    if (resourceIDField == null)
    {
      initResourceInfos();
    }

    return resourceIDField;
  }

  public IDBField getResourcePathField()
  {
    if (resourcePathField == null)
    {
      initResourceInfos();
    }

    return resourcePathField;
  }

  protected void initResourceInfos()
  {
    IPackageManager packageManager = getStore().getRepository().getPackageManager();
    CDOResourceClass resourceClass = packageManager.getCDOResourcePackage().getCDOResourceClass();
    CDOPathFeature pathFeature = packageManager.getCDOResourcePackage().getCDOResourceClass().getCDOPathFeature();

    resourceClassMapping = getClassMapping(resourceClass);
    resourcePathMapping = resourceClassMapping.getAttributeMapping(pathFeature);

    resourceTable = resourceClassMapping.getTable();
    resourceIDField = resourceTable.getField(CDODBSchema.ATTRIBUTES_ID);
    resourcePathField = resourcePathMapping.getField();
  }

  public CloseableIterator<CDOID> readObjectIDs(final IDBStoreAccessor storeAccessor, final boolean withTypes)
  {
    List<CDOClass> classes = getClassesWithObjectInfo();
    final Iterator<CDOClass> classIt = classes.iterator();
    return new ObjectIDIterator(this, storeAccessor, withTypes)
    {
      @Override
      protected ResultSet getNextResultSet()
      {
        while (classIt.hasNext())
        {
          CDOClass cdoClass = classIt.next();
          IClassMapping mapping = getClassMapping(cdoClass);
          if (mapping != null)
          {
            IDBTable table = mapping.getTable();
            if (table != null)
            {
              StringBuilder builder = new StringBuilder();
              builder.append("SELECT DISTINCT ");
              builder.append(CDODBSchema.ATTRIBUTES_ID);
              if (withTypes)
              {
                builder.append(", ");
                builder.append(CDODBSchema.ATTRIBUTES_CLASS);
              }

              builder.append(" FROM ");
              builder.append(table);
              String sql = builder.toString();

              try
              {
                return storeAccessor.getStatement().executeQuery(sql);
              }
              catch (SQLException ex)
              {
                throw new DBException(ex);
              }
            }
          }
        }

        return null;
      }
    };
  }

  public CDOClassRef readObjectType(IDBStoreAccessor storeAccessor, CDOID id)
  {
    // TODO Change to support vertical mappings
    String prefix = "SELECT DISTINCT " + CDODBSchema.ATTRIBUTES_CLASS + " FROM ";
    String suffix = " WHERE " + CDODBSchema.ATTRIBUTES_ID + "=" + id;
    for (CDOClass cdoClass : getClassesWithObjectInfo())
    {
      IClassMapping mapping = getClassMapping(cdoClass);
      if (mapping != null)
      {
        IDBTable table = mapping.getTable();
        if (table != null)
        {
          String sql = prefix + table + suffix;
          if (TRACER.isEnabled()) TRACER.trace(sql);
          ResultSet resultSet = null;

          try
          {
            resultSet = storeAccessor.getStatement().executeQuery(sql);
            if (resultSet.next())
            {
              int classID = resultSet.getInt(1);
              return getClassRef(storeAccessor, classID);
            }
          }
          catch (SQLException ex)
          {
            throw new DBException(ex);
          }
          finally
          {
            DBUtil.close(resultSet);
          }
        }
      }
    }

    throw new DBException("No object with id " + id);
  }

  public CDOClassRef getClassRef(IDBStoreAccessor storeAccessor, int classID)
  {
    CDOClassRef classRef = classRefs.get(classID);
    if (classRef == null)
    {
      if (classID == ClassServerInfo.CDO_RESOURCE_CLASS_DBID)
      {
        IPackageManager packageManager = getStore().getRepository().getPackageManager();
        CDOResourceClass resourceClass = packageManager.getCDOResourcePackage().getCDOResourceClass();
        classRef = resourceClass.createClassRef();
      }
      else
      {
        classRef = storeAccessor.readClassRef(classID);
      }

      classRefs.put(classID, classRef);
    }

    return classRef;
  }

  public CDOID readResourceID(IDBStoreAccessor storeAccessor, String path)
  {
    IDBTable resourceTable = getResourceTable();
    IDBField selectField = getResourceIDField();
    IDBField whereField = getResourcePathField();
    return (CDOID)readResourceInfo(storeAccessor, resourceTable, selectField, whereField, path);
  }

  public String readResourcePath(IDBStoreAccessor storeAccessor, CDOID id)
  {
    IDBTable resourceTable = getResourceTable();
    IDBField selectField = getResourcePathField();
    IDBField whereField = getResourceIDField();
    return (String)readResourceInfo(storeAccessor, resourceTable, selectField, whereField, id);
  }

  protected Object readResourceInfo(IDBStoreAccessor storeAccessor, IDBTable resourceTable, IDBField selectField,
      IDBField whereField, Object whereValue)
  {
    StringBuilder builder = new StringBuilder();
    builder.append("SELECT ");
    builder.append(selectField);
    builder.append(" FROM ");
    builder.append(resourceTable);
    builder.append(" WHERE ");
    builder.append(whereField);
    builder.append("=");
    getStore().getDBAdapter().appendValue(builder, whereField, whereValue);

    String sql = builder.toString();
    if (TRACER.isEnabled()) TRACER.trace(sql);
    ResultSet resultSet = null;

    try
    {
      Statement statement = storeAccessor.getStatement();
      statement.setMaxRows(1); // Reset by DBUtil.close(resultSet)

      resultSet = statement.executeQuery(sql);
      if (!resultSet.next())
      {
        return null;
      }

      if (whereValue instanceof CDOID)
      {
        String path = resultSet.getString(1);
        return path;
      }
      else
      {
        long id = resultSet.getLong(1);
        return CDOIDImpl.create(id);
      }
    }
    catch (SQLException ex)
    {
      throw new DBException(ex);
    }
    finally
    {
      DBUtil.close(resultSet);
    }
  }

  @Override
  public String toString()
  {
    return getType();
  }

  /**
   * The implementation of this method must take care of creating a unique index to prevent duplicate resource paths.
   */
  protected abstract IClassMapping createClassMapping(CDOClass cdoClass);

  protected abstract List<CDOClass> getClassesWithObjectInfo();
}

Back to the top