|
1 #!/bin/bash |
|
2 |
|
3 # Relational pipes |
|
4 # Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
|
5 # |
|
6 # This program is free software: you can redistribute it and/or modify |
|
7 # it under the terms of the GNU General Public License as published by |
|
8 # the Free Software Foundation, version 3 of the License. |
|
9 # |
|
10 # This program is distributed in the hope that it will be useful, |
|
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 # GNU General Public License for more details. |
|
14 # |
|
15 # You should have received a copy of the GNU General Public License |
|
16 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
|
18 |
|
19 # This is a helper script that could be used as a shebang interpreter for SPARQL files. |
|
20 # Just set the SPARQL file executable and add the first line comment. |
|
21 # The endpoint and relation parameters are optional. |
|
22 # Parameters must be at the beginning of the file before any empty lines. |
|
23 # Only the first occurrence of each parameter is relevant. |
|
24 # This interpreter produces a human-readable table if STDOUT is a terminal |
|
25 # and machine-readable relational data if STDOUT is not a terminal. |
|
26 # So the stream can be piped to any relational transformation (e.g. relpipe-tr-sql) |
|
27 # or output filter (e.g. relpipe-out-csv). |
|
28 # |
|
29 # Example: |
|
30 # |
|
31 # #!/usr/bin/env rdf-sparql-interpreter.sh |
|
32 # # endpoint: https://query.wikidata.org/sparql |
|
33 # # relation: few_statements |
|
34 # |
|
35 # SELECT * WHERE { ?subject ?predicate ?object } LIMIT 3 |
|
36 |
|
37 |
|
38 # Execute the SPARQL query and send the results to the STDOUT |
|
39 # either raw or formatted as a table depending on whether STDOUT is a terminal or not. |
|
40 interpret_sparql() { |
|
41 if [ -t 1 ] ; then relpipe-in-sparql "$@" | relpipe-out-tabular; |
|
42 else relpipe-in-sparql "$@"; fi |
|
43 } |
|
44 |
|
45 # Fetches the parameter $2 from file $1. |
|
46 # Parameters must be formatted as comments: „# name: value“. |
|
47 # Only the first occurrence is relevant. And empty line terminates processing. |
|
48 find_parameter() { |
|
49 perl -e 'while(<>) { if (/^\s*$/) { last; } elsif (/^# ($ARGV[0]): (.*)$/) { print "$2"; last; } }' "$1" "$2" |
|
50 } |
|
51 |
|
52 # Appends the "${options[@]}" array with option $2 taken from the $1 file |
|
53 # or from environmental variable RELPIPE_IN_SPARQL_${2^^} (the variable has precedence). |
|
54 prepare_option() { |
|
55 variableName="RELPIPE_IN_SPARQL_${2^^}" |
|
56 parameterName="$2" |
|
57 [[ -n "${!variableName}" ]] && value="${!variableName}" || value="$(find_parameter "$1" "$parameterName")" |
|
58 [[ -n "$value" ]] && options+=("--$parameterName" "$value") |
|
59 } |
|
60 |
|
61 options=() |
|
62 prepare_option "$1" endpoint |
|
63 prepare_option "$1" relation |
|
64 |
|
65 cat "$1" | interpret_sparql "${options[@]}" |