Class: Query

Inherits:
Object
  • Object
show all
Defined in:
lib/active_rdf/queryengine/query.rb

Overview

TODO add log for every method which constitues to the query

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQuery

Returns a new instance of Query.



16
17
18
19
20
21
22
23
24
# File 'lib/active_rdf/queryengine/query.rb', line 16

def initialize
  $log.debug "Query: initializing"
  distinct = false
limit = nil
offset = nil
  @select_clauses = []
  @where_clauses = []
@keywords = {}
end

Instance Attribute Details

#keywordsObject (readonly)

Returns the value of attribute keywords.



13
14
15
# File 'lib/active_rdf/queryengine/query.rb', line 13

def keywords
  @keywords
end

#limitsObject (readonly)

Returns the value of attribute limits.



13
14
15
# File 'lib/active_rdf/queryengine/query.rb', line 13

def limits
  @limits
end

#offsetsObject (readonly)

Returns the value of attribute offsets.



13
14
15
# File 'lib/active_rdf/queryengine/query.rb', line 13

def offsets
  @offsets
end

#select_clausesObject (readonly)

Returns the value of attribute select_clauses.



13
14
15
# File 'lib/active_rdf/queryengine/query.rb', line 13

def select_clauses
  @select_clauses
end

#where_clausesObject (readonly)

Returns the value of attribute where_clauses.



13
14
15
# File 'lib/active_rdf/queryengine/query.rb', line 13

def where_clauses
  @where_clauses
end

Instance Method Details

#askObject



43
44
45
46
47
# File 'lib/active_rdf/queryengine/query.rb', line 43

def ask
  @ask = true
  $log.debug "Query: the current select clauses are: #{@select_clauses.join(', ')}"
  self
end

#clear_selectObject



26
27
28
29
30
# File 'lib/active_rdf/queryengine/query.rb', line 26

def clear_select
   $log.debug "Query: clearing select query"
  @select_clauses = []
  distinct = false
end

#count(*s) ⇒ Object



55
56
57
58
# File 'lib/active_rdf/queryengine/query.rb', line 55

def count *s
  @count = true
  select(*s)
end

#distinct(*s) ⇒ Object Also known as: select_distinct



49
50
51
52
# File 'lib/active_rdf/queryengine/query.rb', line 49

def distinct *s
  @distinct = true
  select(*s)
end

#execute(options = {:flatten => true}, &block) ⇒ Object

execute query on data sources either returns result as array (flattened into single value unless specified otherwise) or executes a block (number of block variables should be same as number of select variables)

usage: results = query.execute usage: query.execute do |s,p,o| … end



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/active_rdf/queryengine/query.rb', line 129

def execute(options={:flatten => true}, &block)
  $log.debug "Query: executing query: #{self.inspect}"
  
  if block_given?
    FederationManager.query(self) do |*clauses|
      block.call(*clauses)
    end
  else
    FederationManager.query(self, options)
  end
end

#keyword_where(s, o) ⇒ Object

adds keyword constraint to the query. You can use all Ferret query syntax in the constraint (e.g. keyword_where(:s,‘eyal|benjamin’)



103
104
105
106
107
108
109
110
111
112
# File 'lib/active_rdf/queryengine/query.rb', line 103

def keyword_where s,o
  @keyword = true
  s = parametrise(s)
  if @keywords.include?(s)
    @keywords[s] = @keywords[s] + ' ' + o
  else
    @keywords[s] = o
  end
  self
end

#limit(i) ⇒ Object



60
61
62
63
# File 'lib/active_rdf/queryengine/query.rb', line 60

def limit(i)
  @limits = i 
  self
end

#offset(i) ⇒ Object



65
66
67
68
# File 'lib/active_rdf/queryengine/query.rb', line 65

def offset(i)
  @offsets = i
  self
end

#select(*s) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/active_rdf/queryengine/query.rb', line 32

def select *s
  @select = true
  s.each do |e|
    @select_clauses << parametrise(e) 
  end
# removing duplicate select clauses
@select_clauses.uniq!
  $log.debug "Query: the current select clauses are: #{@select_clauses.join(', ')}"
  self
end

#to_sObject



141
142
143
144
145
146
147
# File 'lib/active_rdf/queryengine/query.rb', line 141

def to_s
if ConnectionPool.read_adapters.empty?
  inspect 
else
  ConnectionPool.read_adapters.first.translate(self)
end
end

#to_spObject



149
150
151
152
# File 'lib/active_rdf/queryengine/query.rb', line 149

def to_sp
require 'queryengine/query2sparql'
Query2SPARQL.translate(self)
end

#where(s, p, o) ⇒ Object



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
# File 'lib/active_rdf/queryengine/query.rb', line 70

def where s,p,o
case p
when :keyword
  # treat keywords in where-clauses specially
  keyword_where(s,o)
else
  # remove duplicate variable bindings, e.g.
  # where(:s,type,:o).where(:s,type,:oo) we should remove the second clause, 
  # since it doesn't add anything to the query and confuses the query 
  # generator. 
  # if you construct this query manually, you shouldn't! if your select 
  # variable happens to be in one of the removed clauses: tough luck.
  
    unless s.respond_to?(:uri)
      unless s.class == Symbol
        $log.debug "Query: where: got a Subject which is no Symbol, and no RDFS::Resource, but is instead: #{s}"
        raise(ActiveRdfError, "cannot add a where clause, in which s is not a resource and not a variable")
      end
    end
    unless p.respond_to?(:uri)
      unless p.class == Symbol
        $log.debug "Query: where: got a Predicate which is no Symbol, and no RDFS::Resource, but is instead: #{p}"
        raise(ActiveRdfError, "cannot add a where clause, in which s is not a resource and not a variable")
      end
    end
    
  @where_clauses << [s,p,o].collect{|arg| parametrise(arg)}
end
  self
end