Wiki source code of velocityHqlExamples

Version 9.2 by Vincent Massol on 2008/07/31

Hide last authors
Jean-Vincent Drean 5.1 1 #includeMacros("DevGuide.velocityHqlExamplesMacro")
Jean-Vincent Drean 1.36 2
Vincent Massol 1.34 3 1 Velocity HQL query examples
Jean-Vincent Drean 1.1 4
5 XWiki allows user to access documents and objects with [hql>http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html] queries in [Velocity>http://jakarta.apache.org/velocity/docs/user-guide.html] scripts. <br/>
6
Jean-Vincent Drean 1.31 7 #toc("" "" "")
8
Jean-Vincent Drean 1.22 9 1.1 Public API (searchDocuments)
Jean-Vincent Drean 1.3 10
Jean-Vincent Drean 1.6 11 #info("With this API the query consist in the WHERE condition.\\
Sergiu Dumitriu 4.4 12 Any user with edit rights can use this API (as in write a script using it).\\
13 Any user with view rights can view the result of such a query.")
Jean-Vincent Drean 1.3 14
Jean-Vincent Drean 1.1 15 You can execute queries as follows:
16 {code}
Jean-Vincent Drean 1.22 17 #set($query="where doc.creator='XWiki.VincentMassol'")
Jean-Vincent Drean 1.25 18 #set($results = $xwiki.searchDocuments($query, 5, 0))
Jean-Vincent Drean 1.3 19 #foreach ($item in $results)
Sergiu Dumitriu 4.5 20 * $item
Jean-Vincent Drean 1.3 21 #end
22 {code}
23
Vincent Massol 1.33 24 1.1.1 Simple Query
Jean-Vincent Drean 1.22 25 #set($hql="where doc.creator='XWiki.VincentMassol'")
Jean-Vincent Drean 1.3 26 #displayQuery($hql false)
27
Jean-Vincent Drean 1.35 28 1.1.1 Ordered Query
Jean-Vincent Drean 1.22 29 #set($hql="where doc.creator='XWiki.VincentMassol' order by doc.date asc")
Jean-Vincent Drean 1.21 30 #displayQuery($hql false)
31
Jean-Vincent Drean 1.35 32 1.1.1 Advanced Query (date & time)
33
Sergiu Dumitriu 4.5 34 Since there is no [standard way to calculate dates interval in HQL>http://opensource.atlassian.com/projects/hibernate/browse/HHH-2434] those queries are a bit unnatural.
Jean-Vincent Drean 1.35 35
36 #set($hql="where year(doc.date) = year(current_date()) and month(doc.date) = month(current_date()) and day(doc.date) = day(current_date()) and hour(doc.date) > (hour(current_time()) - 1) order by doc.date desc")
Jean-Vincent Drean 1.21 37 #displayQuery($hql false)
38
Jean-Vincent Drean 1.35 39 {code}
40 Other examples, documents modified :
Jean-Vincent Drean 1.21 41
Jean-Vincent Drean 1.35 42 during current day : "where year(doc.date) = year(current_date()) and month(doc.date) = month(current_date()) and day(doc.date) > (day(current_date()) - 1) order by doc.date desc"
43 during current week : "where year(doc.date) = year(current_date()) and month(doc.date) = month(current_date()) and day(doc.date) > (day(current_date()) - 7) order by doc.date desc"
44 during current month : "where year(doc.date) = year(current_date()) and month(doc.date) > (month(current_date()) - 1) order by doc.date desc"
45 {code}
46
47
Jean-Vincent Drean 1.22 48 1.1 Privileged API (search : Documents, Objects, Properties, etc)
Jean-Vincent Drean 1.3 49
Jean-Vincent Drean 1.6 50 #warning("Calls to te privileged API are only executed when the calling page has been saved by an Admin. \\
Sergiu Dumitriu 4.4 51 The reason is that search can be used to send any HQL command like update, delete, etc.")
Jean-Vincent Drean 1.3 52
53 You can execute queries as follows:
54 {code}
Jean-Vincent Drean 1.2 55 #set($query="select doc.name from XWikiDocument doc")
Jean-Vincent Drean 1.25 56 #set($results = $xwiki.search($query, 5, 0))
Jean-Vincent Drean 1.2 57 #foreach ($item in $results)
58 * $item <br/>
59 #end
Jean-Vincent Drean 1.1 60 {code}
61
Vincent Massol 1.33 62 1.1.1 Simply Query
Jean-Vincent Drean 1.1 63 #set($hql="select doc.name from XWikiDocument doc")
Jean-Vincent Drean 1.3 64 #displayQuery($hql true)
Jean-Vincent Drean 1.1 65
Vincent Massol 1.33 66 1.1.1 Count Query
Jean-Vincent Drean 1.19 67
68 {code}
69 #set($query="select count(doc) from XWikiDocument doc")
Jean-Vincent Drean 1.20 70 #set($results = $xwiki.search($query))
Jean-Vincent Drean 1.21 71 ## $xwiki.search returning a list, we get its first element
Jean-Vincent Drean 1.19 72 $query result : $results.get(0)
73 {code}
74
75 #set($query="select count(doc) from XWikiDocument doc")
Jean-Vincent Drean 1.20 76 #set($results = $xwiki.search($query))
77 $query results : $results.get(0)
Jean-Vincent Drean 1.19 78
Jean-Vincent Drean 1.1 79 <br/>
Vincent Massol 1.33 80 1.1.1 Simply Query with multiple fields
Jean-Vincent Drean 1.6 81
Jean-Vincent Drean 1.1 82 {code}
Jean-Vincent Drean 1.25 83 #set($results=$xwiki.search("select doc.name, doc.date from XWikiDocument doc", 5, 0))
Jean-Vincent Drean 1.2 84 #foreach ($row in $results)
85 #foreach ($col in $row)
86 #if ($velocityCount==1)
87 #set($docName=$col)
88 #elseif ($velocityCount==2)
89 #set($docDate=$col)
90 #end
91 #end
92 $docName : $docDate <br/>
93 #end
Jean-Vincent Drean 1.1 94 {code}
95
96 #set($hql="select doc.name, doc.date from XWikiDocument doc")
Jean-Vincent Drean 1.3 97 #displayQuery($hql true)
Jean-Vincent Drean 1.1 98
99 <br/>
Jean-Vincent Drean 1.3 100 1.1.1 Getting objects of a specific class
Jean-Vincent Drean 1.1 101 #set($hql="select obj.name from BaseObject obj where obj.className='XWiki.XWikiUsers'")
Jean-Vincent Drean 1.3 102 #displayQuery($hql true)
Jean-Vincent Drean 1.1 103
104 <br/>
Jean-Vincent Drean 1.3 105 1.1.1 Getting objects' properties
Jean-Vincent Drean 1.1 106 #set($hql="select obj.name, prop.value from BaseObject obj, StringProperty prop where obj.className='XWiki.XWikiUsers' and prop.id.id=obj.id and prop.name='first_name'")
Jean-Vincent Drean 1.3 107 #displayQuery($hql true)
Jean-Vincent Drean 1.19 108
109 <br/>
Jean-Vincent Drean 1.30 110 1.1.1 Getting documents where objects' properties equals some value
111 #set($hql="select doc.fullName from XWikiDocument doc, BaseObject obj, StringProperty prop where doc.fullName=obj.name and obj.className='XWiki.XWikiUsers' and prop.id.id=obj.id and prop.name='first_name' and prop.value='Jean-Vincent'")
Jean-Vincent Drean 1.19 112 #displayQuery($hql true)
113
114
Jean-Vincent Drean 1.30 115

Get Connected