Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d511f7730fd131e5f884fd803162914bc5d24606 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/***************************************************************************
 * 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.CDOIDRangeImpl;
import org.eclipse.emf.cdo.internal.protocol.model.CDOClassImpl;
import org.eclipse.emf.cdo.internal.protocol.model.CDOClassProxy;
import org.eclipse.emf.cdo.internal.protocol.model.CDOClassRefImpl;
import org.eclipse.emf.cdo.internal.protocol.model.CDOFeatureImpl;
import org.eclipse.emf.cdo.internal.protocol.model.CDOPackageImpl;
import org.eclipse.emf.cdo.internal.protocol.model.CDOTypeImpl;
import org.eclipse.emf.cdo.protocol.CDOID;
import org.eclipse.emf.cdo.protocol.CDOIDRange;
import org.eclipse.emf.cdo.protocol.model.CDOClassRef;
import org.eclipse.emf.cdo.protocol.model.CDOType;
import org.eclipse.emf.cdo.protocol.revision.CDORevision;
import org.eclipse.emf.cdo.server.IStoreReader;

import org.eclipse.net4j.db.DBException;
import org.eclipse.net4j.db.DBUtil;
import org.eclipse.net4j.db.IDBRowHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

/**
 * @author Eike Stepper
 */
public class DBStoreReader implements IStoreReader
{
  protected DBStore store;

  protected Connection connection;

  public DBStoreReader(DBStore store) throws DBException
  {
    this.store = store;

    try
    {
      connection = store.getDataSource().getConnection();
    }
    catch (SQLException ex)
    {
      throw new DBException(ex);
    }
  }

  public void release() throws DBException
  {
    try
    {
      connection.close();
    }
    catch (SQLException ex)
    {
      throw new DBException(ex);
    }
  }

  public DBStore getStore()
  {
    return store;
  }

  public Collection<PackageInfo> readPackageInfos()
  {
    final Collection<PackageInfo> result = new ArrayList(0);
    IDBRowHandler rowHandler = new IDBRowHandler()
    {
      public boolean handle(int row, final Object... values)
      {
        result.add(new PackageInfo()
        {
          public String getPackageURI()
          {
            return (String)values[0];
          }

          public boolean isDynamic()
          {
            return values[1] != null;
          }

          public CDOIDRange getMetaIDRange()
          {
            long rangeLB = (Long)values[2];
            long rangeUB = (Long)values[3];
            return CDOIDRangeImpl.create(rangeLB, rangeUB);
          }
        });

        return true;
      }
    };

    DBUtil.select(connection, rowHandler, CDODBSchema.PACKAGES_URI, CDODBSchema.PACKAGES_DYNAMIC,
        CDODBSchema.PACKAGES_RANGE_LB, CDODBSchema.PACKAGES_RANGE_UB);
    return result;
  }

  public void readPackage(final CDOPackageImpl cdoPackage)
  {
    Object[] values = DBUtil.select(connection, "", CDODBSchema.PACKAGES_ID, CDODBSchema.PACKAGES_NAME,
        CDODBSchema.PACKAGES_ECORE);
    cdoPackage.setServerInfo(values[0]);
    cdoPackage.setName((String)values[1]);
    cdoPackage.setEcore((String)values[2]);

    IDBRowHandler rowHandler = new IDBRowHandler()
    {
      public boolean handle(int row, Object... values)
      {
        int classifierID = (Integer)values[1];
        String name = (String)values[2];
        boolean isAbstract = (Boolean)values[3];
        CDOClassImpl cdoClass = new CDOClassImpl(cdoPackage, classifierID, name, isAbstract);
        cdoClass.setServerInfo(values[0]);
        cdoPackage.addClass(cdoClass);
        readClass(cdoClass);
        return true;
      }
    };

    String where = CDODBSchema.CLASSES_PACKAGE.toString() + " = '" + cdoPackage.getPackageURI() + "'";
    DBUtil.select(connection, rowHandler, where, CDODBSchema.CLASSES_ID, CDODBSchema.CLASSES_CLASSIFIER,
        CDODBSchema.CLASSES_NAME, CDODBSchema.CLASSES_ABSTRACT);
  }

