Class: InterMine::PathQuery::QueryBuilder

Inherits:
Object
  • Object
show all
Includes:
REXML::StreamListener
Defined in:
lib/intermine/query.rb

Direct Known Subclasses

TemplateBuilder

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ QueryBuilder

Returns a new instance of QueryBuilder.



76
77
78
79
80
81
82
# File 'lib/intermine/query.rb', line 76

def initialize(model)
    @model = model
    @query_attributes = {}
    @subclass_constraints = []
    @coded_constraints = []
    @joins = []
end

Instance Method Details

#process_constraint(attrs) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/intermine/query.rb', line 120

def process_constraint(attrs)
    if attrs.has_key?("type")
        @subclass_constraints.push({:path => attrs["path"], :sub_class => attrs["type"]})
    else
        args = {}
        args[:path] = attrs["path"]
        args[:op] = attrs["op"]
        args[:value] = attrs["value"] if attrs.has_key?("value")
        args[:loopPath] = attrs["loopPath"] if attrs.has_key?("loopPath")
        args[:extra_value] = attrs["extraValue"] if attrs.has_key?("extraValue")
        args[:code] = attrs["code"]
        if MultiValueConstraint.valid_ops.include?(attrs["op"])
            args[:values] = [] # actual values will be pushed on later
        end
        if attrs.has_key?("loopPath")
            LoopConstraint.xml_ops.each do |k,v|
                args[:op] = k if v == args[:op]
            end
        end
        @coded_constraints.push(args)
    end 
end

#queryObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/intermine/query.rb', line 84

def query
    q = create_query
    # Add first, in case other bits depend on them
    @subclass_constraints.each do |sc|
        q.add_constraint(sc)
    end
    @joins.each do |j|
        q.add_join(*j)
    end
    @coded_constraints.each do |con|
        q.add_constraint(con)
    end
    @query_attributes.sort_by {|k, v| k}.reverse.each do |k,v|
        begin
            q.send(k + "=", v)
        rescue
        end
    end
    return q
end

#tag_start(name, attrs) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/intermine/query.rb', line 105

def tag_start(name, attrs)
    @in_value = false
    if name == "query"
        attrs.each do |a|
            @query_attributes[a.first] = a.last if a.first != "model"
        end
    elsif name=="constraint"
        process_constraint(attrs)
    elsif name=="value"
        @in_value = true
    elsif name=="join"
        @joins.push([attrs["path"], attrs["style"]])
    end
end

#text(t) ⇒ Object



143
144
145
# File 'lib/intermine/query.rb', line 143

def text(t)
    @coded_constraints.last[:values].push(t)
end