Class: Graffiti::SqlMapper

Inherits:
Object
  • Object
show all
Includes:
Debug
Defined in:
lib/graffiti/sql_mapper.rb

Overview

Transform RDF query pattern graph into a relational join expression.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, pattern, negative = [], optional = [], global_filter = '') ⇒ SqlMapper

Returns a new instance of SqlMapper.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/graffiti/sql_mapper.rb', line 97

def initialize(config, pattern, negative = [], optional = [], global_filter = '')
  @config = config
  @global_filter = global_filter

  check_graph(pattern)
  negative.empty? or check_graph(pattern + negative)
  optional.empty? or check_graph(pattern + optional)

  map_predicates(pattern, negative, optional)
  transform
  generate_tables_and_conditions

  @jc = @aliases = @ac = @global_filter = nil
end

Instance Attribute Details

#clausesObject (readonly)

map clause position to table, field, and table alias

position => {
  :subject => {
    :node => node,
    :field => field
  },
  :object => {
    :node => node,
    :field => field
  },
  :map => RdfPropertyMap,
  :bind_mode => < :must_bind | :may_bind | :must_not_bind >,
  :alias => alias
}


128
129
130
# File 'lib/graffiti/sql_mapper.rb', line 128

def clauses
  @clauses
end

#fromObject (readonly)

list of tables for FROM clause of SQL query



144
145
146
# File 'lib/graffiti/sql_mapper.rb', line 144

def from
  @from
end

#nodesObject (readonly)

map node to list of positions in clauses

node => {
  :positions => [
    { :clause => position, :role => < :subject | :object > }
  ],
  :bind_mode => < :must_bind | :may_bind | :must_not_bind >,
  :colors => { color1 => bind_mode1, ... },
  :ground => < true | false >
}


141
142
143
# File 'lib/graffiti/sql_mapper.rb', line 141

def nodes
  @nodes
end

#whereObject (readonly)

conditions for WHERE clause of SQL query



147
148
149
# File 'lib/graffiti/sql_mapper.rb', line 147

def where
  @where
end

Instance Method Details

#bind(node) ⇒ Object

return node’s binding, raise exception if the node isn’t bound



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/graffiti/sql_mapper.rb', line 151

def bind(node)
  (@nodes[node] and @bindings[node] and (binding = @bindings[node].first)
  ) or raise ProgrammingError,
    "Node '#{node}' is not bound by the query pattern"

  @nodes[node][:positions].each do |p|
    if :object == p[:role] and @clauses[ p[:clause] ][:map].subproperty_of

      property = @clauses[ p[:clause] ][:map].property
      return %{select_subproperty(#{binding}, #{bind(property)})}
    end
  end

  binding
end