Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2012-06-25 09:44:01 +0000
committerEike Stepper2012-06-25 09:44:01 +0000
commitd21666610ed6f93201f660b323d286ac18259fe6 (patch)
tree378df6497da272bdc5b045054b6ef87e6626ca9d
parent174ce076700d533cfaae370df7a0d001710a7217 (diff)
downloadcdo-d21666610ed6f93201f660b323d286ac18259fe6.tar.gz
cdo-d21666610ed6f93201f660b323d286ac18259fe6.tar.xz
cdo-d21666610ed6f93201f660b323d286ac18259fe6.zip
[382868] Problem with Date parameter in CDOQuery
https://bugs.eclipse.org/bugs/show_bug.cgi?id=382868
-rw-r--r--plugins/org.eclipse.emf.cdo.server.db/META-INF/MANIFEST.MF18
-rw-r--r--plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/SQLQueryHandler.java743
-rw-r--r--plugins/org.eclipse.emf.cdo.tests.db/META-INF/MANIFEST.MF12
-rw-r--r--plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/SQLQueryTest.java929
4 files changed, 872 insertions, 830 deletions
diff --git a/plugins/org.eclipse.emf.cdo.server.db/META-INF/MANIFEST.MF b/plugins/org.eclipse.emf.cdo.server.db/META-INF/MANIFEST.MF
index b3af04016e..abc75361f8 100644
--- a/plugins/org.eclipse.emf.cdo.server.db/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.emf.cdo.server.db/META-INF/MANIFEST.MF
@@ -1,7 +1,7 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.eclipse.emf.cdo.server.db;singleton:=true
-Bundle-Version: 4.1.0.qualifier
+Bundle-Version: 4.1.100.qualifier
Bundle-Name: %pluginName
Bundle-Vendor: %providerName
Bundle-Localization: plugin
@@ -12,11 +12,11 @@ Bundle-ClassPath: .
Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.4.0,4.0.0)",
org.eclipse.net4j.db;bundle-version="[4.0.0,5.0.0)";visibility:=reexport,
org.eclipse.emf.cdo.server;bundle-version="[4.0.0,5.0.0)";visibility:=reexport
-Export-Package: org.eclipse.emf.cdo.server.db;version="4.1.0",
- org.eclipse.emf.cdo.server.db.mapping;version="4.1.0",
- org.eclipse.emf.cdo.server.internal.db;version="4.1.0";x-friends:="org.eclipse.emf.cdo.tests,org.eclipse.emf.cdo.tests.db",
- org.eclipse.emf.cdo.server.internal.db.bundle;version="4.1.0";x-internal:=true,
- org.eclipse.emf.cdo.server.internal.db.jdbc;version="4.1.0";x-friends:="org.eclipse.emf.cdo.tests,org.eclipse.emf.cdo.tests.db",
- org.eclipse.emf.cdo.server.internal.db.mapping;version="4.1.0";x-friends:="org.eclipse.emf.cdo.tests,org.eclipse.emf.cdo.tests.db",
- org.eclipse.emf.cdo.server.internal.db.mapping.horizontal;version="4.1.0";x-friends:="org.eclipse.emf.cdo.tests,org.eclipse.emf.cdo.tests.db",
- org.eclipse.emf.cdo.server.internal.db.messages;version="4.1.0";x-internal:=true
+Export-Package: org.eclipse.emf.cdo.server.db;version="4.1.100",
+ org.eclipse.emf.cdo.server.db.mapping;version="4.1.100",
+ org.eclipse.emf.cdo.server.internal.db;version="4.1.100";x-friends:="org.eclipse.emf.cdo.tests,org.eclipse.emf.cdo.tests.db",
+ org.eclipse.emf.cdo.server.internal.db.bundle;version="4.1.100";x-internal:=true,
+ org.eclipse.emf.cdo.server.internal.db.jdbc;version="4.1.100";x-friends:="org.eclipse.emf.cdo.tests,org.eclipse.emf.cdo.tests.db",
+ org.eclipse.emf.cdo.server.internal.db.mapping;version="4.1.100";x-friends:="org.eclipse.emf.cdo.tests,org.eclipse.emf.cdo.tests.db",
+ org.eclipse.emf.cdo.server.internal.db.mapping.horizontal;version="4.1.100";x-friends:="org.eclipse.emf.cdo.tests,org.eclipse.emf.cdo.tests.db",
+ org.eclipse.emf.cdo.server.internal.db.messages;version="4.1.100";x-internal:=true
diff --git a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/SQLQueryHandler.java b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/SQLQueryHandler.java
index b0ed55e23a..c23845dff4 100644
--- a/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/SQLQueryHandler.java
+++ b/plugins/org.eclipse.emf.cdo.server.db/src/org/eclipse/emf/cdo/server/internal/db/SQLQueryHandler.java
@@ -1,364 +1,379 @@
-/*
- * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
- * 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:
- * Kai Schlamp - initial API and implementation
- * Eike Stepper - maintenance
- * Kai Schlamp - Bug 284812: [DB] Query non CDO object fails
- * Erdal Karaca - added cdoObjectResultAsMap parameter to return Map<String,Object> in result
- */
-package org.eclipse.emf.cdo.server.internal.db;
-
-import org.eclipse.emf.cdo.common.id.CDOID;
-import org.eclipse.emf.cdo.common.util.CDOQueryInfo;
-import org.eclipse.emf.cdo.server.IQueryContext;
-import org.eclipse.emf.cdo.server.IQueryHandler;
-import org.eclipse.emf.cdo.server.db.IIDHandler;
-
-import org.eclipse.net4j.db.DBException;
-import org.eclipse.net4j.db.DBUtil;
-
-import java.sql.Clob;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * Implements server side SQL query execution.
- *
- * @author Kai Schlamp
- */
-public class SQLQueryHandler implements IQueryHandler
-{
- public static final String QUERY_LANGUAGE = "sql";
-
- public static final String FIRST_RESULT = "firstResult";
-
- public static final String CDO_OBJECT_QUERY = "cdoObjectQuery";
-
- public static final String MAP_QUERY = "mapQuery";
-
- public static final String QUERY_STATEMENT = "queryStatement";
-
- private DBStoreAccessor storeAccessor;
-
- public SQLQueryHandler(DBStoreAccessor storeAccessor)
- {
- this.storeAccessor = storeAccessor;
- }
-
- public DBStoreAccessor getStoreAccessor()
- {
- return storeAccessor;
- }
-
- /**
- * Executes SQL queries. Gets the connection from {@link DBStoreAccessor}, creates a SQL query and sets the parameters
- * taken from the {@link CDOQueryInfo#getParameters()}.
- * <p>
- * Takes into account the {@link CDOQueryInfo#getMaxResults()} and the {@link SQLQueryHandler#FIRST_RESULT} (numbered
- * from 0) values for paging.
- * <p>
- * By default (parameter {@link SQLQueryHandler#CDO_OBJECT_QUERY} == true) a query for CDO Objects is exectued. The
- * SQL query must return the CDO ID in the first column for this to work. If you set
- * {@link SQLQueryHandler#CDO_OBJECT_QUERY} parameter to false, the value of the first column of a row itself is
- * returned.
- * <p>
- * By default (parameter {@link SQLQueryHandler#QUERY_STATEMENT} == true) query statements are executed. Set this
- * parameter to false for update/DDL statements.
- * <p>
- * It is possible to use variables inside the SQL string with ":" as prefix. E.g.
- * "SELECT cdo_id FROM Company WHERE name LIKE :name". The value must then be set by using a parameter. E.g.
- * query.setParameter(":name", "Foo%");
- *
- * @param info
- * the object containing the query and parameters
- * @param context
- * the query results are placed in the context
- * @see IQueryHandler#executeQuery(CDOQueryInfo, IQueryContext)
- */
- public void executeQuery(CDOQueryInfo info, IQueryContext context)
- {
- String language = info.getQueryLanguage();
- if (!QUERY_LANGUAGE.equals(language))
- {
- throw new IllegalArgumentException("Unsupported query language: " + language);
- }
-
- IIDHandler idHandler = storeAccessor.getStore().getIDHandler();
- Connection connection = storeAccessor.getConnection();
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- String query = info.getQueryString();
-
- try
- {
- int firstResult = -1;
- boolean queryStatement = true;
- boolean objectQuery = true;
- boolean mapQuery = false;
-
- HashMap<String, List<Integer>> paramMap = new HashMap<String, List<Integer>>();
- query = parse(query, paramMap);
- statement = connection.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
-
- for (String key : info.getParameters().keySet())
- {
- if (FIRST_RESULT.equalsIgnoreCase(key))
- {
- final Object o = info.getParameters().get(key);
- if (o != null)
- {
- try
- {
- firstResult = (Integer)o;
- }
- catch (ClassCastException ex)
- {
- throw new IllegalArgumentException("Parameter " + FIRST_RESULT + " must be an integer but it is a " + o
- + " class " + o.getClass().getName(), ex);
- }
- }
- }
- else if (QUERY_STATEMENT.equalsIgnoreCase(key))
- {
- final Object o = info.getParameters().get(key);
- if (o != null)
- {
- try
- {
- queryStatement = (Boolean)o;
- }
- catch (ClassCastException ex)
- {
- throw new IllegalArgumentException("Parameter " + QUERY_STATEMENT + " must be an boolean but it is a "
- + o + " class " + o.getClass().getName(), ex);
- }
- }
- }
- else if (CDO_OBJECT_QUERY.equalsIgnoreCase(key))
- {
- final Object o = info.getParameters().get(key);
- if (o != null)
- {
- try
- {
- objectQuery = (Boolean)o;
- }
- catch (ClassCastException ex)
- {
- throw new IllegalArgumentException("Parameter " + CDO_OBJECT_QUERY + " must be a boolean but it is a "
- + o + " class " + o.getClass().getName(), ex);
- }
- }
- }
- else if (MAP_QUERY.equalsIgnoreCase(key))
- {
- final Object o = info.getParameters().get(key);
- if (o != null)
- {
- try
- {
- mapQuery = (Boolean)o;
- }
- catch (ClassCastException ex)
- {
- throw new IllegalArgumentException("Parameter " + MAP_QUERY + " must be a boolean but it is a " + o
- + " class " + o.getClass().getName(), ex);
- }
- }
- }
- else
- {
- if (!paramMap.containsKey(key) || paramMap.get(key) == null)
- {
- throw new IllegalArgumentException("No parameter value found for named parameter " + key);
- }
-
- Integer[] indexes = paramMap.get(key).toArray(new Integer[0]);
- for (int i = 0; i < indexes.length; i++)
- {
- Object parameter = info.getParameters().get(key);
- statement.setObject(indexes[i], parameter);
- }
- }
- }
-
- if (queryStatement)
- {
- resultSet = statement.executeQuery();
- if (firstResult > -1)
- {
- resultSet.absolute(firstResult);
- }
-
- String[] columnNames = null;
- if (mapQuery)
- {
- columnNames = new String[resultSet.getMetaData().getColumnCount()];
- for (int i = 1; i <= columnNames.length; i++)
- {
- columnNames[i - 1] = resultSet.getMetaData().getColumnName(i);
- }
- }
-
- int maxResults = info.getMaxResults();
- int counter = 0;
-
- while (resultSet.next())
- {
- if (maxResults != CDOQueryInfo.UNLIMITED_RESULTS && counter++ >= maxResults)
- {
- break;
- }
-
- if (objectQuery)
- {
- CDOID result = idHandler.getCDOID(resultSet, 1);
- context.addResult(result);
- }
- else
- {
- int columnCount = resultSet.getMetaData().getColumnCount();
- if (columnCount == 1)
- {
- Object result = convertValue(resultSet.getObject(1));
- context.addResult(mapQuery ? toMap(columnNames, new Object[] { result }) : result);
- }
- else
- {
- Object[] results = new Object[columnCount];
- for (int i = 0; i < columnCount; i++)
- {
- results[i] = convertValue(resultSet.getObject(i + 1));
- }
-
- context.addResult(mapQuery ? toMap(columnNames, results) : results);
- }
- }
- }
- }
- else
- {
- int result = statement.executeUpdate();
- context.addResult(result);
- }
- }
- catch (SQLException ex)
- {
- throw new DBException("Problem while executing SQL query: " + query, ex);
- }
- finally
- {
- DBUtil.close(resultSet);
- DBUtil.close(statement);
- }
- }
-
- private Object convertValue(Object value)
- {
- if (value instanceof Clob)
- {
- Clob clob = (Clob)value;
-
- try
- {
- value = clob.getSubString(1, (int)clob.length());
- }
- catch (SQLException ex)
- {
- throw new DBException("Could not extract CLOB value", ex);
- }
- }
-
- return value;
- }
-
- private Map<String, Object> toMap(String[] columnNames, Object[] results)
- {
- Map<String, Object> ret = new HashMap<String, Object>();
-
- for (int i = 0; i < columnNames.length; i++)
- {
- String columnName = columnNames[i];
- ret.put(columnName, results[i]);
- }
-
- return ret;
- }
-
- private String parse(String query, Map<String, List<Integer>> paramMap)
- {
- int length = query.length();
- StringBuilder builder = new StringBuilder(length);
-
- boolean inSingleQuote = false;
- boolean inDoubleQuote = false;
- int index = 1;
-
- for (int i = 0; i < length; i++)
- {
- char c = query.charAt(i);
- if (inSingleQuote)
- {
- if (c == '\'')
- {
- inSingleQuote = false;
- }
- }
- else if (inDoubleQuote)
- {
- if (c == '"')
- {
- inDoubleQuote = false;
- }
- }
- else
- {
- if (c == '\'')
- {
- inSingleQuote = true;
- }
- else if (c == '"')
- {
- inDoubleQuote = true;
- }
- else if (c == ':' && i + 1 < length && Character.isJavaIdentifierStart(query.charAt(i + 1)))
- {
- int j = i + 2;
- while (j < length && Character.isJavaIdentifierPart(query.charAt(j)))
- {
- j++;
- }
-
- String name = query.substring(i + 1, j);
- c = '?';
- i += name.length();
-
- List<Integer> indexList = paramMap.get(name);
- if (indexList == null)
- {
- indexList = new ArrayList<Integer>();
- paramMap.put(name, indexList);
- }
-
- indexList.add(new Integer(index));
- index++;
- }
- }
-
- builder.append(c);
- }
-
- return builder.toString();
- }
-}
+/*
+ * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
+ * 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:
+ * Kai Schlamp - initial API and implementation
+ * Eike Stepper - maintenance
+ * Kai Schlamp - Bug 284812: [DB] Query non CDO object fails
+ * Erdal Karaca - added cdoObjectResultAsMap parameter to return Map<String,Object> in result
+ */
+package org.eclipse.emf.cdo.server.internal.db;
+
+import org.eclipse.emf.cdo.common.id.CDOID;
+import org.eclipse.emf.cdo.common.util.CDOQueryInfo;
+import org.eclipse.emf.cdo.server.IQueryContext;
+import org.eclipse.emf.cdo.server.IQueryHandler;
+import org.eclipse.emf.cdo.server.db.IIDHandler;
+
+import org.eclipse.net4j.db.DBException;
+import org.eclipse.net4j.db.DBUtil;
+
+import java.sql.Clob;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Implements server side SQL query execution.
+ *
+ * @author Kai Schlamp
+ */
+public class SQLQueryHandler implements IQueryHandler
+{
+ public static final String QUERY_LANGUAGE = "sql";
+
+ public static final String FIRST_RESULT = "firstResult";
+
+ public static final String CDO_OBJECT_QUERY = "cdoObjectQuery";
+
+ public static final String MAP_QUERY = "mapQuery";
+
+ public static final String QUERY_STATEMENT = "queryStatement";
+
+ private DBStoreAccessor storeAccessor;
+
+ public SQLQueryHandler(DBStoreAccessor storeAccessor)
+ {
+ this.storeAccessor = storeAccessor;
+ }
+
+ public DBStoreAccessor getStoreAccessor()
+ {
+ return storeAccessor;
+ }
+
+ /**
+ * Executes SQL queries. Gets the connection from {@link DBStoreAccessor}, creates a SQL query and sets the parameters
+ * taken from the {@link CDOQueryInfo#getParameters()}.
+ * <p>
+ * Takes into account the {@link CDOQueryInfo#getMaxResults()} and the {@link SQLQueryHandler#FIRST_RESULT} (numbered
+ * from 0) values for paging.
+ * <p>
+ * By default (parameter {@link SQLQueryHandler#CDO_OBJECT_QUERY} == true) a query for CDO Objects is exectued. The
+ * SQL query must return the CDO ID in the first column for this to work. If you set
+ * {@link SQLQueryHandler#CDO_OBJECT_QUERY} parameter to false, the value of the first column of a row itself is
+ * returned.
+ * <p>
+ * By default (parameter {@link SQLQueryHandler#QUERY_STATEMENT} == true) query statements are executed. Set this
+ * parameter to false for update/DDL statements.
+ * <p>
+ * It is possible to use variables inside the SQL string with ":" as prefix. E.g.
+ * "SELECT cdo_id FROM Company WHERE name LIKE :name". The value must then be set by using a parameter. E.g.
+ * query.setParameter(":name", "Foo%");
+ *
+ * @param info
+ * the object containing the query and parameters
+ * @param context
+ * the query results are placed in the context
+ * @see IQueryHandler#executeQuery(CDOQueryInfo, IQueryContext)
+ */
+ public void executeQuery(CDOQueryInfo info, IQueryContext context)
+ {
+ String language = info.getQueryLanguage();
+ if (!QUERY_LANGUAGE.equals(language))
+ {
+ throw new IllegalArgumentException("Unsupported query language: " + language);
+ }
+
+ IIDHandler idHandler = storeAccessor.getStore().getIDHandler();
+ Connection connection = storeAccessor.getConnection();
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ String query = info.getQueryString();
+
+ try
+ {
+ int firstResult = -1;
+ boolean queryStatement = true;
+ boolean objectQuery = true;
+ boolean mapQuery = false;
+
+ HashMap<String, List<Integer>> paramMap = new HashMap<String, List<Integer>>();
+ query = parse(query, paramMap);
+ statement = connection.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
+
+ for (String key : info.getParameters().keySet())
+ {
+ if (FIRST_RESULT.equalsIgnoreCase(key))
+ {
+ final Object o = info.getParameters().get(key);
+ if (o != null)
+ {
+ try
+ {
+ firstResult = (Integer)o;
+ }
+ catch (ClassCastException ex)
+ {
+ throw new IllegalArgumentException("Parameter " + FIRST_RESULT + " must be an integer but it is a " + o
+ + " class " + o.getClass().getName(), ex);
+ }
+ }
+ }
+ else if (QUERY_STATEMENT.equalsIgnoreCase(key))
+ {
+ final Object o = info.getParameters().get(key);
+ if (o != null)
+ {
+ try
+ {
+ queryStatement = (Boolean)o;
+ }
+ catch (ClassCastException ex)
+ {
+ throw new IllegalArgumentException("Parameter " + QUERY_STATEMENT + " must be an boolean but it is a "
+ + o + " class " + o.getClass().getName(), ex);
+ }
+ }
+ }
+ else if (CDO_OBJECT_QUERY.equalsIgnoreCase(key))
+ {
+ final Object o = info.getParameters().get(key);
+ if (o != null)
+ {
+ try
+ {
+ objectQuery = (Boolean)o;
+ }
+ catch (ClassCastException ex)
+ {
+ throw new IllegalArgumentException("Parameter " + CDO_OBJECT_QUERY + " must be a boolean but it is a "
+ + o + " class " + o.getClass().getName(), ex);
+ }
+ }
+ }
+ else if (MAP_QUERY.equalsIgnoreCase(key))
+ {
+ final Object o = info.getParameters().get(key);
+ if (o != null)
+ {
+ try
+ {
+ mapQuery = (Boolean)o;
+ }
+ catch (ClassCastException ex)
+ {
+ throw new IllegalArgumentException("Parameter " + MAP_QUERY + " must be a boolean but it is a " + o
+ + " class " + o.getClass().getName(), ex);
+ }
+ }
+ }
+ else
+ {
+ if (!paramMap.containsKey(key) || paramMap.get(key) == null)
+ {
+ throw new IllegalArgumentException("No parameter value found for named parameter " + key);
+ }
+
+ Integer[] indexes = paramMap.get(key).toArray(new Integer[0]);
+ for (int i = 0; i < indexes.length; i++)
+ {
+ Object parameter = info.getParameters().get(key);
+ // parameter = convertToSQL(parameter);
+ statement.setObject(indexes[i], parameter);
+ }
+ }
+ }
+
+ if (queryStatement)
+ {
+ resultSet = statement.executeQuery();
+ if (firstResult > -1)
+ {
+ resultSet.absolute(firstResult);
+ }
+
+ String[] columnNames = null;
+ if (mapQuery)
+ {
+ columnNames = new String[resultSet.getMetaData().getColumnCount()];
+ for (int i = 1; i <= columnNames.length; i++)
+ {
+ columnNames[i - 1] = resultSet.getMetaData().getColumnName(i);
+ }
+ }
+
+ int maxResults = info.getMaxResults();
+ int counter = 0;
+
+ while (resultSet.next())
+ {
+ if (maxResults != CDOQueryInfo.UNLIMITED_RESULTS && counter++ >= maxResults)
+ {
+ break;
+ }
+
+ if (objectQuery)
+ {
+ CDOID result = idHandler.getCDOID(resultSet, 1);
+ context.addResult(result);
+ }
+ else
+ {
+ int columnCount = resultSet.getMetaData().getColumnCount();
+ if (columnCount == 1)
+ {
+ Object result = convertFromSQL(resultSet.getObject(1));
+ context.addResult(mapQuery ? toMap(columnNames, new Object[] { result }) : result);
+ }
+ else
+ {
+ Object[] results = new Object[columnCount];
+ for (int i = 0; i < columnCount; i++)
+ {
+ results[i] = convertFromSQL(resultSet.getObject(i + 1));
+ }
+
+ context.addResult(mapQuery ? toMap(columnNames, results) : results);
+ }
+ }
+ }
+ }
+ else
+ {
+ int result = statement.executeUpdate();
+ context.addResult(result);
+ }
+ }
+ catch (SQLException ex)
+ {
+ throw new DBException("Problem while executing SQL query: " + query, ex);
+ }
+ finally
+ {
+ DBUtil.close(resultSet);
+ DBUtil.close(statement);
+ }
+ }
+
+ @SuppressWarnings("unused")
+ private Object convertToSQL(Object value)
+ {
+ if (value instanceof java.util.Date)
+ {
+ java.util.Date date = (java.util.Date)value;
+ value = new java.sql.Date(date.getTime());
+ }
+
+ return value;
+ }
+
+ private Object convertFromSQL(Object value)
+ {
+ // Conversion of java.sql.Date not needed in this direction
+
+ if (value instanceof Clob)
+ {
+ Clob clob = (Clob)value;
+
+ try
+ {
+ value = clob.getSubString(1, (int)clob.length());
+ }
+ catch (SQLException ex)
+ {
+ throw new DBException("Could not extract CLOB value", ex);
+ }
+ }
+
+ return value;
+ }
+
+ private Map<String, Object> toMap(String[] columnNames, Object[] results)
+ {
+ Map<String, Object> ret = new HashMap<String, Object>();
+
+ for (int i = 0; i < columnNames.length; i++)
+ {
+ String columnName = columnNames[i];
+ ret.put(columnName, results[i]);
+ }
+
+ return ret;
+ }
+
+ private String parse(String query, Map<String, List<Integer>> paramMap)
+ {
+ int length = query.length();
+ StringBuilder builder = new StringBuilder(length);
+
+ boolean inSingleQuote = false;
+ boolean inDoubleQuote = false;
+ int index = 1;
+
+ for (int i = 0; i < length; i++)
+ {
+ char c = query.charAt(i);
+ if (inSingleQuote)
+ {
+ if (c == '\'')
+ {
+ inSingleQuote = false;
+ }
+ }
+ else if (inDoubleQuote)
+ {
+ if (c == '"')
+ {
+ inDoubleQuote = false;
+ }
+ }
+ else
+ {
+ if (c == '\'')
+ {
+ inSingleQuote = true;
+ }
+ else if (c == '"')
+ {
+ inDoubleQuote = true;
+ }
+ else if (c == ':' && i + 1 < length && Character.isJavaIdentifierStart(query.charAt(i + 1)))
+ {
+ int j = i + 2;
+ while (j < length && Character.isJavaIdentifierPart(query.charAt(j)))
+ {
+ j++;
+ }
+
+ String name = query.substring(i + 1, j);
+ c = '?';
+ i += name.length();
+
+ List<Integer> indexList = paramMap.get(name);
+ if (indexList == null)
+ {
+ indexList = new ArrayList<Integer>();
+ paramMap.put(name, indexList);
+ }
+
+ indexList.add(new Integer(index));
+ index++;
+ }
+ }
+
+ builder.append(c);
+ }
+
+ return builder.toString();
+ }
+}
diff --git a/plugins/org.eclipse.emf.cdo.tests.db/META-INF/MANIFEST.MF b/plugins/org.eclipse.emf.cdo.tests.db/META-INF/MANIFEST.MF
index 612d53d75f..574500cb8f 100644
--- a/plugins/org.eclipse.emf.cdo.tests.db/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.emf.cdo.tests.db/META-INF/MANIFEST.MF
@@ -1,7 +1,7 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.eclipse.emf.cdo.tests.db;singleton:=true
-Bundle-Version: 4.0.100.qualifier
+Bundle-Version: 4.0.200.qualifier
Bundle-Name: %pluginName
Bundle-Vendor: %providerName
Bundle-Localization: plugin
@@ -23,8 +23,8 @@ Import-Package: com.mysql.jdbc.jdbc2.optional;version="[5.0.0,6.0.0)",
org.hsqldb.jdbc;version="[2.0.0,3.0.0)",
org.postgresql.ds.common;version="[8.3.0,9.0.0)",
org.postgresql.jdbc3;version="[8.3.0,9.0.0)"
-Export-Package: org.eclipse.emf.cdo.tests.db;version="4.0.100";x-friends:="org.eclipse.emf.cdo.tests.all",
- org.eclipse.emf.cdo.tests.db.bundle;version="4.0.100";x-internal:=true,
- org.eclipse.emf.cdo.tests.db.capabilities;version="4.0.100";x-friends:="org.eclipse.emf.cdo.tests.all",
- org.eclipse.emf.cdo.tests.db.performance;version="4.0.100";x-friends:="org.eclipse.emf.cdo.tests.all",
- org.eclipse.emf.cdo.tests.db.verifier;version="4.0.100";x-friends:="org.eclipse.emf.cdo.tests.all"
+Export-Package: org.eclipse.emf.cdo.tests.db;version="4.0.200";x-friends:="org.eclipse.emf.cdo.tests.all",
+ org.eclipse.emf.cdo.tests.db.bundle;version="4.0.200";x-internal:=true,
+ org.eclipse.emf.cdo.tests.db.capabilities;version="4.0.200";x-friends:="org.eclipse.emf.cdo.tests.all",
+ org.eclipse.emf.cdo.tests.db.performance;version="4.0.200";x-friends:="org.eclipse.emf.cdo.tests.all",
+ org.eclipse.emf.cdo.tests.db.verifier;version="4.0.200";x-friends:="org.eclipse.emf.cdo.tests.all"
diff --git a/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/SQLQueryTest.java b/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/SQLQueryTest.java
index 569f29623b..705217403f 100644
--- a/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/SQLQueryTest.java
+++ b/plugins/org.eclipse.emf.cdo.tests.db/src/org/eclipse/emf/cdo/tests/db/SQLQueryTest.java
@@ -1,451 +1,478 @@
-/*
- * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
- * 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:
- * Kai Schlamp - initial API and implementation
- * Eike Stepper - maintenance
- * Kai Schlamp - Bug 284812: [DB] Query non CDO object fails
- * Stefan Winkler - Bug 284812: [DB] Query non CDO object fails
- * Erdal Karaca - added test case for cdoObjectResultAsMap query parameter
- */
-package org.eclipse.emf.cdo.tests.db;
-
-import org.eclipse.emf.cdo.eresource.CDOResource;
-import org.eclipse.emf.cdo.server.internal.db.SQLQueryHandler;
-import org.eclipse.emf.cdo.session.CDOSession;
-import org.eclipse.emf.cdo.tests.AbstractCDOTest;
-import org.eclipse.emf.cdo.tests.model1.Customer;
-import org.eclipse.emf.cdo.tests.model1.Order;
-import org.eclipse.emf.cdo.tests.model1.OrderDetail;
-import org.eclipse.emf.cdo.tests.model1.Product1;
-import org.eclipse.emf.cdo.tests.model1.SalesOrder;
-import org.eclipse.emf.cdo.tests.model1.VAT;
-import org.eclipse.emf.cdo.transaction.CDOTransaction;
-import org.eclipse.emf.cdo.util.CommitException;
-import org.eclipse.emf.cdo.view.CDOQuery;
-
-import org.eclipse.net4j.util.WrappedException;
-import org.eclipse.net4j.util.collection.CloseableIterator;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-
-/**
- * Test different aspects of SQL querying using the CDO query api.
- *
- * @author Kai Schlamp
- */
-public class SQLQueryTest extends AbstractCDOTest
-{
- private static final int NUM_OF_PRODUCTS = 20;
-
- private static final int NUM_OF_CUSTOMERS = 5;
-
- private static final int NUM_OF_PRODUCTS_CUSTOMER = NUM_OF_PRODUCTS / NUM_OF_CUSTOMERS;
-
- private static final int NUM_OF_SALES_ORDERS = 5;
-
- @CleanRepositoriesBefore
- public void testSimpleQueries() throws Exception
- {
- msg("Opening session");
- CDOSession session = openSession();
-
- createTestSet(session);
-
- msg("Opening transaction for querying");
- CDOTransaction transaction = session.openTransaction();
-
- // {
- // msg("Query for products");
- // CDOQuery query = transaction.createQuery("sql", "SELECT CDO_ID FROM PRODUCT1");
- // final List<Product1> products = query.getResult(Product1.class);
- // assertEquals(NUM_OF_PRODUCTS, products.size());
- // }
-
- {
- msg("Query for products with a specific name");
- CDOQuery query = transaction.createQuery("sql", "SELECT CDO_ID FROM MODEL1_PRODUCT1 WHERE name=:name");
- query.setParameter("name", "" + 1);
- final List<Product1> products = query.getResult(Product1.class);
- assertEquals(1, products.size());
- }
-
- {
- msg("Query for Customers");
- CDOQuery query = transaction.createQuery("sql", "SELECT CDO_ID FROM MODEL1_CUSTOMER");
- final List<Customer> customers = query.getResult(Customer.class);
- assertEquals(NUM_OF_CUSTOMERS, customers.size());
- }
-
- {
- msg("Query for products with VAT15");
- CDOQuery query = transaction.createQuery("sql", "SELECT CDO_ID FROM MODEL1_PRODUCT1 WHERE VAT =:vat");
- query.setParameter("vat", VAT.VAT15.getValue());
- final List<Product1> products = query.getResult(Product1.class);
- assertEquals(10, products.size());
- for (Product1 p : products)
- {
- assertEquals(p.getVat(), VAT.VAT15);
- }
- }
-
- transaction.commit();
- }
-
- @CleanRepositoriesBefore
- public void testFunctions() throws Exception
- {
- msg("Opening session");
- CDOSession session = openSession();
-
- createTestSet(session);
-
- msg("Opening transaction for querying");
- CDOTransaction transaction = session.openTransaction();
-
- {
- msg("Count products");
- CDOQuery query = transaction.createQuery("sql", "SELECT COUNT(*) from MODEL1_PRODUCT1");
- query.setParameter(SQLQueryHandler.CDO_OBJECT_QUERY, false);
-
- // we need to handle objects, because different DBs produce either
- // Long or Integer results
- final List<Object> counts = query.getResult(Object.class);
- assertEquals(counts.size(), 1);
-
- Object result = counts.get(0);
- int intResult;
- if (result instanceof Integer)
- {
- intResult = ((Integer)result).intValue();
- }
- else
- {
- assertEquals(true, result instanceof Long);
- intResult = ((Long)result).intValue();
- }
-
- assertEquals(NUM_OF_PRODUCTS, intResult);
- }
-
- transaction.commit();
- }
-
- @CleanRepositoriesBefore
- public void testComplexQuerySalesOrderJoinCustomerProduct() throws Exception
- {
- msg("Opening session");
- CDOSession session = openSession();
-
- createTestSet(session);
-
- msg("Opening transaction for querying");
- CDOTransaction transaction = session.openTransaction();
-
- {
- msg("Query for customers");
- CDOQuery customerQuery = transaction.createQuery("sql", "SELECT CDO_ID FROM MODEL1_CUSTOMER ORDER BY NAME");
- final List<Customer> customers = customerQuery.getResult(Customer.class);
- assertEquals(NUM_OF_CUSTOMERS, customers.size());
-
- msg("Query for products");
- CDOQuery productQuery = transaction.createQuery("sql", "SELECT CDO_ID FROM MODEL1_PRODUCT1");
- final List<Product1> products = productQuery.getResult(Product1.class);
- assertEquals(NUM_OF_PRODUCTS, products.size());
- }
-
- transaction.commit();
- }
-
- public void testPaging() throws Exception
- {
- msg("Opening session");
- CDOSession session = openSession();
-
- createTestSet(session);
-
- msg("Opening transaction for querying");
- CDOTransaction transaction = session.openTransaction();
-
- {
- msg("Query for products in pages");
- int pageSize = 5;
- int numOfPages = NUM_OF_PRODUCTS / pageSize;
- final List<Product1> allProducts = new ArrayList<Product1>();
- for (int page = 0; page < numOfPages; page++)
- {
- CDOQuery productQuery = transaction.createQuery("sql", "SELECT CDO_ID FROM MODEL1_PRODUCT1");
- productQuery.setMaxResults(pageSize);
- productQuery.setParameter(SQLQueryHandler.FIRST_RESULT, page * pageSize);
- final List<Product1> queriedProducts = productQuery.getResult(Product1.class);
- assertEquals(true, queriedProducts.size() <= pageSize);
- // a product should not have been read yet
- for (Product1 newProduct : queriedProducts)
- {
- assertEquals(true, !allProducts.contains(newProduct));
- }
-
- allProducts.addAll(queriedProducts);
- }
-
- assertEquals(NUM_OF_PRODUCTS, allProducts.size());
- }
-
- transaction.commit();
- }
-
- public void testIterator() throws Exception
- {
- msg("Opening session");
- CDOSession session = openSession();
-
- createTestSet(session);
-
- msg("Opening transaction for querying");
- CDOTransaction transaction = session.openTransaction();
-
- {
- msg("Query for products");
- CDOQuery productQuery = transaction.createQuery("sql", "SELECT CDO_ID FROM MODEL1_PRODUCT1");
- final CloseableIterator<Product1> iterator = productQuery.getResultAsync(Product1.class);
- int counter = 0;
- while (iterator.hasNext())
- {
- final Product1 product = iterator.next();
- // meaningless but do something
- assertEquals(true, product != null);
- counter++;
- if (counter == NUM_OF_PRODUCTS / 2)
- {
- iterator.close();
- }
- }
- }
-
- transaction.commit();
- }
-
- public void _testNonCdoObjectQueries() throws Exception
- {
- msg("Opening session");
- CDOSession session = openSession();
-
- createTestSet(session);
-
- msg("Opening transaction for querying");
- CDOTransaction transaction = session.openTransaction();
-
- {
- msg("Query for customer street strings.");
- CDOQuery query = transaction.createQuery("sql", "SELECT STREET FROM MODEL1_CUSTOMER");
- query.setParameter("cdoObjectQuery", false);
- List<String> streets = new ArrayList<String>(query.getResult(String.class));
- for (int i = 0; i < 5; i++)
- {
- assertEquals(true, streets.contains("Street " + i));
- }
- }
- }
-
- public void _testNonCdoObjectQueries_Null() throws Exception
- {
- msg("Opening session");
- CDOSession session = openSession();
-
- createTestSet(session);
-
- msg("Opening transaction for querying");
- CDOTransaction transaction = session.openTransaction();
-
- {
- msg("Query for customer city strings.");
- CDOQuery query = transaction.createQuery("sql", "SELECT CITY FROM MODEL1_CUSTOMER");
- query.setParameter("cdoObjectQuery", false);
- List<String> cities = new ArrayList<String>(query.getResult(String.class));
-
- assertEquals(true, cities.contains(null));
- for (int i = 1; i < 5; i++)
- {
- assertEquals(true, cities.contains("City " + i));
- }
- }
- }
-
- @CleanRepositoriesBefore
- public void testNonCDOObjectQueries_Complex() throws Exception
- {
- msg("Opening session");
- CDOSession session = openSession();
-
- createTestSet(session);
-
- msg("Opening transaction for querying");
- CDOTransaction transaction = session.openTransaction();
-
- {
- msg("Query for customer fields");
- CDOQuery query = transaction.createQuery("sql", "SELECT street, city, name FROM model1_customer ORDER BY street");
- query.setParameter("cdoObjectQuery", false);
-
- List<Object[]> results = query.getResult(Object[].class);
- for (int i = 0; i < NUM_OF_CUSTOMERS; i++)
- {
- assertEquals("Street " + i, results.get(i)[0]);
- Object actual = results.get(i)[1];
- if (i == 0)
- {
- assertEquals(null, actual);
- }
- else
- {
- assertEquals("City " + i, actual);
- }
-
- assertEquals("" + i, results.get(i)[2]);
- }
- }
- }
-
- @CleanRepositoriesBefore
- public void testNonCDOObjectQueries_Complex_MAP() throws Exception
- {
- msg("Opening session");
- CDOSession session = openSession();
-
- createTestSet(session);
-
- msg("Opening transaction for querying");
- CDOTransaction transaction = session.openTransaction();
-
- msg("Query for customer fields");
- CDOQuery query = transaction.createQuery("sql", "SELECT street, city, name FROM model1_customer ORDER BY street");
- query.setParameter("cdoObjectQuery", false);
- query.setParameter("mapQuery", true);
-
- List<Map<String, Object>> results = query.getResult();
- for (int i = 0; i < NUM_OF_CUSTOMERS; i++)
- {
- assertEquals("Street " + i, results.get(i).get("STREET"));
- Object actual = results.get(i).get("CITY");
- if (i == 0)
- {
- assertEquals(null, actual);
- }
- else
- {
- assertEquals("City " + i, actual);
- }
-
- assertEquals("" + i, results.get(i).get("NAME"));
- }
- }
-
- private void createTestSet(CDOSession session)
- {
- msg("Opening transaction");
- CDOTransaction transaction = session.openTransaction();
-
- msg("Creating resource");
- CDOResource resource = transaction.createResource(getResourcePath("/test1"));
-
- fillResource(resource);
-
- try
- {
- msg("Committing");
- transaction.commit();
- }
- catch (CommitException ex)
- {
- throw WrappedException.wrap(ex);
- }
- }
-
- private void fillResource(CDOResource resource)
- {
- msg("Creating Testset");
- final List<Product1> products = new ArrayList<Product1>();
- for (int i = 0; i < NUM_OF_PRODUCTS; i++)
- {
- products.add(createProduct(i));
- }
-
- resource.getContents().addAll(products);
-
- int productCounter = 0;
- for (int i = 0; i < NUM_OF_CUSTOMERS; i++)
- {
- final Customer customer = getModel1Factory().createCustomer();
-
- if (i == 0)
- {
- // set first city null for null-test-case
- customer.setCity(null);
- }
- else
- {
- customer.setCity("City " + i);
- }
-
- customer.setName(i + "");
- customer.setStreet("Street " + i);
- resource.getContents().add(customer);
-
- final List<Product1> customerProducts = products.subList(productCounter, productCounter
- + NUM_OF_PRODUCTS_CUSTOMER);
- for (int k = 0; k < NUM_OF_SALES_ORDERS; k++)
- {
- resource.getContents().add(createSalesOrder(i * 10 + k, customer, customerProducts));
- }
-
- productCounter += NUM_OF_PRODUCTS_CUSTOMER;
- }
- }
-
- private SalesOrder createSalesOrder(int num, Customer customer, List<Product1> products)
- {
- SalesOrder salesOrder = getModel1Factory().createSalesOrder();
- salesOrder.setCustomer(customer);
- salesOrder.setId(num);
- createOrderDetail(salesOrder, num, products);
- return salesOrder;
- }
-
- private List<OrderDetail> createOrderDetail(Order order, int index, List<Product1> products)
- {
- final List<OrderDetail> orderDetails = new ArrayList<OrderDetail>();
- int count = 0;
- for (Product1 product : products)
- {
- OrderDetail orderDetail = getModel1Factory().createOrderDetail();
- orderDetail.setOrder(order);
- orderDetail.setPrice(count++ * index * 1.1f);
- orderDetail.setProduct(product);
- }
-
- return orderDetails;
- }
-
- private Product1 createProduct(int index)
- {
- Product1 product = getModel1Factory().createProduct1();
- product.setDescription("Description " + index);
- product.setName("" + index);
- if (index < 10)
- {
- product.setVat(VAT.VAT15);
- }
- else
- {
- product.setVat(VAT.VAT7);
- }
-
- return product;
- }
-}
+/*
+ * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
+ * 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:
+ * Kai Schlamp - initial API and implementation
+ * Eike Stepper - maintenance
+ * Kai Schlamp - Bug 284812: [DB] Query non CDO object fails
+ * Stefan Winkler - Bug 284812: [DB] Query non CDO object fails
+ * Erdal Karaca - added test case for cdoObjectResultAsMap query parameter
+ */
+package org.eclipse.emf.cdo.tests.db;
+
+import org.eclipse.emf.cdo.eresource.CDOResource;
+import org.eclipse.emf.cdo.server.internal.db.SQLQueryHandler;
+import org.eclipse.emf.cdo.session.CDOSession;
+import org.eclipse.emf.cdo.tests.AbstractCDOTest;
+import org.eclipse.emf.cdo.tests.model1.Customer;
+import org.eclipse.emf.cdo.tests.model1.Order;
+import org.eclipse.emf.cdo.tests.model1.OrderDetail;
+import org.eclipse.emf.cdo.tests.model1.Product1;
+import org.eclipse.emf.cdo.tests.model1.PurchaseOrder;
+import org.eclipse.emf.cdo.tests.model1.SalesOrder;
+import org.eclipse.emf.cdo.tests.model1.VAT;
+import org.eclipse.emf.cdo.transaction.CDOTransaction;
+import org.eclipse.emf.cdo.util.CommitException;
+import org.eclipse.emf.cdo.view.CDOQuery;
+import org.eclipse.emf.cdo.view.CDOView;
+
+import org.eclipse.net4j.util.WrappedException;
+import org.eclipse.net4j.util.collection.CloseableIterator;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Test different aspects of SQL querying using the CDO query api.
+ *
+ * @author Kai Schlamp
+ */
+public class SQLQueryTest extends AbstractCDOTest
+{
+ private static final int NUM_OF_PRODUCTS = 20;
+
+ private static final int NUM_OF_CUSTOMERS = 5;
+
+ private static final int NUM_OF_PRODUCTS_CUSTOMER = NUM_OF_PRODUCTS / NUM_OF_CUSTOMERS;
+
+ private static final int NUM_OF_SALES_ORDERS = 5;
+
+ @CleanRepositoriesBefore
+ public void testSimpleQueries() throws Exception
+ {
+ msg("Opening session");
+ CDOSession session = openSession();
+
+ createTestSet(session);
+
+ msg("Opening transaction for querying");
+ CDOTransaction transaction = session.openTransaction();
+
+ // {
+ // msg("Query for products");
+ // CDOQuery query = transaction.createQuery("sql", "SELECT CDO_ID FROM PRODUCT1");
+ // final List<Product1> products = query.getResult(Product1.class);
+ // assertEquals(NUM_OF_PRODUCTS, products.size());
+ // }
+
+ {
+ msg("Query for products with a specific name");
+ CDOQuery query = transaction.createQuery("sql", "SELECT CDO_ID FROM MODEL1_PRODUCT1 WHERE name=:name");
+ query.setParameter("name", "" + 1);
+ final List<Product1> products = query.getResult(Product1.class);
+ assertEquals(1, products.size());
+ }
+
+ {
+ msg("Query for Customers");
+ CDOQuery query = transaction.createQuery("sql", "SELECT CDO_ID FROM MODEL1_CUSTOMER");
+ final List<Customer> customers = query.getResult(Customer.class);
+ assertEquals(NUM_OF_CUSTOMERS, customers.size());
+ }
+
+ {
+ msg("Query for products with VAT15");
+ CDOQuery query = transaction.createQuery("sql", "SELECT CDO_ID FROM MODEL1_PRODUCT1 WHERE VAT =:vat");
+ query.setParameter("vat", VAT.VAT15.getValue());
+ final List<Product1> products = query.getResult(Product1.class);
+ assertEquals(10, products.size());
+ for (Product1 p : products)
+ {
+ assertEquals(p.getVat(), VAT.VAT15);
+ }
+ }
+
+ transaction.commit();
+ }
+
+ @CleanRepositoriesBefore
+ public void testFunctions() throws Exception
+ {
+ msg("Opening session");
+ CDOSession session = openSession();
+
+ createTestSet(session);
+
+ msg("Opening transaction for querying");
+ CDOTransaction transaction = session.openTransaction();
+
+ {
+ msg("Count products");
+ CDOQuery query = transaction.createQuery("sql", "SELECT COUNT(*) from MODEL1_PRODUCT1");
+ query.setParameter(SQLQueryHandler.CDO_OBJECT_QUERY, false);
+
+ // we need to handle objects, because different DBs produce either
+ // Long or Integer results
+ final List<Object> counts = query.getResult(Object.class);
+ assertEquals(counts.size(), 1);
+
+ Object result = counts.get(0);
+ int intResult;
+ if (result instanceof Integer)
+ {
+ intResult = ((Integer)result).intValue();
+ }
+ else
+ {
+ assertEquals(true, result instanceof Long);
+ intResult = ((Long)result).intValue();
+ }
+
+ assertEquals(NUM_OF_PRODUCTS, intResult);
+ }
+
+ transaction.commit();
+ }
+
+ @CleanRepositoriesBefore
+ public void testComplexQuerySalesOrderJoinCustomerProduct() throws Exception
+ {
+ msg("Opening session");
+ CDOSession session = openSession();
+
+ createTestSet(session);
+
+ msg("Opening transaction for querying");
+ CDOTransaction transaction = session.openTransaction();
+
+ {
+ msg("Query for customers");
+ CDOQuery customerQuery = transaction.createQuery("sql", "SELECT CDO_ID FROM MODEL1_CUSTOMER ORDER BY NAME");
+ final List<Customer> customers = customerQuery.getResult(Customer.class);
+ assertEquals(NUM_OF_CUSTOMERS, customers.size());
+
+ msg("Query for products");
+ CDOQuery productQuery = transaction.createQuery("sql", "SELECT CDO_ID FROM MODEL1_PRODUCT1");
+ final List<Product1> products = productQuery.getResult(Product1.class);
+ assertEquals(NUM_OF_PRODUCTS, products.size());
+ }
+
+ transaction.commit();
+ }
+
+ @CleanRepositoriesBefore
+ public void testDateParameter() throws Exception
+ {
+ Date aDate = new Date();
+ CDOSession session = openSession();
+
+ {
+ PurchaseOrder purchaseOrder = getModel1Factory().createPurchaseOrder();
+ purchaseOrder.setDate(aDate);
+
+ CDOTransaction transaction = session.openTransaction();
+ CDOResource resource = transaction.createResource(getResourcePath("/test1"));
+ resource.getContents().add(purchaseOrder);
+ resource.getContents().add(getModel1Factory().createPurchaseOrder());
+ transaction.commit();
+ }
+
+ CDOView view = session.openView();
+ CDOQuery query = view.createQuery("sql", "SELECT CDO_ID FROM model1_purchaseorder WHERE date0 = :aDate");
+ query.setParameter("aDate", aDate);
+ List<PurchaseOrder> orders = query.getResult(PurchaseOrder.class);
+ assertEquals(1, orders.size());
+ }
+
+ public void testPaging() throws Exception
+ {
+ msg("Opening session");
+ CDOSession session = openSession();
+
+ createTestSet(session);
+
+ msg("Opening transaction for querying");
+ CDOTransaction transaction = session.openTransaction();
+
+ {
+ msg("Query for products in pages");
+ int pageSize = 5;
+ int numOfPages = NUM_OF_PRODUCTS / pageSize;
+ final List<Product1> allProducts = new ArrayList<Product1>();
+ for (int page = 0; page < numOfPages; page++)
+ {
+ CDOQuery productQuery = transaction.createQuery("sql", "SELECT CDO_ID FROM MODEL1_PRODUCT1");
+ productQuery.setMaxResults(pageSize);
+ productQuery.setParameter(SQLQueryHandler.FIRST_RESULT, page * pageSize);
+ final List<Product1> queriedProducts = productQuery.getResult(Product1.class);
+ assertEquals(true, queriedProducts.size() <= pageSize);
+ // a product should not have been read yet
+ for (Product1 newProduct : queriedProducts)
+ {
+ assertEquals(true, !allProducts.contains(newProduct));
+ }
+
+ allProducts.addAll(queriedProducts);
+ }
+
+ assertEquals(NUM_OF_PRODUCTS, allProducts.size());
+ }
+
+ transaction.commit();
+ }
+
+ public void testIterator() throws Exception
+ {
+ msg("Opening session");
+ CDOSession session = openSession();
+
+ createTestSet(session);
+
+ msg("Opening transaction for querying");
+ CDOTransaction transaction = session.openTransaction();
+
+ {
+ msg("Query for products");
+ CDOQuery productQuery = transaction.createQuery("sql", "SELECT CDO_ID FROM MODEL1_PRODUCT1");
+ final CloseableIterator<Product1> iterator = productQuery.getResultAsync(Product1.class);
+ int counter = 0;
+ while (iterator.hasNext())
+ {
+ final Product1 product = iterator.next();
+ // meaningless but do something
+ assertEquals(true, product != null);
+ counter++;
+ if (counter == NUM_OF_PRODUCTS / 2)
+ {
+ iterator.close();
+ }
+ }
+ }
+
+ transaction.commit();
+ }
+
+ public void _testNonCdoObjectQueries() throws Exception
+ {
+ msg("Opening session");
+ CDOSession session = openSession();
+
+ createTestSet(session);
+
+ msg("Opening transaction for querying");
+ CDOTransaction transaction = session.openTransaction();
+
+ {
+ msg("Query for customer street strings.");
+ CDOQuery query = transaction.createQuery("sql", "SELECT STREET FROM MODEL1_CUSTOMER");
+ query.setParameter("cdoObjectQuery", false);
+ List<String> streets = new ArrayList<String>(query.getResult(String.class));
+ for (int i = 0; i < 5; i++)
+ {
+ assertEquals(true, streets.contains("Street " + i));
+ }
+ }
+ }
+
+ public void _testNonCdoObjectQueries_Null() throws Exception
+ {
+ msg("Opening session");
+ CDOSession session = openSession();
+
+ createTestSet(session);
+
+ msg("Opening transaction for querying");
+ CDOTransaction transaction = session.openTransaction();
+
+ {
+ msg("Query for customer city strings.");
+ CDOQuery query = transaction.createQuery("sql", "SELECT CITY FROM MODEL1_CUSTOMER");
+ query.setParameter("cdoObjectQuery", false);
+ List<String> cities = new ArrayList<String>(query.getResult(String.class));
+
+ assertEquals(true, cities.contains(null));
+ for (int i = 1; i < 5; i++)
+ {
+ assertEquals(true, cities.contains("City " + i));
+ }
+ }
+ }
+
+ @CleanRepositoriesBefore
+ public void testNonCDOObjectQueries_Complex() throws Exception
+ {
+ msg("Opening session");
+ CDOSession session = openSession();
+
+ createTestSet(session);
+
+ msg("Opening transaction for querying");
+ CDOTransaction transaction = session.openTransaction();
+
+ {
+ msg("Query for customer fields");
+ CDOQuery query = transaction.createQuery("sql", "SELECT street, city, name FROM model1_customer ORDER BY street");
+ query.setParameter("cdoObjectQuery", false);
+
+ List<Object[]> results = query.getResult(Object[].class);
+ for (int i = 0; i < NUM_OF_CUSTOMERS; i++)
+ {
+ assertEquals("Street " + i, results.get(i)[0]);
+ Object actual = results.get(i)[1];
+ if (i == 0)
+ {
+ assertEquals(null, actual);
+ }
+ else
+ {
+ assertEquals("City " + i, actual);
+ }
+
+ assertEquals("" + i, results.get(i)[2]);
+ }
+ }
+ }
+
+ @CleanRepositoriesBefore
+ public void testNonCDOObjectQueries_Complex_MAP() throws Exception
+ {
+ msg("Opening session");
+ CDOSession session = openSession();
+
+ createTestSet(session);
+
+ msg("Opening transaction for querying");
+ CDOTransaction transaction = session.openTransaction();
+
+ msg("Query for customer fields");
+ CDOQuery query = transaction.createQuery("sql", "SELECT street, city, name FROM model1_customer ORDER BY street");
+ query.setParameter("cdoObjectQuery", false);
+ query.setParameter("mapQuery", true);
+
+ List<Map<String, Object>> results = query.getResult();
+ for (int i = 0; i < NUM_OF_CUSTOMERS; i++)
+ {
+ assertEquals("Street " + i, results.get(i).get("STREET"));
+ Object actual = results.get(i).get("CITY");
+ if (i == 0)
+ {
+ assertEquals(null, actual);
+ }
+ else
+ {
+ assertEquals("City " + i, actual);
+ }
+
+ assertEquals("" + i, results.get(i).get("NAME"));
+ }
+ }
+
+ private void createTestSet(CDOSession session)
+ {
+ msg("Opening transaction");
+ CDOTransaction transaction = session.openTransaction();
+
+ msg("Creating resource");
+ CDOResource resource = transaction.createResource(getResourcePath("/test1"));
+
+ fillResource(resource);
+
+ try
+ {
+ msg("Committing");
+ transaction.commit();
+ }
+ catch (CommitException ex)
+ {
+ throw WrappedException.wrap(ex);
+ }
+ }
+
+ private void fillResource(CDOResource resource)
+ {
+ msg("Creating Testset");
+ final List<Product1> products = new ArrayList<Product1>();
+ for (int i = 0; i < NUM_OF_PRODUCTS; i++)
+ {
+ products.add(createProduct(i));
+ }
+
+ resource.getContents().addAll(products);
+
+ int productCounter = 0;
+ for (int i = 0; i < NUM_OF_CUSTOMERS; i++)
+ {
+ final Customer customer = getModel1Factory().createCustomer();
+
+ if (i == 0)
+ {
+ // set first city null for null-test-case
+ customer.setCity(null);
+ }
+ else
+ {
+ customer.setCity("City " + i);
+ }
+
+ customer.setName(i + "");
+ customer.setStreet("Street " + i);
+ resource.getContents().add(customer);
+
+ final List<Product1> customerProducts = products.subList(productCounter, productCounter
+ + NUM_OF_PRODUCTS_CUSTOMER);
+ for (int k = 0; k < NUM_OF_SALES_ORDERS; k++)
+ {
+ resource.getContents().add(createSalesOrder(i * 10 + k, customer, customerProducts));
+ }
+
+ productCounter += NUM_OF_PRODUCTS_CUSTOMER;
+ }
+ }
+
+ private SalesOrder createSalesOrder(int num, Customer customer, List<Product1> products)
+ {
+ SalesOrder salesOrder = getModel1Factory().createSalesOrder();
+ salesOrder.setCustomer(customer);
+ salesOrder.setId(num);
+ createOrderDetail(salesOrder, num, products);
+ return salesOrder;
+ }
+
+ private List<OrderDetail> createOrderDetail(Order order, int index, List<Product1> products)
+ {
+ final List<OrderDetail> orderDetails = new ArrayList<OrderDetail>();
+ int count = 0;
+ for (Product1 product : products)
+ {
+ OrderDetail orderDetail = getModel1Factory().createOrderDetail();
+ orderDetail.setOrder(order);
+ orderDetail.setPrice(count++ * index * 1.1f);
+ orderDetail.setProduct(product);
+ }
+
+ return orderDetails;
+ }
+
+ private Product1 createProduct(int index)
+ {
+ Product1 product = getModel1Factory().createProduct1();
+ product.setDescription("Description " + index);
+ product.setName("" + index);
+ if (index < 10)
+ {
+ product.setVat(VAT.VAT15);
+ }
+ else
+ {
+ product.setVat(VAT.VAT7);
+ }
+
+ return product;
+ }
+}

Back to the top