Uploaded image for project: 'Jackrabbit Oak'
  1. Jackrabbit Oak
  2. OAK-529

Provide a utility to run JCR queries adopting a fluent-interfaces approach

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Minor
    • Resolution: Won't Fix
    • 0.5
    • None
    • jcr
    • None

    Description

      I propose an approach that, without altering the current codebase, but rather adding a new layer on top of existing APIs, tries to simplify the query building and execution, i.e. rather than coding the following:

      QueryManager qm = session.getWorkspace().getQueryManager();
      Query q = qm.createQuery("select text from [nt:base] where id = $id", Query.JCR_SQL2);
      q.bindValue("id", vf.createValue("1"));
      QueryResult r = q.execute();
      
      String text = null;
      
      RowIterator it = r.getRows();
      if (it.hasNext()) {
          Row row = it.nextRow();
          text = row.getValue("text").getString();
      }
      

      users could write the same sentence using a shortcut like

      String text = on(qm)
                    .sql2Query("select text from [nt:base] where id = $id")
                    .where("id").is("1")
                    .execute(textHandler);
      

      where textHandler is:

      QueryResultHandler<String> textHandler = new QueryResultHandler<String> {
      
          @Override
          public String handle(QueryResult queryResult)
                  throws RepositoryException {
      
              RowIterator it = queryResult.getRows();
              if (it.hasNext()) {
                  Row row = it.nextRow();
                  return row.getValue("text").getString();
              }
      
              return null;
          }
      
      }
      

      the QueryResultHandler should simplify reducing redundant query handling code, improving reusability.

      Attachments

        1. OAK-529.patch
          31 kB
          Simone Tripodi

        Activity

          People

            Unassigned Unassigned
            simone.tripodi Simone Tripodi
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: