Wiki source code of velocityHqlExamples

Version 27.1 by flavius on 2011/12/08

Hide last authors
flavius 27.1 1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc start="2" depth="4"/}}
3 {{/box}}
Vincent Massol 17.1 4
Vincent Massol 20.1 5 = HQL Query Examples in Velocity =
Jean-Vincent Drean 1.1 6
Jean-Vincent Drean 23.3 7 XWiki allows user to access documents and objects with [[HQL>>http://docs.jboss.org/hibernate/stable/core/reference/en/html/queryhql.html]] queries in [[Velocity>>http://jakarta.apache.org/velocity/docs/user-guide.html]] scripts.
Jean-Vincent Drean 1.1 8
Vincent Massol 22.1 9 == Public API (##searchDocuments##) ==
10
flavius 27.1 11 {{velocity}}
12 {{html}}
Vincent Massol 22.1 13 #info("With this API the query consist in the WHERE condition of a full HQL query. Any user with edit rights can write a script using this API. Any user with view rights can view the result of such a query.")
flavius 27.1 14 {{/html}}
15 {{/velocity}}
Vincent Massol 22.1 16
Vincent Massol 21.1 17 General example showing how to display the first 5 results of a given query:
Jean-Vincent Drean 1.3 18
Vincent Massol 21.1 19 {{code language="none"}}
Vincent Massol 22.2 20 #set($hql = "<query here>")
Vincent Massol 21.1 21 #set($results = $xwiki.searchDocuments($hql, 5, 0))
Jean-Vincent Drean 1.3 22 #foreach ($item in $results)
Sergiu Dumitriu 4.5 23 * $item
Jean-Vincent Drean 1.3 24 #end
Vincent Massol 15.1 25 {{/code}}
Jean-Vincent Drean 1.3 26
Vincent Massol 21.1 27 The examples below will show you various HQL queries that you can write.
28
Vincent Massol 15.1 29 === Simple Query ===
30
Vincent Massol 21.1 31 Displays all documents who have been created by the user ##XWiki.JohnDoe##:
Jean-Vincent Drean 1.3 32
Vincent Massol 21.1 33 {{code language="none"}}
34 #set($hql = "where doc.creator='XWiki.JohnDoe'")
35 {{/code}}
36
Vincent Massol 15.1 37 === Ordered Query ===
38
Vincent Massol 21.1 39 Displays all documents who have been created by the user ##XWiki.JohnDoe## and sorted by document's last modification date, in ascending order:
Jean-Vincent Drean 1.21 40
Vincent Massol 21.1 41 {{code language="none"}}
42 #set($hql = "where doc.creator='XWiki.VincentMassol' order by doc.date asc")
43 {{/code}}
44
Vincent Massol 15.1 45 === Advanced Query (date & time) ===
Jean-Vincent Drean 1.35 46
flavius 27.1 47 {{velocity}}
48 {{html wiki="true"}}
Vincent Massol 21.1 49 #info("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.")
flavius 27.1 50 {{/html}}
51 {{/velocity}}
Jean-Vincent Drean 1.21 52
Vincent Massol 21.1 53 {{code language="none"}}
54 #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")
Vincent Massol 15.1 55 {{/code}}
Jean-Vincent Drean 1.35 56
Vincent Massol 21.1 57 Other examples:
flavius 27.1 58
Vincent Massol 21.1 59 * Listing all documents modified during the current day: {{code language="none"}}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{{/code}}
60 * Listing all documents modified during the current week: {{code language="none"}}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{{/code}}
61 * Listing all documents modified during the current month: {{code language="none"}}where year(doc.date) = year(current_date()) and month(doc.date) > (month(current_date()) - 1) order by doc.date desc{{/code}}
flavius 27.1 62 * Comparing two dates: {{code language="none"}}where ((year(date1) > year(date2) OR (year(date1) = year(date2) AND month(date1) > month(date2)) OR (year(date1) = year(date2) AND month(date1) = month(date2) AND day(date1) >= day(date2)){{/code}}
Jean-Vincent Drean 1.35 63
Vincent Massol 22.3 64 == Privileged API (##search##) ==
Jean-Vincent Drean 1.3 65
flavius 27.1 66 {{velocity}}
67 {{html wiki="true"}}
Vincent Massol 22.4 68 #warning("Calls to te privileged API are only executed when the calling page has been saved by a user with **Programming Rights**. The reason is that search can be used to send dangerous HQL command like update, delete, etc.")
flavius 27.1 69 {{/html}}
70 {{/velocity}}
Jean-Vincent Drean 1.1 71
Vincent Massol 22.1 72 General example showing how to display the first 5 results of a given query:
73
74 {{code language="none"}}
Vincent Massol 22.2 75 #set($hql = "<query here>")
Vincent Massol 22.1 76 #set($results = $xwiki.search($hql, 5, 0))
77 #foreach ($item in $results)
78 * $item
79 #end
80 {{/code}}
81
82 The examples below will show you various HQL queries that you can write.
83
Vincent Massol 15.1 84 === Simple Query ===
85
Vincent Massol 21.1 86 {{code language="none"}}
87 #set($hql = "select doc.name from XWikiDocument doc")
88 {{/code}}
Jean-Vincent Drean 1.1 89
Vincent Massol 15.1 90 === Count Query ===
Jean-Vincent Drean 1.19 91
Vincent Massol 21.1 92 {{code language="none"}}
93 #set($results = $xwiki.search("select count(doc) from XWikiDocument doc"))
94 ## Since $xwiki.search is returning a list, we get its first element
95 Count : $results.get(0)
Vincent Massol 15.1 96 {{/code}}
Jean-Vincent Drean 1.19 97
Vincent Massol 15.1 98 === Simple Query with multiple fields ===
99
Vincent Massol 21.1 100 {{code language="none"}}
101 #set($results = $xwiki.search("select doc.name, doc.date from XWikiDocument doc", 5, 0))
Jean-Vincent Drean 1.2 102 #foreach ($row in $results)
103 #foreach ($col in $row)
Vincent Massol 21.1 104 #if ($velocityCount == 1)
105 #set($docName = $col)
106 #elseif ($velocityCount == 2)
107 #set($docDate = $col)
Jean-Vincent Drean 1.2 108 #end
109 #end
Vincent Massol 21.1 110 $docName : $docDate <br/>
Jean-Vincent Drean 1.2 111 #end
Vincent Massol 15.1 112 {{/code}}
Jean-Vincent Drean 1.1 113
Vincent Massol 15.1 114 === Getting objects of a specific class ===
115
Vincent Massol 21.1 116 {{code language="none"}}
117 #set($hql = "select obj.name from BaseObject obj where obj.className='XWiki.XWikiUsers'")
118 {{/code}}
Jean-Vincent Drean 1.1 119
Vincent Massol 15.1 120 === Getting objects' properties ===
121
Vincent Massol 21.1 122 {{code language="none"}}
123 #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'")
124 {{/code}}
Jean-Vincent Drean 1.19 125
Vincent Massol 15.1 126 === Getting documents where objects' properties equals some value ===
127
Vincent Massol 21.1 128 {{code language="none"}}
129 #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'")
130 {{/code}}
Jean-Vincent Drean 1.19 131
Marius Dumitru Florea 26.1 132 {{code language="none"}}
133 #set($hql = ", BaseObject as obj, StringProperty as firstName, StringProperty as lastName where doc.fullName = obj.name and obj.className='XWiki.XWikiUsers' and obj.id=firstName.id.id and firstName.id.name='first_name' and obj.id=lastName.id.id and lastName.id.name='last_name' and firstName.value like 'A%' and lastName.value like 'B%' order by doc.fullName asc")
134 {{/code}}
135
Vincent Massol 15.1 136 === List users currently editing pages ===
137
Vincent Massol 21.1 138 {{code language="none"}}
139 #set($hql = "select distinct lock.userName from XWikiLock lock")
140 {{/code}}
Jean-Vincent Drean 13.1 141
Vincent Massol 15.1 142 === List attachments of a page ===
143
Vincent Massol 21.1 144 {{code language="none"}}
145 #set($hql = "select att.filename from XWikiAttachment att, XWikiDocument doc where doc.fullName='Main.WebHome' and att.docId=doc.id")
146 {{/code}}
Jean-Vincent Drean 14.1 147
Marius Dumitru Florea 23.1 148 === Statistics ===
149
Vincent Massol 26.2 150 * [[Most Viewed Articles Snippet>>extensions:Extension.Most Viewed Articles]]
151 * [[Most Active Contributors In Group Snippet>>extensions:Extension.Most Active Contributors In Group]]
152 * [[Number Of Active Users Per Day And Per Week Snippet>>extensions:Extension.Number Of Active Users Per Day And Per Week]]
153 * [[Number Of Edited Articles Per Day And Per Week Snippet>>extensions:Extension.Number Of Edited Articles Per Day And Per Week]]
154 * [[Number Of Created Articles Per Day And Per Week Snippet>>extensions:Extension.Number Of Created Articles Per Day And Per Week]]
155 * [[Number Of Deleted Articles Per Day And Per Week Snippet>>extensions:Extension.Number Of Deleted Articles Per Day And Per Week]]
Marius Dumitru Florea 23.1 156
Vincent Massol 21.1 157 == Non-exhaustive list of queryable Object Fields ==
flavius 27.1 158
MartinJecny 25.1 159 The full list of available fields can be found in the Hibernate mapping files (*.hbm.xml), which can be found inside WEB-INF/lib/xwiki-core-x.y.jar (where x.y is the XWiki version).
Jean-Vincent Drean 1.19 160
Vincent Massol 15.1 161 === XWikiDocument ===
162
Vincent Massol 21.1 163 * **XWikiDocument.fullName** : full name, including space and page name. Example value: ##Main.WebHome##
164 * XWikiDocument.author : last editor. Example value: ##XWiki.Admin##
165 * XWikiDocument.creator : first editor. Example value: ##XWiki.Admin##
Jean-Vincent Drean 10.1 166
Vincent Massol 15.1 167 === BaseObject ===
Jean-Vincent Drean 10.1 168
Vincent Massol 21.1 169 * **BaseObject.id** : arbitrary unique id of the object. Example value: ##123456789##
170 * BaseObject.className : class. Example value: ##XWiki.XWikiUsers##
Jean-Vincent Drean 10.1 171
Vincent Massol 21.1 172 === *Property (StringProperty, IntegerProperty, etc) ===
Jean-Vincent Drean 10.1 173
Vincent Massol 21.1 174 * **Property.id.id** : unique id of the object the property belongs to. Example value: ##123456789##
175 * Property.name : name of the property. Example value: ##first_name##
176 * Property.value : value. Example value: ##John##

Get Connected