J tricks - Little JIRA Tricks
  • Home
  • Plugins ↓
    • JQL Tricks Plugin
      • JQL Tricks Plugin - Cloud
        • JQLT Cloud Installation
        • JQLT Cloud Configuration
        • JQLT Cloud Usage
        • JQLT Cloud License
        • JQLT Cloud FAQ
      • JQL Tricks Plugin - DC
        • JQLT DC Installation
        • JQLT DC Configuration
        • JQLT DC Usage
          • JQLT Issue Functions
          • JQLT Subtask Functions
          • JQLT Links Functions
          • JQLT Development Functions
          • JQLT Worklog Functions
          • JQLT Project Functions
          • JQLT Component Functions
          • JQLT Version Functions
          • JQLT Group Functions
          • JQLT User Functions
          • JQLT Date Functions
        • JQLT DC License
        • JQLT DC FAQ
        • JQLT DC Known Issues
        • JQLT DC Performance
      • JQL Tricks Cloud Migration
    • Simplified Planner
      • J-Planner Installation
      • J-Planner Configuration
      • J-Planner Usage
        • Creating a plan
        • Editing a plan
        • Deleting a plan
        • Viewing a plan
        • Modifying a plan
      • J-Planner FAQ
    • Atla-Search Plugin
      • Atla-Search Installation
      • Atla-Search Configuration
      • Atla-Search Usage
      • Atla-Search License
      • Atla-Search FAQ
    • Heroku for Compass App
      • Heroku for Compass Installation
      • Heroku for Compass Configuration
      • Heroku for Compass Usage
    • Copy to subtask Plugin
    • All Plugins
  • Tutorials
  • The Book
  • Contact Us
  • Home
  • Plugins ↓
    • JQL Tricks Plugin
      • JQL Tricks Plugin - Cloud
        • JQLT Cloud Installation
        • JQLT Cloud Configuration
        • JQLT Cloud Usage
        • JQLT Cloud License
        • JQLT Cloud FAQ
      • JQL Tricks Plugin - DC
        • JQLT DC Installation
        • JQLT DC Configuration
        • JQLT DC Usage
          • JQLT Issue Functions
          • JQLT Subtask Functions
          • JQLT Links Functions
          • JQLT Development Functions
          • JQLT Worklog Functions
          • JQLT Project Functions
          • JQLT Component Functions
          • JQLT Version Functions
          • JQLT Group Functions
          • JQLT User Functions
          • JQLT Date Functions
        • JQLT DC License
        • JQLT DC FAQ
        • JQLT DC Known Issues
        • JQLT DC Performance
      • JQL Tricks Cloud Migration
    • Simplified Planner
      • J-Planner Installation
      • J-Planner Configuration
      • J-Planner Usage
        • Creating a plan
        • Editing a plan
        • Deleting a plan
        • Viewing a plan
        • Modifying a plan
      • J-Planner FAQ
    • Atla-Search Plugin
      • Atla-Search Installation
      • Atla-Search Configuration
      • Atla-Search Usage
      • Atla-Search License
      • Atla-Search FAQ
    • Heroku for Compass App
      • Heroku for Compass Installation
      • Heroku for Compass Configuration
      • Heroku for Compass Usage
    • Copy to subtask Plugin
    • All Plugins
  • Tutorials
  • The Book
  • Contact Us

JQL Query and Query String conversions

1/21/2011

4 Comments

 
It is sometimes amazing to realize how we missed something so simple! To realize how a little documentation could have saved us few hours (or days for that matter!). Presenting one that I came across while playing cat and mouse with JQL (JIRA query language).

Here is the scenario: I have a SearchRequest object from which I need to get the JQL query String. That's it. Simple, straight forward. Or so I thought!

As it appeared so straight forward, I started with the JAVA docs for SearchRequest. The method getQuery() returns a Query object - half way there!

All I need now is the query String or a String representation of the Query object. Over to the JAVA Docs - Query. There it is - getQueryString(). Job done. Or so I thought until I saw the output as null. Time to scratch the head!! Sure I haven't done anything wrong?

Time to look for alternatives. That is when I came across JqlQueryParser. There we have a useful method, parseQuery - to convert a JQL Query String back to a Query object. Nice but exactly opposite to the need of the hour. Bad day?

To give it one last shot before digging deep into the source code or crying out in the forums, I decided to have a look at SearchService - one that does the actual search operation. And boy, there it is:

String getJqlString(Query query)

Gets the JQL string representation for the passed query. Returns the string from Query.getQueryString() if it exists or generates one if it does not.

if (query.getQueryString() != null)
    return query.getQueryString();
  else
    return getGeneratedJqlString(query);

So what is happening in getGeneratedJqlString(query)?

return jqlStringSupport.generateJqlString(query);

And that is where the actual conversion happens: JqlStringSupport.generateJqlString(Query query) . Not just that, JqlStringSupport has some other helpful utility methods as well. Good Day?

In Summary, if you want to convert a Query object to Jql String, use SearhService.getJqlString(Query query). If you want to convert a JQL String back to a Query object, use JqlQueryParser.parseQuery(String Query). In the latter case, you can also use SearchService.parseQuery(User searcher, String query) which returns SearchService.ParseResult from which Query object can be retrieved.

Phew!
4 Comments
Matt Doar link
1/22/2011 01:18:37 pm

Yes, I got all tangled up that code when I was rewriting the Timecharts plugin for JIRA 4.x. As you say, a little documentation could go a long way there. Thanks for the post.

~Matt

Reply
J-Tricks
1/22/2011 08:02:41 pm

Thanks Matt :) Some days we would have looked at SearchService class first but it was fun in the end. Thought I would share.

Reply
Ashwini .
2/7/2012 10:22:07 pm

Thanks for post.

Reply
thomas
8/10/2012 09:31:33 am

thx man. took me some days to find your solution ;-)

Reply

Your comment will be posted after it is approved.


Leave a Reply.

    Enter your email address:

    Author

    Jobin Kuruvilla - Works in Adaptavist as Head of DevOps Professional Services. 

    Author of JIRA Development Cookbook and JIRA 5.x Development Cookbook.


    RSS Feed

    Categories

    All
    Acive Objects
    Ajs
    Book
    Components
    Condition
    Custom Fields
    Customization
    Events
    Gadgets
    Javascript
    Jql
    Listener
    Mail
    Permissions
    Plugin Framework
    Post Function
    Properties
    Remote Invocation
    Reporting
    Rest
    Scheduled Tasks
    Search
    Services
    Soap
    Summit
    User Interface
    Validator
    Webwork Actions
    Workflow

    Archives

    October 2016
    August 2016
    March 2016
    January 2016
    December 2015
    May 2014
    December 2013
    November 2013
    July 2013
    June 2013
    April 2013
    October 2012
    September 2012
    August 2012
    July 2012
    May 2012
    March 2012
    February 2012
    January 2012
    December 2011
    November 2011
    June 2011
    May 2011
    April 2011
    March 2011
    February 2011
    January 2011
    November 2010
    October 2010
    September 2010
    August 2010

SUPPORT
APPS
TUTORIALS
THE BOOK
© J-Tricks