|
Lines 16-21
Link Here
|
| 16 |
*/ |
16 |
*/ |
| 17 |
package org.eclipse.emf.oda.ecore.impl; |
17 |
package org.eclipse.emf.oda.ecore.impl; |
| 18 |
|
18 |
|
|
|
19 |
import java.lang.reflect.InvocationTargetException; |
| 19 |
import java.math.BigDecimal; |
20 |
import java.math.BigDecimal; |
| 20 |
import java.sql.Date; |
21 |
import java.sql.Date; |
| 21 |
import java.sql.Time; |
22 |
import java.sql.Time; |
|
Lines 37-42
Link Here
|
| 37 |
import org.eclipse.emf.common.util.TreeIterator; |
38 |
import org.eclipse.emf.common.util.TreeIterator; |
| 38 |
import org.eclipse.emf.common.util.URI; |
39 |
import org.eclipse.emf.common.util.URI; |
| 39 |
import org.eclipse.emf.common.util.UniqueEList; |
40 |
import org.eclipse.emf.common.util.UniqueEList; |
|
|
41 |
import org.eclipse.emf.ecore.EClass; |
| 40 |
import org.eclipse.emf.ecore.EClassifier; |
42 |
import org.eclipse.emf.ecore.EClassifier; |
| 41 |
import org.eclipse.emf.ecore.resource.ResourceSet; |
43 |
import org.eclipse.emf.ecore.resource.ResourceSet; |
| 42 |
import org.eclipse.emf.ecore.util.EcoreUtil; |
44 |
import org.eclipse.emf.ecore.util.EcoreUtil; |
|
Lines 58-63
Link Here
|
| 58 |
|
60 |
|
| 59 |
protected QuerySpecification specification = null; |
61 |
protected QuerySpecification specification = null; |
| 60 |
|
62 |
|
|
|
63 |
protected QueryDelegate.Factory factory = null; |
| 61 |
protected QueryDelegate delegate = null; |
64 |
protected QueryDelegate delegate = null; |
| 62 |
protected EClassifier context = null; |
65 |
protected EClassifier context = null; |
| 63 |
protected Map<String, EClassifier> variables = null; |
66 |
protected Map<String, EClassifier> variables = null; |
|
Lines 140-146
Link Here
|
| 140 |
|
143 |
|
| 141 |
try |
144 |
try |
| 142 |
{ |
145 |
{ |
| 143 |
QueryDelegate.Factory factory = (QueryDelegate.Factory)QueryDelegate.Factory.Registry.INSTANCE.get((String)specification.getProperty(DELEGATE_PROPERTY_NAME)); |
146 |
factory = (QueryDelegate.Factory)QueryDelegate.Factory.Registry.INSTANCE.get((String)specification.getProperty(DELEGATE_PROPERTY_NAME)); |
| 144 |
|
147 |
|
| 145 |
ResourceSet resourceSet = connection.getResourceSet(); |
148 |
ResourceSet resourceSet = connection.getResourceSet(); |
| 146 |
|
149 |
|
|
Lines 177-256
Link Here
|
| 177 |
return ResultSetMetaData.create(type); |
180 |
return ResultSetMetaData.create(type); |
| 178 |
} |
181 |
} |
| 179 |
|
182 |
|
|
|
183 |
protected EList<Object> getAllObjectsByType(EList<Object> objects, EClassifier type) |
| 184 |
{ |
| 185 |
for (TreeIterator<Object> allContents = EcoreUtil.getAllContents(connection.getResourceSet(), true); allContents.hasNext();) |
| 186 |
{ |
| 187 |
Object next = allContents.next(); |
| 188 |
|
| 189 |
if (type.isInstance(next)) |
| 190 |
{ |
| 191 |
objects.add(next); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
return objects; |
| 196 |
} |
| 197 |
|
| 198 |
protected EList<Object> getResults( |
| 199 |
EList<Object> results, |
| 200 |
QueryDelegate delegate, |
| 201 |
EList<Object> targets, |
| 202 |
Map<String, Object> arguments, |
| 203 |
EClassifier type) throws InvocationTargetException |
| 204 |
{ |
| 205 |
for (Object target : targets) |
| 206 |
{ |
| 207 |
Object result = delegate.execute(target, arguments); |
| 208 |
|
| 209 |
if (result instanceof Collection< ? >) |
| 210 |
{ |
| 211 |
results.addAll(EcoreUtil.getObjectsByType((Collection< ? >)result, type)); |
| 212 |
} |
| 213 |
else if (type.isInstance(result)) |
| 214 |
{ |
| 215 |
results.add(result); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
return results; |
| 220 |
} |
| 221 |
|
| 180 |
public IResultSet executeQuery() throws OdaException |
222 |
public IResultSet executeQuery() throws OdaException |
| 181 |
{ |
223 |
{ |
| 182 |
assertPrepared(); |
224 |
assertPrepared(); |
| 183 |
|
225 |
|
| 184 |
EList<Object> targets = new UniqueEList.FastCompare<Object>(); |
226 |
EList<Object> targets = new UniqueEList.FastCompare<Object>(); |
| 185 |
|
|
|
| 186 |
Map<String, Object> arguments = new HashMap<String, Object>(); |
227 |
Map<String, Object> arguments = new HashMap<String, Object>(); |
| 187 |
|
228 |
|
| 188 |
for (Map.Entry<ParameterIdentifier, ? > entry : specification.getParameterValues().entrySet()) |
229 |
Map<ParameterIdentifier, ? > parameterValues = specification.getParameterValues(); |
|
|
230 |
|
| 231 |
if (!parameterValues.isEmpty()) |
| 189 |
{ |
232 |
{ |
| 190 |
String name = entry.getKey().getParameterName(); |
233 |
Object targetArgument = null; |
|
|
234 |
|
| 235 |
Map<String, EClassifier> dataTypeParameters = new HashMap<String, EClassifier>(); |
| 236 |
Map<String, Object> dataTypeArguments = new HashMap<String, Object>(); |
| 191 |
|
237 |
|
| 192 |
if (!StringUtil.isEmpty(name)) |
238 |
Map<String, Object> classArguments = new HashMap<String, Object>(); |
|
|
239 |
|
| 240 |
for (Map.Entry<ParameterIdentifier, ? > entry : parameterValues.entrySet()) |
| 193 |
{ |
241 |
{ |
| 194 |
Object value = entry.getValue(); |
242 |
String name = entry.getKey().getParameterName(); |
| 195 |
|
243 |
|
| 196 |
if (value instanceof ResultSet.JavaObject) |
244 |
if (!StringUtil.isEmpty(name)) |
| 197 |
{ |
245 |
{ |
| 198 |
value = ((ResultSet.JavaObject)value).getObject(); |
246 |
Object value = entry.getValue(); |
| 199 |
} |
|
|
| 200 |
|
247 |
|
| 201 |
if (ParameterMetaData.TARGET_PARAMETER_NAME.equals(name)) |
248 |
if (value instanceof ResultSet.JavaObject) |
| 202 |
{ |
|
|
| 203 |
if (value != null && !ParameterMetaData.DEFAULT_PARAMETER_VALUE.equals(value)) |
| 204 |
{ |
249 |
{ |
| 205 |
if (value instanceof Collection< ? >) |
250 |
value = ((ResultSet.JavaObject)value).getObject(); |
|
|
251 |
} |
| 252 |
|
| 253 |
if (value != null) |
| 254 |
{ |
| 255 |
if (ParameterMetaData.TARGET_PARAMETER_NAME.equals(name)) |
| 206 |
{ |
256 |
{ |
| 207 |
targets.addAll((Collection< ? >)value); |
257 |
targetArgument = value; |
| 208 |
} |
258 |
} |
| 209 |
else |
259 |
else |
| 210 |
{ |
260 |
{ |
| 211 |
targets.add(value); |
261 |
EClassifier type = variables.get(name); |
|
|
262 |
|
| 263 |
if (type instanceof EClass) |
| 264 |
{ |
| 265 |
classArguments.put(name, value); |
| 266 |
} |
| 267 |
else |
| 268 |
{ |
| 269 |
dataTypeParameters.put(name, type); |
| 270 |
dataTypeArguments.put(name, value); |
| 271 |
} |
| 212 |
} |
272 |
} |
| 213 |
} |
273 |
} |
| 214 |
} |
274 |
} |
| 215 |
else |
|
|
| 216 |
{ |
| 217 |
arguments.put(name, value); |
| 218 |
} |
| 219 |
} |
275 |
} |
| 220 |
} |
|
|
| 221 |
|
276 |
|
| 222 |
try |
277 |
if (targetArgument == null || ParameterMetaData.DEFAULT_PARAMETER_VALUE.equals(targetArgument)) |
| 223 |
{ |
278 |
{ |
| 224 |
if (targets.isEmpty()) |
279 |
getAllObjectsByType(targets, context); |
|
|
280 |
} |
| 281 |
else if (targetArgument instanceof String) |
| 225 |
{ |
282 |
{ |
| 226 |
for (TreeIterator<Object> allContents = EcoreUtil.getAllContents(connection.getResourceSet(), true); allContents.hasNext();) |
283 |
try |
| 227 |
{ |
284 |
{ |
| 228 |
Object next = allContents.next(); |
285 |
QueryDelegate delegate = factory.createQueryDelegate(context, dataTypeParameters, (String)targetArgument); |
| 229 |
|
286 |
getResults(targets, delegate, getAllObjectsByType(new UniqueEList.FastCompare<Object>(), context), dataTypeArguments, context); |
| 230 |
if (context.isInstance(next)) |
287 |
} |
| 231 |
{ |
288 |
catch (Exception e) |
| 232 |
targets.add(next); |
289 |
{ |
| 233 |
} |
290 |
throw new OdaException(e); |
| 234 |
} |
291 |
} |
| 235 |
} |
292 |
} |
|
|
293 |
else if (targetArgument instanceof Collection< ? >) |
| 294 |
{ |
| 295 |
targets.addAll((Collection< ? >)targetArgument); |
| 296 |
} |
| 297 |
else |
| 298 |
{ |
| 299 |
targets.add(targetArgument); |
| 300 |
} |
| 236 |
|
301 |
|
| 237 |
EList<Object> results = new UniqueEList<Object>(); |
302 |
arguments.putAll(dataTypeArguments); |
| 238 |
|
303 |
|
| 239 |
for (Object target : targets) |
304 |
for (Map.Entry<String, Object> entry : classArguments.entrySet()) |
| 240 |
{ |
305 |
{ |
| 241 |
Object result = delegate.execute(target, arguments); |
306 |
String name = entry.getKey(); |
|
|
307 |
Object value = entry.getValue(); |
| 242 |
|
308 |
|
| 243 |
if (result instanceof Collection< ? >) |
309 |
if (value instanceof String) |
| 244 |
{ |
|
|
| 245 |
results.addAll(EcoreUtil.getObjectsByType((Collection< ? >)result, type)); |
| 246 |
} |
| 247 |
else if (type.isInstance(result)) |
| 248 |
{ |
310 |
{ |
| 249 |
results.add(result); |
311 |
EClassifier type = variables.get(name); |
|
|
312 |
EList<Object> values = getAllObjectsByType(new UniqueEList.FastCompare<Object>(), type); |
| 313 |
|
| 314 |
if (!ParameterMetaData.DEFAULT_PARAMETER_VALUE.equals(value)) |
| 315 |
{ |
| 316 |
try |
| 317 |
{ |
| 318 |
QueryDelegate delegate = factory.createQueryDelegate(type, dataTypeParameters, (String)value); |
| 319 |
values = getResults(new UniqueEList.FastCompare<Object>(), delegate, values, dataTypeArguments, type); |
| 320 |
} |
| 321 |
catch (Exception e) |
| 322 |
{ |
| 323 |
throw new OdaException(e); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
value = values.isEmpty() ? null : values.iterator().next(); |
| 250 |
} |
328 |
} |
|
|
329 |
|
| 330 |
arguments.put(name, value); |
| 251 |
} |
331 |
} |
|
|
332 |
} |
| 252 |
|
333 |
|
| 253 |
IResultSet resultSet = ResultSet.create(type, results); |
334 |
try |
|
|
335 |
{ |
| 336 |
IResultSet resultSet = ResultSet.create(type, getResults(new UniqueEList<Object>(), delegate, targets, arguments, type)); |
| 254 |
resultSet.setMaxRows(getMaxRows()); |
337 |
resultSet.setMaxRows(getMaxRows()); |
| 255 |
return resultSet; |
338 |
return resultSet; |
| 256 |
} |
339 |
} |