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

Get Connected