Author: vmassol
Date: 2007-09-28 10:58:10 +0200 (Fri, 28 Sep 2007)
New Revision: 5122
Modified:
xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/api/XWiki.java
xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/store/XWikiCacheStore.java
xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/store/XWikiHibernateStore.java
xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/store/XWikiStoreInterface.java
xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/store/jcr/XWikiJcrStore.java
Log:
XWIKI-1770: Add new searchDocuments() API that accepts named HQL queries
XWIKI-1768: Cannot delete Space with a simple quote in its name
Merged from trunk (revs 5094 and 5121)
Modified:
xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/api/XWiki.java
===================================================================
---
xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/api/XWiki.java
2007-09-28 08:51:43 UTC (rev 5121)
+++
xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/api/XWiki.java
2007-09-28 08:58:10 UTC (rev 5122)
@@ -418,6 +418,47 @@
}
/**
+ * Search documents by passing HQL where clause values as parameters. This
allows generating
+ * a Named HQL query which will automatically encode the passed values
(like escaping single
+ * quotes). This API is recommended to be used over the other similar
methods where the values
+ * are passed inside the where clause and for which you'll need to do the
encoding/escpaing
+ * yourself before calling them.
+ *
+ * <p>Example</p>
+ * <pre><code>
+ * #set($orphans = $xwiki.searchDocuments(" where doc.fullName <> ? and
(doc.parent = ? or "
+ * + "(doc.parent = ? and doc.web = ?))",
+ * ["${doc.fullName}as", ${doc.fullName}, ${doc.name}, ${doc.web}]))
+ * </code></pre>
+ *
+ * @param parametrizedSqlClause the HQL where clause. For example <code>"
where doc.fullName
+ * <> ? and (doc.parent = ? or (doc.parent = ? and doc.web = ?))"
+ * @param nb the number of rows to return. If 0 then all rows are returned
+ * @param start the number of rows to skip. If 0 don't skip any row
+ * @param parameterValues the where clause values that replace the
question marks (?)
+ * @return a list of document names
+ * @throws XWikiException in case of error while performing the query
+ */
+ public List searchDocuments(String parametrizedSqlClause, int nb, int
start,
+ List parameterValues) throws XWikiException
+ {
+ return xwiki.getStore().searchDocumentsNames(parametrizedSqlClause,
nb, start,
+ parameterValues, getXWikiContext());
+ }
+
+ /**
+ * Same as [EMAIL PROTECTED] #searchDocuments(String, int, int,
java.util.List)} but returns all rows.
+ *
+ * @see #searchDocuments(String, int, int, java.util.List)
+ */
+ public List searchDocuments(String parametrizedSqlClause, List
parameterValues)
+ throws XWikiException
+ {
+ return xwiki.getStore().searchDocumentsNames(parametrizedSqlClause,
+ parameterValues, getXWikiContext());
+ }
+
+ /**
* Function to wrap a list of XWikiDocument into Document objects
*
* @param docs list of XWikiDocument
Modified:
xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/store/XWikiCacheStore.java
===================================================================
---
xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/store/XWikiCacheStore.java
2007-09-28 08:51:43 UTC (rev 5121)
+++
xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/store/XWikiCacheStore.java
2007-09-28 08:58:10 UTC (rev 5122)
@@ -248,6 +248,18 @@
return store.searchDocuments(wheresql, distinctbyname, customMapping,
checkRight, nb, start, context);
}
+ public List searchDocumentsNames(String parametrizedSqlClause, int nb, int
start,
+ List parameterValues, XWikiContext context) throws XWikiException
+ {
+ return store.searchDocumentsNames(parametrizedSqlClause, nb, start,
parameterValues, context);
+ }
+
+ public List searchDocumentsNames(String parametrizedSqlClause, List
parameterValues,
+ XWikiContext context) throws XWikiException
+ {
+ return store.searchDocumentsNames(parametrizedSqlClause,
parameterValues, context);
+ }
+
public XWikiLock loadLock(long docId, XWikiContext context, boolean
bTransaction) throws XWikiException {
return store.loadLock(docId, context, bTransaction);
}
Modified:
xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/store/XWikiHibernateStore.java
===================================================================
---
xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/store/XWikiHibernateStore.java
2007-09-28 08:51:43 UTC (rev 5121)
+++
xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/store/XWikiHibernateStore.java
2007-09-28 08:58:10 UTC (rev 5122)
@@ -1612,6 +1612,19 @@
return ("1".equals(context.getWiki().Param(param + ".read", "1")));
}
+ public List searchDocumentsNames(String parametrizedSqlClause, List
parameterValues,
+ XWikiContext context) throws XWikiException
+ {
+ return searchDocumentsNames(parametrizedSqlClause, 0, 0,
parameterValues, context);
+ }
+
+ public List searchDocumentsNames(String parametrizedSqlClause, int nb, int
start,
+ List parameterValues, XWikiContext context) throws XWikiException
+ {
+ String sql = createSQLQuery("select distinct doc.web, doc.name",
parametrizedSqlClause);
+ return searchDocumentsNamesInternal(sql, nb, start, parameterValues,
context);
+ }
+
public List search(String sql, int nb, int start, Object[][] whereParams,
XWikiContext context) throws XWikiException {
boolean bTransaction = true;
@@ -1740,12 +1753,18 @@
}
}
- public List searchDocumentsNames(String wheresql, int nb, int start,
String selectColumns, XWikiContext context) throws XWikiException {
+ public List searchDocumentsNames(String wheresql, int nb, int start,
String selectColumns, XWikiContext context) throws XWikiException
+ {
+ String sql = createSQLQuery("select distinct doc.web, doc.name",
wheresql);
+ return searchDocumentsNamesInternal(sql, nb, start,
Collections.EMPTY_LIST, context);
+ }
+
+ private List searchDocumentsNamesInternal(String sql, int nb, int start,
List parameterValues,
+ XWikiContext context) throws XWikiException
+ {
boolean bTransaction = false;
MonitorPlugin monitor = Util.getMonitorPlugin(context);
try {
- String sql = createSQLQuery("select distinct doc.web, doc.name",
wheresql);
-
// Start monitoring timer
if (monitor!=null)
monitor.startTimer("hibernate", sql);
@@ -1754,6 +1773,16 @@
bTransaction = beginTransaction(false, context);
Session session = getSession(context);
Query query = session.createQuery(sql);
+
+ if (parameterValues != null)
+ {
+ int i = 0;
+ for (Iterator values = parameterValues.iterator();
values.hasNext();) {
+ query.setString(i, (String) values.next());
+ i++;
+ }
+ }
+
if (start!=0)
query.setFirstResult(start);
if (nb!=0)
@@ -1770,7 +1799,7 @@
catch (Exception e) {
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SEARCH,
- "Exception while searching documents with sql [{0}]", e, new
Object[]{ wheresql });
+ "Exception while searching documents with SQL [{0}]", e, new
Object[]{ sql });
} finally {
try {
if (bTransaction)
@@ -1783,6 +1812,7 @@
}
}
+
/**
* @todo refactor it to remove duplications with the searchDocument API
without the distrinctbylanguage parameter
*/
Modified:
xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/store/XWikiStoreInterface.java
===================================================================
---
xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/store/XWikiStoreInterface.java
2007-09-28 08:51:43 UTC (rev 5121)
+++
xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/store/XWikiStoreInterface.java
2007-09-28 08:58:10 UTC (rev 5122)
@@ -40,6 +40,42 @@
public List searchDocumentsNames(String wheresql, XWikiContext context)
throws XWikiException;
public List searchDocumentsNames(String wheresql, int nb, int start,
XWikiContext context) throws XWikiException;
public List searchDocumentsNames(String wheresql, int nb, int start,
String selectColumns, XWikiContext context) throws XWikiException;
+
+ /**
+ * Search documents by passing HQL where clause values as parameters. This
allows generating
+ * a Named HQL query which will automatically encode the passed values
(like escaping single
+ * quotes). This API is recommended to be used over the other similar
methods where the values
+ * are passed inside the where clause and for which you'll need to do the
encoding/escpaing
+ * yourself before calling them.
+ *
+ * <p>Example</p>
+ * <pre><code>
+ * #set($orphans = $xwiki.searchDocuments(" where doc.fullName <> ? and
(doc.parent = ? or "
+ * + "(doc.parent = ? and doc.web = ?))",
+ * ["${doc.fullName}as", ${doc.fullName}, ${doc.name}, ${doc.web}]))
+ * </code></pre>
+ *
+ * @param parametrizedSqlClause the HQL where clause. For example <code>"
where doc.fullName
+ * <> ? and (doc.parent = ? or (doc.parent = ? and doc.web = ?))"
+ * @param nb the number of rows to return. If 0 then all rows are returned
+ * @param start the number of rows to skip. If 0 don't skip any row
+ * @param parameterValues the where clause values that replace the
question marks (?)
+ * @param context the XWiki context required for getting information about
the execution context
+ * @return a list of document names
+ * @throws XWikiException in case of error while performing the query
+ */
+ public List searchDocumentsNames(String parametrizedSqlClause, int nb, int
start,
+ List parameterValues, XWikiContext context) throws XWikiException;
+
+ /**
+ * Same as [EMAIL PROTECTED] #searchDocumentsNames(String, int, int, List,
XWikiContext)} but returns all
+ * rows.
+ *
+ * @see #searchDocumentsNames(String, int, int, java.util.List,
com.xpn.xwiki.XWikiContext)
+ */
+ public List searchDocumentsNames(String parametrizedSqlClause, List
parameterValues,
+ XWikiContext context) throws XWikiException;
+
public List searchDocuments(String wheresql, boolean distinctbyname,
XWikiContext context) throws XWikiException;
public List searchDocuments(String wheresql, boolean distinctbyname,
boolean customMapping, XWikiContext context) throws XWikiException;
public List searchDocuments(String wheresql, boolean distinctbyname, int
nb, int start, XWikiContext context) throws XWikiException;
Modified:
xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/store/jcr/XWikiJcrStore.java
===================================================================
---
xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/store/jcr/XWikiJcrStore.java
2007-09-28 08:51:43 UTC (rev 5121)
+++
xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/store/jcr/XWikiJcrStore.java
2007-09-28 08:58:10 UTC (rev 5122)
@@ -925,7 +925,21 @@
return null;
}
- public List search(String sql, int nb, int start, XWikiContext context)
throws XWikiException {
+ public List searchDocumentsNames(String parametrizedSqlClause, int nb, int
start,
+ List parameterValues, XWikiContext context) throws XWikiException
+ {
+ notSupportedCall();
+ return null;
+ }
+
+ public List searchDocumentsNames(String parametrizedSqlClause, List
parameterValues,
+ XWikiContext context) throws XWikiException
+ {
+ notSupportedCall();
+ return null;
+ }
+
+ public List search(String sql, int nb, int start, XWikiContext context)
throws XWikiException {
notSupportedCall();
return null;
}
_______________________________________________
notifications mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/notifications