  public void readClass(final CDOClassImpl cdoClass)
  {
    int classID = (Integer)cdoClass.getServerInfo();
    readSuperTypes(cdoClass, classID);
    readFeatures(cdoClass, classID);
  }

  public void readSuperTypes(final CDOClassImpl cdoClass, int classID)
  {
    IDBRowHandler rowHandler = new IDBRowHandler()
    {
      public boolean handle(int row, Object... values)
      {
        String packageURI = (String)values[0];
        int classifierID = (Integer)values[1];
        cdoClass.addSuperType(new CDOClassRefImpl(packageURI, classifierID));
        return true;
      }
    };

    String where = CDODBSchema.SUPERTYPES_TYPE.toString() + " = " + classID;
    DBUtil.select(connection, rowHandler, where, CDODBSchema.SUPERTYPES_SUPERTYPE_PACKAGE,
        CDODBSchema.SUPERTYPES_SUPERTYPE_CLASSIFIER);
  }

  public void readFeatures(final CDOClassImpl cdoClass, int classID)
  {
    IDBRowHandler rowHandler = new IDBRowHandler()
    {
      public boolean handle(int row, Object... values)
      {
        int featureID = (Integer)values[1];
        String name = (String)values[2];
        CDOTypeImpl type = CDOTypeImpl.getType((Integer)values[3]);
        boolean many = (Boolean)values[6];

        CDOFeatureImpl feature;
        if (type == CDOType.OBJECT)
        {
          String packageURI = (String)values[4];
          int classifierID = (Integer)values[5];
          boolean containment = (Boolean)values[7];
          CDOClassRefImpl classRef = new CDOClassRefImpl(packageURI, classifierID);
          CDOClassProxy referenceType = new CDOClassProxy(classRef, cdoClass.getPackageManager());
          feature = new CDOFeatureImpl(cdoClass, featureID, name, referenceType, many, containment);
        }
        else
        {
          feature = new CDOFeatureImpl(cdoClass, featureID, name, type, many);
        }

        feature.setServerInfo(values[0]);
        cdoClass.addFeature(feature);
        return true;
      }
    };

    String where = CDODBSchema.FEATURES_CLASS.toString() + " = " + classID;
    DBUtil.select(connection, rowHandler, where, CDODBSchema.FEATURES_ID, CDODBSchema.FEATURES_FEATURE,
        CDODBSchema.FEATURES_NAME, CDODBSchema.FEATURES_TYPE, CDODBSchema.FEATURES_REFERENCE_PACKAGE,
        CDODBSchema.FEATURES_REFERENCE_CLASSIFIER, CDODBSchema.FEATURES_MANY, CDODBSchema.FEATURES_CONTAINMENT);
  }

  public CDORevision readRevision(CDOID id, long timeStamp)
  {
    // TODO Implement method DBStoreReader.readRevision()
    throw new UnsupportedOperationException("Not yet implemented");
  }

  public CDOID readResourceID(String path)
  {
    // TODO Implement method DBStoreReader.readResourceID()
    throw new UnsupportedOperationException("Not yet implemented");
  }

  public String readResourcePath(CDOID id)
  {
    // TODO Implement method DBStoreReader.readResourcePath()
    throw new UnsupportedOperationException("Not yet implemented");
  }

  public CDORevision readRevision(CDOID id)
  {
    // TODO Implement method DBStoreReader.readRevision()
    throw new UnsupportedOperationException("Not yet implemented");
  }

  public CDOClassRef readObjectType(CDOID id)
  {
    // TODO Implement method DBStoreReader.readObjectType()
    throw new UnsupportedOperationException("Not yet implemented");
  }
}

Back to the top