Author: vmassol
Date: 2008-02-07 10:39:10 +0100 (Thu, 07 Feb 2008)
New Revision: 7336

Added:
   
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/test/java/com/xpn/xwiki/objects/
   
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/test/java/com/xpn/xwiki/objects/classes/
Removed:
   
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/test/java/com/xpn/xwiki/objects/classes/
Modified:
   
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/objects/classes/DBListClass.java
   
xwiki-platform/core/branches/xwiki-core-1.2/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

Merged from trunk (rev 7335)

Modified: 
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/objects/classes/DBListClass.java
===================================================================
--- 
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/objects/classes/DBListClass.java
 2008-02-07 09:35:40 UTC (rev 7335)
+++ 
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/objects/classes/DBListClass.java
 2008-02-07 09:39:10 UTC (rev 7336)
@@ -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/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/objects/classes/DBTreeListClass.java
===================================================================
--- 
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/objects/classes/DBTreeListClass.java
     2008-02-07 09:35:40 UTC (rev 7335)
+++ 
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/objects/classes/DBTreeListClass.java
     2008-02-07 09:39:10 UTC (rev 7336)
@@ -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

Copied: 
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/test/java/com/xpn/xwiki/objects
 (from rev 7335, 
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/objects)

Copied: 
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/test/java/com/xpn/xwiki/objects/classes
 (from rev 7335, 
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/objects/classes)

_______________________________________________
notifications mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/notifications

Reply via email to