diff options
5 files changed, 328 insertions, 51 deletions
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/AttributeMapping.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/AttributeMapping.java index 74a5e85e6c..7331fef0a0 100644 --- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/AttributeMapping.java +++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/AttributeMapping.java @@ -1,13 +1,17 @@ package org.eclipse.emf.cdo.server.internal.db; +import org.eclipse.emf.cdo.internal.protocol.CDOIDImpl; import org.eclipse.emf.cdo.internal.protocol.revision.CDORevisionImpl; import org.eclipse.emf.cdo.protocol.model.CDOFeature; import org.eclipse.emf.cdo.server.db.IAttributeMapping; +import org.eclipse.net4j.db.DBException; import org.eclipse.net4j.db.IDBAdapter; import org.eclipse.net4j.db.IDBField; +import org.eclipse.net4j.util.ImplementationError; import java.sql.ResultSet; +import java.sql.SQLException; /** * @author Eike Stepper @@ -42,8 +46,226 @@ public abstract class AttributeMapping extends FeatureMapping implements IAttrib public void extractValue(ResultSet resultSet, int column, CDORevisionImpl revision) { - revision.setValue(getFeature(), getResultSetValue(resultSet, column)); + if (column != field.getPosition() + 1) + { + throw new ImplementationError("Column mismatch"); + } + + try + { + Object value = getResultSetValue(resultSet, column); + if (resultSet.wasNull()) + { + value = null; + } + + revision.setValue(getFeature(), value); + } + catch (SQLException ex) + { + throw new DBException(ex); + } + } + + protected abstract Object getResultSetValue(ResultSet resultSet, int column) throws SQLException; + + /** + * @author Eike Stepper + */ + public static class AMString extends AttributeMapping + { + public AMString(ValueMapping valueMapping, CDOFeature feature) + { + super(valueMapping, feature); + } + + @Override + protected Object getResultSetValue(ResultSet resultSet, int column) throws SQLException + { + return resultSet.getString(column); + } + } + + /** + * @author Eike Stepper + */ + public static class AMShort extends AttributeMapping + { + public AMShort(ValueMapping valueMapping, CDOFeature feature) + { + super(valueMapping, feature); + } + + @Override + protected Object getResultSetValue(ResultSet resultSet, int column) throws SQLException + { + return resultSet.getShort(column); + } + } + + /** + * @author Eike Stepper + */ + public static class AMObject extends AttributeMapping + { + public AMObject(ValueMapping valueMapping, CDOFeature feature) + { + super(valueMapping, feature); + } + + @Override + protected Object getResultSetValue(ResultSet resultSet, int column) throws SQLException + { + long id = resultSet.getLong(column); + if (resultSet.wasNull()) + { + return null; + } + + return CDOIDImpl.create(id); + } + } + + /** + * @author Eike Stepper + */ + public static class AMLong extends AttributeMapping + { + public AMLong(ValueMapping valueMapping, CDOFeature feature) + { + super(valueMapping, feature); + } + + @Override + protected Object getResultSetValue(ResultSet resultSet, int column) throws SQLException + { + return resultSet.getLong(column); + } + } + + /** + * @author Eike Stepper + */ + public static class AMInteger extends AttributeMapping + { + public AMInteger(ValueMapping valueMapping, CDOFeature feature) + { + super(valueMapping, feature); + } + + @Override + protected Object getResultSetValue(ResultSet resultSet, int column) throws SQLException + { + return resultSet.getInt(column); + } } - protected abstract Object getResultSetValue(ResultSet resultSet, int column); + /** + * @author Eike Stepper + */ + public static class AMFloat extends AttributeMapping + { + public AMFloat(ValueMapping valueMapping, CDOFeature feature) + { + super(valueMapping, feature); + } + + @Override + protected Object getResultSetValue(ResultSet resultSet, int column) throws SQLException + { + return resultSet.getFloat(column); + } + } + + /** + * @author Eike Stepper + */ + public static class AMDouble extends AttributeMapping + { + public AMDouble(ValueMapping valueMapping, CDOFeature feature) + { + super(valueMapping, feature); + } + + @Override + protected Object getResultSetValue(ResultSet resultSet, int column) throws SQLException + { + return resultSet.getDouble(column); + } + } + + /** + * @author Eike Stepper + */ + public static class AMDate extends AttributeMapping + { + public AMDate(ValueMapping valueMapping, CDOFeature feature) + { + super(valueMapping, feature); + } + + @Override + protected Object getResultSetValue(ResultSet resultSet, int column) throws SQLException + { + // TODO Is getDate() correct? + return resultSet.getDate(column); + } + } + + /** + * @author Eike Stepper + */ + public static class AMCharacter extends AttributeMapping + { + public AMCharacter(ValueMapping valueMapping, CDOFeature feature) + { + super(valueMapping, feature); + } + + @Override + protected Object getResultSetValue(ResultSet resultSet, int column) throws SQLException + { + String str = resultSet.getString(column); + if (resultSet.wasNull()) + { + return null; + } + + return str.charAt(0); + } + } + + /** + * @author Eike Stepper + */ + public static class AMByte extends AttributeMapping + { + public AMByte(ValueMapping valueMapping, CDOFeature feature) + { + super(valueMapping, feature); + } + + @Override + protected Object getResultSetValue(ResultSet resultSet, int column) throws SQLException + { + return resultSet.getByte(column); + } + } + + /** + * @author Eike Stepper + */ + public static class AMBoolean extends AttributeMapping + { + public AMBoolean(ValueMapping valueMapping, CDOFeature feature) + { + super(valueMapping, feature); + } + + @Override + protected Object getResultSetValue(ResultSet resultSet, int column) throws SQLException + { + return resultSet.getBoolean(column); + } + } }
\ No newline at end of file diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/DBStore.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/DBStore.java index 0e5e84c5c8..08e522d2a4 100644 --- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/DBStore.java +++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/DBStore.java @@ -11,6 +11,7 @@ package org.eclipse.emf.cdo.server.internal.db; import org.eclipse.emf.cdo.internal.server.Store; +import org.eclipse.emf.cdo.protocol.model.CDOType; import org.eclipse.emf.cdo.server.ISession; import org.eclipse.emf.cdo.server.IView; import org.eclipse.emf.cdo.server.db.IDBStore; @@ -18,10 +19,12 @@ import org.eclipse.emf.cdo.server.db.IMappingStrategy; import org.eclipse.net4j.db.ConnectionProvider; import org.eclipse.net4j.db.DBException; +import org.eclipse.net4j.db.DBType; import org.eclipse.net4j.db.DBUtil; import org.eclipse.net4j.db.IDBAdapter; import org.eclipse.net4j.db.IDBSchema; import org.eclipse.net4j.internal.db.DBSchema; +import org.eclipse.net4j.util.ImplementationError; import java.sql.Connection; @@ -156,4 +159,54 @@ public class DBStore extends Store implements IDBStore String name = getRepository().getName(); return new DBSchema(name); } + + public static DBType getDBType(CDOType type) + { + if (type == CDOType.BOOLEAN || type == CDOType.BOOLEAN_OBJECT) + { + return DBType.BOOLEAN; + } + else if (type == CDOType.BYTE || type == CDOType.BYTE_OBJECT) + { + return DBType.SMALLINT; + } + else if (type == CDOType.CHAR || type == CDOType.CHARACTER_OBJECT) + { + return DBType.CHAR; + } + else if (type == CDOType.DATE) + { + return DBType.DATE; + } + else if (type == CDOType.DOUBLE || type == CDOType.DOUBLE_OBJECT) + { + return DBType.DOUBLE; + } + else if (type == CDOType.FLOAT || type == CDOType.FLOAT_OBJECT) + { + return DBType.FLOAT; + } + else if (type == CDOType.INT || type == CDOType.INTEGER_OBJECT) + { + return DBType.INTEGER; + } + else if (type == CDOType.LONG || type == CDOType.LONG_OBJECT) + { + return DBType.BIGINT; + } + else if (type == CDOType.OBJECT) + { + return DBType.BIGINT; + } + else if (type == CDOType.SHORT || type == CDOType.SHORT_OBJECT) + { + return DBType.SMALLINT; + } + else if (type == CDOType.STRING) + { + return DBType.LONGVARCHAR; + } + + throw new ImplementationError("Unrecognized CDOType: " + type); + } } diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/Mapping.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/Mapping.java index 266ccdabf9..1cae9ad4fd 100644 --- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/Mapping.java +++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/Mapping.java @@ -25,7 +25,6 @@ import org.eclipse.net4j.db.IDBAdapter; 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.ImplementationError; import java.sql.SQLException; import java.sql.Statement; @@ -186,52 +185,7 @@ public abstract class Mapping implements IMapping protected DBType getDBType(CDOType type) { - if (type == CDOType.BOOLEAN || type == CDOType.BOOLEAN_OBJECT) - { - return DBType.BOOLEAN; - } - else if (type == CDOType.BYTE || type == CDOType.BYTE_OBJECT) - { - return DBType.SMALLINT; - } - else if (type == CDOType.CHAR || type == CDOType.CHARACTER_OBJECT) - { - return DBType.CHAR; - } - else if (type == CDOType.DATE) - { - return DBType.DATE; - } - else if (type == CDOType.DOUBLE || type == CDOType.DOUBLE_OBJECT) - { - return DBType.DOUBLE; - } - else if (type == CDOType.FLOAT || type == CDOType.FLOAT_OBJECT) - { - return DBType.FLOAT; - } - else if (type == CDOType.INT || type == CDOType.INTEGER_OBJECT) - { - return DBType.INTEGER; - } - else if (type == CDOType.LONG || type == CDOType.LONG_OBJECT) - { - return DBType.BIGINT; - } - else if (type == CDOType.OBJECT) - { - return DBType.BIGINT; - } - else if (type == CDOType.SHORT || type == CDOType.SHORT_OBJECT) - { - return DBType.SMALLINT; - } - else if (type == CDOType.STRING) - { - return DBType.LONGVARCHAR; - } - - throw new ImplementationError("Unrecognized CDOType: " + type); + return DBStore.getDBType(type); } protected IDBAdapter getDBAdapter() diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/ToOneReferenceMapping.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/ToOneReferenceMapping.java index 63fb9514f0..f8be13cae6 100644 --- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/ToOneReferenceMapping.java +++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/ToOneReferenceMapping.java @@ -17,7 +17,7 @@ import org.eclipse.emf.cdo.protocol.model.CDOFeature; /** * @author Eike Stepper */ -public class ToOneReferenceMapping extends AttributeMapping +public class ToOneReferenceMapping extends AttributeMapping.AMObject { public ToOneReferenceMapping(ValueMapping valueMapping, CDOFeature feature) { diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/ValueMapping.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/ValueMapping.java index 5b0699168d..971d7830b6 100644 --- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/ValueMapping.java +++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/ValueMapping.java @@ -14,6 +14,7 @@ import org.eclipse.emf.cdo.internal.protocol.CDOIDImpl; import org.eclipse.emf.cdo.internal.protocol.revision.CDORevisionImpl; 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.IAttributeMapping; import org.eclipse.emf.cdo.server.db.IDBStoreAccessor; import org.eclipse.emf.cdo.server.db.IReferenceMapping; @@ -23,6 +24,7 @@ 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.internal.util.om.trace.ContextTracer; +import org.eclipse.net4j.util.ImplementationError; import java.sql.ResultSet; import java.sql.SQLException; @@ -161,7 +163,53 @@ public abstract class ValueMapping extends Mapping implements IValueMapping protected AttributeMapping createAttributeMapping(CDOFeature feature) { - return new AttributeMapping(this, feature); + CDOType type = feature.getType(); + if (type == CDOType.BOOLEAN || type == CDOType.BOOLEAN_OBJECT) + { + return new AttributeMapping.AMBoolean(this, feature); + } + else if (type == CDOType.BYTE || type == CDOType.BYTE_OBJECT) + { + return new AttributeMapping.AMByte(this, feature); + } + else if (type == CDOType.CHAR || type == CDOType.CHARACTER_OBJECT) + { + return new AttributeMapping.AMCharacter(this, feature); + } + else if (type == CDOType.DATE) + { + return new AttributeMapping.AMDate(this, feature); + } + else if (type == CDOType.DOUBLE || type == CDOType.DOUBLE_OBJECT) + { + return new AttributeMapping.AMDouble(this, feature); + } + else if (type == CDOType.FLOAT || type == CDOType.FLOAT_OBJECT) + { + return new AttributeMapping.AMFloat(this, feature); + } + else if (type == CDOType.INT || type == CDOType.INTEGER_OBJECT) + { + return new AttributeMapping.AMInteger(this, feature); + } + else if (type == CDOType.LONG || type == CDOType.LONG_OBJECT) + { + return new AttributeMapping.AMLong(this, feature); + } + else if (type == CDOType.OBJECT) + { + return new AttributeMapping.AMObject(this, feature); + } + else if (type == CDOType.SHORT || type == CDOType.SHORT_OBJECT) + { + return new AttributeMapping.AMShort(this, feature); + } + else if (type == CDOType.STRING) + { + return new AttributeMapping.AMString(this, feature); + } + + throw new ImplementationError("Unrecognized CDOType: " + type); } protected ToOneReferenceMapping createToOneReferenceMapping(CDOFeature feature) |