summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2007-08-21 01:21:38 (EDT)
committerEike Stepper2007-08-21 01:21:38 (EDT)
commit4f85d1dd709ddd57d34e60974d6ddc2940208a1e (patch)
treeed77ae9bffe1026e53f01035a325c636b9191988
parentbdc1d09ad76802f1fd904e46f11be69723d63c51 (diff)
downloadcdo-4f85d1dd709ddd57d34e60974d6ddc2940208a1e.zip
cdo-4f85d1dd709ddd57d34e60974d6ddc2940208a1e.tar.gz
cdo-4f85d1dd709ddd57d34e60974d6ddc2940208a1e.tar.bz2
*** empty log message ***
-rw-r--r--plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/db/IMappingStrategy.java6
-rw-r--r--plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/HorizontalMappingStrategy.java124
-rw-r--r--plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/MappingStrategy.java303
-rw-r--r--plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/StandardMappingStrategy.java391
4 files changed, 409 insertions, 415 deletions
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/db/IMappingStrategy.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/db/IMappingStrategy.java
index 735319d..c6f828a 100644
--- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/db/IMappingStrategy.java
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/db/IMappingStrategy.java
@@ -40,12 +40,6 @@ public interface IMappingStrategy
public void setProperties(Map<String, String> properties);
- public ToManyReferenceMapping getToManyReferenceMapping();
-
- public ToOneReferenceMapping getToOneReferenceMapping();
-
- public MappingPrecedence getMappingPrecedence();
-
/**
* @return A collection of the affected tables.
*/
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/HorizontalMappingStrategy.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/HorizontalMappingStrategy.java
index 45fd2f2..0320084 100644
--- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/HorizontalMappingStrategy.java
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/HorizontalMappingStrategy.java
@@ -14,37 +14,26 @@ import org.eclipse.emf.cdo.internal.protocol.model.CDOClassImpl;
import org.eclipse.emf.cdo.internal.protocol.model.CDOFeatureImpl;
import org.eclipse.emf.cdo.internal.protocol.revision.CDORevisionImpl;
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.CDOFeature;
-import org.eclipse.emf.cdo.protocol.model.CDOPackage;
import org.eclipse.emf.cdo.protocol.revision.CDORevision;
-import org.eclipse.emf.cdo.server.IRepository;
-import org.eclipse.emf.cdo.server.db.ToManyReferenceMapping;
import org.eclipse.emf.cdo.server.internal.db.bundle.OM;
-import org.eclipse.net4j.db.DBType;
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 java.sql.Connection;
import java.sql.Date;
-import java.util.HashMap;
import java.util.Map;
-import java.util.Set;
import java.util.Map.Entry;
/**
* @author Eike Stepper
*/
-public class HorizontalMappingStrategy extends MappingStrategy
+public class HorizontalMappingStrategy extends StandardMappingStrategy
{
private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, HorizontalMappingStrategy.class);
- private Map<Object, IDBTable> referenceTables = new HashMap();
-
public HorizontalMappingStrategy()
{
}
@@ -54,117 +43,6 @@ public class HorizontalMappingStrategy extends MappingStrategy
return "horizontal";
}
- @Override
- protected IDBTable mapClass(CDOClass cdoClass, Set<IDBTable> affectedTables)
- {
- return null;
- }
-
- @Override
- protected IDBField mapFeature(CDOClass cdoClass, CDOFeature cdoFeature, Set<IDBTable> affectedTables)
- {
- if (cdoClass.isAbstract())
- {
- return null;
- }
-
- if (cdoFeature.isReference())
- {
- if (cdoFeature.isMany())
- {
- return mapReference(cdoClass, cdoFeature, getToManyReferenceMapping());
- }
- else
- {
- switch (getToOneReferenceMapping())
- {
- case LIKE_ATTRIBUTES:
- return mapAttribute(cdoClass, cdoFeature);
- case LIKE_TO_MANY_REFERENCES:
- return mapReference(cdoClass, cdoFeature, getToManyReferenceMapping());
- default:
- throw new IllegalArgumentException("Invalid mapping: " + getToOneReferenceMapping());
- }
- }
- }
- else
- {
- if (cdoFeature.isMany())
- {
- throw new UnsupportedOperationException();
- }
-
- return mapAttribute(cdoClass, cdoFeature);
- }
- }
-
- protected IDBField mapAttribute(CDOClass cdoClass, CDOFeature cdoFeature)
- {
- DBClassInfo classInfo = (DBClassInfo)cdoClass.getServerInfo();
- IDBTable table = classInfo.getTable();
- if (table == null)
- {
- table = addTable(cdoClass);
- initTable(table, true);
- classInfo.setTable(table);
- }
-
- return addField(cdoFeature, table);
- }
-
- protected IDBField mapReference(CDOClass cdoClass, CDOFeature cdoFeature, ToManyReferenceMapping mapping)
- {
- switch (mapping)
- {
- case ONE_TABLE_PER_REFERENCE:
- return mapReferenceTable(cdoFeature, cdoClass.getName() + "_" + cdoFeature.getName() + "_refs", false);
- case ONE_TABLE_PER_CLASS:
- return mapReferenceTable(cdoClass, cdoClass.getName() + "_refs", true);
- case ONE_TABLE_PER_PACKAGE:
- CDOPackage cdoPackage = cdoClass.getContainingPackage();
- return mapReferenceTable(cdoPackage, cdoPackage.getName() + "_refs", true);
- case ONE_TABLE_PER_REPOSITORY:
- IRepository repository = getStore().getRepository();
- return mapReferenceTable(repository, repository.getName() + "_refs", true);
- case LIKE_ATTRIBUTES:
- return mapReferenceSerialized(cdoClass, cdoFeature);
- default:
- throw new IllegalArgumentException("Invalid mapping: " + mapping);
- }
- }
-
- protected IDBField mapReferenceSerialized(CDOClass cdoClass, CDOFeature cdoFeature)
- {
- // TODO Implement method HorizontalMappingStrategy.mapReferenceSerialized()
- throw new UnsupportedOperationException("Not yet implemented");
- }
-
- protected IDBField mapReferenceTable(Object key, String tableName, boolean withFeature)
- {
- IDBTable table = referenceTables.get(key);
- if (table == null)
- {
- table = addReferenceTable(tableName, withFeature);
- referenceTables.put(key, table);
- }
-
- return table.getField(0);
- }
-
- protected IDBTable addReferenceTable(String tableName, boolean withFeature)
- {
- IDBTable table = addTable(tableName);
- if (withFeature)
- {
- table.addField("cdo_feature", DBType.INTEGER);
- }
-
- table.addField("cdo_idx", DBType.INTEGER);
- table.addField("cdo_source", DBType.BIGINT);
- table.addField("cdo_target", DBType.BIGINT);
- return table;
- }
-
public void writeRevision(Connection connection, CDORevisionImpl revision)
{
if (TRACER.isEnabled())
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/MappingStrategy.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/MappingStrategy.java
index 00e3f61..17aea2a 100644
--- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/MappingStrategy.java
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/MappingStrategy.java
@@ -10,15 +10,11 @@
**************************************************************************/
package org.eclipse.emf.cdo.server.internal.db;
-import org.eclipse.emf.cdo.internal.protocol.model.CDOPackageImpl;
import org.eclipse.emf.cdo.protocol.model.CDOClass;
import org.eclipse.emf.cdo.protocol.model.CDOFeature;
import org.eclipse.emf.cdo.protocol.model.CDOType;
import org.eclipse.emf.cdo.server.db.IDBStore;
import org.eclipse.emf.cdo.server.db.IMappingStrategy;
-import org.eclipse.emf.cdo.server.db.MappingPrecedence;
-import org.eclipse.emf.cdo.server.db.ToManyReferenceMapping;
-import org.eclipse.emf.cdo.server.db.ToOneReferenceMapping;
import org.eclipse.emf.cdo.server.internal.db.bundle.OM;
import org.eclipse.net4j.db.DBException;
@@ -29,17 +25,9 @@ import org.eclipse.net4j.db.IDBTable;
import org.eclipse.net4j.internal.db.DBSchema;
import org.eclipse.net4j.internal.util.om.trace.ContextTracer;
import org.eclipse.net4j.util.ImplementationError;
-import org.eclipse.net4j.util.ObjectUtil;
-import java.text.MessageFormat;
-import java.util.ArrayList;
-import java.util.Arrays;
import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
import java.util.Map;
-import java.util.Set;
-import java.util.Map.Entry;
/**
* @author Eike Stepper
@@ -56,14 +44,6 @@ public abstract class MappingStrategy implements IMappingStrategy
private IDBSchema schema;
- private MappingPrecedence mappingPrecedence;
-
- private ToManyReferenceMapping toManyReferenceMapping;
-
- private ToOneReferenceMapping toOneReferenceMapping;
-
- private Map<CDOClass, ClassMapping> classMappings = new HashMap();
-
public MappingStrategy()
{
}
@@ -95,61 +75,12 @@ public abstract class MappingStrategy implements IMappingStrategy
public IDBSchema getSchema()
{
- return schema;
- }
-
- public MappingPrecedence getMappingPrecedence()
- {
- if (mappingPrecedence == null)
- {
- mappingPrecedence = MappingPrecedence.STRATEGY;
- if (properties != null)
- {
- String value = properties.get("mappingPrecedence");
- if (value != null)
- {
- mappingPrecedence = MappingPrecedence.valueOf(value);
- }
- }
- }
-
- return mappingPrecedence;
- }
-
- public ToManyReferenceMapping getToManyReferenceMapping()
- {
- if (toManyReferenceMapping == null)
- {
- toManyReferenceMapping = ToManyReferenceMapping.ONE_TABLE_PER_REFERENCE;
- if (properties != null)
- {
- String value = properties.get("toManyReferenceMapping");
- if (value != null)
- {
- toManyReferenceMapping = ToManyReferenceMapping.valueOf(value);
- }
- }
- }
-
- return toManyReferenceMapping;
- }
-
- public ToOneReferenceMapping getToOneReferenceMapping()
- {
- if (toOneReferenceMapping == null)
+ if (schema == null)
{
- toOneReferenceMapping = ToOneReferenceMapping.LIKE_ATTRIBUTES;
- if (properties != null)
- {
- String value = properties.get("toOneReferenceMapping");
- if (value != null)
- {
- toOneReferenceMapping = ToOneReferenceMapping.valueOf(value);
- }
- }
+ schema = createSchema();
}
- return toOneReferenceMapping;
+ return schema;
}
@Override
@@ -158,116 +89,6 @@ public abstract class MappingStrategy implements IMappingStrategy
return getType();
}
- public Set<IDBTable> map(CDOPackageImpl[] cdoPackages)
- {
- // Lazily create the schema
- if (schema == null)
- {
- schema = createSchema();
- }
-
- // Prepare data structures
- Set<IDBTable> affectedTables = new HashSet();
- List<CDOClass> cdoClasses = new ArrayList();
-
- // Map all packages before classes are mapped
- for (CDOPackageImpl cdoPackage : cdoPackages)
- {
- ((DBPackageInfo)cdoPackage.getServerInfo()).setSchema(schema);
- if (TRACER.isEnabled())
- {
- TRACER.format("Mapped package: {0} --> {1}", cdoPackage, schema);
- }
- }
-
- // Map all classes before features are mapped
- for (CDOPackageImpl cdoPackage : cdoPackages)
- {
- if (TRACER.isEnabled())
- {
- TRACER.format("Mapping classes of package {0}", cdoPackage);
- }
-
- for (CDOClass cdoClass : cdoPackage.getClasses())
- {
- cdoClasses.add(cdoClass);
- IDBTable table = mapClass(cdoClass, affectedTables);
- if (table != null)
- {
- ((DBClassInfo)cdoClass.getServerInfo()).setTable(table);
- affectedTables.add(table);
- if (TRACER.isEnabled())
- {
- TRACER.format("Mapped class: {0} --> {1}", cdoClass, table);
- }
- }
- }
- }
-
- // Map all features
- for (CDOClass cdoClass : cdoClasses)
- {
- if (TRACER.isEnabled())
- {
- TRACER.format("Mapping features of class {0}", cdoClass);
- }
-
- for (CDOFeature cdoFeature : cdoClass.getAllFeatures())
- {
- IDBField field = mapFeature(cdoClass, cdoFeature, affectedTables);
- if (field != null)
- {
- ((DBFeatureInfo)cdoFeature.getServerInfo()).addField(cdoClass, field);
- affectedTables.add(field.getTable());
- if (TRACER.isEnabled())
- {
- TRACER.format("Mapped feature: {0} --> {1}", cdoFeature, field);
- }
- }
- }
- }
-
- return affectedTables;
- }
-
- /**
- * @param affectedTables
- * Can be used to indicate the creation or modification of additional
- * tables. There is no need to add the returned table to this set of
- * affected tables. The caller takes care of that.
- */
- protected abstract IDBTable mapClass(CDOClass cdoClass, Set<IDBTable> affectedTables);
-
- /**
- * @param affectedTables
- * Can be used to indicate the creation or modification of additional
- * tables. There is no need to add the table of the returned field to
- * this set of affected tables. The caller takes care of that.
- */
- protected abstract IDBField mapFeature(CDOClass cdoClass, CDOFeature cdoFeature, Set<IDBTable> affectedTables);
-
- protected ClassMapping getClassMapping(CDOClass cdoClass)
- {
- ClassMapping classMapping = classMappings.get(cdoClass);
- if (classMapping == null)
- {
- classMapping = new ClassMapping(cdoClass);
- classMappings.put(cdoClass, classMapping);
- for (CDOFeature feature : cdoClass.getAllFeatures())
- {
- DBFeatureInfo featureInfo = (DBFeatureInfo)feature.getServerInfo();
- IDBField field = featureInfo.getField(cdoClass);
- if (field != null)
- {
- classMapping.addFeatureMapping(feature, field);
- }
- }
- }
-
- classMapping.sortFeatureMappings();
- return classMapping;
- }
-
protected String mangleTableName(String name, int attempt)
{
return store.getDBAdapter().mangleTableName(name, attempt);
@@ -284,6 +105,20 @@ public abstract class MappingStrategy implements IMappingStrategy
return new DBSchema(name);
}
+ protected IDBTable addReferenceTable(String tableName, boolean withFeature)
+ {
+ IDBTable table = addTable(tableName);
+ if (withFeature)
+ {
+ table.addField("cdo_feature", DBType.INTEGER);
+ }
+
+ table.addField("cdo_idx", DBType.INTEGER);
+ table.addField("cdo_source", DBType.BIGINT);
+ table.addField("cdo_target", DBType.BIGINT);
+ return table;
+ }
+
protected int initTable(IDBTable table, boolean full)
{
table.addField("cdo_id", DBType.BIGINT);
@@ -397,108 +232,4 @@ public abstract class MappingStrategy implements IMappingStrategy
throw new ImplementationError("Unrecognized CDOType: " + type);
}
-
- /**
- * @author Eike Stepper
- */
- public static final class ClassMapping
- {
- private CDOClass cdoClass;
-
- private Map<IDBTable, FeatureMapping[]> tables;
-
- public ClassMapping(CDOClass cdoClass)
- {
- this.cdoClass = cdoClass;
- }
-
- public CDOClass getCdoClass()
- {
- return cdoClass;
- }
-
- public Map<IDBTable, FeatureMapping[]> getTables()
- {
- return tables;
- }
-
- public void addFeatureMapping(CDOFeature feature, IDBField field)
- {
- if (tables == null)
- {
- tables = new HashMap();
- }
-
- IDBTable table = field.getTable();
- FeatureMapping featureMapping = new FeatureMapping(feature, field);
- FeatureMapping[] featureMappings = tables.get(table);
- if (featureMappings == null)
- {
- FeatureMapping[] newFeatureMappings = { featureMapping };
- tables.put(table, newFeatureMappings);
- }
- else
- {
- FeatureMapping[] newFeatureMappings = ObjectUtil.appendtoArray(featureMappings, featureMapping);
- tables.put(table, newFeatureMappings);
- }
- }
-
- public void sortFeatureMappings()
- {
- for (Entry<IDBTable, FeatureMapping[]> entry : tables.entrySet())
- {
- FeatureMapping[] featureMappings = entry.getValue();
- Arrays.sort(featureMappings);
- }
- }
- }
-
- /**
- * @author Eike Stepper
- */
- public static final class FeatureMapping implements Comparable
- {
- private CDOFeature feature;
-
- private IDBField field;
-
- public FeatureMapping(CDOFeature feature, IDBField field)
- {
- this.feature = feature;
- this.field = field;
- }
-
- public CDOFeature getFeature()
- {
- return feature;
- }
-
- public IDBField getField()
- {
- return field;
- }
-
- public IDBTable getTable()
- {
- return field.getTable();
- }
-
- @Override
- public String toString()
- {
- return MessageFormat.format("FeatureMapping[{0}, {1}]", feature, field);
- }
-
- public int compareTo(Object o)
- {
- if (o instanceof FeatureMapping)
- {
- FeatureMapping that = (FeatureMapping)o;
- return new Integer(field.getPosition()).compareTo(that.getField().getPosition());
- }
-
- throw new IllegalArgumentException("Not a FeatureMapping: " + o);
- }
- }
} \ No newline at end of file
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/StandardMappingStrategy.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/StandardMappingStrategy.java
new file mode 100644
index 0000000..8125730
--- /dev/null
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/StandardMappingStrategy.java
@@ -0,0 +1,391 @@
+/***************************************************************************
+ * 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.model.CDOPackageImpl;
+import org.eclipse.emf.cdo.protocol.model.CDOClass;
+import org.eclipse.emf.cdo.protocol.model.CDOFeature;
+import org.eclipse.emf.cdo.protocol.model.CDOPackage;
+import org.eclipse.emf.cdo.server.IRepository;
+import org.eclipse.emf.cdo.server.db.MappingPrecedence;
+import org.eclipse.emf.cdo.server.db.ToManyReferenceMapping;
+import org.eclipse.emf.cdo.server.db.ToOneReferenceMapping;
+import org.eclipse.emf.cdo.server.internal.db.bundle.OM;
+
+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.ObjectUtil;
+
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+
+/**
+ * @author Eike Stepper
+ */
+public abstract class StandardMappingStrategy extends MappingStrategy
+{
+ private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, StandardMappingStrategy.class);
+
+ private MappingPrecedence mappingPrecedence;
+
+ private ToManyReferenceMapping toManyReferenceMapping;
+
+ private ToOneReferenceMapping toOneReferenceMapping;
+
+ private Map<CDOClass, ClassMapping> classMappings = new HashMap();
+
+ private Map<Object, IDBTable> referenceTables = new HashMap();
+
+ public StandardMappingStrategy()
+ {
+ }
+
+ public MappingPrecedence getMappingPrecedence()
+ {
+ if (mappingPrecedence == null)
+ {
+ mappingPrecedence = MappingPrecedence.STRATEGY;
+ String value = getProperties().get("mappingPrecedence");
+ if (value != null)
+ {
+ mappingPrecedence = MappingPrecedence.valueOf(value);
+ }
+ }
+
+ return mappingPrecedence;
+ }
+
+ public ToManyReferenceMapping getToManyReferenceMapping()
+ {
+ if (toManyReferenceMapping == null)
+ {
+ toManyReferenceMapping = ToManyReferenceMapping.ONE_TABLE_PER_REFERENCE;
+ String value = getProperties().get("toManyReferenceMapping");
+ if (value != null)
+ {
+ toManyReferenceMapping = ToManyReferenceMapping.valueOf(value);
+ }
+ }
+
+ return toManyReferenceMapping;
+ }
+
+ public ToOneReferenceMapping getToOneReferenceMapping()
+ {
+ if (toOneReferenceMapping == null)
+ {
+ toOneReferenceMapping = ToOneReferenceMapping.LIKE_ATTRIBUTES;
+ String value = getProperties().get("toOneReferenceMapping");
+ if (value != null)
+ {
+ toOneReferenceMapping = ToOneReferenceMapping.valueOf(value);
+ }
+ }
+
+ return toOneReferenceMapping;
+ }
+
+ public Set<IDBTable> map(CDOPackageImpl[] cdoPackages)
+ {
+ // Prepare data structures
+ Set<IDBTable> affectedTables = new HashSet();
+ List<CDOClass> cdoClasses = new ArrayList();
+
+ // Map all packages before classes are mapped
+ for (CDOPackageImpl cdoPackage : cdoPackages)
+ {
+ ((DBPackageInfo)cdoPackage.getServerInfo()).setSchema(getSchema());
+ if (TRACER.isEnabled())
+ {
+ TRACER.format("Mapped package: {0} --> {1}", cdoPackage, getSchema());
+ }
+ }
+
+ // Map all classes before features are mapped
+ // for (CDOPackageImpl cdoPackage : cdoPackages)
+ // {
+ // if (TRACER.isEnabled())
+ // {
+ // TRACER.format("Mapping classes of package {0}", cdoPackage);
+ // }
+ //
+ // for (CDOClass cdoClass : cdoPackage.getClasses())
+ // {
+ // cdoClasses.add(cdoClass);
+ // IDBTable table = mapClass(cdoClass, affectedTables);
+ // if (table != null)
+ // {
+ // ((DBClassInfo)cdoClass.getServerInfo()).setTable(table);
+ // affectedTables.add(table);
+ // if (TRACER.isEnabled())
+ // {
+ // TRACER.format("Mapped class: {0} --> {1}", cdoClass, table);
+ // }
+ // }
+ // }
+ // }
+
+ // Map all features
+ for (CDOClass cdoClass : cdoClasses)
+ {
+ if (TRACER.isEnabled())
+ {
+ TRACER.format("Mapping features of class {0}", cdoClass);
+ }
+
+ for (CDOFeature cdoFeature : cdoClass.getAllFeatures())
+ {
+ IDBField field = mapFeature(cdoClass, cdoFeature, affectedTables);
+ if (field != null)
+ {
+ ((DBFeatureInfo)cdoFeature.getServerInfo()).addField(cdoClass, field);
+ affectedTables.add(field.getTable());
+ if (TRACER.isEnabled())
+ {
+ TRACER.format("Mapped feature: {0} --> {1}", cdoFeature, field);
+ }
+ }
+ }
+ }
+
+ return affectedTables;
+ }
+
+ /**
+ * @param affectedTables
+ * Can be used to indicate the creation or modification of additional
+ * tables. There is no need to add the table of the returned field to
+ * this set of affected tables. The caller takes care of that.
+ */
+ protected IDBField mapFeature(CDOClass cdoClass, CDOFeature cdoFeature, Set<IDBTable> affectedTables)
+ {
+ if (cdoClass.isAbstract())
+ {
+ return null;
+ }
+
+ if (cdoFeature.isReference())
+ {
+ if (cdoFeature.isMany())
+ {
+ return mapReference(cdoClass, cdoFeature, getToManyReferenceMapping());
+ }
+ else
+ {
+ switch (getToOneReferenceMapping())
+ {
+ case LIKE_ATTRIBUTES:
+ return mapAttribute(cdoClass, cdoFeature);
+ case LIKE_TO_MANY_REFERENCES:
+ return mapReference(cdoClass, cdoFeature, getToManyReferenceMapping());
+ default:
+ throw new IllegalArgumentException("Invalid mapping: " + getToOneReferenceMapping());
+ }
+ }
+ }
+ else
+ {
+ if (cdoFeature.isMany())
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ return mapAttribute(cdoClass, cdoFeature);
+ }
+ }
+
+ protected IDBField mapAttribute(CDOClass cdoClass, CDOFeature cdoFeature)
+ {
+ DBClassInfo classInfo = (DBClassInfo)cdoClass.getServerInfo();
+ IDBTable table = classInfo.getTable();
+ if (table == null)
+ {
+ table = addTable(cdoClass);
+ initTable(table, true);
+ classInfo.setTable(table);
+ }
+
+ return addField(cdoFeature, table);
+ }
+
+ protected IDBField mapReference(CDOClass cdoClass, CDOFeature cdoFeature, ToManyReferenceMapping mapping)
+ {
+ switch (mapping)
+ {
+ case ONE_TABLE_PER_REFERENCE:
+ return mapReferenceTable(cdoFeature, cdoClass.getName() + "_" + cdoFeature.getName() + "_refs", false);
+ case ONE_TABLE_PER_CLASS:
+ return mapReferenceTable(cdoClass, cdoClass.getName() + "_refs", true);
+ case ONE_TABLE_PER_PACKAGE:
+ CDOPackage cdoPackage = cdoClass.getContainingPackage();
+ return mapReferenceTable(cdoPackage, cdoPackage.getName() + "_refs", true);
+ case ONE_TABLE_PER_REPOSITORY:
+ IRepository repository = getStore().getRepository();
+ return mapReferenceTable(repository, repository.getName() + "_refs", true);
+ case LIKE_ATTRIBUTES:
+ return mapReferenceSerialized(cdoClass, cdoFeature);
+ default:
+ throw new IllegalArgumentException("Invalid mapping: " + mapping);
+ }
+ }
+
+ protected IDBField mapReferenceSerialized(CDOClass cdoClass, CDOFeature cdoFeature)
+ {
+ // TODO Implement method HorizontalMappingStrategy.mapReferenceSerialized()
+ throw new UnsupportedOperationException("Not yet implemented");
+ }
+
+ protected IDBField mapReferenceTable(Object key, String tableName, boolean withFeature)
+ {
+ IDBTable table = referenceTables.get(key);
+ if (table == null)
+ {
+ table = addReferenceTable(tableName, withFeature);
+ referenceTables.put(key, table);
+ }
+
+ return table.getField(0);
+ }
+
+ protected ClassMapping getClassMapping(CDOClass cdoClass)
+ {
+ ClassMapping classMapping = classMappings.get(cdoClass);
+ if (classMapping == null)
+ {
+ classMapping = new ClassMapping(cdoClass);
+ classMappings.put(cdoClass, classMapping);
+ for (CDOFeature feature : cdoClass.getAllFeatures())
+ {
+ DBFeatureInfo featureInfo = (DBFeatureInfo)feature.getServerInfo();
+ IDBField field = featureInfo.getField(cdoClass);
+ if (field != null)
+ {
+ classMapping.addFeatureMapping(feature, field);
+ }
+ }
+ }
+
+ classMapping.sortFeatureMappings();
+ return classMapping;
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ public static final class ClassMapping
+ {
+ private CDOClass cdoClass;
+
+ private Map<IDBTable, FeatureMapping[]> tables;
+
+ public ClassMapping(CDOClass cdoClass)
+ {
+ this.cdoClass = cdoClass;
+ }
+
+ public CDOClass getCdoClass()
+ {
+ return cdoClass;
+ }
+
+ public Map<IDBTable, FeatureMapping[]> getTables()
+ {
+ return tables;
+ }
+
+ public void addFeatureMapping(CDOFeature feature, IDBField field)
+ {
+ if (tables == null)
+ {
+ tables = new HashMap();
+ }
+
+ IDBTable table = field.getTable();
+ FeatureMapping featureMapping = new FeatureMapping(feature, field);
+ FeatureMapping[] featureMappings = tables.get(table);
+ if (featureMappings == null)
+ {
+ FeatureMapping[] newFeatureMappings = { featureMapping };
+ tables.put(table, newFeatureMappings);
+ }
+ else
+ {
+ FeatureMapping[] newFeatureMappings = ObjectUtil.appendtoArray(featureMappings, featureMapping);
+ tables.put(table, newFeatureMappings);
+ }
+ }
+
+ public void sortFeatureMappings()
+ {
+ for (Entry<IDBTable, FeatureMapping[]> entry : tables.entrySet())
+ {
+ FeatureMapping[] featureMappings = entry.getValue();
+ Arrays.sort(featureMappings);
+ }
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ public static final class FeatureMapping implements Comparable
+ {
+ private CDOFeature feature;
+
+ private IDBField field;
+
+ public FeatureMapping(CDOFeature feature, IDBField field)
+ {
+ this.feature = feature;
+ this.field = field;
+ }
+
+ public CDOFeature getFeature()
+ {
+ return feature;
+ }
+
+ public IDBField getField()
+ {
+ return field;
+ }
+
+ public IDBTable getTable()
+ {
+ return field.getTable();
+ }
+
+ @Override
+ public String toString()
+ {
+ return MessageFormat.format("FeatureMapping[{0}, {1}]", feature, field);
+ }
+
+ public int compareTo(Object o)
+ {
+ if (o instanceof FeatureMapping)
+ {
+ FeatureMapping that = (FeatureMapping)o;
+ return new Integer(field.getPosition()).compareTo(that.getField().getPosition());
+ }
+
+ throw new IllegalArgumentException("Not a FeatureMapping: " + o);
+ }
+ }
+} \ No newline at end of file