summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Winkler2009-08-03 07:21:35 (EDT)
committerStefan Winkler2009-08-03 07:21:35 (EDT)
commit1a0cf049ed1f639675c9900cbc27c7ece5592cc5 (patch)
tree4c607821eba6569ee7bcbdd3cb6a965b74406201
parentf2e58fa8281bf1f0004a11ea6c9109897af0aa1b (diff)
downloadcdo-1a0cf049ed1f639675c9900cbc27c7ece5592cc5.zip
cdo-1a0cf049ed1f639675c9900cbc27c7ece5592cc5.tar.gz
cdo-1a0cf049ed1f639675c9900cbc27c7ece5592cc5.tar.bz2
[285270] [DB] Support CUSTOM types
https://bugs.eclipse.org/bugs/show_bug.cgi?id=285270
-rw-r--r--plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/TypeMapping.java39
-rw-r--r--plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/TypeMappingFactory.java14
-rw-r--r--plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/SQLQueryTest.java61
-rw-r--r--plugins/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/AllTestsAllConfigs.java1
-rw-r--r--plugins/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/DynamicXSDTest.java82
5 files changed, 195 insertions, 2 deletions
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/TypeMapping.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/TypeMapping.java
index fba14c7..41c1653 100644
--- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/TypeMapping.java
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/TypeMapping.java
@@ -11,6 +11,7 @@
* Stefan Winkler - bug 275303: [DB] DBStore does not handle BIG_INTEGER and BIG_DECIMAL
* Kai Schlamp - bug 282976: [DB] Influence Mappings through EAnnotations
* Stefan Winkler - bug 282976: [DB] Influence Mappings through EAnnotations
+ * Stefan Winkler - bug 285270: [DB] Support XSD based models
*/
package org.eclipse.emf.cdo.server.internal.db.mapping;
@@ -31,6 +32,8 @@ import org.eclipse.net4j.db.DBType;
import org.eclipse.net4j.db.ddl.IDBField;
import org.eclipse.net4j.db.ddl.IDBTable;
+import org.eclipse.emf.ecore.EDataType;
+import org.eclipse.emf.ecore.EFactory;
import org.eclipse.emf.ecore.EStructuralFeature;
import java.math.BigDecimal;
@@ -523,4 +526,40 @@ public abstract class TypeMapping implements ITypeMapping
stmt.setString(index, ((BigDecimal)value).toPlainString());
}
}
+
+ /**
+ * @author Stefan Winkler
+ */
+ public static class TMCustom extends TypeMapping
+ {
+ private EDataType dataType;
+
+ private EFactory factory;
+
+ public TMCustom(IMappingStrategy mappingStrategy, EStructuralFeature feature, DBType type)
+ {
+ super(mappingStrategy, feature, type);
+ dataType = (EDataType)getFeature().getEType();
+ factory = dataType.getEPackage().getEFactoryInstance();
+ }
+
+ @Override
+ protected void doSetValue(PreparedStatement stmt, int index, Object value) throws SQLException
+ {
+ String svalue = factory.convertToString(dataType, value);
+ stmt.setString(index, svalue);
+ }
+
+ @Override
+ protected Object getResultSetValue(ResultSet resultSet, int column) throws SQLException
+ {
+ String val = resultSet.getString(column);
+ if (resultSet.wasNull())
+ {
+ return null;
+ }
+
+ return factory.createFromString(dataType, val);
+ }
+ }
}
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/TypeMappingFactory.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/TypeMappingFactory.java
index 5f0bdd7..0b099fc 100644
--- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/TypeMappingFactory.java
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/mapping/TypeMappingFactory.java
@@ -8,6 +8,7 @@
* Contributors:
* Stefan Winkler - initial API and implementation
* Eike Stepper - maintenance
+ * Stefan Winkler - bug 285270: [DB] Support XSD based models
*/
package org.eclipse.emf.cdo.server.internal.db.mapping;
@@ -170,6 +171,15 @@ public enum TypeMappingFactory
{
return new TypeMapping.TMBytes(mappingStrategy, feature, type);
}
+ },
+
+ CUSTOM_MAPPING
+ {
+ @Override
+ public ITypeMapping doCreateTypeMapping(IMappingStrategy mappingStrategy, EStructuralFeature feature, DBType type)
+ {
+ return new TypeMapping.TMCustom(mappingStrategy, feature, type);
+ }
};
private static Map<EClassifier, DBType> defaultTypeMap = new HashMap<EClassifier, DBType>();
@@ -242,6 +252,9 @@ public enum TypeMappingFactory
mappingTable.put(new Pair<CDOType, DBType>(CDOType.STRING, DBType.VARCHAR), STRING_MAPPING);
mappingTable.put(new Pair<CDOType, DBType>(CDOType.STRING, DBType.CLOB), STRING_MAPPING);
+ mappingTable.put(new Pair<CDOType, DBType>(CDOType.CUSTOM, DBType.VARCHAR), CUSTOM_MAPPING);
+ mappingTable.put(new Pair<CDOType, DBType>(CDOType.CUSTOM, DBType.CLOB), CUSTOM_MAPPING);
+
defaultFeatureMapDBTypes = new HashSet<DBType>(defaultTypeMap.values());
}
@@ -300,6 +313,7 @@ public enum TypeMappingFactory
return dbType;
}
+ // Fallback (e.g., for CUSTOM types)
return DBType.VARCHAR;
}
diff --git a/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/SQLQueryTest.java b/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/SQLQueryTest.java
index abdd39b..e3061fa 100644
--- a/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/SQLQueryTest.java
+++ b/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/SQLQueryTest.java
@@ -8,6 +8,8 @@
* Contributors:
* Kai Schlamp - initial API and implementation
* Eike Stepper - maintenance
+ * Kai Schlamp - Bug 284812: [DB] Query non CDO object fails
+ * Stefan Winkler - Bug 284812: [DB] Query non CDO object fails
*/
package org.eclipse.emf.cdo.tests.db;
@@ -211,9 +213,55 @@ public class SQLQueryTest extends AbstractCDOTest
enableConsole();
}
+ public void testNonCdoObjectQueries() throws Exception
+ {
+ msg("Opening session");
+ CDOSession session = openModel1Session();
+
+ createTestSet(session);
+
+ msg("Opening transaction for querying");
+ CDOTransaction transaction = session.openTransaction();
+
+ {
+ msg("Query for customer street strings.");
+ CDOQuery cdoQuery = transaction.createQuery("sql", "SELECT STREET FROM REPO1.CUSTOMER");
+ cdoQuery.setParameter("cdoObjectQuery", false);
+ List<String> streets = new ArrayList<String>(cdoQuery.getResult(String.class));
+ for (int i = 0; i < 5; i++)
+ {
+ assertTrue(streets.contains("Street " + i));
+ }
+ }
+ }
+
+ public void testNonCdoObjectQueries_Null() throws Exception
+ {
+ msg("Opening session");
+ CDOSession session = openModel1Session();
+
+ createTestSet(session);
+
+ msg("Opening transaction for querying");
+ CDOTransaction transaction = session.openTransaction();
+
+ {
+ msg("Query for customer city strings.");
+ CDOQuery cdoQuery = transaction.createQuery("sql", "SELECT CITY FROM REPO1.CUSTOMER");
+ cdoQuery.setParameter("cdoObjectQuery", false);
+ List<String> cities = new ArrayList<String>(cdoQuery.getResult(String.class));
+
+ assertTrue(cities.contains(null));
+ for (int i = 1; i < 5; i++)
+ {
+ assertTrue(cities.contains("City " + i));
+ }
+ }
+ }
+
private void createTestSet(CDOSession session)
{
- disableConsole();
+ // disableConsole();
msg("Opening transaction");
CDOTransaction transaction = session.openTransaction();
@@ -242,7 +290,16 @@ public class SQLQueryTest extends AbstractCDOTest
for (int i = 0; i < NUM_OF_CUSTOMERS; i++)
{
final Customer customer = getModel1Factory().createCustomer();
- customer.setCity("City " + i);
+
+ if (i == 0)
+ {
+ // set first city null for null-test-case
+ customer.setCity(null);
+ }
+ else
+ {
+ customer.setCity("City " + i);
+ }
customer.setName(i + "");
customer.setStreet("Street " + i);
resource.getContents().add(customer);
diff --git a/plugins/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/AllTestsAllConfigs.java b/plugins/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/AllTestsAllConfigs.java
index a32ec99..28e578c 100644
--- a/plugins/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/AllTestsAllConfigs.java
+++ b/plugins/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/AllTestsAllConfigs.java
@@ -105,6 +105,7 @@ public abstract class AllTestsAllConfigs extends ConfigTestSuite
testClasses.add(FeatureMapTest.class);
testClasses.add(AdapterManagerTest.class);
testClasses.add(ConflictResolverTest.class);
+ testClasses.add(DynamicXSDTest.class);
// Specific for MEMStore
testClasses.add(MEMStoreQueryTest.class);
diff --git a/plugins/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/DynamicXSDTest.java b/plugins/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/DynamicXSDTest.java
new file mode 100644
index 0000000..719ac18
--- /dev/null
+++ b/plugins/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/DynamicXSDTest.java
@@ -0,0 +1,82 @@
+/**
+ * Copyright (c) 2004 - 2009 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:
+ * Eike Stepper - initial API and implementation
+ * Christopher Albert - adaption to XSD
+ */
+package org.eclipse.emf.cdo.tests;
+
+import org.eclipse.emf.cdo.CDOObject;
+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.transaction.CDOTransaction;
+
+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.util.EcoreUtil;
+import org.eclipse.emf.ecore.util.ExtendedMetaData;
+import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
+
+/**
+ * @author Eike Stepper
+ */
+public class DynamicXSDTest extends AbstractCDOTest
+{
+ public void testDynamicEcore() throws Exception
+ {
+ {
+ // Obtain model
+ EPackage ecore = createPackage();
+ EClass companyClass = (EClass)ecore.getEClassifier("Company");
+ EAttribute nameAttribute = (EAttribute)companyClass.getEStructuralFeature("name");
+
+ // Create resource in session 1
+ CDOSession session = openSession();
+ session.getPackageRegistry().putEPackage(ecore);
+ CDOTransaction transaction = session.openTransaction();
+ CDOResource res = transaction.createResource("/res");
+
+ EObject company = EcoreUtil.create(companyClass);
+ company.eSet(nameAttribute, "Eike");
+ res.getContents().add(company);
+ transaction.commit();
+ }
+
+ {
+ // Load resource in session 2
+ CDOSession session = openSession();
+ CDOTransaction transaction = session.openTransaction();
+ CDOResource res = transaction.getResource("/res");
+
+ CDOObject company = (CDOObject)res.getContents().get(0);
+ EClass companyClass = company.eClass();
+ EAttribute nameAttribute = (EAttribute)companyClass.getEStructuralFeature("name");
+ String name = (String)company.eGet(nameAttribute);
+ assertEquals("Eike", name);
+ }
+ }
+
+ private static EPackage createPackage()
+ {
+ EPackage result = EMFUtil.createEPackage("xsdmodel", "xsdmodel",
+ "http://www.eclipse.org/emf/CDO/tests/xsdmodel/1.0.0");
+ EClass company = EMFUtil.createEClass(result, "Company", false, false);
+ ExtendedMetaData.INSTANCE.setName(company, "Company");
+ ExtendedMetaData.INSTANCE.setContentKind(company, ExtendedMetaData.ELEMENT_ONLY_CONTENT);
+
+ EAttribute att = EMFUtil.createEAttribute(company, "name", XMLTypePackage.eINSTANCE.getString());
+ att.setLowerBound(1);
+ ExtendedMetaData.INSTANCE.setName(att, "name");
+ ExtendedMetaData.INSTANCE.setFeatureKind(att, ExtendedMetaData.ELEMENT_FEATURE);
+
+ return result;
+ }
+}