|
Lines 9-14
Link Here
|
| 9 |
* Kai Schlamp - initial API and implementation |
9 |
* Kai Schlamp - initial API and implementation |
| 10 |
* Eike Stepper - maintenance |
10 |
* Eike Stepper - maintenance |
| 11 |
* Kai Schlamp - Bug 284812: [DB] Query non CDO object fails |
11 |
* Kai Schlamp - Bug 284812: [DB] Query non CDO object fails |
|
|
12 |
* Erdal Karaca - added cdoObjectResultAsMap parameter to return Map<String,Object> in result |
| 12 |
*/ |
13 |
*/ |
| 13 |
package org.eclipse.emf.cdo.server.internal.db; |
14 |
package org.eclipse.emf.cdo.server.internal.db; |
| 14 |
|
15 |
|
|
Lines 43-48
Link Here
|
| 43 |
|
44 |
|
| 44 |
public static final String CDO_OBJECT_QUERY = "cdoObjectQuery"; |
45 |
public static final String CDO_OBJECT_QUERY = "cdoObjectQuery"; |
| 45 |
|
46 |
|
|
|
47 |
public static final String CDO_OBJECT_RESULT_AS_MAP = "cdoObjectResultAsMap"; |
| 48 |
|
| 46 |
public static final String QUERY_STATEMENT = "queryStatement"; |
49 |
public static final String QUERY_STATEMENT = "queryStatement"; |
| 47 |
|
50 |
|
| 48 |
private DBStoreAccessor storeAccessor; |
51 |
private DBStoreAccessor storeAccessor; |
|
Lines 101-106
Link Here
|
| 101 |
int firstResult = -1; |
104 |
int firstResult = -1; |
| 102 |
boolean queryStatement = true; |
105 |
boolean queryStatement = true; |
| 103 |
boolean objectQuery = true; |
106 |
boolean objectQuery = true; |
|
|
107 |
boolean asMap = false; |
| 104 |
|
108 |
|
| 105 |
HashMap<String, List<Integer>> paramMap = new HashMap<String, List<Integer>>(); |
109 |
HashMap<String, List<Integer>> paramMap = new HashMap<String, List<Integer>>(); |
| 106 |
query = parse(query, paramMap); |
110 |
query = parse(query, paramMap); |
|
Lines 156-161
Link Here
|
| 156 |
} |
160 |
} |
| 157 |
} |
161 |
} |
| 158 |
} |
162 |
} |
|
|
163 |
else if (CDO_OBJECT_RESULT_AS_MAP.equalsIgnoreCase(key)) |
| 164 |
{ |
| 165 |
final Object o = info.getParameters().get(key); |
| 166 |
if (o != null) |
| 167 |
{ |
| 168 |
try |
| 169 |
{ |
| 170 |
asMap = (Boolean)o; |
| 171 |
} |
| 172 |
catch (ClassCastException ex) |
| 173 |
{ |
| 174 |
throw new IllegalArgumentException("Parameter cdoObjectResultAsMap must be a boolean but it is a " + o |
| 175 |
+ " class " + o.getClass().getName(), ex); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 159 |
else |
179 |
else |
| 160 |
{ |
180 |
{ |
| 161 |
if (!paramMap.containsKey(key) || paramMap.get(key) == null) |
181 |
if (!paramMap.containsKey(key) || paramMap.get(key) == null) |
|
Lines 182-187
Link Here
|
| 182 |
|
202 |
|
| 183 |
int maxResults = info.getMaxResults(); |
203 |
int maxResults = info.getMaxResults(); |
| 184 |
int counter = 0; |
204 |
int counter = 0; |
|
|
205 |
|
| 206 |
String[] columnNames = new String[resultSet.getMetaData().getColumnCount()]; |
| 207 |
|
| 208 |
for (int i = 1; i <= columnNames.length; i++) |
| 209 |
{ |
| 210 |
columnNames[i - 1] = resultSet.getMetaData().getColumnName(i); |
| 211 |
} |
| 212 |
|
| 185 |
while (resultSet.next()) |
213 |
while (resultSet.next()) |
| 186 |
{ |
214 |
{ |
| 187 |
if (maxResults != CDOQueryInfo.UNLIMITED_RESULTS && counter++ >= maxResults) |
215 |
if (maxResults != CDOQueryInfo.UNLIMITED_RESULTS && counter++ >= maxResults) |
|
Lines 197-206
Link Here
|
| 197 |
else |
225 |
else |
| 198 |
{ |
226 |
{ |
| 199 |
int columnCount = resultSet.getMetaData().getColumnCount(); |
227 |
int columnCount = resultSet.getMetaData().getColumnCount(); |
|
|
228 |
|
| 200 |
if (columnCount == 1) |
229 |
if (columnCount == 1) |
| 201 |
{ |
230 |
{ |
| 202 |
Object result = resultSet.getObject(1); |
231 |
Object result = resultSet.getObject(1); |
| 203 |
context.addResult(result); |
232 |
context.addResult(asMap ? toMap(columnNames, new Object[] { result }) : result); |
| 204 |
} |
233 |
} |
| 205 |
else |
234 |
else |
| 206 |
{ |
235 |
{ |
|
Lines 210-216
Link Here
|
| 210 |
results[i] = resultSet.getObject(i + 1); |
239 |
results[i] = resultSet.getObject(i + 1); |
| 211 |
} |
240 |
} |
| 212 |
|
241 |
|
| 213 |
context.addResult(results); |
242 |
context.addResult(asMap ? toMap(columnNames, results) : results); |
| 214 |
} |
243 |
} |
| 215 |
} |
244 |
} |
| 216 |
} |
245 |
} |
|
Lines 232-237
Link Here
|
| 232 |
} |
261 |
} |
| 233 |
} |
262 |
} |
| 234 |
|
263 |
|
|
|
264 |
private Map<String, Object> toMap(String[] columnNames, Object[] results) |
| 265 |
{ |
| 266 |
Map<String, Object> ret = new HashMap<String, Object>(); |
| 267 |
|
| 268 |
for (int i = 0; i < columnNames.length; i++) |
| 269 |
{ |
| 270 |
String columnName = columnNames[i]; |
| 271 |
ret.put(columnName, results[i]); |
| 272 |
} |
| 273 |
|
| 274 |
return ret; |
| 275 |
} |
| 276 |
|
| 235 |
private String parse(String query, Map<String, List<Integer>> paramMap) |
277 |
private String parse(String query, Map<String, List<Integer>> paramMap) |
| 236 |
{ |
278 |
{ |
| 237 |
int length = query.length(); |
279 |
int length = query.length(); |