Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c8adebce3eb216a51483e4eee215ee7dedbf608b (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/***************************************************************************
 * 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.CDOType;
import org.eclipse.emf.cdo.server.IStore;
import org.eclipse.emf.cdo.server.db.IMappingStrategy;

import org.eclipse.net4j.db.DBType;
import org.eclipse.net4j.db.IDBField;
import org.eclipse.net4j.db.IDBSchema;
import org.eclipse.net4j.db.IDBTable;
import org.eclipse.net4j.internal.db.DBSchema;
import org.eclipse.net4j.util.ImplementationError;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;

/**
 * @author Eike Stepper
 */
public abstract class MappingStrategy implements IMappingStrategy
{
  private IStore store;

  private Properties properties;

  private IDBSchema schema;

  public MappingStrategy()
  {
  }

  public IStore getStore()
  {
    return store;
  }

  public void setStore(IStore store)
  {
    this.store = store;
  }

  public Properties getProperties()
  {
    return properties;
  }

  public void setProperties(Properties properties)
  {
    this.properties = properties;
  }

  public IDBSchema getSchema()
  {
    return schema;
  }

  @Override
  public String toString()
  {
    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);
    }

    // Map all classes before features are mapped
    for (CDOPackageImpl cdoPackage : cdoPackages)
    {
      for (CDOClass cdoClass : cdoPackage.getClasses())
      {
        cdoClasses.add(cdoClass);
        IDBTable table = map(schema, cdoClass, affectedTables);
        if (table != null)
        {
          ((DBClassInfo)cdoClass.getServerInfo()).setTable(table);
          affectedTables.add(table);
        }
      }
    }

    // Map all features
    for (CDOClass cdoClass : cdoClasses)
    {
      for (CDOFeature cdoFeature : cdoClass.getAllFeatures())
      {
        IDBField field = map(schema, cdoClass, cdoFeature, affectedTables);
        if (field != null)
        {
          ((DBFeatureInfo)cdoFeature.getServerInfo()).setField(field);
          affectedTables.add(field.getTable());
        }
      }
    }

    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 map(IDBSchema schema, 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 map(IDBSchema schema, CDOClass cdoClass, CDOFeature cdoFeature,
      Set<IDBTable> affectedTables);

  protected IDBSchema createSchema()
  {
    String name = store.getRepository().getName();
    return new DBSchema(name);
  }

  protected void initTable(IDBTable table, boolean full)
  {
    table.addField("cdo_id", DBType.BIGINT);
    if (full)
    {
      table.addField("cdo_class", DBType.INTEGER);
    }
  }

  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.INTEGER_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);
  }
}

Back to the top