Changes for page Query API

Last modified by Vincent Massol on 2024/02/26 17:31

<
From version < 43.18 >
edited by Thomas Menamparampan
on 2018/06/02 11:38
To version < 44.1 >
edited by Alex Cotiugă
on 2018/07/18 13:20
>
Change comment: There is no comment for this version

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -xwiki:XWiki.menamparampan
1 +xwiki:XWiki.acotiuga
ExtensionCode.ExtensionClass[0]
Description
... ... @@ -23,13 +23,13 @@
23 23  
24 24  Full example:
25 25  
26 -##$services.query.xwql("where doc.creationDate > :date").bindValue('date', $datetool.toDate('yyyy-MM-dd', '2008-01-01')).execute()##
26 +##$services.query.xwql('where doc.creationDate > :date').bindValue('date', $datetool.toDate('yyyy-MM-dd', '2008-01-01')).execute()##
27 27  )))|(((
28 28  ##where doc.creationDate > :date##
29 29  
30 30  Full example:
31 31  
32 -##$services.query.hql("where doc.creationDate > :date").bindValue('date', $datetool.toDate('yyyy-MM-dd', '2008-01-01')).execute()##
32 +##$services.query.hql('where doc.creationDate > :date').bindValue('date', $datetool.toDate('yyyy-MM-dd', '2008-01-01')).execute()##
33 33  )))
34 34  |Query listing all documents last updated by a given user|##where doc.author = 'XWiki.LudovicDubost'##|##where doc.author = 'XWiki.LudovicDubost'##
35 35  |Query listing all documents containing XWiki Objects (XObject) of a given XWiki Class (XClass)|##from doc.object(XWiki.XWikiUsers) as user##|##, BaseObject as obj where doc.fullName = obj.name and obj.className = 'XWiki.XWikiUsers'##
... ... @@ -59,50 +59,50 @@
59 59  
60 60  * Return all the results matching the XWQL statement:(((
61 61  {{code language="velocity"}}
62 -$services.query.xwql("xwqlstatement").execute()
62 +$services.query.xwql('xwqlstatement').execute()
63 63  {{/code}}
64 64  )))
65 65  * Return only 5 results at most:(((
66 66  {{code language="velocity"}}
67 -$services.query.xwql("xwqlstatement").setLimit(5).execute()
67 +$services.query.xwql('xwqlstatement').setLimit(5).execute()
68 68  {{/code}}
69 69  )))
70 70  * Set the starting offset (starts at result number 5 and beyond):(((
71 71  {{code language="velocity"}}
72 -$services.query.xwql("xwqlstatement").setOffset(5).execute()
72 +$services.query.xwql('xwqlstatement').setOffset(5).execute()
73 73  {{/code}}
74 74  )))
75 75  * Set the wiki on which to run the query (requires Programming Rights before XWiki 4.1):(((
76 76  {{code language="velocity"}}
77 -$services.query.xwql("xwqlstatement").setWiki("mywiki").execute()
77 +$services.query.xwql('xwqlstatement').setWiki('mywiki').execute()
78 78  {{/code}}
79 79  )))
80 80  * You can add filters to apply to the query and multiple filters can be added to a query.
81 81  ** The ##hidden## filter will exclude documents marked as hidden from the query results, except if the user has chosen to see hidden documents in his profile preferences:(((
82 82  {{code language="velocity"}}
83 -$services.query.xwql("xwqlstatement").addFilter("hidden").execute()
83 +$services.query.xwql('xwqlstatement').addFilter('hidden').execute()
84 84  {{/code}}
85 85  )))
86 86  ** The ##unique## filter will make sure you don't get duplicate results with a short-form query:(((
87 87  {{code language="velocity"}}
88 -$services.query.xwql("xwqlstatement").addFilter("unique").execute()
88 +$services.query.xwql('xwqlstatement').addFilter('unique').execute()
89 89  {{/code}}
90 90  )))
91 91  ** The ##count## filter allows to transform a short-form query into a count of the distinct results it would return. Unfortunately [[this doesn't work with XWQL on versions older than 4.4.1>>https://jira.xwiki.org/browse/XWIKI-8719]]:(((
92 92  {{code language="velocity"}}
93 -$services.query.xwql("xwqlstatement").addFilter("count").execute()
93 +$services.query.xwql('xwqlstatement').addFilter('count').execute()
94 94  ## You can also use the following shortcut
95 -$services.query.xwql("xwqlstatement").count()
95 +$services.query.xwql('xwqlstatement').count()
96 96  {{/code}}
97 97  )))
98 98  ** The ##language## filter will transform the query to also return document languages (the result you'll get is ##List<Object[]>## with ##Object[0]## being the document name and ##Object[1]## being the document language, e.g. ##en##, ##fr##, ##fr_FR##, etc): {{info}}New in 5.1{{/info}}(((
99 99  {{code language="velocity"}}
100 -$services.query.xwql("xwqlstatement").addFilter("language").execute()
100 +$services.query.xwql('xwqlstatement').addFilter('language').execute()
101 101  {{/code}}
102 102  )))
103 103  ** The ##currentlanguage## filter will only return documents in the current language: {{info}}New in 5.1{{/info}}(((
104 104  {{code language="velocity"}}
105 -$services.query.xwql("xwqlstatement").addFilter("currentlanguage").execute()
105 +$services.query.xwql('xwqlstatement').addFilter('currentlanguage').execute()
106 106  {{/code}}
107 107  )))
108 108  * Binding a LIKE value {{info}}New in 8.4.5, 9.3RC1{{/info}}:(((
... ... @@ -114,7 +114,7 @@
114 114  Thus the recommended approach is to do the following:
115 115  
116 116  {{code language="velocity"}}
117 -$services.query.xwql("xwqlstatement like :ref").bindValue("ref").literal("${documentReference}.").anyChars().query().execute()
117 +$services.query.xwql('xwqlstatement like :ref').bindValue('ref').literal("${documentReference}.").anyChars().query().execute()
118 118  {{/code}}
119 119  
120 120  The full API is:
... ... @@ -135,13 +135,13 @@
135 135  )))
136 136  * Bind a non-LIKE value:(((
137 137  {{code language="velocity"}}
138 -$services.query.xwql("xwqlstatement containing :var").bindValue("var", "value").execute()
138 +$services.query.xwql('xwqlstatement containing :var').bindValue('var', 'value').execute()
139 139  {{/code}}
140 140  
141 141  Specific example that will return all documents that have comments containing the ##test## string:
142 142  
143 143  {{code language="velocity"}}
144 -$services.query.xwql("select doc.fullName from Document doc, doc.object(XWiki.XWikiComments) com WHERE doc.translation = 0 and com.comment LIKE :value").bindValue("value", "%test%").execute()
144 +$services.query.xwql('select doc.fullName from Document doc, doc.object(XWiki.XWikiComments) com WHERE doc.translation = 0 and com.comment LIKE :value').bindValue('value', '%test%').execute()
145 145  {{/code}}
146 146  
147 147  You can also bind a list or an array to a query parameter: {{info}}New in 6.0{{/info}}
... ... @@ -153,7 +153,7 @@
153 153  )))
154 154  * Behaviors can be chained:(((
155 155  {{code language="velocity"}}
156 -$services.query.xwql("xwqlstatement").setWiki("mywiki").setOffset(2).setLimit(5).execute()
156 +$services.query.xwql('xwqlstatement').setWiki('mywiki').setOffset(2).setLimit(5).execute()
157 157  {{/code}}
158 158  )))
159 159  
... ... @@ -160,7 +160,7 @@
160 160  For HQL:
161 161  
162 162  {{code language="velocity"}}
163 -$services.query.hql("hqlstatement").execute()
163 +$services.query.hql('hqlstatement').execute()
164 164  {{/code}}
165 165  
166 166  {{info}}
... ... @@ -168,9 +168,9 @@
168 168  
169 169  {{code language="velocity"}}
170 170  ## For XWQL
171 -$xwiki.queryManager.xwql("xwqlstatement").execute()
171 +$xwiki.queryManager.xwql('xwqlstatement').execute()
172 172  ## For HQL
173 -$xwiki.queryManager.createQuery("hqlstatement", "hql").execute()
173 +$xwiki.queryManager.createQuery('hqlstatement', 'hql').execute()
174 174  {{/code}}
175 175  {{/info}}
176 176  
... ... @@ -179,13 +179,13 @@
179 179  For XWQL (example with Groovy):
180 180  
181 181  {{code language="groovy"}}
182 -services.query.xwql("xwqlstatement").execute()
182 +services.query.xwql('xwqlstatement').execute()
183 183  {{/code}}
184 184  
185 185  For HQL:
186 186  
187 187  {{code language="groovy"}}
188 -services.query.hql("hqlstatement").execute()
188 +services.query.hql('hqlstatement').execute()
189 189  {{/code}}
190 190  
191 191  === From Java components ===
... ... @@ -223,8 +223,8 @@
223 223  You may be tempted to write:
224 224  
225 225  {{code}}
226 -#set ($query = $services.query.xwql("from doc.object('Space.Class') as obj where doc.space like :space"))
227 -#set ($query = $query.bindValue('space', "space1.space2With\.Dot.%")
226 +#set ($query = $services.query.xwql('from doc.object(Space.Class) as obj where doc.space like :space'))
227 +#set ($query = $query.bindValue('space', 'space1.space2With\.Dot.%')
228 228  $query.execute()
229 229  {{/code}}
230 230  
... ... @@ -233,8 +233,8 @@
233 233  To make this query work on all DBs you need to write:
234 234  
235 235  {{code}}
236 -#set ($query = $services.query.xwql("from doc.object('Space.Class') as obj where doc.space like :space escape '!'"))
237 -#set ($spaceReferenceString = "space1.space2With\.Dot")
236 +#set ($query = $services.query.xwql("from doc.object(Space.Class) as obj where doc.space like :space escape '!'"))
237 +#set ($spaceReferenceString = 'space1.space2With\.Dot')
238 238  #set ($spaceLike = $spaceReferenceString.replaceAll('([%_!])', '!$1').concat('.%'))
239 239  #set ($query = $query.bindValue('space', $spaceLike))
240 240  $query.execute()

Get Connected