Apostrophe in xwql queries

I have a problem with apostrophe in xwql queries in velocity.
This query works just fine

#set($ls1=$services.query.xwql(“from doc.object(‘Gestion de risques .Plan de gestion des risques.Risques et enjeux.Code.Risques et enjeuxClass’) as risques order by risques.name”).execute())

This one doesn’t

#set($ls1=$services.query.xwql(‘from doc.object(“Gestion de risques .Plan de gestion des risques.Mesures d’atténuation.Code.Mesures d’atténuationClass”) as mesures order by mesures.name’).execute())

Any tips?

Thanks

Hi @MichaelA,

In the first case, double quotes wrap the entire query string and inside them you can use as many single quotes you want without breaking the syntax.
In the second case single quotes are wrongly paired: (‘from with d’atténuation and d’atténuationClass with name’) so the syntax is broken by them.

In fact, pasting this in a text editor, I can see that at the beginning of the query string is used a single quote, while the enclosing one is an apostrophe.

Hope it helps,
Alex

Thanks, but I tried every permutations of quotes. Nothing worked.
I know that in JPQL double single quotes escapes single quotes, but this also didn’t work.

See https://stackoverflow.com/questions/19579748/escaping-quotes-in-velocity-template .

Thanks for the replies

Unfortunately, they don’t seem to solve my problems.

Using escapetools still gives me an error message, for example:

$services.query.xwql("from doc.object ('Pilotage.Pilotage SST.Règles d${escapetool.s}affaires SST.Code.Règles d${escapetool.s}affaires SSTClass') as processus where doc.title <> ''").execute())

still gives me:

Failed to execute the [velocity] macro. Cause: [[1,88] expecting: ‘)’]. Click on this message for details.

I’m having a whole lot of trouble with apostrophes in Xwiki, it’s probably my biggest headache

Normally you just need to double the quote to escape it in Velocity. For example:

#set ($xwql = "from doc.object('Pilotage.Pilotage SST.Règles d''affaires SST.Code.Règles d''affaires SSTClass') as processus where doc.title <> ''")
$services.query.xwql($xwql).execute())

However that won’t work in this case. If you check the stacktrace you get you’ll see:

Caused by: org.xwiki.query.jpql.parser.ParserException: [1,87] expecting: ')'
	at org.xwiki.query.jpql.parser.Parser.parse(Parser.java:1385)
	at org.xwiki.query.jpql.internal.JPQLParser.parse(JPQLParser.java:46)
	at org.xwiki.query.xwql.internal.hql.XWQLtoHQLTranslator.translate(XWQLtoHQLTranslator.java:55)

What this means is that the parser we use to convert from XWQL syntax to Hibernate HQL syntax doesn’t support it. And it seems this parser doesn’t support escaping quotes. See https://github.com/xwiki/xwiki-platform/blob/master/xwiki-platform-core/xwiki-platform-query/xwiki-platform-query-jpql-parser/src/main/sablecc/JPQL.grammar#L16

So right now the only solution I see for you would be to use HQL and not XWQL. See https://extensions.xwiki.org/xwiki/bin/view/Extension/Query%20Module

You could open a jira issue at https://jira.xwiki.org to suggest adding support for quote escapes. It’s an important limitation IMO.

Thanks

PS: You could also rename your doc to not have quotes :wink:

Thanks,

I guess I’ll look into HQL.

Renaming the docs was my initial solution, but with 70+ objects replicated and modified in 20+ subwikis, it no longer seems like a viable option.

I’ve never opened an issue, in what category should it be?

“XWIKI” project and “Query” component: https://jira.xwiki.org/issues/?jql=project%20%3D%20XWIKI%20AND%20component%20%3D%20Query

Thanks

Ok, I’m doing something wrong with HQL.

#set($hql=", BaseObject as obj and obj.className='Pilotage.Pilotage SST.Règles d''affaires SST.Code.Règles d''affaires SSTClass'") $services.query.hql($hql).execute()

is the right way to do it?
It gives me :

Failed to execute the [velocity] macro. Cause: [unexpected token: and near line 1, column 104 [select doc.fullName from com.xpn.xwiki.doc.XWikiDocument doc , com.xpn.xwiki.objects.BaseObject as obj and obj.className=‘Pilotage.Pilotage SST.Règles d’‘affaires SST.Code.Règles d’‘affaires SSTClass’]]. Click on this message for details.

You can read the table at https://extensions.xwiki.org/xwiki/bin/view/Extension/Query%20Module, for example “Query listing all documents containing XWiki Objects (XObject) of a given XWiki Class (XClass)”:

{{velocity}}
#set ($hql = ", BaseObject as obj where doc.fullName = obj.name and obj.className = 'Pilotage.Pilotage SST.Règles d''affaires SST.Code.Règles d''affaires SSTClass'")
$services.query.hql($hql).execute()
{{/velocity}}