marklogic-community / roxy

Deployment tool for MarkLogic applications. Also provides optional unit test and XQuery MVC structure
Other
87 stars 66 forks source link

Allow running SPARQL and maybe SQL using execute_query #869

Open grtjn opened 6 years ago

grtjn commented 6 years ago

See also #821

rhdunn commented 6 years ago

I have been using:

import module namespace sem = "http://marklogic.com/semantics" at "/MarkLogic/semantics.xqy";
let $query := "$QUERY_STRING"
let $vars := ()
let $options := $OPTIONS
return try {
  let $ret     := $FUNCTION
  let $triples := for $item in $ret where $item instance of sem:triple return $item
  let $other   := for $item in $ret where not($item instance of sem:triple) return $item
  return if (count($triples) > 0) then
    let $fmt := sem:rdf-serialize($triples, "$TRIPLE_FORMAT")
    let $_ := xdmp:add-response-header("X-Content-Type", "$CONTENT_TYPE")
    return ($fmt, $other)
  else
    $ret
} catch ($e) { $e }

to run SPARQL queries in a plugin of mine, replacing $FUNCTION, etc. with string replacement logic.

rhdunn commented 6 years ago

X-Content-Type is used because setting Content-Type changes the multipart content type and breaks things. This could be removed for the roxy version. $FUNCTION is the appropriate invoke/eval function, allowing support for profiling and debugging.

grtjn commented 6 years ago

I had in mind using /v1/graph/sparql. Would sending your code to /v1/eval provide more possibilities?

rhdunn commented 6 years ago

Using /v1/graph/sparql is probably better for roxy. I'm using eval to make use of things like better exception reporting, profiling, and debugging if available.

grtjn commented 6 years ago

Thanks for sharing though, appreciated!