Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2013-05-20 06:14:45 -0400
committerEike Stepper2013-05-20 06:14:45 -0400
commitaa8bfe023cad0c187cc2d88518456b1e8abf7828 (patch)
tree2d6992f73313f19b37670bffc506f7a727561b28
parent75a58509574d64c76cd03c78a8e9969f7e188f00 (diff)
downloadcdo-aa8bfe023cad0c187cc2d88518456b1e8abf7828.tar.gz
cdo-aa8bfe023cad0c187cc2d88518456b1e8abf7828.tar.xz
cdo-aa8bfe023cad0c187cc2d88518456b1e8abf7828.zip
[350137] [DB] [PostgreSQL] Exception when persisting EChar attribute
with default '\u0000' value https://bugs.eclipse.org/bugs/show_bug.cgi?id=350137
-rw-r--r--plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/CoreTypeMappings.java47
-rw-r--r--plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/TypeMappingRegistry.java24
-rw-r--r--plugins/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/bugzilla/Bugzilla_350137_Test.java88
3 files changed, 159 insertions, 0 deletions
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/CoreTypeMappings.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/CoreTypeMappings.java
index acc7ab3c7f..24ddf4fb18 100644
--- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/CoreTypeMappings.java
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/CoreTypeMappings.java
@@ -632,6 +632,53 @@ public class CoreTypeMappings
}
/**
+ * @author Stefan Winkler
+ */
+ public static class TMCharacter2Integer extends AbstractTypeMapping
+ {
+ public static final Factory FACTORY = new Factory(TypeMappingUtil.createDescriptor(
+ ID_PREFIX + ".Character2Integer", EcorePackage.eINSTANCE.getEChar(), DBType.INTEGER));
+
+ public static final Factory FACTORY_OBJECT = new Factory(TypeMappingUtil.createDescriptor(ID_PREFIX
+ + ".CharacterObject2Integer", EcorePackage.eINSTANCE.getECharacterObject(), DBType.INTEGER));
+
+ @Override
+ public Object getResultSetValue(ResultSet resultSet) throws SQLException
+ {
+ int intRepresentation = resultSet.getInt(getField().getName());
+ if (resultSet.wasNull())
+ {
+ return getFeature().isUnsettable() ? CDORevisionData.NIL : null;
+ }
+
+ return Character.valueOf((char)intRepresentation);
+ }
+
+ @Override
+ protected void doSetValue(PreparedStatement stmt, int index, Object value) throws SQLException
+ {
+ stmt.setInt(index, /* (int) */((Character)value).charValue());
+ }
+
+ /**
+ * @author Stefan Winkler
+ */
+ public static class Factory extends AbstractTypeMappingFactory
+ {
+ public Factory(Descriptor descriptor)
+ {
+ super(descriptor);
+ }
+
+ @Override
+ public ITypeMapping create(String description) throws ProductCreationException
+ {
+ return new TMCharacter2Integer();
+ }
+ }
+ }
+
+ /**
* @author Eike Stepper
*/
public static class TMByte extends AbstractTypeMapping
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/TypeMappingRegistry.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/TypeMappingRegistry.java
index dc1c3bb354..44c9c64f52 100644
--- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/TypeMappingRegistry.java
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/TypeMappingRegistry.java
@@ -17,6 +17,7 @@ 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;
@@ -104,6 +105,27 @@ public class TypeMappingRegistry implements ITypeMapping.Registry, ITypeMapping.
{
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>();
@@ -145,6 +167,8 @@ public class TypeMappingRegistry implements ITypeMapping.Registry, ITypeMapping.
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);
diff --git a/plugins/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/bugzilla/Bugzilla_350137_Test.java b/plugins/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/bugzilla/Bugzilla_350137_Test.java
new file mode 100644
index 0000000000..30364e4abb
--- /dev/null
+++ b/plugins/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/bugzilla/Bugzilla_350137_Test.java
@@ -0,0 +1,88 @@
+/**
+ * Copyright (c) 2004 - 2011 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
+ */
+package org.eclipse.emf.cdo.tests.bugzilla;
+
+import org.eclipse.emf.cdo.common.model.EMFUtil;
+import org.eclipse.emf.cdo.eresource.CDOResource;
+import org.eclipse.emf.cdo.session.CDOSession;
+import org.eclipse.emf.cdo.tests.AbstractCDOTest;
+import org.eclipse.emf.cdo.transaction.CDOTransaction;
+import org.eclipse.emf.cdo.util.CDOUtil;
+
+import org.eclipse.emf.ecore.EAttribute;
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EPackage;
+import org.eclipse.emf.ecore.EcorePackage;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+
+/**
+ * @author Stefan Winkler
+ */
+public class Bugzilla_350137_Test extends AbstractCDOTest
+{
+ /**
+ * Just check if an EChar attribute can be stored to the database per default. (PostgreSQL has problems with this).
+ */
+ public void testDefault() throws Exception
+ {
+ EPackage pkg = EMFUtil.createEPackage("Test", "t", "http://cdo.eclipse.org/tests/Bugzilla350137_Test1.ecore");
+ EClass cls = EMFUtil.createEClass(pkg, "foo", false, false);
+
+ @SuppressWarnings("unused")
+ EAttribute att = EMFUtil.createEAttribute(cls, "bar", EcorePackage.eINSTANCE.getEChar());
+
+ if (!isConfig(LEGACY))
+ {
+ CDOUtil.prepareDynamicEPackage(pkg);
+ }
+
+ EObject obj = EcoreUtil.create(cls);
+
+ CDOSession session = openSession();
+ CDOTransaction transaction = session.openTransaction();
+ CDOResource resource = transaction.createResource(getResourcePath("/test"));
+
+ resource.getContents().add(obj);
+ transaction.commit();
+ transaction.close();
+ session.close();
+ }
+
+ /**
+ * Check if an EChar attribute with explicit zero value can be stored to the database per default. (PostgreSQL has
+ * problems with this).
+ */
+ public void testExplicitZero() throws Exception
+ {
+ EPackage pkg = EMFUtil.createEPackage("Test", "t", "http://cdo.eclipse.org/tests/Bugzilla350137_Test2.ecore");
+ EClass cls = EMFUtil.createEClass(pkg, "foo2", false, false);
+
+ EAttribute att = EMFUtil.createEAttribute(cls, "bar", EcorePackage.eINSTANCE.getEChar());
+
+ if (!isConfig(LEGACY))
+ {
+ CDOUtil.prepareDynamicEPackage(pkg);
+ }
+
+ EObject obj = EcoreUtil.create(cls);
+ obj.eSet(att, '\u0000');
+
+ CDOSession session = openSession();
+ CDOTransaction transaction = session.openTransaction();
+ CDOResource resource = transaction.createResource(getResourcePath("/test"));
+
+ resource.getContents().add(obj);
+ transaction.commit();
+ transaction.close();
+ session.close();
+ }
+}

Back to the top