Wiki source code of velocityHqlExamples
Version 1.29 by Jean-Vincent Drean on 2007/10/09
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
1.1 | 1 | #################################### |
![]() |
1.3 | 2 | ## Macro to display the result of any search query in a table |
3 | #macro(displayQuery $query $privileged) | ||
![]() |
1.1 | 4 | #################################### |
![]() |
1.12 | 5 | #set ($results = "") |
![]() |
1.3 | 6 | #if (!$privileged) |
![]() |
1.25 | 7 | #set($results = $xwiki.searchDocuments($query,5,0)) |
![]() |
1.26 | 8 | *\xwiki.searchDocuments(\"${query}\", 5, 0) returns:* |
![]() |
1.23 | 9 | |
![]() |
1.3 | 10 | #else |
![]() |
1.27 | 11 | #set($results = $xwiki.search($query,5,0)) |
![]() |
1.26 | 12 | *\xwiki.search(\"${query}\", 5, 0) returns:* |
![]() |
1.19 | 13 | |
![]() |
1.3 | 14 | #end |
![]() |
1.1 | 15 | {table} |
16 | #foreach ($row in $results) | ||
17 | #if ($velocityCount==1) ### only once per table | ||
18 | ##-------------------------- | ||
19 | ## establish num columns: | ||
20 | ##-------------------------- | ||
21 | #set($numCols=0) | ||
22 | #foreach ($col in $row) | ||
23 | #set($numCols=$velocityCount) | ||
24 | #end | ||
25 | ##-------------------------- | ||
26 | ## print header: | ||
27 | ##-------------------------- | ||
28 | #if ($numCols>0) | ||
![]() |
1.18 | 29 | #foreach ($col in [1..$numCols]) field $col | #end |
30 | |||
![]() |
1.1 | 31 | #else |
32 | Single field | ||
33 | #end | ||
34 | #end ### end only once per table | ||
35 | ##-------------------------- | ||
36 | ## table body: | ||
37 | ##-------------------------- | ||
38 | #if ($numCols>0) | ||
![]() |
1.18 | 39 | #foreach ($column in $row) {pre}$column{/pre} | #end |
40 | |||
![]() |
1.1 | 41 | #else |
![]() |
1.17 | 42 | {pre}$row{/pre} |
![]() |
1.1 | 43 | #end |
44 | #end | ||
45 | {table} | ||
46 | #################################### | ||
47 | #end | ||
48 | #################################### | ||
49 | |||
50 | 1 Velocity HQL query examples. | ||
51 | |||
52 | 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/> | ||
53 | |||
![]() |
1.28 | 54 | #warning("This page can only be edited by Admins since it contains calls to the privileged API. \\ |
![]() |
1.29 | 55 | If you want to make an addition or a correction please post a comment here or send it to the mailing lists.") |
![]() |
1.28 | 56 | |
![]() |
1.22 | 57 | 1.1 Public API (searchDocuments) |
![]() |
1.3 | 58 | |
![]() |
1.6 | 59 | #info("With this API the query consist in the WHERE condition.\\ |
60 | Any user with edit rights can use this API") | ||
![]() |
1.3 | 61 | |
![]() |
1.1 | 62 | You can execute queries as follows: |
63 | {code} | ||
![]() |
1.22 | 64 | #set($query="where doc.creator='XWiki.VincentMassol'") |
![]() |
1.25 | 65 | #set($results = $xwiki.searchDocuments($query, 5, 0)) |
![]() |
1.3 | 66 | #foreach ($item in $results) |
67 | * $item <br/> | ||
68 | #end | ||
69 | {code} | ||
70 | |||
![]() |
1.21 | 71 | 1.1.1 Simple Query: |
![]() |
1.22 | 72 | #set($hql="where doc.creator='XWiki.VincentMassol'") |
![]() |
1.3 | 73 | #displayQuery($hql false) |
74 | |||
![]() |
1.21 | 75 | <br/> |
76 | 1.1.1 Simple Ordered Query: | ||
![]() |
1.22 | 77 | #set($hql="where doc.creator='XWiki.VincentMassol' order by doc.date asc") |
![]() |
1.21 | 78 | #displayQuery($hql false) |
79 | |||
80 | <br/> | ||
81 | 1.1.1 Less Simple Ordered Query: | ||
![]() |
1.22 | 82 | #set($hql="where doc.creator='XWiki.VincentMassol' and doc.date > subdate(curdate(), 30) order by doc.date asc") |
![]() |
1.21 | 83 | #displayQuery($hql false) |
84 | |||
85 | |||
![]() |
1.22 | 86 | 1.1 Privileged API (search : Documents, Objects, Properties, etc) |
![]() |
1.3 | 87 | |
![]() |
1.6 | 88 | #warning("Calls to te privileged API are only executed when the calling page has been saved by an Admin. \\ |
89 | The reason is that search can be used to send any HQL command like update, delete, etc. \\ ") | ||
![]() |
1.3 | 90 | |
91 | You can execute queries as follows: | ||
92 | {code} | ||
![]() |
1.2 | 93 | #set($query="select doc.name from XWikiDocument doc") |
![]() |
1.25 | 94 | #set($results = $xwiki.search($query, 5, 0)) |
![]() |
1.2 | 95 | #foreach ($item in $results) |
96 | * $item <br/> | ||
97 | #end | ||
![]() |
1.1 | 98 | {code} |
99 | |||
![]() |
1.3 | 100 | 1.1.1 Simply Query: |
![]() |
1.1 | 101 | #set($hql="select doc.name from XWikiDocument doc") |
![]() |
1.3 | 102 | #displayQuery($hql true) |
![]() |
1.1 | 103 | |
![]() |
1.19 | 104 | 1.1.1 Count Query: |
105 | |||
106 | {code} | ||
107 | #set($query="select count(doc) from XWikiDocument doc") | ||
![]() |
1.20 | 108 | #set($results = $xwiki.search($query)) |
![]() |
1.21 | 109 | ## $xwiki.search returning a list, we get its first element |
![]() |
1.19 | 110 | $query result : $results.get(0) |
111 | {code} | ||
112 | |||
113 | #set($query="select count(doc) from XWikiDocument doc") | ||
![]() |
1.20 | 114 | #set($results = $xwiki.search($query)) |
115 | $query results : $results.get(0) | ||
![]() |
1.19 | 116 | |
![]() |
1.1 | 117 | <br/> |
![]() |
1.3 | 118 | 1.1.1 Simply Query with multiple fields: |
![]() |
1.6 | 119 | |
![]() |
1.1 | 120 | {code} |
![]() |
1.25 | 121 | #set($results=$xwiki.search("select doc.name, doc.date from XWikiDocument doc", 5, 0)) |
![]() |
1.2 | 122 | #foreach ($row in $results) |
123 | #foreach ($col in $row) | ||
124 | #if ($velocityCount==1) | ||
125 | #set($docName=$col) | ||
126 | #elseif ($velocityCount==2) | ||
127 | #set($docDate=$col) | ||
128 | #end | ||
129 | #end | ||
130 | $docName : $docDate <br/> | ||
131 | #end | ||
![]() |
1.1 | 132 | {code} |
133 | |||
134 | #set($hql="select doc.name, doc.date from XWikiDocument doc") | ||
![]() |
1.3 | 135 | #displayQuery($hql true) |
![]() |
1.1 | 136 | |
137 | <br/> | ||
![]() |
1.3 | 138 | 1.1.1 Getting objects of a specific class |
![]() |
1.1 | 139 | #set($hql="select obj.name from BaseObject obj where obj.className='XWiki.XWikiUsers'") |
![]() |
1.3 | 140 | #displayQuery($hql true) |
![]() |
1.1 | 141 | |
142 | <br/> | ||
![]() |
1.3 | 143 | 1.1.1 Getting objects' properties |
![]() |
1.1 | 144 | #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 | 145 | #displayQuery($hql true) |
![]() |
1.19 | 146 | |
147 | <br/> | ||
148 | 1.1.1 Getting objects' properties | ||
149 | #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'") | ||
150 | #displayQuery($hql true) | ||
151 | |||
152 |