Wiki source code of velocityHqlExamples

Version 20.1 by Vincent Massol on 2009/09/08

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 15.1 3 {{include document="DevGuide.velocityHqlExamplesMacro"/}}
Jean-Vincent Drean 1.36 4
Vincent Massol 20.1 5 = HQL Query Examples in Velocity =
Jean-Vincent Drean 1.1 6
Vincent Massol 19.1 7 XWiki allows user to access documents and objects with [[HQL>>http://www.hibernate.org/hib_docs/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 15.1 9 == Public API (searchDocuments) ==
Jean-Vincent Drean 1.3 10
Vincent Massol 15.1 11 {{velocity filter="none"}}
12 {{html clean="false" wiki="true"}}
Jean-Vincent Drean 1.6 13 #info("With this API the query consist in the WHERE condition.\\
Sergiu Dumitriu 4.4 14 Any user with edit rights can use this API (as in write a script using it).\\
15 Any user with view rights can view the result of such a query.")
Vincent Massol 15.1 16 <p/>
17 You can execute queries as follows:
Jean-Vincent Drean 1.3 18
Vincent Massol 15.1 19 {{code}}
Jean-Vincent Drean 1.22 20 #set($query="where doc.creator='XWiki.VincentMassol'")
Jean-Vincent Drean 1.25 21 #set($results = $xwiki.searchDocuments($query, 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 15.1 27 === Simple Query ===
28
Jean-Vincent Drean 1.22 29 #set($hql="where doc.creator='XWiki.VincentMassol'")
Jean-Vincent Drean 1.3 30 #displayQuery($hql false)
31
Vincent Massol 15.1 32 === Ordered Query ===
33
Jean-Vincent Drean 1.22 34 #set($hql="where doc.creator='XWiki.VincentMassol' order by doc.date asc")
Jean-Vincent Drean 1.21 35 #displayQuery($hql false)
36
Vincent Massol 15.1 37 === Advanced Query (date & time) ===
Jean-Vincent Drean 1.35 38
Vincent Massol 15.1 39 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.
40 <p/>
Jean-Vincent Drean 1.35 41 #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")
Jean-Vincent Drean 1.21 42 #displayQuery($hql false)
43
Vincent Massol 15.1 44 {{code}}
Jean-Vincent Drean 1.35 45 Other examples, documents modified :
Jean-Vincent Drean 1.21 46
Jean-Vincent Drean 1.35 47 during current day : "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"
48 during current week : "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"
49 during current month : "where year(doc.date) = year(current_date()) and month(doc.date) > (month(current_date()) - 1) order by doc.date desc"
Vincent Massol 15.1 50 {{/code}}
Jean-Vincent Drean 1.35 51
Vincent Massol 15.1 52 == Privileged API (search : Documents, Objects, Properties, etc) ==
Jean-Vincent Drean 1.35 53
Jean-Vincent Drean 1.6 54 #warning("Calls to te privileged API are only executed when the calling page has been saved by an Admin. \\
Sergiu Dumitriu 4.4 55 The reason is that search can be used to send any HQL command like update, delete, etc.")
Vincent Massol 15.1 56 <p/>
57 You can execute queries as follows:
Jean-Vincent Drean 1.3 58
Vincent Massol 15.1 59 {{code}}
Jean-Vincent Drean 1.2 60 #set($query="select doc.name from XWikiDocument doc")
Jean-Vincent Drean 1.25 61 #set($results = $xwiki.search($query, 5, 0))
Jean-Vincent Drean 1.2 62 #foreach ($item in $results)
63 * $item <br/>
64 #end
Vincent Massol 15.1 65 {{/code}}
Jean-Vincent Drean 1.1 66
Vincent Massol 15.1 67 === Simple Query ===
68
Jean-Vincent Drean 1.1 69 #set($hql="select doc.name from XWikiDocument doc")
Jean-Vincent Drean 1.3 70 #displayQuery($hql true)
Jean-Vincent Drean 1.1 71
Vincent Massol 15.1 72 === Count Query ===
Jean-Vincent Drean 1.19 73
Vincent Massol 15.1 74 {{code}}
Jean-Vincent Drean 1.19 75 #set($query="select count(doc) from XWikiDocument doc")
Jean-Vincent Drean 1.20 76 #set($results = $xwiki.search($query))
Jean-Vincent Drean 1.21 77 ## $xwiki.search returning a list, we get its first element
Jean-Vincent Drean 1.19 78 $query result : $results.get(0)
Vincent Massol 15.1 79 {{/code}}
Jean-Vincent Drean 1.19 80
81 #set($query="select count(doc) from XWikiDocument doc")
Jean-Vincent Drean 1.20 82 #set($results = $xwiki.search($query))
83 $query results : $results.get(0)
Vincent Massol 15.1 84 <p/>
Jean-Vincent Drean 1.1 85 <br/>
Jean-Vincent Drean 1.6 86
Vincent Massol 15.1 87 === Simple Query with multiple fields ===
88
89 {{code}}
Jean-Vincent Drean 1.25 90 #set($results=$xwiki.search("select doc.name, doc.date from XWikiDocument doc", 5, 0))
Jean-Vincent Drean 1.2 91 #foreach ($row in $results)
92 #foreach ($col in $row)
93 #if ($velocityCount==1)
94 #set($docName=$col)
95 #elseif ($velocityCount==2)
96 #set($docDate=$col)
97 #end
98 #end
99 $docName : $docDate <br/>
100 #end
Vincent Massol 15.1 101 {{/code}}
Jean-Vincent Drean 1.1 102
103 #set($hql="select doc.name, doc.date from XWikiDocument doc")
Jean-Vincent Drean 1.3 104 #displayQuery($hql true)
Vincent Massol 15.1 105 <p/>
106 <br/>
Jean-Vincent Drean 1.1 107
Vincent Massol 15.1 108 === Getting objects of a specific class ===
109
Jean-Vincent Drean 1.1 110 #set($hql="select obj.name from BaseObject obj where obj.className='XWiki.XWikiUsers'")
Jean-Vincent Drean 1.3 111 #displayQuery($hql true)
Vincent Massol 15.1 112 <p/>
113 <br/>
Jean-Vincent Drean 1.1 114
Vincent Massol 15.1 115 === Getting objects' properties ===
116
Jean-Vincent Drean 1.1 117 #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'")
Jean-Vincent Drean 1.3 118 #displayQuery($hql true)
Vincent Massol 15.1 119 <p/>
120 <br/>
Jean-Vincent Drean 1.19 121
Vincent Massol 15.1 122 === Getting documents where objects' properties equals some value ===
123
Jean-Vincent Drean 1.30 124 #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'")
Jean-Vincent Drean 1.19 125 #displayQuery($hql true)
126
Vincent Massol 15.1 127 === List users currently editing pages ===
128
Jean-Vincent Drean 13.1 129 #set($hql="select distinct lock.userName from XWikiLock lock")
130 #displayQuery($hql true)
131
Vincent Massol 15.1 132 === List attachments of a page ===
133
Jean-Vincent Drean 14.1 134 #set($hql="select att.filename from XWikiAttachment att, XWikiDocument doc where doc.fullName='Main.WebHome' and att.docId=doc.id")
135 #displayQuery($hql true)
136
Vincent Massol 15.1 137 == Non-exhaustive list of queryable object fields ==
Jean-Vincent Drean 1.19 138
Vincent Massol 15.1 139 #macro(exval $value)Example of value : //$value//#end
Jean-Vincent Drean 1.30 140
Jean-Vincent Drean 10.1 141
Vincent Massol 15.1 142 === XWikiDocument ===
143
144 * **XWikiDocument.fullName** : full name, including space and page name. #exval("Main.WebHome").
Jean-Vincent Drean 10.1 145 * XWikiDocument.author : last editor. #exval("XWiki.Admin").
146 * XWikiDocument.creator : first editor. #exval("XWiki.Admin").
147
Vincent Massol 15.1 148 === BaseObject ===
Jean-Vincent Drean 10.1 149
Vincent Massol 15.1 150 * **BaseObject.id** : arbitrary unique id of the object. #exval("123456789").
Jean-Vincent Drean 10.1 151 * BaseObject.className : class. #exval("XWiki.XWikiUsers").
152
Vincent Massol 15.1 153 === *Property (StringProperty, etc) ===
Jean-Vincent Drean 10.1 154
Vincent Massol 15.1 155 * **Property.id.id** : unique id of the object the property belongs to. #exval("123456789").
Jean-Vincent Drean 10.1 156 * Property.name : name of the property. #exval("first_name").
157 * Property.value : value. #exval("John").
Vincent Massol 15.1 158 {{/html}}
159 {{/velocity}}

Get Connected