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