[jbosstools-commits] JBoss Tools SVN: r39434 - in trunk/hibernatetools/plugins: org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views and 3 other directories.
jbosstools-commits at lists.jboss.org
jbosstools-commits at lists.jboss.org
Mon Mar 12 07:15:52 EDT 2012
Author: dgeraskov
Date: 2012-03-12 07:15:51 -0400 (Mon, 12 Mar 2012)
New Revision: 39434
Added:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/QueryHelper.java
trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_6/src/org/jboss/tools/hibernate3_6/QueryHelper.java
trunk/hibernatetools/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/QueryHelper.java
Removed:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/QueryExecutor.java
trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_6/src/org/jboss/tools/hibernate3_6/QueryExecutor.java
trunk/hibernatetools/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/QueryExecutor.java
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/ExecuteQueryAction.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/DynamicSQLPreviewView.java
trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/HibernateExtension3_5.java
trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_6/src/org/jboss/tools/hibernate3_6/HibernateExtension3_6.java
trunk/hibernatetools/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/HibernateExtension4_0.java
Log:
https://issues.jboss.org/browse/JBIDE-11226
Fix session factory not created problem
Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/ExecuteQueryAction.java
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/ExecuteQueryAction.java 2012-03-12 08:35:13 UTC (rev 39433)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/ExecuteQueryAction.java 2012-03-12 11:15:51 UTC (rev 39434)
@@ -63,9 +63,10 @@
ConsoleConfiguration cfg = queryEditor.getConsoleConfiguration();
if (cfg != null) {
- if (!cfg.getHibernateExtension().isSessionFactoryCreated()) {
+ //keep states of ConsoleConfiguration and HibernateExtension synchronized
+ if (!(cfg.isSessionFactoryCreated() && cfg.getHibernateExtension().isSessionFactoryCreated())) {
if (queryEditor.askUserForConfiguration(cfg.getName())) {
- if (!cfg.getHibernateExtension().hasConfiguration()) {
+ if (!(cfg.hasConfiguration() && cfg.getHibernateExtension().hasConfiguration())) {
try {
cfg.build();
} catch (HibernateException he) {
@@ -75,8 +76,8 @@
' ' + cfg.getName(), he);
}
}
- if (cfg.getHibernateExtension().hasConfiguration()) {
- cfg.getHibernateExtension().buildSessionFactory();
+ if (cfg.hasConfiguration() && cfg.getHibernateExtension().hasConfiguration()) {
+ cfg.buildSessionFactory();
queryEditor.executeQuery(cfg);
}
}
Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/DynamicSQLPreviewView.java
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/DynamicSQLPreviewView.java 2012-03-12 08:35:13 UTC (rev 39433)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/DynamicSQLPreviewView.java 2012-03-12 11:15:51 UTC (rev 39434)
@@ -46,6 +46,7 @@
import org.hibernate.SessionFactory;
import org.hibernate.console.ConsoleConfiguration;
import org.hibernate.console.execution.ExecutionContext;
+import org.hibernate.console.ext.HibernateExtension;
import org.hibernate.eclipse.console.HibernateConsoleMessages;
import org.hibernate.eclipse.console.utils.QLFormatHelper;
import org.hibernate.eclipse.hqleditor.HQLEditor;
@@ -116,7 +117,10 @@
}
}
private void setCurrentEditor(HQLEditor editor) {
- if(editor==currentEditor) return;
+ if(editor==currentEditor) {
+ updateText(currentEditor);
+ return;
+ }
if(currentEditor!=null) {
reconciler.uninstall();
}
@@ -139,8 +143,9 @@
if(StringHelper.isEmpty( editor.getQueryString() )) {
textViewer.getDocument().set( HibernateConsoleMessages.DynamicSQLPreviewView_empty_hql_query );
} else if(consoleConfiguration!=null) {
- if(consoleConfiguration.isSessionFactoryCreated()) {
- String generateSQL = generateSQL(consoleConfiguration.getExecutionContext(), consoleConfiguration.getSessionFactory(), editor.getQueryString());
+ HibernateExtension hibernateExtension = consoleConfiguration.getHibernateExtension();
+ if(hibernateExtension.isSessionFactoryCreated()) {
+ String generateSQL = hibernateExtension.generateSQL(editor.getQueryString());
if(StringHelper.isEmpty( generateSQL )) {
textViewer.getDocument().set( HibernateConsoleMessages.DynamicSQLPreviewView_no_sql_generated );
} else {
@@ -158,74 +163,6 @@
}
}
- public String generateSQL(ExecutionContext context, final SessionFactory sf, final String query) {
-
- if(StringHelper.isEmpty(query)) return ""; //$NON-NLS-1$
-
- String result;
-
- result = (String) context.execute(new ExecutionContext.Command() {
- public Object execute() {
- try {
- SessionFactoryImpl sfimpl = (SessionFactoryImpl) sf; // hack - to get to the actual queries..
- StringBuffer str = new StringBuffer(256);
- HQLQueryPlan plan = new HQLQueryPlan(query, false, Collections.EMPTY_MAP, sfimpl);
-
- QueryTranslator[] translators = plan.getTranslators();
- for (int i = 0; i < translators.length; i++) {
- QueryTranslator translator = translators[i];
- if(translator.isManipulationStatement()) {
- str.append(HibernateConsoleMessages.DynamicSQLPreviewView_manipulation_of + i + ":"); //$NON-NLS-1$
- Iterator<?> iterator = translator.getQuerySpaces().iterator();
- while ( iterator.hasNext() ) {
- Object qspace = iterator.next();
- str.append(qspace);
- if(iterator.hasNext()) { str.append(", "); } //$NON-NLS-1$
- }
-
- } else {
- Type[] returnTypes = translator.getReturnTypes();
- str.append(i +": "); //$NON-NLS-1$
- for (int j = 0; j < returnTypes.length; j++) {
- Type returnType = returnTypes[j];
- str.append(returnType.getName());
- if(j<returnTypes.length-1) { str.append(", "); } //$NON-NLS-1$
- }
- }
- str.append("\n-----------------\n"); //$NON-NLS-1$
- Iterator<?> sqls = translator.collectSqlStrings().iterator();
- while ( sqls.hasNext() ) {
- String sql = (String) sqls.next();
- str.append(QLFormatHelper.formatForScreen(sql));
- str.append("\n\n"); //$NON-NLS-1$
- }
- }
- return str.toString();
- } catch(Throwable t) {
- //StringWriter sw = new StringWriter();
- StringBuffer msgs = new StringBuffer();
-
- Throwable cause = t;
- while(cause!=null) {
- msgs.append(t);
- if(cause.getCause()==cause) {
- cause=null;
- } else {
- cause = cause.getCause();
- if(cause!=null) msgs.append(HibernateConsoleMessages.DynamicSQLPreviewView_caused_by);
- }
- }
- //t.printStackTrace(new PrintWriter(sw));
- //return sw.getBuffer().toString();
- return msgs.toString();
- }
-
- }
- });
-
- return result;
- }
-
public void createPartControl(Composite parent) {
textViewer = new HQLSourceViewer( parent, new VerticalRuler(1), null, false, SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL );
//textViewer.setEditable(false);
Modified: trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/HibernateExtension3_5.java
===================================================================
--- trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/HibernateExtension3_5.java 2012-03-12 08:35:13 UTC (rev 39433)
+++ trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/HibernateExtension3_5.java 2012-03-12 11:15:51 UTC (rev 39434)
@@ -73,7 +73,7 @@
try {
try {
session = sessionFactory.openSession();
- return QueryExecutor.executeHQLQuery(session, hql, queryParameters);
+ return QueryHelper.executeHQLQuery(session, hql, queryParameters);
} catch (Throwable e){
//Incompatible library versions could throw subclasses of Error, like AbstractMethodError
//may be there is a sense to say to user that the reason is probably a wrong CC version
@@ -98,7 +98,7 @@
try {
try {
session = sessionFactory.openSession();
- return QueryExecutor.executeCriteriaQuery(session, criteriaCode, model);
+ return QueryHelper.executeCriteriaQuery(session, criteriaCode, model);
} catch (Throwable e){
//Incompatible library versions could throw subclasses of Error, like AbstractMethodError
//may be there is a sense to say to user that the reason is probably a wrong CC version
@@ -320,4 +320,8 @@
public boolean isSessionFactoryCreated() {
return sessionFactory != null;
}
+
+ public String generateSQL(final String query) {
+ return QueryHelper.generateSQL(executionContext, sessionFactory, query);
+ }
}
Deleted: trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/QueryExecutor.java
===================================================================
--- trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/QueryExecutor.java 2012-03-12 08:35:13 UTC (rev 39433)
+++ trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/QueryExecutor.java 2012-03-12 11:15:51 UTC (rev 39434)
@@ -1,204 +0,0 @@
-/*******************************************************************************
- * 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();
- QueryResultImpl result = new QueryResultImpl(list,
- queryTime);
- try {
- 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);
- }
- 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;
- }
-
-}
Copied: trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/QueryHelper.java (from rev 38910, trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/QueryExecutor.java)
===================================================================
--- trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/QueryHelper.java (rev 0)
+++ trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_5/src/org/jboss/tools/hibernate3_5/QueryHelper.java 2012-03-12 11:15:51 UTC (rev 39434)
@@ -0,0 +1,275 @@
+/*******************************************************************************
+ * 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.SessionFactory;
+import org.hibernate.console.ConsoleMessages;
+import org.hibernate.console.ConsoleQueryParameter;
+import org.hibernate.console.QueryInputModel;
+import org.hibernate.console.execution.ExecutionContext;
+import org.hibernate.console.ext.HibernateException;
+import org.hibernate.console.ext.QueryResult;
+import org.hibernate.console.ext.QueryResultImpl;
+import org.hibernate.eclipse.console.HibernateConsoleMessages;
+import org.hibernate.eclipse.console.utils.QLFormatHelper;
+import org.hibernate.engine.SessionImplementor;
+import org.hibernate.engine.query.HQLQueryPlan;
+import org.hibernate.hql.QueryTranslator;
+import org.hibernate.impl.SessionFactoryImpl;
+import org.hibernate.type.Type;
+import org.hibernate.util.xpl.StringHelper;
+
+import bsh.EvalError;
+import bsh.Interpreter;
+
+/**
+ * @author Dmitry Geraskov
+ *
+ */
+public class QueryHelper {
+
+ 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();
+ QueryResultImpl result = new QueryResultImpl(list,
+ queryTime);
+ try {
+ 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);
+ }
+ 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;
+ }
+
+ static String generateSQL(ExecutionContext executionContext, final SessionFactory sessionFactory, final String query) {
+
+ if(StringHelper.isEmpty(query)) return ""; //$NON-NLS-1$
+
+ String result = (String) executionContext.execute(new ExecutionContext.Command() {
+ public Object execute() {
+ try {
+ SessionFactoryImpl sfimpl = (SessionFactoryImpl) sessionFactory; // hack - to get to the actual queries..
+ StringBuffer str = new StringBuffer(256);
+ HQLQueryPlan plan = new HQLQueryPlan(query, false, Collections.EMPTY_MAP, sfimpl);
+
+ QueryTranslator[] translators = plan.getTranslators();
+ for (int i = 0; i < translators.length; i++) {
+ QueryTranslator translator = translators[i];
+ if(translator.isManipulationStatement()) {
+ str.append(HibernateConsoleMessages.DynamicSQLPreviewView_manipulation_of + i + ":"); //$NON-NLS-1$
+ Iterator<?> iterator = translator.getQuerySpaces().iterator();
+ while ( iterator.hasNext() ) {
+ Object qspace = iterator.next();
+ str.append(qspace);
+ if(iterator.hasNext()) { str.append(", "); } //$NON-NLS-1$
+ }
+
+ } else {
+ Type[] returnTypes = translator.getReturnTypes();
+ str.append(i +": "); //$NON-NLS-1$
+ for (int j = 0; j < returnTypes.length; j++) {
+ Type returnType = returnTypes[j];
+ str.append(returnType.getName());
+ if(j<returnTypes.length-1) { str.append(", "); } //$NON-NLS-1$
+ }
+ }
+ str.append("\n-----------------\n"); //$NON-NLS-1$
+ Iterator<?> sqls = translator.collectSqlStrings().iterator();
+ while ( sqls.hasNext() ) {
+ String sql = (String) sqls.next();
+ str.append(QLFormatHelper.formatForScreen(sql));
+ str.append("\n\n"); //$NON-NLS-1$
+ }
+ }
+ return str.toString();
+ } catch(Throwable t) {
+ StringBuffer msgs = new StringBuffer();
+
+ Throwable cause = t;
+ while(cause!=null) {
+ msgs.append(t);
+ if(cause.getCause()==cause) {
+ cause=null;
+ } else {
+ cause = cause.getCause();
+ if(cause!=null) msgs.append(HibernateConsoleMessages.DynamicSQLPreviewView_caused_by);
+ }
+ }
+ return msgs.toString();
+ }
+
+ }
+ });
+
+ return result;
+ }
+
+}
Modified: trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_6/src/org/jboss/tools/hibernate3_6/HibernateExtension3_6.java
===================================================================
--- trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_6/src/org/jboss/tools/hibernate3_6/HibernateExtension3_6.java 2012-03-12 08:35:13 UTC (rev 39433)
+++ trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_6/src/org/jboss/tools/hibernate3_6/HibernateExtension3_6.java 2012-03-12 11:15:51 UTC (rev 39434)
@@ -71,7 +71,7 @@
try {
try {
session = sessionFactory.openSession();
- return QueryExecutor.executeHQLQuery(this, session, hql, queryParameters);
+ return QueryHelper.executeHQLQuery(this, session, hql, queryParameters);
} catch (Throwable e){
//Incompatible library versions could throw subclasses of Error, like AbstractMethodError
//may be there is a sense to say to user that the reason is probably a wrong CC version
@@ -98,7 +98,7 @@
try {
try {
session = sessionFactory.openSession();
- return QueryExecutor.executeCriteriaQuery(session, criteriaCode, model);
+ return QueryHelper.executeCriteriaQuery(session, criteriaCode, model);
} catch (Throwable e){
//Incompatible library versions could throw subclasses of Error, like AbstractMethodError
//may be there is a sense to say to user that the reason is probably a wrong CC version
@@ -238,6 +238,10 @@
return prefs.getName();
}
+ public ExecutionContext getExecutionContext() {
+ return executionContext;
+ }
+
public Object execute(Command c) {
if (executionContext != null) {
return executionContext.execute(c);
@@ -323,4 +327,8 @@
public boolean isSessionFactoryCreated() {
return sessionFactory != null;
}
+
+ public String generateSQL(final String query) {
+ return QueryHelper.generateSQL(executionContext, sessionFactory, query);
+ }
}
\ No newline at end of file
Deleted: trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_6/src/org/jboss/tools/hibernate3_6/QueryExecutor.java
===================================================================
--- trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_6/src/org/jboss/tools/hibernate3_6/QueryExecutor.java 2012-03-12 08:35:13 UTC (rev 39433)
+++ trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_6/src/org/jboss/tools/hibernate3_6/QueryExecutor.java 2012-03-12 11:15:51 UTC (rev 39434)
@@ -1,230 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2012 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_6;
-
-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.HibernateConsoleRuntimeException;
-import org.hibernate.console.QueryInputModel;
-import org.hibernate.console.execution.ExecutionContext.Command;
-import org.hibernate.console.ext.HibernateException;
-import org.hibernate.console.ext.HibernateExtension;
-import org.hibernate.console.ext.QueryResult;
-import org.hibernate.console.ext.QueryResultImpl;
-import org.hibernate.engine.SessionImplementor;
-import org.hibernate.type.Type;
-import org.hibernate.util.ReflectHelper;
-
-import bsh.EvalError;
-import bsh.Interpreter;
-
-/**
- * @author Dmitry Geraskov
- *
- */
-public class QueryExecutor {
-
- public static QueryResult executeHQLQuery(HibernateExtension hibernateExtension, Session session, String hql,
- final QueryInputModel queryParameters) {
-
- final Query query = session.createQuery(hql);
- List<Object> list = Collections.emptyList();
- long queryTime = 0;
-
- list = new ArrayList<Object>();
- hibernateExtension.execute(new Command() {
-
- @Override
- public Object execute() {
- setupParameters(query, queryParameters);
- return null;
- }
- });
-
- long startTime = System.currentTimeMillis();
- QueryResultImpl result = new QueryResultImpl(list,
- queryTime);
- try {
- 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);
- }
- 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];
-
- String typeName = parameter.getType().getClass().getName();
- try {
- int pos = Integer.parseInt(parameter.getName());
- //FIXME no method to set positioned list value
- query2.setParameter(pos, calcValue( parameter ), convertToType(typeName));
- } catch(NumberFormatException nfe) {
- Object value = parameter.getValue();
- if (value != null && value.getClass().isArray()){
- Object[] values = (Object[])value;
- query2.setParameterList(parameter.getName(), Arrays.asList(values), convertToType(typeName));
- } else {
- query2.setParameter(parameter.getName(), calcValue( parameter ), convertToType(typeName));
- }
- }
- }
- }
-
- /**
- * Method converts Hibernate3 to Hibernate3_6 classes
- * @param typeClassName
- * @return
- */
- private static Type convertToType(String typeClassName){
- try {
- return (Type) ReflectHelper.classForName(typeClassName).newInstance();
- } catch (Exception e) {
- throw new HibernateConsoleRuntimeException("Can't instantiate hibernate type " + typeClassName, e);
- }
- }
-
- 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;
- }
-
-}
Copied: trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_6/src/org/jboss/tools/hibernate3_6/QueryHelper.java (from rev 39050, trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_6/src/org/jboss/tools/hibernate3_6/QueryExecutor.java)
===================================================================
--- trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_6/src/org/jboss/tools/hibernate3_6/QueryHelper.java (rev 0)
+++ trunk/hibernatetools/plugins/org.jboss.tools.hibernate3_6/src/org/jboss/tools/hibernate3_6/QueryHelper.java 2012-03-12 11:15:51 UTC (rev 39434)
@@ -0,0 +1,302 @@
+/*******************************************************************************
+ * Copyright (c) 2012 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_6;
+
+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.SessionFactory;
+import org.hibernate.console.ConsoleMessages;
+import org.hibernate.console.ConsoleQueryParameter;
+import org.hibernate.console.HibernateConsoleRuntimeException;
+import org.hibernate.console.QueryInputModel;
+import org.hibernate.console.execution.ExecutionContext;
+import org.hibernate.console.execution.ExecutionContext.Command;
+import org.hibernate.console.ext.HibernateException;
+import org.hibernate.console.ext.HibernateExtension;
+import org.hibernate.console.ext.QueryResult;
+import org.hibernate.console.ext.QueryResultImpl;
+import org.hibernate.eclipse.console.HibernateConsoleMessages;
+import org.hibernate.eclipse.console.utils.QLFormatHelper;
+import org.hibernate.engine.SessionImplementor;
+import org.hibernate.engine.query.HQLQueryPlan;
+import org.hibernate.hql.QueryTranslator;
+import org.hibernate.impl.SessionFactoryImpl;
+import org.hibernate.type.Type;
+import org.hibernate.util.ReflectHelper;
+import org.hibernate.util.xpl.StringHelper;
+
+import bsh.EvalError;
+import bsh.Interpreter;
+
+/**
+ * @author Dmitry Geraskov
+ *
+ */
+public class QueryHelper {
+
+ public static QueryResult executeHQLQuery(HibernateExtension hibernateExtension, Session session, String hql,
+ final QueryInputModel queryParameters) {
+
+ final Query query = session.createQuery(hql);
+ List<Object> list = Collections.emptyList();
+ long queryTime = 0;
+
+ list = new ArrayList<Object>();
+ hibernateExtension.execute(new Command() {
+
+ @Override
+ public Object execute() {
+ setupParameters(query, queryParameters);
+ return null;
+ }
+ });
+
+ long startTime = System.currentTimeMillis();
+ QueryResultImpl result = new QueryResultImpl(list,
+ queryTime);
+ try {
+ 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);
+ }
+ 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];
+
+ String typeName = parameter.getType().getClass().getName();
+ try {
+ int pos = Integer.parseInt(parameter.getName());
+ //FIXME no method to set positioned list value
+ query2.setParameter(pos, calcValue( parameter ), convertToType(typeName));
+ } catch(NumberFormatException nfe) {
+ Object value = parameter.getValue();
+ if (value != null && value.getClass().isArray()){
+ Object[] values = (Object[])value;
+ query2.setParameterList(parameter.getName(), Arrays.asList(values), convertToType(typeName));
+ } else {
+ query2.setParameter(parameter.getName(), calcValue( parameter ), convertToType(typeName));
+ }
+ }
+ }
+ }
+
+ /**
+ * Method converts Hibernate3 to Hibernate3_6 classes
+ * @param typeClassName
+ * @return
+ */
+ private static Type convertToType(String typeClassName){
+ try {
+ return (Type) ReflectHelper.classForName(typeClassName).newInstance();
+ } catch (Exception e) {
+ throw new HibernateConsoleRuntimeException("Can't instantiate hibernate type " + typeClassName, e);
+ }
+ }
+
+ 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;
+ }
+
+ static String generateSQL(ExecutionContext executionContext, final SessionFactory sessionFactory, final String query) {
+
+ if(StringHelper.isEmpty(query)) return ""; //$NON-NLS-1$
+
+ String result = (String) executionContext.execute(new ExecutionContext.Command() {
+ public Object execute() {
+ try {
+ SessionFactoryImpl sfimpl = (SessionFactoryImpl) sessionFactory; // hack - to get to the actual queries..
+ StringBuffer str = new StringBuffer(256);
+ HQLQueryPlan plan = new HQLQueryPlan(query, false, Collections.EMPTY_MAP, sfimpl);
+
+ QueryTranslator[] translators = plan.getTranslators();
+ for (int i = 0; i < translators.length; i++) {
+ QueryTranslator translator = translators[i];
+ if(translator.isManipulationStatement()) {
+ str.append(HibernateConsoleMessages.DynamicSQLPreviewView_manipulation_of + i + ":"); //$NON-NLS-1$
+ Iterator<?> iterator = translator.getQuerySpaces().iterator();
+ while ( iterator.hasNext() ) {
+ Object qspace = iterator.next();
+ str.append(qspace);
+ if(iterator.hasNext()) { str.append(", "); } //$NON-NLS-1$
+ }
+
+ } else {
+ Type[] returnTypes = translator.getReturnTypes();
+ str.append(i +": "); //$NON-NLS-1$
+ for (int j = 0; j < returnTypes.length; j++) {
+ Type returnType = returnTypes[j];
+ str.append(returnType.getName());
+ if(j<returnTypes.length-1) { str.append(", "); } //$NON-NLS-1$
+ }
+ }
+ str.append("\n-----------------\n"); //$NON-NLS-1$
+ Iterator<?> sqls = translator.collectSqlStrings().iterator();
+ while ( sqls.hasNext() ) {
+ String sql = (String) sqls.next();
+ str.append(QLFormatHelper.formatForScreen(sql));
+ str.append("\n\n"); //$NON-NLS-1$
+ }
+ }
+ return str.toString();
+ } catch(Throwable t) {
+ StringBuffer msgs = new StringBuffer();
+
+ Throwable cause = t;
+ while(cause!=null) {
+ msgs.append(t);
+ if(cause.getCause()==cause) {
+ cause=null;
+ } else {
+ cause = cause.getCause();
+ if(cause!=null) msgs.append(HibernateConsoleMessages.DynamicSQLPreviewView_caused_by);
+ }
+ }
+ return msgs.toString();
+ }
+
+ }
+ });
+
+ return result;
+ }
+
+
+}
Modified: trunk/hibernatetools/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/HibernateExtension4_0.java
===================================================================
--- trunk/hibernatetools/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/HibernateExtension4_0.java 2012-03-12 08:35:13 UTC (rev 39433)
+++ trunk/hibernatetools/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/HibernateExtension4_0.java 2012-03-12 11:15:51 UTC (rev 39434)
@@ -15,6 +15,7 @@
import java.security.PrivilegedAction;
import java.sql.DriverManager;
import java.sql.SQLException;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@@ -36,9 +37,16 @@
import org.hibernate.console.ext.QueryResultImpl;
import org.hibernate.console.preferences.ConsoleConfigurationPreferences;
import org.hibernate.console.preferences.PreferencesClassPathUtils;
+import org.hibernate.eclipse.console.HibernateConsoleMessages;
+import org.hibernate.eclipse.console.utils.QLFormatHelper;
+import org.hibernate.engine.query.spi.HQLQueryPlan;
+import org.hibernate.hql.spi.QueryTranslator;
+import org.hibernate.internal.SessionFactoryImpl;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.service.internal.StandardServiceRegistryImpl;
+import org.hibernate.type.Type;
+import org.hibernate.util.xpl.StringHelper;
/**
*
@@ -76,7 +84,7 @@
try {
try {
session = sessionFactory.openSession();
- return QueryExecutor.executeHQLQuery(this, session, hql, queryParameters);
+ return QueryHelper.executeHQLQuery(this, session, hql, queryParameters);
} catch (Throwable e){
//Incompatible library versions could throw subclasses of Error, like AbstractMethodError
//may be there is a sense to say to user that the reason is probably a wrong CC version
@@ -103,7 +111,7 @@
try {
try {
session = sessionFactory.openSession();
- return QueryExecutor.executeCriteriaQuery(session, criteriaCode, model);
+ return QueryHelper.executeCriteriaQuery(session, criteriaCode, model);
} catch (Throwable e){
//Incompatible library versions could throw subclasses of Error, like AbstractMethodError
//may be there is a sense to say to user that the reason is probably a wrong CC version
@@ -248,6 +256,10 @@
return prefs.getName();
}
+ public ExecutionContext getExecutionContext() {
+ return executionContext;
+ }
+
public Object execute(Command c) {
if (executionContext != null) {
return executionContext.execute(c);
@@ -333,4 +345,8 @@
public boolean isSessionFactoryCreated() {
return sessionFactory != null;
}
+
+ public String generateSQL(final String query) {
+ return QueryHelper.generateSQL(executionContext, sessionFactory, query);
+ }
}
\ No newline at end of file
Deleted: trunk/hibernatetools/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/QueryExecutor.java
===================================================================
--- trunk/hibernatetools/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/QueryExecutor.java 2012-03-12 08:35:13 UTC (rev 39433)
+++ trunk/hibernatetools/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/QueryExecutor.java 2012-03-12 11:15:51 UTC (rev 39434)
@@ -1,230 +0,0 @@
-/*******************************************************************************
- * 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.HibernateConsoleRuntimeException;
-import org.hibernate.console.QueryInputModel;
-import org.hibernate.console.execution.ExecutionContext.Command;
-import org.hibernate.console.ext.HibernateException;
-import org.hibernate.console.ext.HibernateExtension;
-import org.hibernate.console.ext.QueryResult;
-import org.hibernate.console.ext.QueryResultImpl;
-import org.hibernate.engine.spi.SessionImplementor;
-import org.hibernate.internal.util.ReflectHelper;
-import org.hibernate.type.Type;
-
-import bsh.EvalError;
-import bsh.Interpreter;
-
-/**
- * @author Dmitry Geraskov
- *
- */
-public class QueryExecutor {
-
- public static QueryResult executeHQLQuery(HibernateExtension hibernateExtension, Session session, String hql,
- final QueryInputModel queryParameters) {
-
- final Query query = session.createQuery(hql);
- List<Object> list = Collections.emptyList();
- long queryTime = 0;
-
- list = new ArrayList<Object>();
- hibernateExtension.execute(new Command() {
-
- @Override
- public Object execute() {
- setupParameters(query, queryParameters);
- return null;
- }
- });
-
- long startTime = System.currentTimeMillis();
- QueryResultImpl result = new QueryResultImpl(list,
- queryTime);
- try {
- 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);
- }
- 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];
-
- String typeName = parameter.getType().getClass().getName();
- try {
- int pos = Integer.parseInt(parameter.getName());
- //FIXME no method to set positioned list value
- query2.setParameter(pos, calcValue( parameter ), convertToType(typeName));
- } catch(NumberFormatException nfe) {
- Object value = parameter.getValue();
- if (value != null && value.getClass().isArray()){
- Object[] values = (Object[])value;
- query2.setParameterList(parameter.getName(), Arrays.asList(values), convertToType(typeName));
- } else {
- query2.setParameter(parameter.getName(), calcValue( parameter ), convertToType(typeName));
- }
- }
- }
- }
-
- /**
- * Method converts Hibernate3 to Hibernate4 classes
- * @param typeClassName
- * @return
- */
- private static Type convertToType(String typeClassName){
- try {
- return (Type) ReflectHelper.classForName(typeClassName).newInstance();
- } catch (Exception e) {
- throw new HibernateConsoleRuntimeException("Can't instantiate hibernate type " + typeClassName, e);
- }
- }
-
- 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;
- }
-
-}
Copied: trunk/hibernatetools/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/QueryHelper.java (from rev 38910, trunk/hibernatetools/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/QueryExecutor.java)
===================================================================
--- trunk/hibernatetools/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/QueryHelper.java (rev 0)
+++ trunk/hibernatetools/plugins/org.jboss.tools.hibernate4_0/src/org/jboss/tools/hibernate4_0/QueryHelper.java 2012-03-12 11:15:51 UTC (rev 39434)
@@ -0,0 +1,302 @@
+/*******************************************************************************
+ * 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.SessionFactory;
+import org.hibernate.console.ConsoleMessages;
+import org.hibernate.console.ConsoleQueryParameter;
+import org.hibernate.console.HibernateConsoleRuntimeException;
+import org.hibernate.console.QueryInputModel;
+import org.hibernate.console.execution.ExecutionContext;
+import org.hibernate.console.execution.ExecutionContext.Command;
+import org.hibernate.console.ext.HibernateException;
+import org.hibernate.console.ext.HibernateExtension;
+import org.hibernate.console.ext.QueryResult;
+import org.hibernate.console.ext.QueryResultImpl;
+import org.hibernate.eclipse.console.HibernateConsoleMessages;
+import org.hibernate.eclipse.console.utils.QLFormatHelper;
+import org.hibernate.engine.query.spi.HQLQueryPlan;
+import org.hibernate.engine.spi.SessionImplementor;
+import org.hibernate.hql.spi.QueryTranslator;
+import org.hibernate.internal.SessionFactoryImpl;
+import org.hibernate.internal.util.ReflectHelper;
+import org.hibernate.type.Type;
+import org.hibernate.util.xpl.StringHelper;
+
+import bsh.EvalError;
+import bsh.Interpreter;
+
+/**
+ * @author Dmitry Geraskov
+ *
+ */
+public class QueryHelper {
+
+ public static QueryResult executeHQLQuery(HibernateExtension hibernateExtension, Session session, String hql,
+ final QueryInputModel queryParameters) {
+
+ final Query query = session.createQuery(hql);
+ List<Object> list = Collections.emptyList();
+ long queryTime = 0;
+
+ list = new ArrayList<Object>();
+ hibernateExtension.execute(new Command() {
+
+ @Override
+ public Object execute() {
+ setupParameters(query, queryParameters);
+ return null;
+ }
+ });
+
+ long startTime = System.currentTimeMillis();
+ QueryResultImpl result = new QueryResultImpl(list,
+ queryTime);
+ try {
+ 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);
+ }
+ 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];
+
+ String typeName = parameter.getType().getClass().getName();
+ try {
+ int pos = Integer.parseInt(parameter.getName());
+ //FIXME no method to set positioned list value
+ query2.setParameter(pos, calcValue( parameter ), convertToType(typeName));
+ } catch(NumberFormatException nfe) {
+ Object value = parameter.getValue();
+ if (value != null && value.getClass().isArray()){
+ Object[] values = (Object[])value;
+ query2.setParameterList(parameter.getName(), Arrays.asList(values), convertToType(typeName));
+ } else {
+ query2.setParameter(parameter.getName(), calcValue( parameter ), convertToType(typeName));
+ }
+ }
+ }
+ }
+
+ /**
+ * Method converts Hibernate3 to Hibernate4 classes
+ * @param typeClassName
+ * @return
+ */
+ private static Type convertToType(String typeClassName){
+ try {
+ return (Type) ReflectHelper.classForName(typeClassName).newInstance();
+ } catch (Exception e) {
+ throw new HibernateConsoleRuntimeException("Can't instantiate hibernate type " + typeClassName, e);
+ }
+ }
+
+ 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;
+ }
+
+ static String generateSQL(ExecutionContext executionContext, final SessionFactory sessionFactory, final String query) {
+
+ if(StringHelper.isEmpty(query)) return ""; //$NON-NLS-1$
+
+ String result = (String) executionContext.execute(new ExecutionContext.Command() {
+ public Object execute() {
+ try {
+ SessionFactoryImpl sfimpl = (SessionFactoryImpl) sessionFactory; // hack - to get to the actual queries..
+ StringBuffer str = new StringBuffer(256);
+ HQLQueryPlan plan = new HQLQueryPlan(query, false, Collections.EMPTY_MAP, sfimpl);
+
+ QueryTranslator[] translators = plan.getTranslators();
+ for (int i = 0; i < translators.length; i++) {
+ QueryTranslator translator = translators[i];
+ if(translator.isManipulationStatement()) {
+ str.append(HibernateConsoleMessages.DynamicSQLPreviewView_manipulation_of + i + ":"); //$NON-NLS-1$
+ Iterator<?> iterator = translator.getQuerySpaces().iterator();
+ while ( iterator.hasNext() ) {
+ Object qspace = iterator.next();
+ str.append(qspace);
+ if(iterator.hasNext()) { str.append(", "); } //$NON-NLS-1$
+ }
+
+ } else {
+ Type[] returnTypes = translator.getReturnTypes();
+ str.append(i +": "); //$NON-NLS-1$
+ for (int j = 0; j < returnTypes.length; j++) {
+ Type returnType = returnTypes[j];
+ str.append(returnType.getName());
+ if(j<returnTypes.length-1) { str.append(", "); } //$NON-NLS-1$
+ }
+ }
+ str.append("\n-----------------\n"); //$NON-NLS-1$
+ Iterator<?> sqls = translator.collectSqlStrings().iterator();
+ while ( sqls.hasNext() ) {
+ String sql = (String) sqls.next();
+ str.append(QLFormatHelper.formatForScreen(sql));
+ str.append("\n\n"); //$NON-NLS-1$
+ }
+ }
+ return str.toString();
+ } catch(Throwable t) {
+ StringBuffer msgs = new StringBuffer();
+
+ Throwable cause = t;
+ while(cause!=null) {
+ msgs.append(t);
+ if(cause.getCause()==cause) {
+ cause=null;
+ } else {
+ cause = cause.getCause();
+ if(cause!=null) msgs.append(HibernateConsoleMessages.DynamicSQLPreviewView_caused_by);
+ }
+ }
+ return msgs.toString();
+ }
+
+ }
+ });
+
+ return result;
+ }
+
+
+}
More information about the jbosstools-commits
mailing list