Grails
  1. Grails
  2. GRAILS-3481

Regular Expression into Grails-Hibernate

    Details

    • Type: New Feature New Feature
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.0.3
    • Fix Version/s: 1.1.1, 1.2-M1
    • Component/s: Persistence
    • Labels:
      None
    • Patch Submitted:
      Yes

      Description

      More and more DBMS are supporting queries with regular expressions. Eg. MySql uses 'rlike' or 'regexp' in the same syntax as 'like', and Oracle uses REGEXP as a function.

      It seems that Hibernate supports this feature - at least the 'rlike' is included in the WhereParser, even though I could not find any rlikeExpression.

      Since the rlike operator has the exact same syntax as the like operator, I imagine it would be relatively simple to include this feature in a future release of Grails.

      I've only studied the code for the Grails project for very short time, but I think it could be doable somehow if a new Criterion class 'RlikeExpression' is created as a subclass of the 'IlikeExpression' class, and then override the toSqlString() method. Then it should be wired in to the 'HibernateCriteriaBuilder' class, and maybe a few other places that I haven't thought about.

      I would like to patch it myself, but right now I think it is quite a mouthful to understand how this whole Grails project is structured.

      1. AbstractClausedStaticPersistentMethod.java
        17 kB
        Christian Sonne Jensen
      2. HibernateCriteriaBuilder.java
        42 kB
        Christian Sonne Jensen
      3. RlikeExpression.java
        2 kB
        Christian Sonne Jensen

        Activity

        Hide
        Christian Sonne Jensen added a comment -

        I've tested the solution myself, and I've found a way to make it work. I've attached the patched files and a new file to make this work. The idea is to make a copy of the track for the ILIKE operator, and correct it for RLIKE. The solution has been tested in my own grails app, and criterion expression seems to work . Now I can write something like:

        def events = c

        { rlike("message", "some regular expression") order("timestampMillis") maxResults(5000) }

        I've tested with HQL as well, but this doesn't work. My analysis suggests that it has to do with Hibernate not supporting rlike or even ilike expressions! I guess HQL will have to wait until Hibernate catches up...

        I'll try to attach the files i've added and patched, but I'm not sure if I'm allowed...

        Show
        Christian Sonne Jensen added a comment - I've tested the solution myself, and I've found a way to make it work. I've attached the patched files and a new file to make this work. The idea is to make a copy of the track for the ILIKE operator, and correct it for RLIKE. The solution has been tested in my own grails app, and criterion expression seems to work . Now I can write something like: def events = c { rlike("message", "some regular expression") order("timestampMillis") maxResults(5000) } I've tested with HQL as well, but this doesn't work. My analysis suggests that it has to do with Hibernate not supporting rlike or even ilike expressions! I guess HQL will have to wait until Hibernate catches up... I'll try to attach the files i've added and patched, but I'm not sure if I'm allowed...
        Hide
        Christian Sonne Jensen added a comment -

        The RlikeExpression which is basically a copy of Hibernates IlikeExpression. It should work for MySql and Oracle. Other dialects will just get a 'like' operator

        Show
        Christian Sonne Jensen added a comment - The RlikeExpression which is basically a copy of Hibernates IlikeExpression. It should work for MySql and Oracle. Other dialects will just get a 'like' operator
        Hide
        Christian Sonne Jensen added a comment -

        This file is patched to support rlike

        Show
        Christian Sonne Jensen added a comment - This file is patched to support rlike
        Hide
        Christian Sonne Jensen added a comment -

        This file is patched to support rlike

        Show
        Christian Sonne Jensen added a comment - This file is patched to support rlike
        Hide
        Graeme Rocher added a comment -

        Didn't see this one, because Patch submitted not ticked. Marking it as so hopefully we can itnegrate your patch in the future

        Show
        Graeme Rocher added a comment - Didn't see this one, because Patch submitted not ticked. Marking it as so hopefully we can itnegrate your patch in the future
        Hide
        Graeme Rocher added a comment -

        Thanks for the contribution

        Show
        Graeme Rocher added a comment - Thanks for the contribution

          People

          • Assignee:
            Graeme Rocher
            Reporter:
            Christian Sonne Jensen
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development