Author: dgeraskov
Date: 2010-09-10 09:56:27 -0400 (Fri, 10 Sep 2010)
New Revision: 24868
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/HQLQueryPage.java
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/ConsoleConfigurationTest.java
Log:
https://jira.jboss.org/browse/JBIDE-6974
Support comments in HQL Editor
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-09-10
13:38:56 UTC (rev 24867)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/HQLQueryPage.java 2010-09-10
13:56:27 UTC (rev 24868)
@@ -102,7 +102,7 @@
public void setSession(Session s) {
super.setSession(s);
try {
- query = this.getSession().createQuery(queryString);
+ query = this.getSession().createQuery(getHQLQueryString());
} catch (HibernateException e) {
addException(e);
} catch (Exception e) {
@@ -116,6 +116,58 @@
public String getQueryString() {
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/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/ConsoleConfigurationTest.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/ConsoleConfigurationTest.java 2010-09-10
13:38:56 UTC (rev 24867)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/ConsoleConfigurationTest.java 2010-09-10
13:56:27 UTC (rev 24868)
@@ -84,7 +84,22 @@
}
+
+ public void testHQLComments() {
+ consoleCfg.build();
+ consoleCfg.buildSessionFactory();
+ try {
+ consoleCfg.buildSessionFactory();
+ fail(ConsoleTestMessages.ConsoleConfigurationTest_factory_already_exists);
+ } catch (HibernateConsoleRuntimeException hcre) {
+
+ }
+
+ QueryPage qp = consoleCfg.executeHQLQuery("from java.lang.Object --this is my
comment"); //$NON-NLS-1$
+ assertNotNull(qp);
+ }
+
/*public void testCleanup() throws InterruptedException {
for(int cnt=0;cnt<10000;cnt++) {