Wiki source code of velocityHqlExamples
Version 19.1 by Vincent Massol on 2009/09/08
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 | |
![]() |
15.1 | 3 | {{include document="DevGuide.velocityHqlExamplesMacro"/}} |
![]() |
1.36 | 4 | |
![]() |
15.1 | 5 | = Velocity HQL query examples = |
![]() |
1.1 | 6 | |
![]() |
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. \\ |
![]() |
1.1 | 8 | |
![]() |
15.1 | 9 | == Public API (searchDocuments) == |
![]() |
1.3 | 10 | |
![]() |
15.1 | 11 | {{velocity filter="none"}} |
12 | {{html clean="false" wiki="true"}} | ||
![]() |
1.6 | 13 | #info("With this API the query consist in the WHERE condition.\\ |
![]() |
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.") | ||
![]() |
15.1 | 16 | <p/> |
17 | You can execute queries as follows: | ||
![]() |
1.3 | 18 | |
![]() |
15.1 | 19 | {{code}} |
![]() |
1.22 | 20 | #set($query="where doc.creator='XWiki.VincentMassol'") |
![]() |
1.25 | 21 | #set($results = $xwiki.searchDocuments($query, 5, 0)) |
![]() |
1.3 | 22 | #foreach ($item in $results) |
![]() |
4.5 | 23 | * $item |
![]() |
1.3 | 24 | #end |
![]() |
15.1 | 25 | {{/code}} |
![]() |
1.3 | 26 | |
![]() |
15.1 | 27 | === Simple Query === |
28 | |||
![]() |
1.22 | 29 | #set($hql="where doc.creator='XWiki.VincentMassol'") |
![]() |
1.3 | 30 | #displayQuery($hql false) |
31 | |||
![]() |
15.1 | 32 | === Ordered Query === |
33 | |||
![]() |
1.22 | 34 | #set($hql="where doc.creator='XWiki.VincentMassol' order by doc.date asc") |
![]() |
1.21 | 35 | #displayQuery($hql false) |
36 | |||
![]() |
15.1 | 37 | === Advanced Query (date & time) === |
![]() |
1.35 | 38 | |
![]() |
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/> | ||
![]() |
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") |
![]() |
1.21 | 42 | #displayQuery($hql false) |
43 | |||
![]() |
15.1 | 44 | {{code}} |
![]() |
1.35 | 45 | Other examples, documents modified : |
![]() |
1.21 | 46 | |
![]() |
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" | ||
![]() |
15.1 | 50 | {{/code}} |
![]() |
1.35 | 51 | |
![]() |
15.1 | 52 | == Privileged API (search : Documents, Objects, Properties, etc) == |
![]() |
1.35 | 53 | |
![]() |
1.6 | 54 | #warning("Calls to te privileged API are only executed when the calling page has been saved by an Admin. \\ |
![]() |
4.4 | 55 | The reason is that search can be used to send any HQL command like update, delete, etc.") |
![]() |
15.1 | 56 | <p/> |
57 | You can execute queries as follows: | ||
![]() |
1.3 | 58 | |
![]() |
15.1 | 59 | {{code}} |
![]() |
1.2 | 60 | #set($query="select doc.name from XWikiDocument doc") |
![]() |
1.25 | 61 | #set($results = $xwiki.search($query, 5, 0)) |
![]() |
1.2 | 62 | #foreach ($item in $results) |
63 | * $item <br/> | ||
64 | #end | ||
![]() |
15.1 | 65 | {{/code}} |
![]() |
1.1 | 66 | |
![]() |
15.1 | 67 | === Simple Query === |
68 | |||
![]() |
1.1 | 69 | #set($hql="select doc.name from XWikiDocument doc") |
![]() |
1.3 | 70 | #displayQuery($hql true) |
![]() |
1.1 | 71 | |
![]() |
15.1 | 72 | === Count Query === |
![]() |
1.19 | 73 | |
![]() |
15.1 | 74 | {{code}} |
![]() |
1.19 | 75 | #set($query="select count(doc) from XWikiDocument doc") |
![]() |
1.20 | 76 | #set($results = $xwiki.search($query)) |
![]() |
1.21 | 77 | ## $xwiki.search returning a list, we get its first element |
![]() |
1.19 | 78 | $query result : $results.get(0) |
![]() |
15.1 | 79 | {{/code}} |
![]() |
1.19 | 80 | |
81 | #set($query="select count(doc) from XWikiDocument doc") | ||
![]() |
1.20 | 82 | #set($results = $xwiki.search($query)) |
83 | $query results : $results.get(0) | ||
![]() |
15.1 | 84 | <p/> |
![]() |
1.1 | 85 | <br/> |
![]() |
1.6 | 86 | |
![]() |
15.1 | 87 | === Simple Query with multiple fields === |
88 | |||
89 | {{code}} | ||
![]() |
1.25 | 90 | #set($results=$xwiki.search("select doc.name, doc.date from XWikiDocument doc", 5, 0)) |
![]() |
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 | ||
![]() |
15.1 | 101 | {{/code}} |
![]() |
1.1 | 102 | |
103 | #set($hql="select doc.name, doc.date from XWikiDocument doc") | ||
![]() |
1.3 | 104 | #displayQuery($hql true) |
![]() |
15.1 | 105 | <p/> |
106 | <br/> | ||
![]() |
1.1 | 107 | |
![]() |
15.1 | 108 | === Getting objects of a specific class === |
109 | |||
![]() |
1.1 | 110 | #set($hql="select obj.name from BaseObject obj where obj.className='XWiki.XWikiUsers'") |
![]() |
1.3 | 111 | #displayQuery($hql true) |
![]() |
15.1 | 112 | <p/> |
113 | <br/> | ||
![]() |
1.1 | 114 | |
![]() |
15.1 | 115 | === Getting objects' properties === |
116 | |||
![]() |
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'") |
![]() |
1.3 | 118 | #displayQuery($hql true) |
![]() |
15.1 | 119 | <p/> |
120 | <br/> | ||
![]() |
1.19 | 121 | |
![]() |
15.1 | 122 | === Getting documents where objects' properties equals some value === |
123 | |||
![]() |
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'") |
![]() |
1.19 | 125 | #displayQuery($hql true) |
126 | |||
![]() |
15.1 | 127 | === List users currently editing pages === |
128 | |||
![]() |
13.1 | 129 | #set($hql="select distinct lock.userName from XWikiLock lock") |
130 | #displayQuery($hql true) | ||
131 | |||
![]() |
15.1 | 132 | === List attachments of a page === |
133 | |||
![]() |
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 | |||
![]() |
15.1 | 137 | == Non-exhaustive list of queryable object fields == |
![]() |
1.19 | 138 | |
![]() |
15.1 | 139 | #macro(exval $value)Example of value : //$value//#end |
![]() |
1.30 | 140 | |
![]() |
10.1 | 141 | |
![]() |
15.1 | 142 | === XWikiDocument === |
143 | |||
144 | * **XWikiDocument.fullName** : full name, including space and page name. #exval("Main.WebHome"). | ||
![]() |
10.1 | 145 | * XWikiDocument.author : last editor. #exval("XWiki.Admin"). |
146 | * XWikiDocument.creator : first editor. #exval("XWiki.Admin"). | ||
147 | |||
![]() |
15.1 | 148 | === BaseObject === |
![]() |
10.1 | 149 | |
![]() |
15.1 | 150 | * **BaseObject.id** : arbitrary unique id of the object. #exval("123456789"). |
![]() |
10.1 | 151 | * BaseObject.className : class. #exval("XWiki.XWikiUsers"). |
152 | |||
![]() |
15.1 | 153 | === *Property (StringProperty, etc) === |
![]() |
10.1 | 154 | |
![]() |
15.1 | 155 | * **Property.id.id** : unique id of the object the property belongs to. #exval("123456789"). |
![]() |
10.1 | 156 | * Property.name : name of the property. #exval("first_name"). |
157 | * Property.value : value. #exval("John"). | ||
![]() |
15.1 | 158 | {{/html}} |
159 | {{/velocity}} |