Wiki source code of velocityHqlExamples

Version 3.2 by Vincent Massol on 2008/04/23

Hide last authors
Jean-Vincent Drean 1.36 1 <style>
2 .code {
Jean-Vincent Drean 1.37 3 width: 100%;
Jean-Vincent Drean 1.36 4 }
5 </style>
6
Vincent Massol 3.1 7 #includeMacros("DevGuide.velocityHqlExamplesMacro")
Jean-Vincent Drean 1.23 8
Vincent Massol 1.34 9 1 Velocity HQL query examples
Jean-Vincent Drean 1.1 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
Jean-Vincent Drean 1.31 13 #toc("" "" "")
14
Jean-Vincent Drean 1.28 15 #warning("This page can only be edited by Admins since it contains calls to the privileged API. \\
Jean-Vincent Drean 1.29 16 If you want to make an addition or a correction please post a comment here or send it to the mailing lists.")
Jean-Vincent Drean 1.28 17
Jean-Vincent Drean 1.22 18 1.1 Public API (searchDocuments)
Jean-Vincent Drean 1.3 19
Jean-Vincent Drean 1.6 20 #info("With this API the query consist in the WHERE condition.\\
21 Any user with edit rights can use this API")
Jean-Vincent Drean 1.3 22
Jean-Vincent Drean 1.1 23 You can execute queries as follows:
24 {code}
Jean-Vincent Drean 1.22 25 #set($query="where doc.creator='XWiki.VincentMassol'")
Jean-Vincent Drean 1.25 26 #set($results = $xwiki.searchDocuments($query, 5, 0))
Jean-Vincent Drean 1.3 27 #foreach ($item in $results)
28 * $item <br/>
29 #end
30 {code}
31
Vincent Massol 1.33 32 1.1.1 Simple Query
Jean-Vincent Drean 1.22 33 #set($hql="where doc.creator='XWiki.VincentMassol'")
Jean-Vincent Drean 1.3 34 #displayQuery($hql false)
35
Jean-Vincent Drean 1.21 36 <br/>
Jean-Vincent Drean 1.35 37 1.1.1 Ordered Query
Jean-Vincent Drean 1.22 38 #set($hql="where doc.creator='XWiki.VincentMassol' order by doc.date asc")
Jean-Vincent Drean 1.21 39 #displayQuery($hql false)
40
41 <br/>
Jean-Vincent Drean 1.35 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")
Jean-Vincent Drean 1.21 47 #displayQuery($hql false)
48
Jean-Vincent Drean 1.35 49 {code}
50 Other examples, documents modified :
Jean-Vincent Drean 1.21 51
Jean-Vincent Drean 1.35 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
Jean-Vincent Drean 1.22 58 1.1 Privileged API (search : Documents, Objects, Properties, etc)
Jean-Vincent Drean 1.3 59
Jean-Vincent Drean 1.6 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. \\ ")
Jean-Vincent Drean 1.3 62
63 You can execute queries as follows:
64 {code}
Jean-Vincent Drean 1.2 65 #set($query="select doc.name from XWikiDocument doc")
Jean-Vincent Drean 1.25 66 #set($results = $xwiki.search($query, 5, 0))
Jean-Vincent Drean 1.2 67 #foreach ($item in $results)
68 * $item <br/>
69 #end
Jean-Vincent Drean 1.1 70 {code}
71
Vincent Massol 1.33 72 1.1.1 Simply Query
Jean-Vincent Drean 1.1 73 #set($hql="select doc.name from XWikiDocument doc")
Jean-Vincent Drean 1.3 74 #displayQuery($hql true)
Jean-Vincent Drean 1.1 75
Vincent Massol 1.33 76 1.1.1 Count Query
Jean-Vincent Drean 1.19 77
78 {code}
79 #set($query="select count(doc) from XWikiDocument doc")
Jean-Vincent Drean 1.20 80 #set($results = $xwiki.search($query))
Jean-Vincent Drean 1.21 81 ## $xwiki.search returning a list, we get its first element
Jean-Vincent Drean 1.19 82 $query result : $results.get(0)
83 {code}
84
85 #set($query="select count(doc) from XWikiDocument doc")
Jean-Vincent Drean 1.20 86 #set($results = $xwiki.search($query))
87 $query results : $results.get(0)
Jean-Vincent Drean 1.19 88
Jean-Vincent Drean 1.1 89 <br/>
Vincent Massol 1.33 90 1.1.1 Simply Query with multiple fields
Jean-Vincent Drean 1.6 91
Jean-Vincent Drean 1.1 92 {code}
Jean-Vincent Drean 1.25 93 #set($results=$xwiki.search("select doc.name, doc.date from XWikiDocument doc", 5, 0))
Jean-Vincent Drean 1.2 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
Jean-Vincent Drean 1.1 104 {code}
105
106 #set($hql="select doc.name, doc.date from XWikiDocument doc")
Jean-Vincent Drean 1.3 107 #displayQuery($hql true)
Jean-Vincent Drean 1.1 108
109 <br/>
Jean-Vincent Drean 1.3 110 1.1.1 Getting objects of a specific class
Jean-Vincent Drean 1.1 111 #set($hql="select obj.name from BaseObject obj where obj.className='XWiki.XWikiUsers'")
Jean-Vincent Drean 1.3 112 #displayQuery($hql true)
Jean-Vincent Drean 1.1 113
114 <br/>
Jean-Vincent Drean 1.3 115 1.1.1 Getting objects' properties
Jean-Vincent Drean 1.1 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'")
Jean-Vincent Drean 1.3 117 #displayQuery($hql true)
Jean-Vincent Drean 1.19 118
119 <br/>
Jean-Vincent Drean 1.30 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'")
Jean-Vincent Drean 1.19 122 #displayQuery($hql true)
123
124
Jean-Vincent Drean 1.30 125

Get Connected