Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6a91e2934b74c80b154b9ef2635d13f9da86bb84 (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
package org.eclipse.emf.cdo.server.internal.db;

import org.eclipse.emf.cdo.internal.protocol.revision.CDORevisionImpl;
import org.eclipse.emf.cdo.protocol.CDOID;
import org.eclipse.emf.cdo.protocol.model.CDOClass;
import org.eclipse.emf.cdo.protocol.model.CDOFeature;
import org.eclipse.emf.cdo.protocol.model.CDOPackage;
import org.eclipse.emf.cdo.server.IRepository;
import org.eclipse.emf.cdo.server.db.IDBStoreAccessor;
import org.eclipse.emf.cdo.server.db.IReferenceMapping;

import org.eclipse.net4j.db.DBType;
import org.eclipse.net4j.db.IDBTable;

import java.util.Map;

/**
 * @author Eike Stepper
 */
public class ReferenceMapping extends FeatureMapping implements IReferenceMapping
{
  public static final String FIELD_NAME_FEATURE = "cdo_feature";

  public static final String FIELD_NAME_SOURCE = "cdo_source";

  public static final String FIELD_NAME_VERSION = "cdo_version";

  public static final String FIELD_NAME_IDX = "cdo_idx";

  public static final String FIELD_NAME_TARGET = "cdo_target";

  private IDBTable table;

  private ToMany toMany;

  private boolean withFeature;

  private String constantPrefix;

  public ReferenceMapping(ValueMapping valueMapping, CDOFeature feature, ToMany toMany)
  {
    super(valueMapping, feature);
    this.toMany = toMany;
    mapReference(valueMapping.getCDOClass(), feature);

    // Build the constant SQL prefix
    StringBuilder builder = new StringBuilder();
    builder.append("INSERT INTO ");
    builder.append(table);
    builder.append(" VALUES (");
    if (withFeature)
    {
      builder.append(FeatureServerInfo.getDBID(feature));
      builder.append(", ");
    }

    constantPrefix = builder.toString();
  }

  public IDBTable getTable()
  {
    return table;
  }

  public void writeReference(IDBStoreAccessor storeAccessor, CDORevisionImpl revision)
  {
    int idx = 0;
    long source = revision.getID().getValue();
    int version = revision.getVersion();
    for (Object element : revision.getList(getFeature()))
    {
      long target = ((CDOID)element).getValue();
      StringBuilder builder = new StringBuilder(constantPrefix);
      builder.append(source);
      builder.append(", ");
      builder.append(version);
      builder.append(", ");
      builder.append(idx++);
      builder.append(", ");
      builder.append(target);
      builder.append(")");
      getValueMapping().sqlUpdate(storeAccessor, builder.toString());
    }
  }

  public void readReference(IDBStoreAccessor storeAccessor, CDORevisionImpl revision)
  {
    // TODO Implement method ReferenceMapping.readReference()
    throw new UnsupportedOperationException("Not yet implemented");
  }

  protected void mapReference(CDOClass cdoClass, CDOFeature cdoFeature)
  {
    switch (toMany)
    {
    case PER_REFERENCE:
      withFeature = false;
      table = mapReferenceTable(cdoFeature, cdoClass.getName() + "_" + cdoFeature.getName() + "_refs");
      break;

    case PER_CLASS:
      withFeature = true;
      table = mapReferenceTable(cdoClass, cdoClass.getName() + "_refs");
      break;

    case PER_PACKAGE:
      withFeature = true;
      CDOPackage cdoPackage = cdoClass.getContainingPackage();
      table = mapReferenceTable(cdoPackage, cdoPackage.getName() + "_refs");
      break;

    case PER_REPOSITORY:
      withFeature = true;
      IRepository repository = getValueMapping().getMappingStrategy().getStore().getRepository();
      table = mapReferenceTable(repository, repository.getName() + "_refs");
      break;

    default:
      throw new IllegalArgumentException("Invalid mapping: " + toMany);
    }
  }

  protected IDBTable mapReferenceTable(Object key, String tableName)
  {
    Map<Object, IDBTable> referenceTables = getValueMapping().getMappingStrategy().getReferenceTables();
    IDBTable table = referenceTables.get(key);
    if (table == null)
    {
      table = addReferenceTable(tableName);
      referenceTables.put(key, table);
    }

    return table;
  }

  protected IDBTable addReferenceTable(String tableName)
  {
    IDBTable table = getValueMapping().addTable(tableName);
    if (withFeature)
    {
      table.addField(FIELD_NAME_FEATURE, DBType.INTEGER);
    }

    table.addField(FIELD_NAME_SOURCE, DBType.BIGINT);
    table.addField(FIELD_NAME_VERSION, DBType.INTEGER);
    table.addField(FIELD_NAME_IDX, DBType.INTEGER);
    table.addField(FIELD_NAME_TARGET, DBType.BIGINT);
    return table;
  }

}

Back to the top