JDOQL DATANUCLEUS filtering using String matches

2020-07-30 03:51发布

问题:

Using JDO with Datanucleus, I'm trying to filter some data from my database (using jdoql). I would like to use the regular expression for some sophisticated searchs, I found that JDO provide the String method "matches" that accepts a regular expression, and according to the DATANUCLEUS documentation, this method can receive any type of ExpReg:

matches(String pattern) : Returns whether string matches the passed expression. The pattern argument follows the rules of java.lang.String.matches method.

I was able to do filtering based on some regular expression (like " .* ", ".", ". * ") But not with others (like [abcd])

Can someone confirm that not all the set of regular expression syntax are supported ??

Query q = pm.newQuery(cl, "this.name.matches(filterName)");
q.declareParameters("String filterName");
List results = (List)q.execute("Bo.*");
return pm.detachCopyAll(results);

--> Return Book, Book2

But with : q.execute("B[aoe]ok")  return nothing !

Thanks

回答1:

After dozens of failed tests, i can say that (with DN 2.1) the MATCHES does not work (tested against JAVA string.matches(regex)): http://www.datanucleus.org/products/accessplatform_2_1/jdo/jdoql_methods.html

In my tests i have the following regex:

(PT)?999999990

Is translated by JDOQL into:

LIKE '(PT)<UNPRINTABLE>999999990'

The database contains many records like:
999999990
PT999999990
...

Also, depending on the REGEX, the JDOQL interpreter seems to change the expression...

The following regex:

(?i)(PT)?999999990 

Translated by JDOQL into:

LIKE '(PT)<UNPRINTABLE>999999990'