Class: Dydra::Command::Query

Inherits:
Dydra::Command show all
Defined in:
lib/dydra/command/query.rb

Overview

Queries a repository.

Instance Method Summary (collapse)

Methods inherited from Dydra::Command

#abort, #basename, #catch_errors, #debug?, #initialize, #parse_repository_specs, #parse_resource_specs, #require_gem!, #stderr, #stdout, #validate_repository_specs, #validate_resource_specs, #verbose?, #wrap_errors

Methods included from Dydra::Client

authenticate!, credentials_file, delete, get, get_json, post, reset!, resource, rpc, setup?

Constructor Details

This class inherits a constructor from Dydra::Command

Instance Method Details

- execute(repository_spec, query)

This method returns an undefined value.

Parameters:

  • repository_spec (String)
  • query (String, #to_s)


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
# File 'lib/dydra/command/query.rb', line 10

def execute(repository_spec, query)
  begin
    repository = Repository.new(repository_spec)

    @query = case query
      when nil       then $stdin.read             # < filename.rq
      when /^@(.*)$/ then open($1) { |f| f.read } # @filename.rq, @url.rq
      else query.to_s                             # 'SELECT ...'
    end

    @options[:result_format] ||= case Repository.query_form(query)
      when :select, :ask
        :parsed
      when :construct, :describe
        :turtle
    end

    result = repository.query(@query, :format => @options[:result_format])

    if @options[:result_format] == :parsed && result.respond_to?(:empty?) && !result.empty?
      variables = result.first.each_name.to_a
      puts variables.join("\t")
      result.each do |binding|
        puts variables.map { |n| binding.to_hash[n] }.join("\t")
      end
    else
      puts result
    end
  rescue RestClient::NotAcceptable
    puts "Invalid result format: #{@options[:result_format]} for #{Repository.query_form(query)}"
  end
end