Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7c6ac5b350e4f50a22e4f1bed06f1a423ef5a1fd (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
/*
 * 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.tests.db;

import org.eclipse.emf.cdo.common.model.EMFUtil;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.server.db.mapping.AbstractTypeMapping;
import org.eclipse.emf.cdo.server.db.mapping.AbstractTypeMappingFactory;
import org.eclipse.emf.cdo.server.db.mapping.ITypeMapping;
import org.eclipse.emf.cdo.server.internal.db.mapping.TypeMappingUtil;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.tests.AbstractCDOTest;
import org.eclipse.emf.cdo.tests.db.verifier.DBStoreVerifier;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.util.CommitException;

import org.eclipse.net4j.db.DBType;
import org.eclipse.net4j.db.DBUtil;
import org.eclipse.net4j.util.container.IPluginContainer;

import org.eclipse.emf.ecore.EAnnotation;
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.EcoreFactory;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.util.EcoreUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author Stefan Winkler
 */
public class CustomTypeMappingTest extends AbstractCDOTest
{
  public void testCustomTypeMapping() throws CommitException
  {
    // Manually register type mapping
    MyIntToVarcharTypeMapping.Factory factory = new MyIntToVarcharTypeMapping.Factory();
    IPluginContainer.INSTANCE.registerFactory(factory);

    try
    {
      final EPackage pkg = createUniquePackage();

      EClass cls = EMFUtil.createEClass(pkg, "foo", false, false);
      EAttribute att = EMFUtil.createEAttribute(cls, "bar", EcorePackage.eINSTANCE.getEInt());

      // Annotate type mapping and column type
      EAnnotation annotation = EcoreFactory.eINSTANCE.createEAnnotation();
      annotation.setSource("http://www.eclipse.org/CDO/DBStore");
      annotation.getDetails().put("typeMapping", "org.eclipse.emf.cdo.tests.db.EIntToVarchar");
      annotation.getDetails().put("columnType", DBType.VARCHAR.getKeyword());
      att.getEAnnotations().add(annotation);

      if (!isConfig(LEGACY))
      {
        CDOUtil.prepareDynamicEPackage(pkg);
      }

      CDOSession session = openSession();
      CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.createResource(getResourcePath("/test"));

      EObject obj = EcoreUtil.create(cls);
      obj.eSet(att, 42);

      resource.getContents().add(obj);
      transaction.commit();
      transaction.close();
      session.close();

      msg("Check if type was mapped to string...");
      new DBStoreVerifier(getRepository())
      {
        @Override
        protected void doVerify() throws Exception
        {
          Statement stmt = null;
          ResultSet rset = null;

          try
          {
            stmt = getStatement();
            rset = stmt.executeQuery("SELECT bar FROM " + pkg.getName() + "_foo");
            assertEquals("java.lang.String", rset.getMetaData().getColumnClassName(1));

            rset.next();
            assertEquals("2a", rset.getString(1));
          }
          finally
          {
            DBUtil.close(rset);
            DBUtil.close(stmt);
          }
        }
      }.verify();
    }
    finally
    {
      IPluginContainer.INSTANCE.getFactoryRegistry().remove(factory.getKey());
    }
  }

  /**
   * @author Stefan Winkler
   */
  public static class MyIntToVarcharTypeMapping extends AbstractTypeMapping
  {
    public static final ITypeMapping.Descriptor DESCRIPTOR = TypeMappingUtil.createDescriptor(
        "org.eclipse.emf.cdo.tests.db.EIntToVarchar", EcorePackage.eINSTANCE.getEInt(), DBType.VARCHAR);

    public MyIntToVarcharTypeMapping()
    {
    }

    @Override
    protected void doSetValue(PreparedStatement stmt, int index, Object value) throws SQLException
    {
      Integer val = (Integer)value;
      stmt.setString(index, Integer.toHexString(val));
    }

    @Override
    protected Object getResultSetValue(ResultSet resultSet) throws SQLException
    {
      String stringVal = resultSet.getString(getField().getName());
      return Integer.parseInt(stringVal, 16);
    }

    /**
     * @author Stefan Winkler
     */
    public static class Factory extends AbstractTypeMappingFactory
    {
      public Factory()
      {
        super(DESCRIPTOR);
      }

      @Override
      public ITypeMapping create(String description)
      {
        return new MyIntToVarcharTypeMapping();
      }
    }
  }
}

Back to the top