Wiki source code of velocityHqlExamples
Version 30.1 by Paul Libbrecht on 2015/01/12
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
27.1 | 1 | {{box cssClass="floatinginfobox" title="**Contents**"}} |
2 | {{toc start="2" depth="4"/}} | ||
3 | {{/box}} | ||
![]() |
17.1 | 4 | |
![]() |
20.1 | 5 | = HQL Query Examples in Velocity = |
![]() |
1.1 | 6 | |
![]() |
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. |
![]() |
1.1 | 8 | |
![]() |
22.1 | 9 | == Public API (##searchDocuments##) == |
10 | |||
![]() |
27.1 | 11 | {{velocity}} |
12 | {{html}} | ||
![]() |
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.") |
![]() |
27.1 | 14 | {{/html}} |
15 | {{/velocity}} | ||
![]() |
22.1 | 16 | |
![]() |
21.1 | 17 | General example showing how to display the first 5 results of a given query: |
![]() |
1.3 | 18 | |
![]() |
21.1 | 19 | {{code language="none"}} |
![]() |
22.2 | 20 | #set($hql = "<query here>") |
![]() |
21.1 | 21 | #set($results = $xwiki.searchDocuments($hql, 5, 0)) |
![]() |
1.3 | 22 | #foreach ($item in $results) |
![]() |
4.5 | 23 | * $item |
![]() |
1.3 | 24 | #end |
![]() |
15.1 | 25 | {{/code}} |
![]() |
1.3 | 26 | |
![]() |
21.1 | 27 | The examples below will show you various HQL queries that you can write. |
28 | |||
![]() |
15.1 | 29 | === Simple Query === |
30 | |||
![]() |
21.1 | 31 | Displays all documents who have been created by the user ##XWiki.JohnDoe##: |
![]() |
1.3 | 32 | |
![]() |
21.1 | 33 | {{code language="none"}} |
34 | #set($hql = "where doc.creator='XWiki.JohnDoe'") | ||
35 | {{/code}} | ||
36 | |||
![]() |
15.1 | 37 | === Ordered Query === |
38 | |||
![]() |
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: |
![]() |
1.21 | 40 | |
![]() |
21.1 | 41 | {{code language="none"}} |
42 | #set($hql = "where doc.creator='XWiki.VincentMassol' order by doc.date asc") | ||
43 | {{/code}} | ||
44 | |||
![]() |
15.1 | 45 | === Advanced Query (date & time) === |
![]() |
1.35 | 46 | |
![]() |
27.1 | 47 | {{velocity}} |
48 | {{html wiki="true"}} | ||
![]() |
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.") |
![]() |
27.1 | 50 | {{/html}} |
51 | {{/velocity}} | ||
![]() |
1.21 | 52 | |
![]() |
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") | ||
![]() |
15.1 | 55 | {{/code}} |
![]() |
1.35 | 56 | |
|
30.1 | 57 | |
58 | === Query documents older than 3 days === | ||
59 | |||
60 | The following snippet deletes all documents in the space TempDocs that are older than 3 days. | ||
61 | For the computation of the relative age, a calendar object created by the $datetool is used and passed to the hibernate query. Tested with MySQL only. | ||
62 | |||
63 | {{code language="none"}} | ||
64 | #set($hql="select doc.fullName from XWikiDocument as doc where doc.web = ? and doc.creationDate < ? ") | ||
65 | #set($cal = $datetool.systemCalendar)$cal.add(7,-3) | ||
66 | #set($olderTempDocs=$xwiki.search($hql,5000, 0, ["TempdDocs",$datetool.toDate($cal)])) | ||
67 | #foreach($x in $olderTempDocs)$xwiki.getDocument($x).deleteWithProgrammingRights()#end | ||
68 | {{/code}} | ||
69 | |||
70 | |||
![]() |
21.1 | 71 | Other examples: |
![]() |
27.1 | 72 | |
![]() |
21.1 | 73 | * 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}} |
74 | * 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}} | ||
75 | * 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}} | ||
![]() |
1.35 | 76 | |
![]() |
22.3 | 77 | == Privileged API (##search##) == |
![]() |
1.3 | 78 | |
![]() |
27.1 | 79 | {{velocity}} |
80 | {{html wiki="true"}} | ||
![]() |
29.1 | 81 | #warning("Calls to the 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.") |
![]() |
27.1 | 82 | {{/html}} |
83 | {{/velocity}} | ||
![]() |
1.1 | 84 | |
![]() |
22.1 | 85 | General example showing how to display the first 5 results of a given query: |
86 | |||
87 | {{code language="none"}} | ||
![]() |
22.2 | 88 | #set($hql = "<query here>") |
![]() |
22.1 | 89 | #set($results = $xwiki.search($hql, 5, 0)) |
90 | #foreach ($item in $results) | ||
91 | * $item | ||
92 | #end | ||
93 | {{/code}} | ||
94 | |||
95 | The examples below will show you various HQL queries that you can write. | ||
96 | |||
![]() |
15.1 | 97 | === Simple Query === |
98 | |||
![]() |
21.1 | 99 | {{code language="none"}} |
100 | #set($hql = "select doc.name from XWikiDocument doc") | ||
101 | {{/code}} | ||
![]() |
1.1 | 102 | |
![]() |
15.1 | 103 | === Count Query === |
![]() |
1.19 | 104 | |
![]() |
21.1 | 105 | {{code language="none"}} |
106 | #set($results = $xwiki.search("select count(doc) from XWikiDocument doc")) | ||
107 | ## Since $xwiki.search is returning a list, we get its first element | ||
108 | Count : $results.get(0) | ||
![]() |
15.1 | 109 | {{/code}} |
![]() |
1.19 | 110 | |
![]() |
15.1 | 111 | === Simple Query with multiple fields === |
112 | |||
![]() |
21.1 | 113 | {{code language="none"}} |
114 | #set($results = $xwiki.search("select doc.name, doc.date from XWikiDocument doc", 5, 0)) | ||
![]() |
1.2 | 115 | #foreach ($row in $results) |
116 | #foreach ($col in $row) | ||
![]() |
21.1 | 117 | #if ($velocityCount == 1) |
118 | #set($docName = $col) | ||
119 | #elseif ($velocityCount == 2) | ||
120 | #set($docDate = $col) | ||
![]() |
1.2 | 121 | #end |
122 | #end | ||
![]() |
21.1 | 123 | $docName : $docDate <br/> |
![]() |
1.2 | 124 | #end |
![]() |
15.1 | 125 | {{/code}} |
![]() |
1.1 | 126 | |
![]() |
15.1 | 127 | === Getting objects of a specific class === |
128 | |||
![]() |
21.1 | 129 | {{code language="none"}} |
130 | #set($hql = "select obj.name from BaseObject obj where obj.className='XWiki.XWikiUsers'") | ||
131 | {{/code}} | ||
![]() |
1.1 | 132 | |
![]() |
15.1 | 133 | === Getting objects' properties === |
134 | |||
![]() |
21.1 | 135 | {{code language="none"}} |
136 | #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'") | ||
137 | {{/code}} | ||
![]() |
1.19 | 138 | |
![]() |
15.1 | 139 | === Getting documents where objects' properties equals some value === |
140 | |||
![]() |
21.1 | 141 | {{code language="none"}} |
142 | #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'") | ||
143 | {{/code}} | ||
![]() |
1.19 | 144 | |
![]() |
26.1 | 145 | {{code language="none"}} |
146 | #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") | ||
147 | {{/code}} | ||
148 | |||
![]() |
15.1 | 149 | === List users currently editing pages === |
150 | |||
![]() |
21.1 | 151 | {{code language="none"}} |
152 | #set($hql = "select distinct lock.userName from XWikiLock lock") | ||
153 | {{/code}} | ||
![]() |
13.1 | 154 | |
![]() |
15.1 | 155 | === List attachments of a page === |
156 | |||
![]() |
21.1 | 157 | {{code language="none"}} |
158 | #set($hql = "select att.filename from XWikiAttachment att, XWikiDocument doc where doc.fullName='Main.WebHome' and att.docId=doc.id") | ||
159 | {{/code}} | ||
![]() |
14.1 | 160 | |
![]() |
23.1 | 161 | === Statistics === |
162 | |||
![]() |
26.2 | 163 | * [[Most Viewed Articles Snippet>>extensions:Extension.Most Viewed Articles]] |
164 | * [[Most Active Contributors In Group Snippet>>extensions:Extension.Most Active Contributors In Group]] | ||
165 | * [[Number Of Active Users Per Day And Per Week Snippet>>extensions:Extension.Number Of Active Users Per Day And Per Week]] | ||
166 | * [[Number Of Edited Articles Per Day And Per Week Snippet>>extensions:Extension.Number Of Edited Articles Per Day And Per Week]] | ||
167 | * [[Number Of Created Articles Per Day And Per Week Snippet>>extensions:Extension.Number Of Created Articles Per Day And Per Week]] | ||
168 | * [[Number Of Deleted Articles Per Day And Per Week Snippet>>extensions:Extension.Number Of Deleted Articles Per Day And Per Week]] | ||
![]() |
23.1 | 169 | |
![]() |
21.1 | 170 | == Non-exhaustive list of queryable Object Fields == |
![]() |
27.1 | 171 | |
![]() |
25.1 | 172 | 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). |
![]() |
1.19 | 173 | |
![]() |
15.1 | 174 | === XWikiDocument === |
175 | |||
![]() |
21.1 | 176 | * **XWikiDocument.fullName** : full name, including space and page name. Example value: ##Main.WebHome## |
177 | * XWikiDocument.author : last editor. Example value: ##XWiki.Admin## | ||
178 | * XWikiDocument.creator : first editor. Example value: ##XWiki.Admin## | ||
![]() |
10.1 | 179 | |
![]() |
15.1 | 180 | === BaseObject === |
![]() |
10.1 | 181 | |
![]() |
21.1 | 182 | * **BaseObject.id** : arbitrary unique id of the object. Example value: ##123456789## |
183 | * BaseObject.className : class. Example value: ##XWiki.XWikiUsers## | ||
![]() |
10.1 | 184 | |
![]() |
21.1 | 185 | === *Property (StringProperty, IntegerProperty, etc) === |
![]() |
10.1 | 186 | |
![]() |
21.1 | 187 | * **Property.id.id** : unique id of the object the property belongs to. Example value: ##123456789## |
188 | * Property.name : name of the property. Example value: ##first_name## | ||
189 | * Property.value : value. Example value: ##John## |