Wiki source code of velocityHqlExamples

Version 26.4 by Sergiu Dumitriu on 2011/11/02

Hide last authors
Vincent Massol 18.1 1 {{box cssClass="floatinginfobox" title="**Contents**"}}{{toc start="2" depth="4"/}}{{/box}}
Vincent Massol 17.1 2
Vincent Massol 20.1 3 = HQL Query Examples in Velocity =
Jean-Vincent Drean 1.1 4
Jean-Vincent Drean 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.
Jean-Vincent Drean 1.1 6
Vincent Massol 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
Vincent Massol 21.1 13 General example showing how to display the first 5 results of a given query:
Jean-Vincent Drean 1.3 14
Vincent Massol 21.1 15 {{code language="none"}}
Vincent Massol 22.2 16 #set($hql = "<query here>")
Vincent Massol 21.1 17 #set($results = $xwiki.searchDocuments($hql, 5, 0))
Jean-Vincent Drean 1.3 18 #foreach ($item in $results)
Sergiu Dumitriu 4.5 19 * $item
Jean-Vincent Drean 1.3 20 #end
Vincent Massol 15.1 21 {{/code}}
Jean-Vincent Drean 1.3 22
Vincent Massol 21.1 23 The examples below will show you various HQL queries that you can write.
24
Vincent Massol 15.1 25 === Simple Query ===
26
Vincent Massol 21.1 27 Displays all documents who have been created by the user ##XWiki.JohnDoe##:
Jean-Vincent Drean 1.3 28
Vincent Massol 21.1 29 {{code language="none"}}
30 #set($hql = "where doc.creator='XWiki.JohnDoe'")
31 {{/code}}
32
Vincent Massol 15.1 33 === Ordered Query ===
34
Vincent Massol 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:
Jean-Vincent Drean 1.21 36
Vincent Massol 21.1 37 {{code language="none"}}
38 #set($hql = "where doc.creator='XWiki.VincentMassol' order by doc.date asc")
39 {{/code}}
40
Vincent Massol 15.1 41 === Advanced Query (date & time) ===
Jean-Vincent Drean 1.35 42
Vincent Massol 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}}
Jean-Vincent Drean 1.21 46
Vincent Massol 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")
Vincent Massol 15.1 49 {{/code}}
Jean-Vincent Drean 1.35 50
Vincent Massol 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}}
Jean-Vincent Drean 1.35 55
Vincent Massol 22.3 56 == Privileged API (##search##) ==
Jean-Vincent Drean 1.3 57
Vincent Massol 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.")
Vincent Massol 21.1 60 {{/html}}{{/velocity}}
Jean-Vincent Drean 1.1 61
Vincent Massol 22.1 62 General example showing how to display the first 5 results of a given query:
63
64 {{code language="none"}}
Vincent Massol 22.2 65 #set($hql = "<query here>")
Vincent Massol 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
Vincent Massol 15.1 74 === Simple Query ===
75
Vincent Massol 21.1 76 {{code language="none"}}
77 #set($hql = "select doc.name from XWikiDocument doc")
78 {{/code}}
Jean-Vincent Drean 1.1 79
Vincent Massol 15.1 80 === Count Query ===
Jean-Vincent Drean 1.19 81
Vincent Massol 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)
Vincent Massol 15.1 86 {{/code}}
Jean-Vincent Drean 1.19 87
Vincent Massol 15.1 88 === Simple Query with multiple fields ===
89
Vincent Massol 21.1 90 {{code language="none"}}
91 #set($results = $xwiki.search("select doc.name, doc.date from XWikiDocument doc", 5, 0))
Jean-Vincent Drean 1.2 92 #foreach ($row in $results)
93 #foreach ($col in $row)
Vincent Massol 21.1 94 #if ($velocityCount == 1)
95 #set($docName = $col)
96 #elseif ($velocityCount == 2)
97 #set($docDate = $col)
Jean-Vincent Drean 1.2 98 #end
99 #end
Vincent Massol 21.1 100 $docName : $docDate <br/>
Jean-Vincent Drean 1.2 101 #end
Vincent Massol 15.1 102 {{/code}}
Jean-Vincent Drean 1.1 103
Vincent Massol 15.1 104 === Getting objects of a specific class ===
105
Vincent Massol 21.1 106 {{code language="none"}}
107 #set($hql = "select obj.name from BaseObject obj where obj.className='XWiki.XWikiUsers'")
108 {{/code}}
Jean-Vincent Drean 1.1 109
Vincent Massol 15.1 110 === Getting objects' properties ===
111
Vincent Massol 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}}
Jean-Vincent Drean 1.19 115
Vincent Massol 15.1 116 === Getting documents where objects' properties equals some value ===
117
Vincent Massol 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}}
Jean-Vincent Drean 1.19 121
Marius Dumitru Florea 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
Vincent Massol 15.1 126 === List users currently editing pages ===
127
Vincent Massol 21.1 128 {{code language="none"}}
129 #set($hql = "select distinct lock.userName from XWikiLock lock")
130 {{/code}}
Jean-Vincent Drean 13.1 131
Vincent Massol 15.1 132 === List attachments of a page ===
133
Vincent Massol 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}}
Jean-Vincent Drean 14.1 137
Marius Dumitru Florea 23.1 138 === Statistics ===
139
Vincent Massol 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]]
Marius Dumitru Florea 23.1 146
Vincent Massol 21.1 147 == Non-exhaustive list of queryable Object Fields ==
MartinJecny 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).
Jean-Vincent Drean 1.19 149
Vincent Massol 15.1 150 === XWikiDocument ===
151
Vincent Massol 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##
Jean-Vincent Drean 10.1 155
Vincent Massol 15.1 156 === BaseObject ===
Jean-Vincent Drean 10.1 157
Vincent Massol 21.1 158 * **BaseObject.id** : arbitrary unique id of the object. Example value: ##123456789##
159 * BaseObject.className : class. Example value: ##XWiki.XWikiUsers##
Jean-Vincent Drean 10.1 160
Vincent Massol 21.1 161 === *Property (StringProperty, IntegerProperty, etc) ===
Jean-Vincent Drean 10.1 162
Vincent Massol 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

Get Connected