sparql-mode

A SPARQL mode for emacs

#+BEGIN_SRC emacs-lisp (add-to-list ‘load-path “/path/to/sparql-mode-dir”) (autoload ‘sparql-mode “sparql-mode.el” “Major mode for editing SPARQL files” t) (add-to-list ‘auto-mode-alist ‘(”\.sparql$” . sparql-mode)) (add-to-list ‘auto-mode-alist ‘(”\.rq$” . sparql-mode)) #+END_SRC

Now sparql-mode will load whenever you visit a file whose name ends with .sparql. Alternatively, run =M-x sparql-mode= in an existing buffer containing SPARQL commands.

It is also possible to add #+BEGIN_SRC emacs-lisp -- mode: sparql -- #+END_SRC to the top of the file. This is a comment read by emacs to discover what mode to use.

** Auto complete SPARQL-mode has basic support for both =auto-complete-mode= and =company-mode=. Just add your favourite completion mode to =sparql-mode-hook=.

#+BEGIN_SRC emacs-lisp (add-hook ‘sparql-mode-hook ‘your-favourite-completion-mode) #+END_SRC

  • Executing SPARQL Queries from within Emacs From a buffer that is in sparql-mode, execute =M-x sparql-query-region=. You will be prompted for a SPARQL HTTP endpoint in the minibuffer, which defaults to =http://localhost:2020/=. Once set, it will be used for all subsequent queries in that buffer. Results will be displayed in another buffer in CSV format.

  • Org-babel support It is also possible to use =sparql-mode= with org-mode and executing queries with org-babel. You can do that by adding the following snippet or adding =(sparql . t)= to languages org-babel can load:

#+BEGIN_SRC emacs-lisp (org-babel-do-load-languages ‘org-babel-load-languages ‘((sparql . t))) #+END_SRC

You can then execute the query by pressing =C-c C-c= on the source-block header.

By default results formatted as =text/csv= will be converted to an org table. This can be disabled by adding =:results scalar= to the header. See examples.

*** Example Default behaviour for results in csv is to convert the result to a table:

#+BEGIN_SRC org
  ,#+BEGIN_SRC sparql :url http://live.dbpedia.org/sparql :format text/csv
    SELECT DISTINCT ?Concept WHERE {
      [] a ?Concept
    } LIMIT 5
  ,#+END_SRC

  ,#+RESULTS:
  | Concept                                                          |
  |------------------------------------------------------------------|
  | http://www.openlinksw.com/schemas/virtrdf#QuadMapFormat          |
  | http://www.openlinksw.com/schemas/virtrdf#QuadStorage            |
  | http://www.openlinksw.com/schemas/virtrdf#array-of-QuadMapFormat |
  | http://www.openlinksw.com/schemas/virtrdf#QuadMap                |
  | http://www.openlinksw.com/schemas/virtrdf#QuadMapValue           |
#+END_SRC

If you don't want to convert the result to a table, you can
override that behaviour by specifying that the result should be
=scalar=:

#+BEGIN_SRC org
  ,#+HEADER: :url http://live.dbpedia.org/sparql
  ,#+HEADER: :format text/csv
  ,#+HEADER: :results scalar
  ,#+BEGIN_SRC sparql
    SELECT DISTINCT ?Concept WHERE {
      [] a ?Concept
    } LIMIT 5
  ,#+END_SRC

  ,#+RESULTS:
  : "Concept"
  : "http://www.openlinksw.com/schemas/virtrdf#QuadMapFormat"
  : "http://www.openlinksw.com/schemas/virtrdf#QuadStorage"
  : "http://www.openlinksw.com/schemas/virtrdf#array-of-QuadMapFormat"
  : "http://www.openlinksw.com/schemas/virtrdf#QuadMap"
  : "http://www.openlinksw.com/schemas/virtrdf#QuadMapValue"
#+END_SRC

We can also declare variables that will then be expanded before
execution. For example the following query:

#+BEGIN_SRC sparql :var x="friend:Julia"
  SELECT * WHERE {
    ?x foaf:name ?name.
    $x foaf:email ?email.
  }
#+END_SRC

will be transformed into the following before it is executed:

#+BEGIN_SRC sparql
  SELECT * WHERE {
    friend:Julia foaf:name ?name.
    friend:Julia foaf:email ?email.
  }
#+END_SRC

Notice that =$x= and =?x= are treated as identical. This is
because SPARQL treats them as identical.

By combining this with the =#+call= syntax we can create and reuse
queries:

#+BEGIN_SRC org
  ,#+NAME: count-statements-in-graph
  ,#+BEGIN_SRC sparql :var graph="<>"
    SELECT COUNT(*) WHERE {
      GRAPH $graph {
        ?s ?p ?o .
      }
    }
  ,#+END_SRC

  ,#+CALL: count-statements-in-graph("<http://example.com/my-graph>")

  ,#+RESULTS:
  : "callret-0"
  : 1100

  ,#+CALL: count-statements-in-graph("<http://example.com/my-other-graph>")

  ,#+RESULTS:
  : "callret-0"
  : 100
#+END_SRC

Notice that the server request is done synchronously and will
therefore lock the editor if the request takes a long time.

Related Repositories

sparql-mode

sparql-mode

A SPARQL mode for emacs ...


Top Contributors

ljos candera robsyme alf kenda syohex ajtucker lueck donpellegrino TheBB jgrzebyta olejorgenb

Releases

-   v0.11.2 zip tar
-   v0.11.1 zip tar
-   v0.11.0 zip tar
-   v0.10.1 zip tar
-   v0.9.3 zip tar
-   v0.9.2 zip tar
-   v0.9.1 zip tar
-   v0.9.0 zip tar
-   v0.8.4 zip tar
-   v0.8.3 zip tar
-   v0.8.2 zip tar
-   v0.8.1 zip tar
-   v0.8.0 zip tar
-   v0.7.3 zip tar
-   v0.7.1 zip tar
-   v0.7.0 zip tar
-   v0.6.3 zip tar
-   v0.6.2 zip tar
-   v0.6.1 zip tar
-   v0.6.0 zip tar
-   v0.5.0 zip tar
-   v0.4.0 zip tar
-   v0.3.0 zip tar
-   0.10.0 zip tar