Author: vmassol
Date: 2008-02-07 10:35:40 +0100 (Thu, 07 Feb 2008)
New Revision: 7335
Added:
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/objects/
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/objects/classes/
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/objects/classes/DBListClassTest.java
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/objects/classes/DBTreeListClassTest.java
Modified:
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/objects/classes/DBListClass.java
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/objects/classes/DBTreeListClass.java
Log:
XWIKI-2064: DBListClass and DBTreeListClass are building invalid SQL statements
Patch submitted by Yoshtec
Reviewed and applied with minor modifications by Vincent Massol
* Also added improved error handling when the SQL is specified and cannot be
rendered
Modified:
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/objects/classes/DBListClass.java
===================================================================
---
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/objects/classes/DBListClass.java
2008-02-07 09:13:57 UTC (rev 7334)
+++
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/objects/classes/DBListClass.java
2008-02-07 09:35:40 UTC (rev 7335)
@@ -34,6 +34,8 @@
import java.util.StringTokenizer;
import org.apache.commons.lang.StringUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.apache.ecs.xhtml.input;
import com.xpn.xwiki.objects.BaseCollection;
import com.xpn.xwiki.objects.BaseProperty;
@@ -44,6 +46,8 @@
public class DBListClass extends ListClass
{
+ private static final Log LOG = LogFactory.getLog(DBListClass.class);
+
private List cachedDBList;
public DBListClass(String name, String prettyname, PropertyMetaClass
wclass)
@@ -146,14 +150,15 @@
}
return map;
}
-
+
public String getQuery(XWikiContext context)
{
String sql = getSql();
try {
sql = context.getDoc().getRenderedContent(sql, context);
} catch (Exception e) {
- e.printStackTrace();
+ LOG.warn("Failed to render SQL script [" + sql + "]. Internal
error ["
+ + e.getMessage() + "]. Continuing with non-rendered script.");
}
if ((sql == null) || (sql.trim().equals(""))) {
String classname = getClassname();
@@ -163,33 +168,39 @@
valueField = idField;
}
if (context.getWiki().getHibernateStore() != null) {
- String select = "select ";
- String tables = " from XWikiDocument as doc, BaseObject as
obj";
- String where =
- " where doc.fullName=obj.name and obj.className='" +
classname + "'";
+ StringBuffer select = new StringBuffer("select ");
+ StringBuffer tables =
+ new StringBuffer(" from XWikiDocument as doc, BaseObject
as obj");
+ StringBuffer where =
+ new StringBuffer(" where doc.fullName=obj.name and
obj.className='");
+ where.append(classname).append("'");
+
if (idField.startsWith("doc.") || idField.startsWith("obj.")) {
- select += idField + ",";
+ select.append(idField);
} else {
- select += "idprop.value,";
- tables += ", StringProperty as idprop";
- where += " and obj.id=idprop.id.id and idprop.id.name='" +
idField + "'";
+ select.append("idprop.value");
+ tables.append(", StringProperty as idprop");
+ where.append(" and obj.id=idprop.id.id and
idprop.id.name='")
+ .append(idField)
+ .append("'");
}
+
if (valueField.startsWith("doc.") ||
valueField.startsWith("obj.")) {
- select += valueField + ",";
+ select.append(", ").append(valueField);
}
else {
if (idField.equals(valueField)) {
- select += "idprop.value,";
+ select.append(", idprop.value");
} else {
- select += "valueprop.value,";
- tables += ", StringProperty as valueprop";
- where +=
- " and obj.id=valueprop.id.id and
valueprop.id.name='" + valueField
- + "'";
+ select.append(", valueprop.value");
+ tables.append(", StringProperty as valueprop");
+ where.append(" and obj.id=valueprop.id.id and
valueprop.id.name='")
+ .append(valueField)
+ .append("'");
}
}
// Let's create the sql
- sql = select + tables + where;
+ sql = select.append(tables).append(where).toString();
} else {
// TODO: query plugin impl.
// We need to generate the right query for the query plugin
Modified:
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/objects/classes/DBTreeListClass.java
===================================================================
---
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/objects/classes/DBTreeListClass.java
2008-02-07 09:13:57 UTC (rev 7334)
+++
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/objects/classes/DBTreeListClass.java
2008-02-07 09:35:40 UTC (rev 7335)
@@ -29,6 +29,8 @@
import org.apache.ecs.xhtml.option;
import org.apache.ecs.xhtml.select;
import org.apache.velocity.VelocityContext;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.objects.BaseCollection;
@@ -37,7 +39,9 @@
import com.xpn.xwiki.objects.ListProperty;
import com.xpn.xwiki.objects.meta.PropertyMetaClass;
-public class DBTreeListClass extends DBListClass {
+public class DBTreeListClass extends DBListClass
+{
+ private static final Log LOG = LogFactory.getLog(DBTreeListClass.class);
public DBTreeListClass(PropertyMetaClass wclass) {
super("dbtreelist", "DB Tree List", wclass);
@@ -277,55 +281,69 @@
public String getQuery(XWikiContext context) {
String sql = getSql();
try {
- sql = context.getDoc().getRenderedContent(sql, context);
+ sql = context.getDoc().getRenderedContent(sql, context);
} catch (Exception e) {
- e.printStackTrace();
+ LOG.warn("Failed to render SQL script [" + sql + "]. Internal
error ["
+ + e.getMessage() + "]. Continuing with non-rendered script.");
}
- if ((sql==null)||(sql.trim().equals(""))) {
+ if ((sql == null) || (sql.trim().equals(""))) {
String classname = getClassname();
String idField = getIdField();
String valueField = getValueField();
- String parentField = getParentField();
- if ((valueField==null)||(valueField.trim().equals("")))
- valueField = idField;
- if (context.getWiki().getHibernateStore()!=null) {
- String select = "select ";
- String tables = " from XWikiDocument as doc, BaseObject as obj";
- String where = " where doc.fullName=obj.name and obj.className='"
+ classname + "'";
- if (idField.startsWith("doc.")||idField.startsWith("obj."))
- select += idField + ",";
- else {
- select += "idprop.value,";
- tables += ", StringProperty as idprop";
- where += " and obj.id=idprop.id.id and idprop.id.name='" +
idField + "'";
+ if ((valueField == null) || (valueField.trim().equals(""))) {
+ valueField = idField;
+ }
+ if (context.getWiki().getHibernateStore() != null) {
+ StringBuffer select = new StringBuffer("select ");
+ StringBuffer tables = new StringBuffer(" from XWikiDocument as
doc, BaseObject as obj");
+ StringBuffer where = new StringBuffer(" where
doc.fullName=obj.name and obj.className='");
+ where.append(classname).append("'");
+
+ if (idField.startsWith("doc.") || idField.startsWith("obj.")) {
+ select.append(idField);
+ } else {
+ select.append("idprop.value");
+ tables.append(", StringProperty as idprop");
+ where.append(" and obj.id=idprop.id.id and
idprop.id.name='")
+ .append(idField)
+ .append("'");
}
- if
(valueField.startsWith("doc.")||valueField.startsWith("obj."))
- select += valueField + ",";
+
+ if (valueField.startsWith("doc.") ||
valueField.startsWith("obj.")) {
+ select.append(", ").append(valueField);
+ }
else {
if (idField.equals(valueField)) {
- select += "idprop.value,";
+ select.append(", idprop.value");
} else {
- select += "valueprop.value,";
- tables += ", StringProperty as valueprop";
- where += " and obj.id=valueprop.id.id and
valueprop.id.name='" + valueField + "'";
+ select.append(", valueprop.value");
+ tables.append(", StringProperty as valueprop");
+ where.append(" and obj.id=valueprop.id.id and
valueprop.id.name='")
+ .append(valueField)
+ .append("'");
}
}
-
+
+ // DBTreeList specific part
+ String parentField = getParentField();
if
(parentField.startsWith("doc.")||parentField.startsWith("obj."))
- select += parentField + ",";
+ select.append(", ").append(parentField);
else {
if (idField.equals(parentField)) {
- select += "idprop.value,";
+ select.append(", idprop.value");
} else if (valueField.equals(parentField)) {
- select += "valueprop.value,";
+ select.append(", valueprop.value");
} else {
- select += "parentprop.value,";
- tables += ", StringProperty as parentprop";
- where += " and obj.id=parentprop.id.id and
parentprop.id.name='" + parentField + "'";
+ select.append(", parentprop.value");
+ tables.append(", StringProperty as parentprop");
+ where.append(" and obj.id=parentprop.id.id and
parentprop.id.name='")
+ .append(parentField)
+ .append("'");
}
}
+
// Let's create the sql
- sql = select + tables + where;
+ sql = select.append(tables).append(where).toString();
} else {
// TODO: query plugin impl.
// We need to generate the right query for the query plugin
Added:
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/objects/classes/DBListClassTest.java
===================================================================
---
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/objects/classes/DBListClassTest.java
(rev 0)
+++
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/objects/classes/DBListClassTest.java
2008-02-07 09:35:40 UTC (rev 7335)
@@ -0,0 +1,55 @@
+/*
+ * See the NOTICE file distributed with this work for additional
+ * information regarding copyright ownership.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ */
+package com.xpn.xwiki.objects.classes;
+
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.XWikiConfig;
+import com.xpn.xwiki.XWiki;
+import com.xpn.xwiki.store.XWikiHibernateStore;
+import com.xpn.xwiki.doc.XWikiDocument;
+import junit.framework.TestCase;
+
+/**
+ * Unit tests for [EMAIL PROTECTED] DBListClass}.
+ *
+ * @version $Id: $
+ */
+public class DBListClassTest extends TestCase
+{
+ private XWikiContext context;
+
+ protected void setUp() throws Exception
+ {
+ this.context = new XWikiContext();
+ this.context.setDoc(new XWikiDocument());
+ XWikiHibernateStore store = new XWikiHibernateStore("dummy");
+ XWiki xwiki = new XWiki(new XWikiConfig(), context);
+ xwiki.setStore(store);
+ }
+
+ public void testGetQueryWhenNoSQLSCriptSpecified()
+ {
+ DBListClass dblc = new DBListClass();
+ assertEquals("select idprop.value, idprop.value from XWikiDocument as
doc, BaseObject as obj, "
+ + "StringProperty as idprop where doc.fullName=obj.name and
obj.className='' and obj.id=idprop.id.id "
+ + "and idprop.id.name=''", dblc.getQuery(this.context));
+ }
+}
Added:
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/objects/classes/DBTreeListClassTest.java
===================================================================
---
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/objects/classes/DBTreeListClassTest.java
(rev 0)
+++
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/objects/classes/DBTreeListClassTest.java
2008-02-07 09:35:40 UTC (rev 7335)
@@ -0,0 +1,55 @@
+/*
+ * See the NOTICE file distributed with this work for additional
+ * information regarding copyright ownership.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ */
+package com.xpn.xwiki.objects.classes;
+
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.XWikiConfig;
+import com.xpn.xwiki.XWiki;
+import com.xpn.xwiki.store.XWikiHibernateStore;
+import com.xpn.xwiki.doc.XWikiDocument;
+import junit.framework.TestCase;
+
+/**
+ * Unit tests for [EMAIL PROTECTED]
com.xpn.xwiki.objects.classes.DBTreeListClass}.
+ *
+ * @version $Id: $
+ */
+public class DBTreeListClassTest extends TestCase
+{
+ private XWikiContext context;
+
+ protected void setUp() throws Exception
+ {
+ this.context = new XWikiContext();
+ this.context.setDoc(new XWikiDocument());
+ XWikiHibernateStore store = new XWikiHibernateStore("dummy");
+ XWiki xwiki = new XWiki(new XWikiConfig(), context);
+ xwiki.setStore(store);
+ }
+
+ public void testGetQueryWhenNoSQLSCriptSpecified()
+ {
+ DBTreeListClass dbtlc = new DBTreeListClass();
+ assertEquals("select idprop.value, idprop.value, idprop.value from
XWikiDocument as doc, "
+ + "BaseObject as obj, StringProperty as idprop where
doc.fullName=obj.name and obj.className='' and "
+ + "obj.id=idprop.id.id and idprop.id.name=''",
dbtlc.getQuery(this.context));
+ }
+}
\ No newline at end of file
_______________________________________________
notifications mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/notifications