Author: dgeraskov
Date: 2011-10-07 04:06:58 -0400 (Fri, 07 Oct 2011)
New Revision: 35463
Added:
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/QueryExecutor.java
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/lib/bsh-core-2.0b4.jar
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/QueryExecutor.java
Modified:
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/HibernateExtension3_5.java
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/.classpath
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/META-INF/MANIFEST.MF
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/build.properties
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/HibernateExtension4_0.java
Log:
Execute Criteria Query
Modified:
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/HibernateExtension3_5.java
===================================================================
---
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/HibernateExtension3_5.java 2011-10-07
07:29:26 UTC (rev 35462)
+++
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/HibernateExtension3_5.java 2011-10-07
08:06:58 UTC (rev 35463)
@@ -3,22 +3,15 @@
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
import org.eclipse.osgi.util.NLS;
-import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.console.ConfigurationFactory;
import org.hibernate.console.ConsoleConfigClassLoader;
import org.hibernate.console.ConsoleMessages;
-import org.hibernate.console.ConsoleQueryParameter;
import org.hibernate.console.QueryInputModel;
import org.hibernate.console.execution.DefaultExecutionContext;
import org.hibernate.console.execution.ExecutionContext;
@@ -30,7 +23,6 @@
import org.hibernate.console.preferences.ConsoleConfigurationPreferences;
import org.hibernate.console.preferences.PreferencesClassPathUtils;
import org.hibernate.eclipse.libs.FakeDelegatingDriver;
-import org.hibernate.type.Type;
public class HibernateExtension3_5 implements HibernateExtension {
@@ -53,111 +45,49 @@
}
@Override
- public QueryResult executeHQLQuery(final String hql,
- final QueryInputModel queryParameters) {
- System.out.println("Execute HQLQuery in " + getClass().getName());
- Session session = sessionFactory.openSession();
- Query query = session.createQuery(hql);
- List<Object> list = Collections.emptyList();
- long queryTime = 0;
+ public QueryResult executeHQLQuery(String hql,
+ QueryInputModel queryParameters) {
+ Session session = null;
try {
- list = new ArrayList<Object>();
- setupParameters(query, queryParameters);
- long startTime = System.currentTimeMillis();
- Iterator<?> iter = query.list().iterator(); // need to be user-controllable to
toggle between iterate, scroll etc.
- queryTime = System.currentTimeMillis() - startTime;
- while (iter.hasNext() ) {
- Object element = iter.next();
- list.add(element);
+ try {
+ session = sessionFactory.openSession();
+ } catch (Exception e){
+ return new QueryResultImpl(e);
}
- }
- catch (HibernateException e) {
- e.printStackTrace();
+ return QueryExecutor.executeHQLQuery(session, hql, queryParameters);
+ } finally {
+ if (session != null && session.isOpen()){
+ try {
+ session.close();
+ } catch (HibernateException e) {
+ return new QueryResultImpl(e);
+ }
+ }
}
- return new QueryResultImpl(list,
- getPathNames(query), queryTime);
}
-
- public List<String> getPathNames(Query query) {
- List<String> l = Collections.emptyList();
-
- try {
- if(query==null) return l;
- String[] returnAliases = null;
- try {
- returnAliases = query.getReturnAliases();
- } catch(NullPointerException e) {
- // ignore -
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2188
- }
- if(returnAliases==null) {
- Type[] t;
- try {
- t = query.getReturnTypes();
- } catch(NullPointerException npe) {
- t = new Type[] { null };
- // ignore -
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2188
- }
- l = new ArrayList<String>(t.length);
-
- for (int i = 0; i < t.length; i++) {
- Type type = t[i];
- if(type==null) {
- l.add("<multiple types>"); //$NON-NLS-1$
- } else {
- l.add(type.getName() );
- }
- }
- } else {
- String[] t = returnAliases;
- l = new ArrayList<String>(t.length);
-
- for (int i = 0; i < t.length; i++) {
- l.add(t[i]);
- }
- }
- } catch (HibernateException he) {
- he.printStackTrace();
- }
-
- return l;
- }
-
- private void setupParameters(Query query2, QueryInputModel model) {
-
- if(model.getMaxResults()!=null) {
- query2.setMaxResults( model.getMaxResults().intValue() );
- }
-
- ConsoleQueryParameter[] qp = model.getQueryParameters();
- for (int i = 0; i < qp.length; i++) {
- ConsoleQueryParameter parameter = qp[i];
-
+
+ @Override
+ public QueryResult executeCriteriaQuery(String criteriaCode,
+ QueryInputModel model) {
+ Session session = null;
+ try {
try {
- int pos = Integer.parseInt(parameter.getName());
- //FIXME no method to set positioned list value
- query2.setParameter(pos, calcValue( parameter ), parameter.getType());
- } catch(NumberFormatException nfe) {
- Object value = parameter.getValue();
- if (value != null && value.getClass().isArray()){
- Object[] values = (Object[])value;
- query2.setParameterList(parameter.getName(), Arrays.asList(values),
parameter.getType());
- } else {
- query2.setParameter(parameter.getName(), calcValue( parameter ),
parameter.getType());
- }
+ session = sessionFactory.openSession();
+ } catch (Exception e){
+ return new QueryResultImpl(e);
}
- }
+ return QueryExecutor.executeCriteriaQuery(session, criteriaCode, model);
+ } finally {
+ if (session != null && session.isOpen()){
+ try {
+ session.close();
+ } catch (HibernateException e) {
+ return new QueryResultImpl(e);
+ }
+ }
+ }
}
-
- private Object calcValue(ConsoleQueryParameter parameter) {
- return parameter.getValueForQuery();
- }
- @Override
- public QueryResult executeCriteriaQuery(String criteria,
- QueryInputModel queryParameters) {
- return null;
- }
-
/**
* @param ConsoleConfigurationPreferences the prefs to set
*/
Added:
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/QueryExecutor.java
===================================================================
---
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/QueryExecutor.java
(rev 0)
+++
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/QueryExecutor.java 2011-10-07
08:06:58 UTC (rev 35463)
@@ -0,0 +1,205 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.hibernate3_5;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.hibernate.Criteria;
+import org.hibernate.Query;
+import org.hibernate.Session;
+import org.hibernate.console.ConsoleMessages;
+import org.hibernate.console.ConsoleQueryParameter;
+import org.hibernate.console.QueryInputModel;
+import org.hibernate.console.ext.HibernateException;
+import org.hibernate.console.ext.QueryResult;
+import org.hibernate.console.ext.QueryResultImpl;
+import org.hibernate.engine.SessionImplementor;
+import org.hibernate.type.Type;
+
+import bsh.EvalError;
+import bsh.Interpreter;
+
+/**
+ * @author Dmitry Geraskov
+ *
+ */
+public class QueryExecutor {
+
+ public static QueryResult executeHQLQuery(Session session, String hql,
+ QueryInputModel queryParameters) {
+
+ Query query = session.createQuery(hql);
+ List<Object> list = Collections.emptyList();
+ long queryTime = 0;
+
+ list = new ArrayList<Object>();
+ setupParameters(query, queryParameters);
+ long startTime = System.currentTimeMillis();
+ Iterator<?> iter = query.list().iterator(); // need to be user-controllable to
toggle between iterate, scroll etc.
+ queryTime = System.currentTimeMillis() - startTime;
+ while (iter.hasNext() ) {
+ Object element = iter.next();
+ list.add(element);
+ }
+
+ QueryResultImpl result = new QueryResultImpl(list,
+ queryTime);
+ try{
+ result.setPathNames(getHQLPathNames(query));
+ } catch (HibernateException e){
+ result.addException(e);
+ }
+ return result;
+
+ }
+
+ public static QueryResult executeCriteriaQuery(Session session, String criteriaCode,
+ QueryInputModel model) {
+
+ try {
+ List<Object> list = Collections.emptyList();
+ long queryTime = 0;
+ if (criteriaCode.indexOf("System.exit") >= 0) { // TODO: externalize run
so we don't need this bogus check! //$NON-NLS-1$
+ return new QueryResultImpl(new IllegalArgumentException(
+ ConsoleMessages.JavaPage_not_allowed));
+ } else {
+ Interpreter ip = setupInterpreter(session);
+ Object o = ip.eval(criteriaCode);
+ // ugly! TODO: make un-ugly!
+ if (o instanceof Criteria) {
+ Criteria criteria = (Criteria) o;
+ if (model.getMaxResults() != null) {
+ criteria.setMaxResults(model.getMaxResults().intValue());
+ }
+
+ long startTime = System.currentTimeMillis();
+ list = criteria.list();
+ queryTime = System.currentTimeMillis() - startTime;
+ } else if (o instanceof List<?>) {
+ list = (List<Object>) o;
+ if (model.getMaxResults() != null) {
+ list = list.subList(0, Math.min(list.size(), model
+ .getMaxResults().intValue()));
+ }
+ } else {
+ list = new ArrayList<Object>();
+ list.add(o);
+ }
+ }
+ return new QueryResultImpl(list,
+ Collections.singletonList(ConsoleMessages.JavaPage_no_info), queryTime);
+ } catch (EvalError e) {
+ return new QueryResultImpl(e);
+ } catch (HibernateException e) {
+ return new QueryResultImpl(e);
+ }
+ }
+
+ private static List<String> getHQLPathNames(Query query) {
+ List<String> l = Collections.emptyList();
+
+ if(query==null) return l;
+ String[] returnAliases = null;
+ try {
+ returnAliases = query.getReturnAliases();
+ } catch(NullPointerException e) {
+ // ignore -
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2188
+ }
+ if(returnAliases==null) {
+ Type[] t;
+ try {
+ t = query.getReturnTypes();
+ } catch(NullPointerException npe) {
+ t = new Type[] { null };
+ // ignore -
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2188
+ }
+ l = new ArrayList<String>(t.length);
+
+ for (int i = 0; i < t.length; i++) {
+ Type type = t[i];
+ if(type==null) {
+ l.add("<multiple types>"); //$NON-NLS-1$
+ } else {
+ l.add(type.getName() );
+ }
+ }
+ } else {
+ String[] t = returnAliases;
+ l = new ArrayList<String>(t.length);
+
+ for (int i = 0; i < t.length; i++) {
+ l.add(t[i]);
+ }
+ }
+
+ return l;
+ }
+
+ private static void setupParameters(Query query2, QueryInputModel model) {
+ if(model.getMaxResults()!=null) {
+ query2.setMaxResults( model.getMaxResults().intValue() );
+ }
+ ConsoleQueryParameter[] qp = model.getQueryParameters();
+ for (int i = 0; i < qp.length; i++) {
+ ConsoleQueryParameter parameter = qp[i];
+
+ try {
+ int pos = Integer.parseInt(parameter.getName());
+ //FIXME no method to set positioned list value
+ query2.setParameter(pos, calcValue( parameter ), parameter.getType());
+ } catch(NumberFormatException nfe) {
+ Object value = parameter.getValue();
+ if (value != null && value.getClass().isArray()){
+ Object[] values = (Object[])value;
+ query2.setParameterList(parameter.getName(), Arrays.asList(values),
parameter.getType());
+ } else {
+ query2.setParameter(parameter.getName(), calcValue( parameter ),
parameter.getType());
+ }
+ }
+ }
+ }
+
+ private static Object calcValue(ConsoleQueryParameter parameter) {
+ return parameter.getValueForQuery();
+ }
+
+
+ private static Interpreter setupInterpreter(Session session) throws EvalError,
HibernateException {
+ Interpreter interpreter = new Interpreter();
+
+ interpreter.set("session", session); //$NON-NLS-1$
+ interpreter.setClassLoader( Thread.currentThread().getContextClassLoader() );
+ SessionImplementor si = (SessionImplementor)session;
+
+ Map<String, ?> map = si.getFactory().getAllClassMetadata();
+
+ Iterator<String> iterator = map.keySet().iterator();
+ //TODO: filter non classes.
+ String imports = ""; //$NON-NLS-1$
+ while (iterator.hasNext() ) {
+ String element = iterator.next();
+ imports += "import " + element + ";\n"; //$NON-NLS-1$
//$NON-NLS-2$
+ }
+
+ imports += "import org.hibernate.criterion.*;\n"; //$NON-NLS-1$
+ imports += "import org.hibernate.*;\n"; //$NON-NLS-1$
+ // TODO: expose the parameters as values to be used in the code.
+ interpreter.eval(imports);
+
+ return interpreter;
+ }
+
+}
Modified:
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/.classpath
===================================================================
---
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/.classpath 2011-10-07
07:29:26 UTC (rev 35462)
+++
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/.classpath 2011-10-07
08:06:58 UTC (rev 35463)
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
+ <classpathentry exported="true" kind="lib"
path="lib/bsh-core-2.0b4.jar"/>
<classpathentry exported="true" kind="lib"
path="lib/optional/ehcache/ehcache-core-2.4.3.jar"/>
<classpathentry exported="true" kind="lib"
path="lib/optional/ehcache/hibernate-ehcache-4.0.0.CR3.jar"/>
<classpathentry exported="true" kind="lib"
path="lib/optional/infinispan/hibernate-infinispan-4.0.0.CR3.jar"/>
Modified:
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/META-INF/MANIFEST.MF
===================================================================
---
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/META-INF/MANIFEST.MF 2011-10-07
07:29:26 UTC (rev 35462)
+++
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/META-INF/MANIFEST.MF 2011-10-07
08:06:58 UTC (rev 35463)
@@ -24,4 +24,5 @@
lib/required/javassist-3.12.0.GA.jar,
lib/required/jta-1.1.jar,
lib/jpa/hibernate-entitymanager-4.0.0.CR3.jar,
+ lib/bsh-core-2.0b4.jar,
.
Modified:
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/build.properties
===================================================================
---
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/build.properties 2011-10-07
07:29:26 UTC (rev 35462)
+++
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/build.properties 2011-10-07
08:06:58 UTC (rev 35463)
@@ -28,4 +28,5 @@
lib/optional/infinispan/jgroups-2.12.0.Final.jar,\
lib/optional/infinispan/rhq-pluginAnnotations-3.0.1.jar,\
lib/optional/proxool/hibernate-proxool-4.0.0.CR3.jar,\
- lib/optional/proxool/proxool-0.8.3.jar
+ lib/optional/proxool/proxool-0.8.3.jar,\
+ lib/bsh-core-2.0b4.jar
Added:
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/lib/bsh-core-2.0b4.jar
===================================================================
(Binary files differ)
Property changes on:
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/lib/bsh-core-2.0b4.jar
___________________________________________________________________
Added: svn:mime-type
+ application/octet-stream
Modified:
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/HibernateExtension4_0.java
===================================================================
---
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/HibernateExtension4_0.java 2011-10-07
07:29:26 UTC (rev 35462)
+++
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/HibernateExtension4_0.java 2011-10-07
08:06:58 UTC (rev 35463)
@@ -5,22 +5,16 @@
import java.security.PrivilegedAction;
import java.sql.DriverManager;
import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
-import java.util.List;
import java.util.Map;
import org.eclipse.osgi.util.NLS;
-import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.console.ConsoleConfigClassLoader;
import org.hibernate.console.ConsoleMessages;
-import org.hibernate.console.ConsoleQueryParameter;
import org.hibernate.console.QueryInputModel;
import org.hibernate.console.execution.DefaultExecutionContext;
import org.hibernate.console.execution.ExecutionContext;
@@ -34,7 +28,6 @@
import org.hibernate.service.BasicServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.service.internal.BasicServiceRegistryImpl;
-import org.hibernate.type.Type;
public class HibernateExtension4_0 implements HibernateExtension {
@@ -59,118 +52,51 @@
public String getHibernateVersion() {
return "4.0";
}
-
+
@Override
public QueryResult executeHQLQuery(String hql,
QueryInputModel queryParameters) {
- System.out.println("Execute HQLQuery in " + getClass().getName());
Session session = null;
try {
- session = sessionFactory.openSession();
- } catch (Exception e){
- e.printStackTrace();
- }
- Query query = session.createQuery(hql);
- List<Object> list = Collections.emptyList();
- long queryTime = 0;
- try {
- list = new ArrayList<Object>();
- setupParameters(query, queryParameters);
- long startTime = System.currentTimeMillis();
- Iterator<?> iter = query.list().iterator(); // need to be user-controllable to
toggle between iterate, scroll etc.
- queryTime = System.currentTimeMillis() - startTime;
- while (iter.hasNext() ) {
- Object element = iter.next();
- list.add(element);
+ try {
+ session = sessionFactory.openSession();
+ } catch (Exception e){
+ return new QueryResultImpl(e);
}
- }
- catch (HibernateException e) {
- e.printStackTrace();
+ return QueryExecutor.executeHQLQuery(session, hql, queryParameters);
+ } finally {
+ if (session != null && session.isOpen()){
+ try {
+ session.close();
+ } catch (HibernateException e) {
+ return new QueryResultImpl(e);
+ }
+ }
}
- return new QueryResultImpl(list,
- getPathNames(query), queryTime);
}
-
- public List<String> getPathNames(Query query) {
- List<String> l = Collections.emptyList();
-
- try {
- if(query==null) return l;
- String[] returnAliases = null;
- try {
- returnAliases = query.getReturnAliases();
- } catch(NullPointerException e) {
- // ignore -
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2188
- }
- if(returnAliases==null) {
- Type[] t;
- try {
- t = query.getReturnTypes();
- } catch(NullPointerException npe) {
- t = new Type[] { null };
- // ignore -
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2188
- }
- l = new ArrayList<String>(t.length);
-
- for (int i = 0; i < t.length; i++) {
- Type type = t[i];
- if(type==null) {
- l.add("<multiple types>"); //$NON-NLS-1$
- } else {
- l.add(type.getName() );
- }
- }
- } else {
- String[] t = returnAliases;
- l = new ArrayList<String>(t.length);
-
- for (int i = 0; i < t.length; i++) {
- l.add(t[i]);
- }
- }
- } catch (HibernateException he) {
- he.printStackTrace();
- }
-
- return l;
- }
-
- private void setupParameters(Query query2, QueryInputModel model) {
-
- if(model.getMaxResults()!=null) {
- query2.setMaxResults( model.getMaxResults().intValue() );
- }
-
- ConsoleQueryParameter[] qp = model.getQueryParameters();
- for (int i = 0; i < qp.length; i++) {
- ConsoleQueryParameter parameter = qp[i];
-
+
+ @Override
+ public QueryResult executeCriteriaQuery(String criteriaCode,
+ QueryInputModel model) {
+ Session session = null;
+ try {
try {
- int pos = Integer.parseInt(parameter.getName());
- //FIXME no method to set positioned list value
- query2.setParameter(pos, calcValue( parameter ), parameter.getType());
- } catch(NumberFormatException nfe) {
- Object value = parameter.getValue();
- if (value != null && value.getClass().isArray()){
- Object[] values = (Object[])value;
- query2.setParameterList(parameter.getName(), Arrays.asList(values),
parameter.getType());
- } else {
- query2.setParameter(parameter.getName(), calcValue( parameter ),
parameter.getType());
- }
+ session = sessionFactory.openSession();
+ } catch (Exception e){
+ return new QueryResultImpl(e);
}
- }
+ return QueryExecutor.executeCriteriaQuery(session, criteriaCode, model);
+ } finally {
+ if (session != null && session.isOpen()){
+ try {
+ session.close();
+ } catch (HibernateException e) {
+ return new QueryResultImpl(e);
+ }
+ }
+ }
}
-
- private Object calcValue(ConsoleQueryParameter parameter) {
- return parameter.getValueForQuery();
- }
- @Override
- public QueryResult executeCriteriaQuery(String criteria,
- QueryInputModel queryParameters) {
- return null;
- }
-
/**
* @param ConsoleConfigurationPreferences the prefs to set
*/
Added:
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/QueryExecutor.java
===================================================================
---
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/QueryExecutor.java
(rev 0)
+++
branches/dead/hibernatetools-multiversion2/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/QueryExecutor.java 2011-10-07
08:06:58 UTC (rev 35463)
@@ -0,0 +1,205 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.hibernate4_0;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.hibernate.Criteria;
+import org.hibernate.Query;
+import org.hibernate.Session;
+import org.hibernate.console.ConsoleMessages;
+import org.hibernate.console.ConsoleQueryParameter;
+import org.hibernate.console.QueryInputModel;
+import org.hibernate.console.ext.HibernateException;
+import org.hibernate.console.ext.QueryResult;
+import org.hibernate.console.ext.QueryResultImpl;
+import org.hibernate.engine.spi.SessionImplementor;
+import org.hibernate.type.Type;
+
+import bsh.EvalError;
+import bsh.Interpreter;
+
+/**
+ * @author Dmitry Geraskov
+ *
+ */
+public class QueryExecutor {
+
+ public static QueryResult executeHQLQuery(Session session, String hql,
+ QueryInputModel queryParameters) {
+
+ Query query = session.createQuery(hql);
+ List<Object> list = Collections.emptyList();
+ long queryTime = 0;
+
+ list = new ArrayList<Object>();
+ setupParameters(query, queryParameters);
+ long startTime = System.currentTimeMillis();
+ Iterator<?> iter = query.list().iterator(); // need to be user-controllable to
toggle between iterate, scroll etc.
+ queryTime = System.currentTimeMillis() - startTime;
+ while (iter.hasNext() ) {
+ Object element = iter.next();
+ list.add(element);
+ }
+
+ QueryResultImpl result = new QueryResultImpl(list,
+ queryTime);
+ try{
+ result.setPathNames(getHQLPathNames(query));
+ } catch (HibernateException e){
+ result.addException(e);
+ }
+ return result;
+
+ }
+
+ public static QueryResult executeCriteriaQuery(Session session, String criteriaCode,
+ QueryInputModel model) {
+
+ try {
+ List<Object> list = Collections.emptyList();
+ long queryTime = 0;
+ if (criteriaCode.indexOf("System.exit") >= 0) { // TODO: externalize run
so we don't need this bogus check! //$NON-NLS-1$
+ return new QueryResultImpl(new IllegalArgumentException(
+ ConsoleMessages.JavaPage_not_allowed));
+ } else {
+ Interpreter ip = setupInterpreter(session);
+ Object o = ip.eval(criteriaCode);
+ // ugly! TODO: make un-ugly!
+ if (o instanceof Criteria) {
+ Criteria criteria = (Criteria) o;
+ if (model.getMaxResults() != null) {
+ criteria.setMaxResults(model.getMaxResults().intValue());
+ }
+
+ long startTime = System.currentTimeMillis();
+ list = criteria.list();
+ queryTime = System.currentTimeMillis() - startTime;
+ } else if (o instanceof List<?>) {
+ list = (List<Object>) o;
+ if (model.getMaxResults() != null) {
+ list = list.subList(0, Math.min(list.size(), model
+ .getMaxResults().intValue()));
+ }
+ } else {
+ list = new ArrayList<Object>();
+ list.add(o);
+ }
+ }
+ return new QueryResultImpl(list,
+ Collections.singletonList(ConsoleMessages.JavaPage_no_info), queryTime);
+ } catch (EvalError e) {
+ return new QueryResultImpl(e);
+ } catch (HibernateException e) {
+ return new QueryResultImpl(e);
+ }
+ }
+
+ private static List<String> getHQLPathNames(Query query) {
+ List<String> l = Collections.emptyList();
+
+ if(query==null) return l;
+ String[] returnAliases = null;
+ try {
+ returnAliases = query.getReturnAliases();
+ } catch(NullPointerException e) {
+ // ignore -
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2188
+ }
+ if(returnAliases==null) {
+ Type[] t;
+ try {
+ t = query.getReturnTypes();
+ } catch(NullPointerException npe) {
+ t = new Type[] { null };
+ // ignore -
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2188
+ }
+ l = new ArrayList<String>(t.length);
+
+ for (int i = 0; i < t.length; i++) {
+ Type type = t[i];
+ if(type==null) {
+ l.add("<multiple types>"); //$NON-NLS-1$
+ } else {
+ l.add(type.getName() );
+ }
+ }
+ } else {
+ String[] t = returnAliases;
+ l = new ArrayList<String>(t.length);
+
+ for (int i = 0; i < t.length; i++) {
+ l.add(t[i]);
+ }
+ }
+
+ return l;
+ }
+
+ private static void setupParameters(Query query2, QueryInputModel model) {
+ if(model.getMaxResults()!=null) {
+ query2.setMaxResults( model.getMaxResults().intValue() );
+ }
+ ConsoleQueryParameter[] qp = model.getQueryParameters();
+ for (int i = 0; i < qp.length; i++) {
+ ConsoleQueryParameter parameter = qp[i];
+
+ try {
+ int pos = Integer.parseInt(parameter.getName());
+ //FIXME no method to set positioned list value
+ query2.setParameter(pos, calcValue( parameter ), parameter.getType());
+ } catch(NumberFormatException nfe) {
+ Object value = parameter.getValue();
+ if (value != null && value.getClass().isArray()){
+ Object[] values = (Object[])value;
+ query2.setParameterList(parameter.getName(), Arrays.asList(values),
parameter.getType());
+ } else {
+ query2.setParameter(parameter.getName(), calcValue( parameter ),
parameter.getType());
+ }
+ }
+ }
+ }
+
+ private static Object calcValue(ConsoleQueryParameter parameter) {
+ return parameter.getValueForQuery();
+ }
+
+
+ private static Interpreter setupInterpreter(Session session) throws EvalError,
HibernateException {
+ Interpreter interpreter = new Interpreter();
+
+ interpreter.set("session", session); //$NON-NLS-1$
+ interpreter.setClassLoader( Thread.currentThread().getContextClassLoader() );
+ SessionImplementor si = (SessionImplementor)session;
+
+ Map<String, ?> map = si.getFactory().getAllClassMetadata();
+
+ Iterator<String> iterator = map.keySet().iterator();
+ //TODO: filter non classes.
+ String imports = ""; //$NON-NLS-1$
+ while (iterator.hasNext() ) {
+ String element = iterator.next();
+ imports += "import " + element + ";\n"; //$NON-NLS-1$
//$NON-NLS-2$
+ }
+
+ imports += "import org.hibernate.criterion.*;\n"; //$NON-NLS-1$
+ imports += "import org.hibernate.*;\n"; //$NON-NLS-1$
+ // TODO: expose the parameters as values to be used in the code.
+ interpreter.eval(imports);
+
+ return interpreter;
+ }
+
+}