Wiki source code of velocityHqlExamples
Version 3.2 by Vincent Massol on 2008/04/23
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | <style> | ||
2 | .code { | ||
3 | width: 100%; | ||
4 | } | ||
5 | </style> | ||
6 | |||
7 | #includeMacros("DevGuide.velocityHqlExamplesMacro") | ||
8 | |||
9 | 1 Velocity HQL query examples | ||
10 | |||
11 | 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. <br/> | ||
12 | |||
13 | #toc("" "" "") | ||
14 | |||
15 | #warning("This page can only be edited by Admins since it contains calls to the privileged API. \\ | ||
16 | If you want to make an addition or a correction please post a comment here or send it to the mailing lists.") | ||
17 | |||
18 | 1.1 Public API (searchDocuments) | ||
19 | |||
20 | #info("With this API the query consist in the WHERE condition.\\ | ||
21 | Any user with edit rights can use this API") | ||
22 | |||
23 | You can execute queries as follows: | ||
24 | {code} | ||
25 | #set($query="where doc.creator='XWiki.VincentMassol'") | ||
26 | #set($results = $xwiki.searchDocuments($query, 5, 0)) | ||
27 | #foreach ($item in $results) | ||
28 | * $item <br/> | ||
29 | #end | ||
30 | {code} | ||
31 | |||
32 | 1.1.1 Simple Query | ||
33 | #set($hql="where doc.creator='XWiki.VincentMassol'") | ||
34 | #displayQuery($hql false) | ||
35 | |||
36 | <br/> | ||
37 | 1.1.1 Ordered Query | ||
38 | #set($hql="where doc.creator='XWiki.VincentMassol' order by doc.date asc") | ||
39 | #displayQuery($hql false) | ||
40 | |||
41 | <br/> | ||
42 | 1.1.1 Advanced Query (date & time) | ||
43 | |||
44 | 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 unatural. | ||
45 | |||
46 | #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") | ||
47 | #displayQuery($hql false) | ||
48 | |||
49 | {code} | ||
50 | Other examples, documents modified : | ||
51 | |||
52 | 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" | ||
53 | 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" | ||
54 | during current month : "where year(doc.date) = year(current_date()) and month(doc.date) > (month(current_date()) - 1) order by doc.date desc" | ||
55 | {code} | ||
56 | |||
57 | |||
58 | 1.1 Privileged API (search : Documents, Objects, Properties, etc) | ||
59 | |||
60 | #warning("Calls to te privileged API are only executed when the calling page has been saved by an Admin. \\ | ||
61 | The reason is that search can be used to send any HQL command like update, delete, etc. \\ ") | ||
62 | |||
63 | You can execute queries as follows: | ||
64 | {code} | ||
65 | #set($query="select doc.name from XWikiDocument doc") | ||
66 | #set($results = $xwiki.search($query, 5, 0)) | ||
67 | #foreach ($item in $results) | ||
68 | * $item <br/> | ||
69 | #end | ||
70 | {code} | ||
71 | |||
72 | 1.1.1 Simply Query | ||
73 | #set($hql="select doc.name from XWikiDocument doc") | ||
74 | #displayQuery($hql true) | ||
75 | |||
76 | 1.1.1 Count Query | ||
77 | |||
78 | {code} | ||
79 | #set($query="select count(doc) from XWikiDocument doc") | ||
80 | #set($results = $xwiki.search($query)) | ||
81 | ## $xwiki.search returning a list, we get its first element | ||
82 | $query result : $results.get(0) | ||
83 | {code} | ||
84 | |||
85 | #set($query="select count(doc) from XWikiDocument doc") | ||
86 | #set($results = $xwiki.search($query)) | ||
87 | $query results : $results.get(0) | ||
88 | |||
89 | <br/> | ||
90 | 1.1.1 Simply Query with multiple fields | ||
91 | |||
92 | {code} | ||
93 | #set($results=$xwiki.search("select doc.name, doc.date from XWikiDocument doc", 5, 0)) | ||
94 | #foreach ($row in $results) | ||
95 | #foreach ($col in $row) | ||
96 | #if ($velocityCount==1) | ||
97 | #set($docName=$col) | ||
98 | #elseif ($velocityCount==2) | ||
99 | #set($docDate=$col) | ||
100 | #end | ||
101 | #end | ||
102 | $docName : $docDate <br/> | ||
103 | #end | ||
104 | {code} | ||
105 | |||
106 | #set($hql="select doc.name, doc.date from XWikiDocument doc") | ||
107 | #displayQuery($hql true) | ||
108 | |||
109 | <br/> | ||
110 | 1.1.1 Getting objects of a specific class | ||
111 | #set($hql="select obj.name from BaseObject obj where obj.className='XWiki.XWikiUsers'") | ||
112 | #displayQuery($hql true) | ||
113 | |||
114 | <br/> | ||
115 | 1.1.1 Getting objects' properties | ||
116 | #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'") | ||
117 | #displayQuery($hql true) | ||
118 | |||
119 | <br/> | ||
120 | 1.1.1 Getting documents where objects' properties equals some value | ||
121 | #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'") | ||
122 | #displayQuery($hql true) |