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