Uploaded image for project: 'Jackrabbit Content Repository'
  1. Jackrabbit Content Repository
  2. JCR-3487

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

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • None
    • None
    • jackrabbit-jcr-commons
    • None

    Description

      (moving the discussion from OAK-529)

      I propose an approach that, without altering the current codebase, but rather adding a new package 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

        Activity

          People

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

            Dates

              Created:
              Updated: