Author: dgeraskov
Date: 2010-11-17 05:07:01 -0500 (Wed, 17 Nov 2010)
New Revision: 26662
Added:
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/hqleditor/HQLEditorTest.java
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/AbstractQueryEditor.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/QueryEditor.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/QueryParametersView.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/hqleditor/HQLEditor.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/SaveQueryEditorListener.java
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/HQLQueryPage.java
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/META-INF/MANIFEST.MF
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/ConsolePluginAllTests.java
Log:
https://jira.jboss.org/browse/JBIDE-7609
Fix hql-comment problems
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/HQLQueryPage.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/HQLQueryPage.java 2010-11-17
09:45:49 UTC (rev 26661)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/HQLQueryPage.java 2010-11-17
10:07:01 UTC (rev 26662)
@@ -111,7 +111,7 @@
public void setSession(Session s) {
super.setSession(s);
try {
- query = this.getSession().createQuery(getHQLQueryString());
+ query = this.getSession().createQuery(getQueryString());
} catch (HibernateException e) {
addException(e);
} catch (Exception e) {
@@ -126,57 +126,6 @@
return queryString; // cannot use query since it might be null because of an error!
}
- /**
- * The method removes SQL comments from <code>queryString</code>
- * as HSL doesn't support comments.
- * @return
- */
- public String getHQLQueryString(){
- StringBuilder clearHQL = new StringBuilder();
- int state = 0;
-
- for (int j = 0; j < queryString.length(); j++) {
- if ((queryString.charAt(j) == '\n')
- || ((queryString.charAt(j) == '\r')
- && (j + 1 < queryString.length())
- && (queryString.charAt(j + 1) == '\r'))) {
- state = 0;
- }
-
- switch (state) {
- case -1:// skip all till the end of the line
- break;
- case 0:// initial state
- switch (queryString.charAt(j)) {
- case '-':
- if (queryString.length() > j + 1 && queryString.charAt(j + 1) ==
'-') {
- state = -1;
- }
- break;
- case '\'':
- state = 1;
- break;
- }
- break;
- case 1:// quoted string
- /*
- * Escape character for the quote is doubled quote:
- * Example: 'This is escaped quote ('') inside 1 string'.
- * Our parser switches to state 0 and back to state 1, hence works correct
- * without additional efforts.
- */
- if (queryString.charAt(j) == '\'') {/*there is no way to escape it in HQL
string*/
- state = 0;
- }
- break;
- }
- if (state != -1) {
- clearHQL.append(queryString.charAt(j));
- }
- }
- return clearHQL.toString();
- }
-
public void setQueryString(String queryString) {
this.queryString = queryString;
list = null;
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/AbstractQueryEditor.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/AbstractQueryEditor.java 2010-11-17
09:45:49 UTC (rev 26661)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/AbstractQueryEditor.java 2010-11-17
10:07:01 UTC (rev 26662)
@@ -114,7 +114,7 @@
if(getEditorInput() instanceof QueryEditorInput) {
QueryEditorInput hei = (QueryEditorInput) getEditorInput();
hei.setConsoleConfigurationName( name );
- hei.setQuery( getQueryString() );
+ hei.setQuery( getEditorText() );
hei.resetName();
}
this.consoleConfigurationName = name;
@@ -150,7 +150,7 @@
QueryEditorInput hei = null;
if (getEditorInput() instanceof QueryEditorInput) {
hei = (QueryEditorInput) getEditorInput();
- hei.setQuery( getQueryString() );
+ hei.setQuery( getEditorText() );
}
IDocumentProvider p = getDocumentProvider();
if (p != null && p.isDeleted(getEditorInput())) {
@@ -175,12 +175,16 @@
super.doSetInput(input);
}
- final public String getQueryString() {
+ final public String getEditorText() {
IEditorInput editorInput = getEditorInput();
IDocumentProvider docProvider = getDocumentProvider();
IDocument doc = docProvider.getDocument( editorInput );
return doc.get();
}
+
+ public String getQueryString() {
+ return getEditorText();
+ }
/**
* Dispose of resources held by this editor.
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/QueryEditor.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/QueryEditor.java 2010-11-17
09:45:49 UTC (rev 26661)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/QueryEditor.java 2010-11-17
10:07:01 UTC (rev 26662)
@@ -31,6 +31,16 @@
boolean askUserForConfiguration(String name);
+ /**
+ *
+ * @return the text typed in the editor
+ */
+ String getEditorText();
+
+ /**
+ * @return the text typed in the editor which should be executed ( without comments ).
+ * Compare with {@link #getEditorString}
+ */
String getQueryString();
QueryInputModel getQueryInputModel();
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/QueryParametersView.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/QueryParametersView.java 2010-11-17
09:45:49 UTC (rev 26661)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/QueryParametersView.java 2010-11-17
10:07:01 UTC (rev 26662)
@@ -60,7 +60,7 @@
}
protected void doDestroyPage(IWorkbenchPart part, PageRec pageRecord) {
- QueryParametersPage qppage = (QueryParametersPage)pageRecord.page;
+ IQueryParametersPage qppage = (IQueryParametersPage)pageRecord.page;
qppage.dispose();
pageRecord.dispose();
}
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/hqleditor/HQLEditor.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/hqleditor/HQLEditor.java 2010-11-17
09:45:49 UTC (rev 26661)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/hqleditor/HQLEditor.java 2010-11-17
10:07:01 UTC (rev 26662)
@@ -473,4 +473,55 @@
protected String getSaveAsFileExtension() {
return "*.hql"; //$NON-NLS-1$
}
+
+ /**
+ * @return the query without single line comments
+ */
+ @Override
+ public String getQueryString() {
+ String queryString = getEditorText();
+ StringBuilder clearHQL = new StringBuilder();
+ int state = 0;
+
+ for (int j = 0; j < queryString.length(); j++) {
+ if ((queryString.charAt(j) == '\n')
+ || ((queryString.charAt(j) == '\r')
+ && (j + 1 < queryString.length())
+ && (queryString.charAt(j + 1) == '\r'))) {
+ state = 0;
+ }
+
+ switch (state) {
+ case -1:// skip all till the end of the line
+ break;
+ case 0:// initial state
+ switch (queryString.charAt(j)) {
+ case '-':
+ if (queryString.length() > j + 1 && queryString.charAt(j + 1) ==
'-') {
+ state = -1;
+ }
+ break;
+ case '\'':
+ state = 1;
+ break;
+ }
+ break;
+ case 1:// quoted string
+ /*
+ * Escape character for the quote is doubled quote:
+ * Example: 'This is escaped quote ('') inside 1 string'.
+ * Our parser switches to state 0 and back to state 1, hence works correct
+ * without additional efforts.
+ */
+ if (queryString.charAt(j) == '\'') {/*there is no way to escape it in HQL
string*/
+ state = 0;
+ }
+ break;
+ }
+ if (state != -1) {
+ clearHQL.append(queryString.charAt(j));
+ }
+ }
+ return clearHQL.toString();
+ }
}
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/SaveQueryEditorListener.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/SaveQueryEditorListener.java 2010-11-17
09:45:49 UTC (rev 26661)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/SaveQueryEditorListener.java 2010-11-17
10:07:01 UTC (rev 26662)
@@ -137,7 +137,7 @@
return;
}
- final String newQuery = editor.getQueryString();
+ final String newQuery = editor.getEditorText();
final String wizard_title =
NLS.bind(JdtUiMessages.SaveQueryEditorListener_refactoringtitle, editor_name);
Modified:
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/META-INF/MANIFEST.MF
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/META-INF/MANIFEST.MF 2010-11-17
09:45:49 UTC (rev 26661)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/META-INF/MANIFEST.MF 2010-11-17
10:07:01 UTC (rev 26662)
@@ -57,7 +57,8 @@
org.eclipse.debug.ui,
org.eclipse.datatools.connectivity.ui,
org.eclipse.datatools.connectivity,
- org.eclipse.ui.workbench.texteditor;bundle-version="3.5.0"
+ org.eclipse.ui.workbench.texteditor,
+ org.eclipse.ui.editors
Bundle-ActivationPolicy: lazy
Bundle-Activator: org.hibernate.eclipse.console.test.HibernateConsoleTestPlugin
Eclipse-RegisterBuddy: org.hibernate.eclipse
Modified:
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/ConsolePluginAllTests.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/ConsolePluginAllTests.java 2010-11-17
09:45:49 UTC (rev 26661)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/ConsolePluginAllTests.java 2010-11-17
10:07:01 UTC (rev 26662)
@@ -2,15 +2,16 @@
import java.io.IOException;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
import org.hibernate.eclipse.console.test.mappingproject.MappingTestsCore;
import org.hibernate.eclipse.console.test.mappingproject.MappingTestsJpa;
import org.hibernate.eclipse.console.views.test.QueryPageViewerTest;
+import org.hibernate.eclipse.hqleditor.HQLEditorTest;
import org.hibernate.eclipse.hqleditor.preferences.HQLEditorPreferencePageTest;
import org.hibernate.eclipse.mapper.HBMInfoExtractorTest;
-import junit.framework.Test;
-import junit.framework.TestSuite;
-
public class ConsolePluginAllTests {
public static Test suite() throws IOException {
@@ -23,6 +24,7 @@
suite.addTestSuite(ConsoleConfigurationTest.class);
suite.addTestSuite(JavaFormattingTest.class);
suite.addTestSuite(RefactoringTest.class);
+ suite.addTestSuite(HQLEditorTest.class);
suite.addTestSuite(MappingTestsCore.class);
suite.addTestSuite(MappingTestsJpa.class);
Added:
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/hqleditor/HQLEditorTest.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/hqleditor/HQLEditorTest.java
(rev 0)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/hqleditor/HQLEditorTest.java 2010-11-17
10:07:01 UTC (rev 26662)
@@ -0,0 +1,89 @@
+/*******************************************************************************
+ * Copyright (c) 2010 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.hibernate.eclipse.hqleditor;
+
+import junit.framework.TestCase;
+
+import org.eclipse.jface.action.ActionContributionItem;
+import org.eclipse.jface.action.IContributionItem;
+import org.eclipse.jface.action.IToolBarManager;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IViewPart;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.part.IPage;
+import org.hibernate.console.QueryInputModel;
+import org.hibernate.eclipse.console.HibernateConsoleMessages;
+import org.hibernate.eclipse.console.HibernateConsolePlugin;
+import org.hibernate.eclipse.console.views.QueryParametersPage;
+import org.hibernate.eclipse.console.views.QueryParametersView;
+
+/**
+ * @author Dmitry Geraskov
+ *
+ */
+public class HQLEditorTest extends TestCase {
+
+ public void testHQLEditorOpen(){
+ IEditorPart editorPart = HibernateConsolePlugin.getDefault()
+ .openScratchHQLEditor(null, "");
+ assertNotNull("Editor was not opened", editorPart);
+ assertTrue("Opened editor is not HQLEditor", editorPart instanceof
HQLEditor);
+
+ HQLEditor editor = (HQLEditor)editorPart;
+ QueryInputModel model = editor.getQueryInputModel();
+ assertNotNull("Model is NULL", model);
+ }
+
+ public void testSingleLineCommentsCutOff() throws PartInitException{
+ String query = "from pack.Article a\n" +
+ "where a.articleid in (:a, :b) --or a.articleid = :c";
+ IEditorPart editorPart = HibernateConsolePlugin.getDefault()
+ .openScratchHQLEditor(null, query);
+ assertTrue("Opened editor is not HQLEditor", editorPart instanceof
HQLEditor);
+
+ HQLEditor editor = (HQLEditor)editorPart;
+ assertEquals(editor.getEditorText(), query);
+ assertFalse("Comments were not cut off",
editor.getQueryString().contains("--"));
+
+ QueryInputModel model = editor.getQueryInputModel();
+ assertTrue(model.getParameterCount() == 0);
+
+ IViewPart view = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
+ .getActivePage().showView("org.hibernate.eclipse.console.views.QueryParametersView");
+ assertNotNull("View was not opened", view);
+ assertTrue("Opened view is not QueryParametersView", view instanceof
QueryParametersView);
+
+ QueryParametersView paramView = (QueryParametersView)view;
+ IPage ipage = paramView.getCurrentPage();
+ assertNotNull("Current Page is NULL", ipage);
+ assertTrue("Page is not Query Parameters Page", ipage instanceof
QueryParametersPage);
+
+ QueryParametersPage page = (QueryParametersPage)ipage;
+ IToolBarManager manager = page.getSite().getActionBars().getToolBarManager();
+ IContributionItem[] items = manager.getItems();
+ ActionContributionItem addParamItem = null;
+ for (int i = 0; i < items.length; i++) {
+ ActionContributionItem item = (ActionContributionItem) items[i];
+ if (item.getAction().getClass().getName().endsWith("NewRowAction")){
+ addParamItem = item;
+ break;
+ }
+ }
+ assertNotNull(HibernateConsoleMessages.QueryParametersPage_add_query_parameter_tooltip
+ + " item not found", addParamItem);
+
+ addParamItem.getAction().run();//add query parameters automatically
+ assertTrue(model.getParameterCount() == 2);//a and b
+
+ }
+
+}