Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ca297bf59314c27b8011ccd895465cc7d791904e (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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
 * Copyright (c) 2010-2013, 2015 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:
 *    Stefan Winkler - initial API and implementation
 *    Stefan Winkler - Bug 285426: [DB] Implement user-defined typeMapping support
 */
package org.eclipse.emf.cdo.server.internal.db.mapping;

import org.eclipse.emf.cdo.common.model.CDOModelUtil;
import org.eclipse.emf.cdo.etypes.EtypesPackage;
import org.eclipse.emf.cdo.server.db.IIDHandler;
import org.eclipse.emf.cdo.server.db.mapping.ColumnTypeModifier;
import org.eclipse.emf.cdo.server.db.mapping.IMappingStrategy;
import org.eclipse.emf.cdo.server.db.mapping.ITypeMapping;
import org.eclipse.emf.cdo.server.db.mapping.ITypeMapping.Provider;
import org.eclipse.emf.cdo.server.internal.db.DBAnnotation;
import org.eclipse.emf.cdo.server.internal.db.bundle.OM;
import org.eclipse.emf.cdo.server.internal.db.mapping.TypeMappingUtil.FactoryTypeParserException;
import org.eclipse.emf.cdo.server.internal.db.messages.Messages;

import org.eclipse.net4j.db.DBType;
import org.eclipse.net4j.db.IDBAdapter;
import org.eclipse.net4j.util.collection.Pair;
import org.eclipse.net4j.util.container.ContainerEvent;
import org.eclipse.net4j.util.container.FactoryNotFoundException;
import org.eclipse.net4j.util.container.IContainerDelta;
import org.eclipse.net4j.util.container.IContainerDelta.Kind;
import org.eclipse.net4j.util.container.IManagedContainer;
import org.eclipse.net4j.util.container.IPluginContainer;
import org.eclipse.net4j.util.event.IEvent;
import org.eclipse.net4j.util.event.IListener;
import org.eclipse.net4j.util.factory.IFactory;
import org.eclipse.net4j.util.factory.IFactoryKey;
import org.eclipse.net4j.util.factory.ProductCreationException;
import org.eclipse.net4j.util.om.trace.ContextTracer;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EEnum;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;

import java.text.MessageFormat;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * An implementation of both the Registry and Provider interfaces for type mappings. This class is a singleton which
 * keeps itself in sync with the global factory registry. It reads the available factoryTypes for the type mappings
 * product type and populates indexes which make it easier to determine and look up the correct factories for a needed
 * type mapping.
 *
 * @author Stefan Winkler
 */
public class TypeMappingRegistry implements ITypeMapping.Registry, ITypeMapping.Provider
{
  private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, TypeMappingRegistry.class);

  /**
   * Contains a map from model types to db types which represent default mappings. (I.e., if a model element without db
   * type annotation is encountered, this map is consulted to retrieve the default type mapping. This map is populated
   * on a come-first basis. The first mapping for a particular {@link EClassifier} is set as default.
   */
  private Map<EClassifier, DBType> classifierDefaultMapping;

  /**
   * The main TypeMapping index. For any known pair of model and db types the {@link ITypeMapping.Descriptor} is
   * registered here.
   */
  private Map<Pair<EClassifier, DBType>, ITypeMapping.Descriptor> typeMappingByTypes;

  /**
   * ID-based index. Can be used to lookup an {@link ITypeMapping.Descriptor} for a given ID.
   */
  private Map<String, ITypeMapping.Descriptor> typeMappingsById;

  /**
   * A set of all known mapped DBTypes. This is needed for the feature map mappings.
   */
  private Set<DBType> defaultFeatureMapDBTypes;

  /**
   * A populator which is used to keep the registry in sync with the registered factories of the
   * {@link IManagedContainer}.
   */
  private RegistryPopulator populator = new RegistryPopulator();

  public TypeMappingRegistry()
  {
    init();
  }

  public void init()
  {
    populator.disconnect();

    registerColumnTypeModifier("postgresql", new ColumnTypeModifier()
    {
      @Override
      public DBType modify(Provider provider, IMappingStrategy mappingStrategy, EStructuralFeature feature,
          DBType dbType)
      {
        EClassifier classifier = feature.getEType();
        if (classifier == EcorePackage.eINSTANCE.getEChar())
        {
          return DBType.INTEGER;
        }

        if (classifier == EcorePackage.eINSTANCE.getECharacterObject())
        {
          return DBType.INTEGER;
        }

        return dbType;
      }
    });

    defaultFeatureMapDBTypes = new HashSet<DBType>();
    typeMappingsById = new HashMap<String, ITypeMapping.Descriptor>();
    typeMappingByTypes = new HashMap<Pair<EClassifier, DBType>, ITypeMapping.Descriptor>();
    classifierDefaultMapping = new HashMap<EClassifier, DBType>();

    registerCoreTypeMappings();
    populator.connect();
  }

  public void registerColumnTypeModifier(String factoryType, final ColumnTypeModifier columnTypeModifier)
  {
    getContainer().registerFactory(new ColumnTypeModifier.Factory(factoryType)
    {
      @Override
      public ColumnTypeModifier create(String description) throws ProductCreationException
      {
        return columnTypeModifier;
      }
    });
  }

  /**
   * Register builtin type mapping factories
   */
  private void registerCoreTypeMappings()
  {
    IManagedContainer container = getContainer();
    container.registerFactory(CoreTypeMappings.TMBigDecimal.FACTORY);
    container.registerFactory(CoreTypeMappings.TMBigDecimal.FACTORY_LONG_VARCHAR);
    container.registerFactory(CoreTypeMappings.TMBigInteger.FACTORY);
    container.registerFactory(CoreTypeMappings.TMBigInteger.FACTORY_LONG_VARCHAR);
    container.registerFactory(CoreTypeMappings.TMBoolean.FACTORY);
    container.registerFactory(CoreTypeMappings.TMBoolean.FACTORY_SMALLINT);
    container.registerFactory(CoreTypeMappings.TMBoolean.FACTORY_OBJECT);
    container.registerFactory(CoreTypeMappings.TMBoolean.FACTORY_OBJECT_SMALLINT);
    container.registerFactory(CoreTypeMappings.TMByte.FACTORY);
    container.registerFactory(CoreTypeMappings.TMByte.FACTORY_OBJECT);
    container.registerFactory(CoreTypeMappings.TMBytes.FACTORY);
    container.registerFactory(CoreTypeMappings.TMBytesVarbinary.FACTORY);
    container.registerFactory(CoreTypeMappings.TMCharacter.FACTORY);
    container.registerFactory(CoreTypeMappings.TMCharacter.FACTORY_OBJECT);
    container.registerFactory(CoreTypeMappings.TMCharacter2Integer.FACTORY);
    container.registerFactory(CoreTypeMappings.TMCharacter2Integer.FACTORY_OBJECT);
    container.registerFactory(CoreTypeMappings.TMCustom.FACTORY_VARCHAR);
    container.registerFactory(CoreTypeMappings.TMCustom.FACTORY_CLOB);
    container.registerFactory(CoreTypeMappings.TMCustom.FACTORY_LONG_VARCHAR);
    container.registerFactory(CoreTypeMappings.TMDate2Date.FACTORY);
    container.registerFactory(CoreTypeMappings.TMDate2Time.FACTORY);
    container.registerFactory(CoreTypeMappings.TMDate2Timestamp.FACTORY);
    container.registerFactory(CoreTypeMappings.TMDouble.FACTORY);
    container.registerFactory(CoreTypeMappings.TMDouble.FACTORY_OBJECT);
    container.registerFactory(CoreTypeMappings.TMEnum.FACTORY);
    container.registerFactory(CoreTypeMappings.TMFloat.FACTORY);
    container.registerFactory(CoreTypeMappings.TMFloat.FACTORY_OBJECT);
    container.registerFactory(CoreTypeMappings.TMInteger.FACTORY);
    container.registerFactory(CoreTypeMappings.TMInteger.FACTORY_OBJECT);
    container.registerFactory(CoreTypeMappings.TMLong.FACTORY);
    container.registerFactory(CoreTypeMappings.TMLong.FACTORY_OBJECT);
    container.registerFactory(CoreTypeMappings.TMShort.FACTORY);
    container.registerFactory(CoreTypeMappings.TMShort.FACTORY_OBJECT);
    container.registerFactory(CoreTypeMappings.TMString.FACTORY_VARCHAR);
    container.registerFactory(CoreTypeMappings.TMString.FACTORY_CLOB);
    container.registerFactory(CoreTypeMappings.TMString.FACTORY_LONG_VARCHAR);
    container.registerFactory(CoreTypeMappings.TMJavaClass.FACTORY_VARCHAR);
    container.registerFactory(CoreTypeMappings.TMJavaObject.FACTORY);
    container.registerFactory(CoreTypeMappings.TMBlob.FACTORY_VARCHAR);
    container.registerFactory(CoreTypeMappings.TMBlob.FACTORY_LONG_VARCHAR);
    container.registerFactory(CoreTypeMappings.TMClob.FACTORY_VARCHAR);
    container.registerFactory(CoreTypeMappings.TMClob.FACTORY_LONG_VARCHAR);

    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEDataType(), DBType.VARCHAR);

    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEBigDecimal(), DBType.VARCHAR);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEBigInteger(), DBType.VARCHAR);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEBoolean(), DBType.BOOLEAN);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEBooleanObject(), DBType.BOOLEAN);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEByte(), DBType.SMALLINT);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEByteObject(), DBType.SMALLINT);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEByteArray(), DBType.BLOB);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEChar(), DBType.CHAR);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getECharacterObject(), DBType.CHAR);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEDate(), DBType.TIMESTAMP);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEDouble(), DBType.DOUBLE);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEDoubleObject(), DBType.DOUBLE);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEEnum(), DBType.INTEGER);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEFloat(), DBType.FLOAT);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEFloatObject(), DBType.FLOAT);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEInt(), DBType.INTEGER);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEIntegerObject(), DBType.INTEGER);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getELong(), DBType.BIGINT);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getELongObject(), DBType.BIGINT);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEShort(), DBType.SMALLINT);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEShortObject(), DBType.SMALLINT);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEString(), DBType.VARCHAR);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEJavaClass(), DBType.VARCHAR);
    classifierDefaultMapping.put(EcorePackage.eINSTANCE.getEJavaObject(), DBType.BLOB);

    classifierDefaultMapping.put(EtypesPackage.eINSTANCE.getBlob(), DBType.VARCHAR); // TODO Should be DBType.BLOB?
    classifierDefaultMapping.put(EtypesPackage.eINSTANCE.getClob(), DBType.VARCHAR); // TODO Should be DBType.CLOB?
  }

  protected IManagedContainer getContainer()
  {
    return IPluginContainer.INSTANCE;
  }

  public void registerTypeMapping(ITypeMapping.Descriptor descriptor)
  {
    if (TRACER.isEnabled())
    {
      TRACER.format("Registering {0}", descriptor);
    }

    EClassifier eClassifier = descriptor.getEClassifier();
    DBType dbType = descriptor.getDBType();
    Pair<EClassifier, DBType> sourceTargetPair = Pair.create(eClassifier, dbType);

    // currently we do not support more than one typeMapping per source-target type pair
    if (typeMappingByTypes.containsKey(sourceTargetPair))
    {
      OM.LOG.error(Messages.getString("TypeMappingRegistry.4"));
      return;
    }

    if (typeMappingsById.containsKey(descriptor.getID()))
    {
      OM.LOG.error(MessageFormat.format(Messages.getString("TypeMappingRegistry.5"), descriptor.getID()));
      return;
    }

    typeMappingsById.put(descriptor.getID(), descriptor);

    // register first dbType for classifier as default
    if (!classifierDefaultMapping.containsKey(eClassifier))
    {
      classifierDefaultMapping.put(eClassifier, dbType);
    }

    defaultFeatureMapDBTypes.add(dbType);

    typeMappingByTypes.put(sourceTargetPair, descriptor);
  }

  public ITypeMapping createTypeMapping(IMappingStrategy mappingStrategy, EStructuralFeature feature)
  {
    ITypeMapping typeMapping = null;
    if (feature instanceof EReference)
    {
      IIDHandler idHandler = mappingStrategy.getStore().getIDHandler();
      typeMapping = idHandler.getObjectTypeMapping();
      typeMapping.setDBType(idHandler.getDBType());
    }
    else
    {
      DBType dbType = getDBType(mappingStrategy, feature);

      ITypeMapping.Descriptor descriptor = null;

      String typeMappingID = DBAnnotation.TYPE_MAPPING.getValue(feature);
      if (typeMappingID != null)
      {
        // lookup annotated mapping
        descriptor = typeMappingsById.get(typeMappingID);

        if (descriptor == null)
        {
          OM.LOG.warn(MessageFormat.format(Messages.getString("TypeMappingRegistry.2"), //
              typeMappingID, feature.toString()));
        }
      }

      if (descriptor == null)
      {
        // try to find suitable mapping by type
        descriptor = getMappingByType(feature, dbType);
      }

      if (descriptor == null)
      {
        EClassifier type = getEType(feature);
        throw new IllegalStateException(MessageFormat.format(Messages.getString("TypeMappingRegistry.1"),
            feature.getEContainingClass().getName() + "." + feature.getName(),
            type.getEPackage().getName() + "." + type.getName(), dbType.getKeyword()));
      }

      IFactory factory = getContainer().getFactory(ITypeMapping.Factory.PRODUCT_GROUP, descriptor.getFactoryType());
      typeMapping = (ITypeMapping)factory.create(null);
      typeMapping.setDBType(dbType);
    }

    typeMapping.setMappingStrategy(mappingStrategy);
    typeMapping.setFeature(feature);

    return typeMapping;
  }

  private EClassifier getEType(EStructuralFeature feature)
  {
    EClassifier classifier = feature.getEType();
    if (classifier instanceof EEnum)
    {
      return EcorePackage.eINSTANCE.getEEnum();
    }

    if (classifier instanceof EClass)
    {
      return EcorePackage.eINSTANCE.getEClass();
    }

    EPackage ePackage = classifier.getEPackage();
    if (CDOModelUtil.isCorePackage(ePackage))
    {
      return classifier;
    }

    if (CDOModelUtil.isTypesPackage(ePackage))
    {
      return classifier;
    }

    return EcorePackage.eINSTANCE.getEDataType();
  }

  private DBType getDBType(IMappingStrategy mappingStrategy, EStructuralFeature feature)
  {
    DBType dbType;

    String typeKeyword = DBAnnotation.COLUMN_TYPE.getValue(feature);
    if (typeKeyword != null)
    {
      dbType = DBType.getTypeByKeyword(typeKeyword);
      if (dbType == null)
      {
        throw new IllegalArgumentException(
            "Unsupported columnType (" + typeKeyword + ") annotation of feature " + feature.getName());
      }
    }
    else
    {
      // No annotation present - lookup default DB type.
      IDBAdapter dbAdapter = mappingStrategy.getStore().getDBAdapter();
      dbType = getDefaultDBType(getEType(feature), dbAdapter);
    }

    ColumnTypeModifier columnTypeModifier = getColumnTypeModifier(mappingStrategy);
    if (columnTypeModifier != null)
    {
      dbType = columnTypeModifier.modify(this, mappingStrategy, feature, dbType);
    }

    return dbType;
  }

  private ColumnTypeModifier getColumnTypeModifier(IMappingStrategy mappingStrategy)
  {
    String factoryType = mappingStrategy.getProperties().get(IMappingStrategy.PROP_COLUMN_TYPE_MODIFIER);
    if (factoryType == null)
    {
      factoryType = mappingStrategy.getStore().getDBAdapter().getName();
    }

    ColumnTypeModifier columnTypeModifier = null;

    try
    {
      columnTypeModifier = (ColumnTypeModifier)IPluginContainer.INSTANCE
          .getElement(ColumnTypeModifier.Factory.PRODUCT_GROUP, factoryType, null);
    }
    catch (FactoryNotFoundException ex)
    {
      //$FALL-THROUGH$
    }
    catch (ProductCreationException ex)
    {
      //$FALL-THROUGH$
    }
    return columnTypeModifier;
  }

  private DBType getDefaultDBType(EClassifier type, IDBAdapter dbAdapter)
  {
    DBType result = classifierDefaultMapping.get(type);

    if (result == null)
    {
      result = DBType.VARCHAR;
    }

    // Give the DBAdapter a chance to override the default type, if it's not supported
    return dbAdapter.adaptType(result);
  }

  private ITypeMapping.Descriptor getMappingByType(EStructuralFeature feature, DBType dbType)
  {
    // First try: lookup specific mapping for the immediate type.
    ITypeMapping.Descriptor descriptor = typeMappingByTypes.get(Pair.create(feature.getEType(), dbType));

    if (descriptor == null)
    {
      // Second try: lookup general mapping
      descriptor = typeMappingByTypes.get(Pair.create(getEType(feature), dbType));
      if (descriptor == null)
      {
        // Lookup failed. Give up
        return null;
      }
    }

    return descriptor;
  }

  public Collection<DBType> getDefaultFeatureMapDBTypes()
  {
    return defaultFeatureMapDBTypes;
  }

  /**
   * Keeps the {@link TypeMappingRegistry} in sync with {@link IManagedContainer#getFactoryRegistry()}.
   *
   * @author Stefan Winkler
   */
  private class RegistryPopulator implements IListener
  {
    private IManagedContainer container = getContainer();

    public RegistryPopulator()
    {
    }

    /**
     * Connect to the factory registry.
     */
    public void connect()
    {
      populateTypeMappingRegistry();
      container.getFactoryRegistry().addListener(this);
    }

    public void disconnect()
    {
      container.getFactoryRegistry().removeListener(this);
    }

    private void populateTypeMappingRegistry()
    {
      // Get available factory types
      Set<String> factoryTypes = container.getFactoryTypes(ITypeMapping.Factory.PRODUCT_GROUP);

      // Parse the descriptor of each factory type
      for (String factoryType : factoryTypes)
      {
        registerFactoryType(factoryType);
      }
    }

    private void registerFactoryType(String factoryType)
    {
      ITypeMapping.Descriptor desc;

      try
      {
        desc = TypeMappingUtil.descriptorFromFactoryType(factoryType);
        registerTypeMapping(desc);
      }
      catch (FactoryTypeParserException ex)
      {
        OM.LOG.warn(ex);
      }
    }

    public void notifyEvent(IEvent event)
    {
      if (event instanceof ContainerEvent<?>)
      {
        @SuppressWarnings("unchecked")
        ContainerEvent<Map.Entry<IFactoryKey, IFactory>> ev = (ContainerEvent<Entry<IFactoryKey, IFactory>>)event;

        for (IContainerDelta<Map.Entry<IFactoryKey, IFactory>> delta : ev.getDeltas())
        {
          IFactoryKey key = delta.getElement().getKey();
          String productGroup = key.getProductGroup();
          if (productGroup.equals(ITypeMapping.Factory.PRODUCT_GROUP))
          {
            if (delta.getKind() == Kind.ADDED)
            {
              String factoryType = delta.getElement().getKey().getType();
              registerFactoryType(factoryType);
            }
            else
              // delta.getKind() == Kind.REMOVED
            {
              // XXX Runtime removal of typeMappingFactories removal of type mappings is currently not supported.
              OM.LOG.warn(Messages.getString("TypeMappingRegistry.3"));
            }
          }
        }
      }
    }
  }
}

Back to the top