/
sparql
executable file
·154 lines (139 loc) · 5.14 KB
/
sparql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env ruby
require 'rubygems'
$:.unshift("../../lib", __FILE__)
require 'logger'
require 'sparql'
begin
require 'linkeddata'
rescue LoadError
require 'rdf/ntriples'
end
require 'getoptlong'
def display_results(res, **options)
puts res.inspect if options[:verbose]
puts case res
when RDF::Graph then res.dump(options.fetch(:format, :ttl), base_uri: query.base_uri, prefixes: query.prefixes, standard_prefixes: true)
when RDF::Literal then res.inspect
else
case options[:format]
when :json then res.to_json
when :html then res.to_html
when :xml then res.to_xml
when :csv then res.to_csv
when :tsv then res.to_tsv
else res.map {|s| s.bindings.map {|k,v| "#{k}: #{v}"}}.join("\n")
end
end
end
def run(input, **options)
if options[:debug]
puts "input graph:\n#{options[:dataset].dump(:trig, standard_prefixes: true)}\n" if options[:dataset]
puts "query:\n#{input}\n"
end
options[:dataset] ||= RDF::Repository.new
if options[:verbose]
puts ("\nSPARQL:\n" + input)
end
query = if options[:sse]
SPARQL::Algebra.parse(input, logger: options[:logger], update: options[:update])
else
# Only do grammar debugging if we're generating SSE
SPARQL::Grammar.parse(input, **options)
end
puts ("\nSSE:\n" + query.to_sse) if options[:debug]
if options[:parse_only]
case options[:format]
when :sparql
puts ("\nSPARQL:\n" + query.to_sparql)
when nil, :sse
puts ("\nSSE:\n" + query.to_sse)
else
$stderr.puts "Unknown output format for parsing: #{options[:format]}. Use 'sse' or 'sparql'"
end
else
res = query.execute(options[:dataset], logger: options[:logger])
display_results(res, **options)
end
end
def server(options)
app = SPARQL::Server.application(**options)
Rack::Server.start(app: app, Port: options.fetch(:port, 9292))
rescue LoadError
$stderr.puts "Running SPARQL server requires Rack and Sinatra to be in environment: #{$!.message}"
end
def usage
puts "Usage: #{File.basename($0)} execute [options] query-file Execute a query against the specified dataset"
puts " #{File.basename($0)} parse [options] query-file Parse a query into SPARQL S-Expressions (SSE)"
puts " #{File.basename($0)} query [options] end-point query-file Run the query against a remote end-point"
puts " #{File.basename($0)} server [options] dataset-file Start a server initialized from the specified dataset"
puts "Options:"
puts " --dataset: File containing RDF graph or dataset"
puts " --debug: Display detailed debug output"
puts " --execute,-e: Use option argument as the SPARQL input if no query-file given"
puts " --format: Output format for results (json, xml, csv, tsv, html, sparql, sse, or another RDF format)"
puts " --port,-p Port on which to run server; defaults to 9292"
puts " --sse: Query input is in SSE format"
puts " --update: Process query as a SPARQL Update"
puts " --verbose: Display details of processing"
puts " --help,-?: This message"
exit(0)
end
cmd, input = ARGV.shift, nil
opts = GetoptLong.new(
["--dataset", GetoptLong::REQUIRED_ARGUMENT],
["--debug", GetoptLong::NO_ARGUMENT],
["--execute", "-e", GetoptLong::REQUIRED_ARGUMENT],
["--format", GetoptLong::REQUIRED_ARGUMENT],
["--port", "-p", GetoptLong::REQUIRED_ARGUMENT],
["--sse", GetoptLong::NO_ARGUMENT],
["--update", GetoptLong::NO_ARGUMENT],
["--verbose", GetoptLong::NO_ARGUMENT],
["--help", "-?", GetoptLong::NO_ARGUMENT]
)
logger = Logger.new(STDERR)
logger.level = Logger::WARN
logger.formatter = lambda {|severity, datetime, progname, msg| "%5s %s\n" % [severity, msg]}
options = {
dataset: RDF::Repository.new,
logger: logger,
}
opts.each do |opt, arg|
case opt
when '--dataset' then options[:dataset].load(arg, rdfstar: true)
when '--debug' then options[:debug] = true ; logger.level = Logger::DEBUG
when '--execute' then input = arg
when '--format' then options[:format] = arg.to_sym
when '--port' then options[:port] = arg.to_i
when '--sse' then options[:sse] = true
when '--update' then options[:update] = true
when '--verbose' then options[:verbose] = true ; logger.level = Logger::INFO
when "--help" then usage
end
end
unless %w(execute query parse server help).include?(cmd)
$stderr.puts "Unrecognized command #{cmd}"
usage
end
case cmd
when 'execute', 'parse'
options[:parse_only] = true if cmd == 'parse'
input ||= ARGV.empty? ? $stdin.read : RDF::Util::File.open_file(ARGV.first).read
run(input, **options)
when 'query'
endpoint = ARGV.shift
unless endpoint
$stderr.puts "Expected SPARQL endpoint URL"
usage
end
input ||= ARGV.empty? ? $stdin.read : RDF::Util::File.open_file(ARGV.first).read
SPARQL::Client.new(endpoint) do |client|
res = client.query(input)
display_results(res, **options)
end
when 'server'
if data_file = ARGV.shift
options[:dataset] = RDF::Repository.load(data_file)
end
server(options)
else usage
end